More intellegent error reporting than 'parse error' with Bison (Tergiver)
13 Oct 2003 00:29:09 -0400

          From comp.compilers

Related articles
More intellegent error reporting than 'parse error' with Bison (2003-10-13)
Re: More intellegent error reporting than 'parse error' with Bison (Carl Cerecke) (2003-10-13)
Re: More intellegent error reporting than 'parse error' with Bison (2003-10-31)
| List of all articles for this month |

From: (Tergiver)
Newsgroups: comp.compilers
Date: 13 Oct 2003 00:29:09 -0400
Keywords: yacc, question, errors
Posted-Date: 13 Oct 2003 00:29:09 EDT

** .Y **
        '{' '}'
        | '{' statement_list '}'

        IF '(' expr ')' compound_statement

** .SCRIPT **
if (1)
if (2)

** note the missing '{' in the first if

You can see that my selection and iteration statements require the
curly braces but getting the parser to report this intellegently is
difficult. This is only one example of where I need to do this kind of
thing. I managed to do it (below), but there must be a better way:

        _test_ '{' '}'
        | _test_ '{' statement_list '}'

{ if (yychar != '{' ) yyerror("expected '{'"); }

Is there?

Seems to me that:

        '{' '}'
        | '{' statement_list '}'
        | error { yyerror(); }

should work, but it doesn't.

Obviously I dont understand the parser very well, but if you read the
verbose output file you see this:

state 195

        selection_statement -> IF '(' expr ')' compound_statement (rule

        '{' shift, and go to state 36

        compound_statement go to state 204

As I understand it, if its in state 195 and it doesn't see a '{' it
generates "parse error". Well how do I get control here so I can
display something other than that?

Oh.. I've also tried adding YYERROR_VERBOSE, but that does nothing.
If you trace into the code it looks like this:

if (yyn > YYFLAG && yyn < YYLAST)
    ... show something more intellegent
          yyerror("parse error");

yyn is -44
YYFLAG is 32768
YYLAST is 435

Not gonna happen.
[Look through the archives. This question has come up a lot, without many
useful answers. LALR parsers do a fine job on correct input, but on
incorrect input they tend to throw up their hands unless you put in lots
and lots of error rules along with adhoc stuff to track the context.

Post a followup to this message

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