首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Bison终止而不是移位错误

Bison终止而不是移位错误
EN

Stack Overflow用户
提问于 2017-06-12 03:03:27
回答 1查看 120关注 0票数 0

我有一个很好的语法,只是它不能容忍语法错误。我试图在error令牌中工作,以便它能够优雅地恢复。我已经阅读了关于错误恢复的野牛手册,但是有些东西并没有加起来。

下面是语法的一个片段:

代码语言:javascript
复制
%start start
%token WORD WORDB SP CRLF

%%

start : A B C
      | error CRLF start

A     : WORD SP WORD CRLF

...

下面是bison生成的用于描述语法的输出文件片段

代码语言:javascript
复制
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 -> ...,但是当我运行解析器时,它实际上生成以下内容:

代码语言:javascript
复制
--(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。实际上,它只是很难失败。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-06-12 06:02:09

error转换不会抑制对yyerror()的调用,因此如果yyerror实现执行类似于调用exit()的操作,则错误恢复将无法进行。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44490848

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档