启用Bison的parse.lac full选项后错误恢复失效问题求助
Bison启用
parse.lac full后错误恢复功能失效问题 为了避免生成不正确的错误消息,按照Bison文档说明启用了parse.lac full选项,但启用后错误恢复功能无法正常工作。
示例代码
%{ #include <stdio.h> int yylex(); void yyerror(const char *); %} %define parse.lac full %define parse.error detailed %% prog: init statements { printf("program finished successfully!\n"); } ; init: %empty | '!' '\n' { printf("an optional \"special\" first line\n"); } ; statements: %empty | statements 'x' '\n' { printf("correct line\n"); } | statements error '\n' { yyerrok; printf("error line\n"); } ; %% int yylex() { char c; if (scanf("%c", &c) != EOF) return c; else return YYEOF; } void yyerror(const char *s) { fprintf(stderr, "%s\n", s); } int main() { return yyparse(); }
测试输入与输出对比
测试输入
illegal line illegal line
未启用LAC的预期输出
syntax error, unexpected invalid token, expecting end of file or 'x' error line syntax error, unexpected invalid token, expecting end of file or 'x' error line program finished successfully!
启用LAC后的实际输出
syntax error, unexpected invalid token, expecting end of file or '!' or 'x'
调试日志信息
Starting parse Entering state 0 Stack now 0 Reading a token Next token is token invalid token () LAC: initial context established for invalid token LAC: checking lookahead invalid token: Always Err Constructing syntax error message LAC: checking lookahead end of file: R2 G3 R4 G6 R1 G2 S5 LAC: checking lookahead '!': S1 LAC: checking lookahead '\n': R2 G3 R4 G6 Err LAC: checking lookahead 'x': R2 G3 R4 G6 S8 syntax error, unexpected invalid token, expecting end of file or '!' or 'x' Cleanup: discarding lookahead token invalid token () Stack now 0
疑问
推测问题可能源于解析器的Always Err决策,但对LAC工作原理了解有限。请问这是LAC的bug还是程序本身存在问题?能否修复该问题,或是LAC并不适用于当前场景?
已在Bison邮件列表提问,但暂未收到回复。
内容的提问来源于stack exchange,提问作者Piotr Siupa
相关产品推荐
相关产品推荐

