LL(1)悬空其他语法 [英] LL(1) grammar for dangling else

查看:75
本文介绍了LL(1)悬空其他语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在编译器构造中,主要的歧义问题之一是悬而未决
正如Aho,Lam,Sethi和Ullman撰写的《编译器:原理,技术和工具》一书中提到的,悬挂的语法不能与LL(1)解析器一起使用。

In compiler construction, one of the main ambiguity problems is dangling else. As mentioned in Compilers: Principles, Techniques, and Tools book by Aho, Lam, Sethi and Ullman, the grammar for the dangling else can't be used with LL(1) parsers.

是不是不能将其处理为LL(1)?

Is it true that it can't be handled to be LL(1)?

推荐答案

确实如此,LL(k)或LALR(k)不能以其纯格式对其进行解析。问题是悬空其他有两种可能的解释。这是一个模糊性问题( else属于最接近的 if,或者不属于)。

Its true, it can't be parsed by LL(k) or by LALR(k) in their pure forms. The problem is that there are two possible interpretations of the dangling else; its an ambiguity problem ("else" belongs to nearest "if", or does not belong).

通常通过坚持治愈只是两种解释之一,例如否则属于最接近的条件。

It is usually cured by insisting on just one of the two interpretations, e.g., "else belongs to nearest if".

许多解析器生成器可以无意中获得这种权限; LL的解决方案是接受第一个有效的解析,然后首先尝试其他附加到最接近的解析。 LR的解决方案是转移其他,这很容易导致,只需在减少动作之前检查转移动作即可。

Many parser generators can get this "right by accident"; the solution with LL is to accept the first parse that works and try "else attaches to closest" first. The solution with LR is "shift on else" which is easily caused, by simply checking for "shift" actions before "reduce" actions.

解析器出现的问题,该解析器实际上会获取模棱两可的解析,例如GLR。这里可以提供语法外的提示,例如转移其他。

It only gets to be a problem with a parser that will really pick up ambiguous parses, such as GLR. Here one can provide extra-grammatical hints, e.g, "shift on else".

这篇关于LL(1)悬空其他语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆