【发布时间】:2017-06-12 03:03:27
【问题描述】:
我有一个运行良好的语法,但它不能容忍语法错误。我正在尝试使用error 令牌,以便它可以正常恢复。我已经通读了the Bison manual 关于错误恢复的内容,但有些东西没有加起来。
这是语法中的一个 sn-p:
%start start
%token WORD WORDB SP CRLF
%%
start : A B C
| error CRLF start
A : WORD SP WORD CRLF
...
这是bison 生成的描述语法的输出文件的 sn-p
State 0
0 $accept: . start $end
error shift, and go to state 1
WORD shift, and go to state 2
start go to state 3
A go to state 4
State 1
2 start: error . CRLF start
CRLF shift, and go to state 5
State 5
2 start: error CRLF . start
error shift, and go to state 1
WORD shift, and go to state 2
start go to state 25
A go to state 4
鉴于输入标记WORDB CRLF WORD SP WORD CRLF .....,我希望状态转换为0 -> 1 -> 5 -> 2 -> ...,但是当我运行解析器时,它实际上会产生以下结果:
--(end of buffer or a NUL)
--accepting rule at line 49 ("WORDB")
Starting parse
Entering state 0
Reading a token: Next token is token WORDB ()
syntax error, unexpected WORDB, expecting WORD
据我所知,如果解析器处于状态 0 并且它看到除 WORD 之外的标记,它应该将标记解释为好像它是 error 并且应该进入状态 1。实际上它只是很难失败。
【问题讨论】:
-
你的
yyerror()函数是做什么的?error转换不会抑制对yyerror()的调用。 -
exit(1)。韦尔普。这样就可以了。我已经放弃调试生成的解析器,因为变量名非常简洁。 -
好的,已经回答了。