LALR(2) 悬空 else
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,悬挂的 else 问题是一个歧义,所以再多的前瞻也无济于事。
No, the dangling else problem is an ambiguity, so no amount of lookahead helps.
它是不明确的,但是,这不是问题,因为行为良好的 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.