我有一个很好的语法,只是它不能容忍语法错误。我试图在error
令牌中工作,以便它能够优雅地恢复。我已经阅读了关于错误恢复的野牛手册,但是有些东西并没有加起来。
下面是语法的一个片段:
%start start
%token WORD WORDB SP CRLF
%%
start : A B C
| error CRLF start
A : WORD SP WORD CRLF
...
下面是bison
生成的用于描述语法的输出文件片段
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
,并且应该转到State 1。实际上,它只是很难失败。
发布于 2017-06-11 22:02:09
error
转换不会抑制对yyerror()
的调用,因此如果yyerror
实现执行类似于调用exit()
的操作,则错误恢复将无法进行。
https://stackoverflow.com/questions/44490848
复制相似问题