More intellegent error reporting than 'parse error' with Bison

tergiver@msn.com (Tergiver)
13 Oct 2003 00:29:09 -0400

          From comp.compilers

Related articles
More intellegent error reporting than 'parse error' with Bison tergiver@msn.com (2003-10-13)
Re: More intellegent error reporting than 'parse error' with Bison cdc@maxnet.co.nz (Carl Cerecke) (2003-10-13)
Re: More intellegent error reporting than 'parse error' with Bison nr@labrador.eecs.harvard.edu (2003-10-31)
| List of all articles for this month |
From: tergiver@msn.com (Tergiver)
Newsgroups: comp.compilers
Date: 13 Oct 2003 00:29:09 -0400
Organization: http://groups.google.com
Keywords: yacc, question, errors
Posted-Date: 13 Oct 2003 00:29:09 EDT

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


selection_statement:
        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:


compound_statement:
        _test_ '{' '}'
        | _test_ '{' statement_list '}'


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


Is there?


Seems to me that:


compound_statement:
        '{' '}'
        | '{' 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
109)


        '{' 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
}
else
          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.
-John]



Post a followup to this message

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