LALR(2) 悬空 else

发布于 2024-08-12 22:42:53 字数 69 浏览 3 评论 0原文

LALR(2) 是否能够自然地处理悬空 else 情况(没有任何特殊规则,与 LALR(1) 一样)?

谢谢

Is LALR(2) able to handle the dangling else case naturally (without any special rules, as with LALR(1))?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

淡莣 2024-08-19 22:42:53

不,悬挂的 else 问题是一个歧义,所以再多的前瞻也无济于事。

No, the dangling else problem is an ambiguity, so no amount of lookahead helps.

◇流星雨 2024-08-19 22:42:53

它是不明确的,但是,这不是问题,因为行为良好的 LALR 解析器生成器将通过选择移位而不是归约选择来解决不明确性。这就是您想要的——“else”与前面的“if”语句相匹配。所以结论是:没有问题。您只需要了解解析器生成器为所有移位减少歧义所做的默认选择。可以使用消除歧义规则或“yacc”中的“%prec”运算符来覆盖此默认值。
但这是一个更高级的话题。

It's ambiguous, however, it's not a problem, because a well behaved LALR parser generator will resolve the ambiguity by choosing the shift instead of the reduce choice. This is what you want -- the "else" to be matched with the previous "if" statement. So the conclusion is: there is no problem. You just have to understand the default choice which the parser generator makes for all shift-reduce ambiguities. This default can be overridden with disambiguating rules or something like the "%prec" operator in "yacc".
But that is a more advanced topic.

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