0

How to get more parse error information from lex / yacc?

Currently in the lex file I am using:

int yyerror(const char *msg)
{
    fprintf(stderr, "Parse error: %s\n", msg);
    return 0;
}

But when I run my program yyerror outputs a blank message. I tried adding yylineno and yytext to yyerror but these cause compilation errors. I tried adding "%error-verbose" to the yacc file and "%option debug" to the lex file but these made no difference to the message.

I did notice however that yyparse outputs a bracket: '{' from the file I am trying to parse, I don't know the significance of this.

I am using win_flex and win_bison.

gornvix
  • 3,154
  • 6
  • 35
  • 74
  • Here's maybe some more helpful information for you [**error handling in YACC**](http://stackoverflow.com/questions/9796608/error-handling-in-yacc) – πάντα ῥεῖ Jan 14 '15 at 13:02
  • Did you declare yyerror in your bison file? Do you compile with `-Wall` so that you are warned about missing prototypes? Do you specify `%option nodefault` in your flex file so that you are warned about input which matches no pattern? – rici Jan 14 '15 at 15:10

1 Answers1

0

Much of this is described in the official bison manual when it shows how to use yyerror to get improved error messages.

In particular, it suggests you use %define parse.error verbose in the Bison declarations section to get enhanced error messages.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129