3
votes

I've been reading the manuals about using the error symbol, yyerrok, etc.. My concern is how to add error rules to the parser without overdoing it or not adding enough.

Are there any general guidelines I should follow, considering I'll be parsing a simplified Java program? Should every rule contain some error detection/recovery? Is there a simplest approach which proves to be proficient at performing these tasks? I'm only looking for syntax errors at the moment.

Thanks for any/all answers in advance.

1

1 Answers

3
votes

Generally you want to use error recovery rules sparingly in a yacc/bison file. It's best to have only one or two top-level error rules rather than lots of error rules in leaf productions, as actually resynchronizing properly in a leaf rule is very hard.

The simplest approach for a Java-like language is to have just a rule like:

BlockStatement: error ';'

In this case, if you get any kind of syntax error while parsing a statement, the parser will just throw away input tokens until it gets to a ; and act as if it has just completed a statement. This works adequately for errors OTHER than a missing ; at the end of block.