YACC解析器不想打印ACCPET,即使输入正确?

发布于 2025-01-30 22:42:52 字数 123 浏览 2 评论 0原文

该程序规则对“零一个”输入不起作用,为什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不寐倦长更 2025-02-06 22:42:52

我尝试通过野牛运行您的语法,并提出了两个警告:

警告:3换档/减少冲突[-wconflicts-sr]
警告:由于冲突[-Wother]

,在解析器中无用的规则无用

第二个警告适用于生产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:

warning: 3 shift/reduce conflicts [-Wconflicts-sr]
warning: rule useless in parser due to conflicts [-Wother]

The second warning applies to the production T : %empty which is the second alternative for T.

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 reducing T with an empty match. The first production involves shifting one as the first token in a new T.

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. Each one starts a new T, 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文