野牛终止而不是转移错误
Bison terminating instead of shifting error
我有一个运行良好的语法,但它不能容忍语法错误。我正在尝试使用 error
个令牌,以便它可以正常恢复。我已经通读了 the Bison manual 关于错误恢复的内容,但有些东西没有加起来。
这是语法的一个片段:
%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
并且应该转到状态 1 . 在实践中它只是很难失败。
error
转换不会抑制对 yyerror()
的调用,因此如果您的 yyerror
实现执行类似调用 exit()
的操作,错误恢复将无法继续。
我有一个运行良好的语法,但它不能容忍语法错误。我正在尝试使用 error
个令牌,以便它可以正常恢复。我已经通读了 the Bison manual 关于错误恢复的内容,但有些东西没有加起来。
这是语法的一个片段:
%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
并且应该转到状态 1 . 在实践中它只是很难失败。
error
转换不会抑制对 yyerror()
的调用,因此如果您的 yyerror
实现执行类似调用 exit()
的操作,错误恢复将无法继续。