问题
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.
回答1:
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.
来源:https://stackoverflow.com/questions/27943284/how-to-get-more-parse-error-information-from-lex-yacc