简体   繁体   English

LL 和 LR 解析的区别

[英]Difference between LL and LR Parsing

Currently studying context free grammars and methods for parsing them.目前正在研究上下文无关语法和解析它们的方法。 From my understanding, context free grammars can be parsed via top down/LL or bottom up/LR.根据我的理解,上下文无关文法可以通过自顶向下/LL 或自底向上/LR 来解析。 Is it correct understood that, LL parsers require grammars to have strictly unambiguous production rules before it can be parsed?是否正确理解,LL 解析器要求语法在解析之前具有严格明确的产生式规则? And that LR parsers, on the other hand, also require grammars to be unambiguous, but instead of having to rewrite any ambiguous productions rules, additional precedence rules can added to the production rules to solve their ambiguity?而 LR 解析器,另一方面,也要求语法是明确的,但不必重写任何歧义的产生式规则,而是可以在产生式规则中添加额外的优先规则来解决它们的歧义? But how does look ahead fit into all this?但是,展望未来如何适应这一切?

From my understanding, context free grammars can be parsed via top down/LL or bottom up/LR.根据我的理解,上下文无关文法可以通过自顶向下/LL 或自底向上/LR 来解析。

Yes, LL parsing works top-down.是的,LL 解析是自上而下的。 LR parsing is usually considered a bottom-up parsing approach, though some authors consider it to be a hybrid of top-down and bottom-up because it uses context about what can appear where in a generated parse tree. LR 解析通常被认为是自底向上的解析方法,尽管一些作者认为它是自顶向下和自底向上的混合,因为它使用了关于什么可以出现在生成的解析树中的上下文。

Is it correct understood that, LL parsers require grammars to have strictly unambiguous production rules before it can be parsed?是否正确理解,LL 解析器要求语法在解析之前具有严格明确的产生式规则?

LL parsers only work for unambiguous grammars. LL 解析器仅适用于明确的文法。 The most common classes of LL parsers (LL(1), LL(*)) do not work on all grammars and require some extra restrictions beyond that the grammar is unambiguous.最常见的 LL 解析器类(LL(1)、LL(*))不适用于所有文法,并且除了文法明确之外还需要一些额外的限制。 For example, LL(1) parsers cannot handle left recursion.例如,LL(1) 解析器无法处理左递归。

And that LR parsers, on the other hand, also require grammars to be unambiguous, but instead of having to rewrite any ambiguous productions rules, additional precedence rules can added to the production rules to solve their ambiguity?而 LR 解析器,另一方面,也要求语法是明确的,但不必重写任何歧义的产生式规则,而是可以在产生式规则中添加额外的优先规则来解决它们的歧义?

Yes, and no.是的,也不是。 It is true that, like LL parsers, the most common types of LR parsers (LR(0), SLR(1), LALR(1), LR(1), IELR(1)) require the grammar to be unambiguous.确实,与 LL 解析器一样,最常见的 LR 解析器类型(LR(0)、SLR(1)、LALR(1)、LR(1)、IELR(1))要求语法是明确的。 You are correct that many ambiguities can be resolved with precedence declarations that tiebreak otherwise ambiguous grammars, but this can't resolve all ambiguities.您是正确的,可以使用优先声明来解决许多歧义,这些优先声明可以打破其他歧义的语法,但这并不能解决所有歧义。 Additionally, there are some unambiguous grammars that cannot be parsed by any LR(k) parser.此外,还有一些明确的语法无法被任何 LR(k) 解析器解析。

But how does look ahead fit into all this?但是,展望未来如何适应这一切?

Adding lookahead to an LL or LR parser gives the parser more context with which to decide which production rules to apply (for LL parsers) or whether to shift or reduce (LR parsers).向 LL 或 LR 解析器添加前瞻为解析器提供更多上下文,以决定应用哪些产生式规则(对于 LL 解析器)或是否移动或减少(LR 解析器)。 Intuitively, being able to see further into the token sequence allows the parser to rule out some options that couldn't work because they couldn't match what comes next.直观地说,能够进一步查看令牌序列允许解析器排除一些无法工作的选项,因为它们无法匹配接下来的内容。 The specific rules for how this lookahead works depends on the parsing algorithm;这种前瞻如何工作的具体规则取决于解析算法; for example, LR(2) parsers have some nuances that don't show up in LR(1) parsers.例如, LR(2) 解析器有一些在 LR(1) 解析器中没有出现的细微差别。 You'll likely find the information you're looking for by specifically reading up on LL(1) parsing, LR(0) parsing, and LR(1) parsing and can use that as a launching point.通过专门阅读 LL(1) 解析、LR(0) 解析和 LR(1) 解析,您可能会找到您正在寻找的信息,并且可以将其用作启动点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM