yacc error recovery

aybee@ceci.mit.edu (Adam Feder)
Wed, 24 Mar 1993 23:21:40 GMT

          From comp.compilers

Related articles
yacc error recovery aybee@ceci.mit.edu (1993-03-24)
| List of all articles for this month |

Newsgroups: comp.compilers
From: aybee@ceci.mit.edu (Adam Feder)
Keywords: yacc, errors, comment
Organization: "Center for Educational Computer Inintiatives at MIT"
Date: Wed, 24 Mar 1993 23:21:40 GMT

hi ---

I'm writing an interpretter for the control language of the multi-media
platform that we are developing here and I'm using lex and yacc.

I am having trouble making yacc resynchronize properly after finding
errors and I've heard that this is a common problem, so Ia'm hoping someone
will have a good answer.


1) Am I required to place a resynchronization token after "error" in my
error productions? Can I leave it out? Can I put a non-terminal there
instead? My yacc does not complain if I leave it out, but I want to be
sure that its not just MY yacc that doesn't mind (this has to be portable
to almost any unix platform, Macs, and PC's).

2) here's an example of my main problem:

/********** proper syntax ****************/

class child : parent1, parent2
string s;

on hello : string name
print("hi" & name);

/****** error i'm interested in handling nicely *******/

class child : parent1, parent2
string s;

on hello : string name
                                                                          // notice that the { is missing!
print("hi" & name);

I would like to resynch the parser very quickly after a missing { so that
I don't work on the class/method definition in the middle. I am willing
to either skip it or parse it, but I'd rather not do a combination. In
general, I want to be able to pretend that I saw punctuation that I didn't
really see, note its absence, and continue my parsing.

How can I do this? I have considered making a rule "openSquiggly" in my
grammar to match either a '{' or any one single token, but I want to keep
that token, and pretend that I saw a '{', but I don't want it to match ANY
error, just ONE token...and I don't want it to discard tokens trying to
recover. (There's got to be a way to do this, using yyerrok, and all those
other fancy macros I haven't seen anyone really use, but I don't know it!)

I haven't seen any good examples of yacc grammars that do much error
handling, so I would be interested in direct responses to my questions, or
helpful pointers to code that does this kind of thing.

(adam feder: aybee@ceci.mit.edu)
[The answer to the first question is no, you don't need to put anything
particular after an error token, though it usually helps the resynchronization
process if you do, since it makes it easier for the parser to find a resync
point. But your general strategy has problems, since yacc's error recovery
works by discarding tokens, not adding them. -John]

Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.