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

有些话、适合烂在心里 提交于 2019-12-24 22:32:10

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!