YACC解析器不想打印ACCPET,即使输入正确?
该程序规则对“零一个”输入不起作用,为什么?
SS: S { printf("Accepted"); }
;
S: zero T one T one
;
T : one T |
;
This program rules doesn't work on "zero one one" input why?
SS: S { printf("Accepted"); }
;
S: zero T one T one
;
T : one T |
;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我尝试通过野牛运行您的语法,并提出了两个警告:
第二个警告适用于生产
t:%empty
,这是t
t t t t t t t t t ,这是> t
t ,限制在解析器中无用。代码>。尽管被描述为警告,但这些消息几乎总是表明您的语法问题,因此应该解决它们(或者至少要提及您的语法为什么不起作用)。
预期的是减少冲突。当解析器看到
Zero
时,它在状态(•指示当前的解析位置):s:Zero•T One T One T One Tone
此时,它必须确定
t
的第一个或第二次生产应应用。第二个生产涉及用空匹配立即减少t
。第一个制作涉及将新t
中的第一个令牌转移为一个。解析器无法确定这些替代方案中的哪个适用,因为在这两种情况下,接下来的令牌都是
一个
。对于轮班行动,LR解析器不需要做出决定。它可以并行探索两个不同的换档动作。但是减少需要立即或永远不会进行;终端结束时必须识别。 的事实并不能阻止它被识别。(对于空的减少,这是立即的。但是,减少是空 然后使用一个简单的默认策略:优先换档。因此,从语法中删除了空
t
的减少(野牛也警告过您)。如果没有减少,解析就永远无法终止。每个一个
启动一个新的t
,直到达到输入结束为止。到那时,什么也没做到(因为从语法中删除了减少,而解析器报告了语法错误。由于我不知道您希望能够识别哪些句子,因此我无法为您提供有关如何修复语法的任何好建议。
I tried running your grammar through Bison, and it produced two warnings:
The second warning applies to the production
T : %empty
which is the second alternative forT
.Although described as warnings, these messages almost always indicate a problem with your grammar, so they should be addressed (or at least mentioned if you're asking why your grammar doesn't work).
The shift-reduce conflict is to be expected. When the parser has seen a
zero
, it's in the state (• indicates the current parse position):S : zero • T one T one
At this point, it has to decide whether the first or second production for
T
should apply. The second production involves immediately reducingT
with an empty match. The first production involves shiftingone
as the first token in a newT
.The parser cannot tell which of these alternatives apply because in both cases the next token will be
one
. For shift actions, an LR parser doesn't need to make a decision; it can explore two different shift actions in parallel. But reductions need to be taken immediately or never; the terminal must be recognised when it ends. (For empty reductions, that's right away. But the fact that the reduction is empty doesn't stop it from having to be recognised.)When Bison can't figure out what the parser should do in a given state, it reports a conflict and then uses a simple default strategy: prefer shift. Thus, the reduction of the empty
T
is removed from the grammar (which Bison also warned you about). Without that reduction, the parse can never terminate. Eachone
starts a newT
, until the end of the input is reached. At that point, nothing more can be done (since the reduction was removed from the grammar) and the parser reports a syntax error.Since I have no idea what sentences you expect to be able to recognise, I can't really offer you any good advice about how to fix the grammar.