More yacc error recovery questions... (Mike Moretti)
Sat, 4 Feb 1995 21:05:07 GMT

          From comp.compilers

Related articles
More yacc error recovery questions... (1995-02-04)
| List of all articles for this month |

Newsgroups: comp.compilers
From: (Mike Moretti)
Keywords: yacc, errors, question
Organization: Object House, Inc.
Date: Sat, 4 Feb 1995 21:05:07 GMT


I can't seem to figure out the best way to do good error checking in

Here's an example of what I would like to do:

  IfStatement : IF LPAREN Condition RPAREN Statement

I want to be able to print a valid error message (instead of just
"parse error") for certain cases:

If the LPAREN is missing or something else is in its place I'd like
to print the error "expecting a '('" or something similar.
The same goes for RPAREN.

Or another case with even more error checking:

  Statement : ID ASSIGN ID SEMI
                      | ID ASSIGN ID LPAREN Expression RPAREN SEMI /* function call */

I would want to be able to know if the ASSIGN operator is missing or
something else was used in its place (say assign was := and someone used =
instead) and print the appropriate message. Or if any IDs were missing,

Also, I want every error on the line to be display. So if someone
tried something like this:

  blah := ( ;

I'd want them to get two messages saying that the id is missing and also
the right paren is missing.

What is the best way to go about doing this?

I've come up with many ways to try to do this but none of them work.
This is the latest:

  Statement : IdToken AssignToken IdToken SEMI
                      | IdToken AssignToken IdToken LParenToken ExpressionOrError
                          RParenToken SEMI /* function call */

  IdToken : ID
                  | error {printf("expecting an id\n");

  AssignToken : ASSIGN
                  | error {printf("expecting ':='\n");


But this doesn't seem to work either. I've tried using yyclearin and
yyerrok to make all the errors come out (and not to skip any) but that
doesn't work either because sometimes the next line parsed after the
erroneous one gets a parse error at the first token...

I do alot of semantic checking on the ids, etc already but now I want to
do better syntax checking.

I've tried looking at other grammars on the net (somewhere around 20
different grammars or so) but I can't seem to find any that give decent
error messages (or if they do, I can't figure out how they did it) except
gcc and gcc's grammar seems a little too cryptic to me to figure out...

Could someone please help me?

Thanx in advance!
  Mike Moretti
  Software Engineer
  Object House, Inc.

Post a followup to this message

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