简体   繁体   English

如何识别语法是否为LR(n),LL(n)

[英]How to identify whether a grammar is LR(n), LL(n)

For a language that is not LL(1) or LR(1) how can one try to find out if some number n exists such that the grammar can be LL(n) or LR(n) ? 对于不是LL(1)LR(1)的语言,如何尝试找出某个数字n存在,使得语法可以是LL(n)LR(n)

You check if a grammar is LR(0) by looking at the canonical collection of LR(0) items. 您检查是否有语法是LR(0)通过查看的规范集合LR(0)项目。 Then, assuming it wasn't LR(0) , you can check if it is LR(1) by introducing the lookahead symbol. 然后,假设它不是LR(0) ,则可以通过引入前瞻符号来检查它是否为LR(1) My simple reasoning tells me that, to check whether it is LR(2) or not, you probably have to make the lookahead contain the next two symbols instead of just one. 我的简单推理告诉我,要检查它是否为LR(2) ,可能必须使前行包含接下来的两个符号而不是仅一个。 For LR(3) you have to take three symbols into consideration etc. 对于LR(3)您必须考虑三个符号等。

Even if this is the case, even though I doubt it, I am struggling to think of how can one try to identify (or even get a hint at) an n , or the non-existence thereof, for which a specific grammar can be LR(n) and/or LL(n) without checking incrementally from an arbitary LR(m) upwards. 即使是这种情况,尽管我对此表示怀疑,但我仍在努力思考如何才能尝试识别(甚至暗示) nn的不存在,为此可以使用一种特定的语法LR(n)和/或LL(n)无需从任意LR(m)向上逐步检查。

  1. If a language is LR( k ) for some k >1, then it is LR(1). 如果某语言的 k > 1是LR( k ),则它是LR(1)。 (That's not true for a grammar, of course.) That is, if you have an LR( k ) grammar for a language, then you can mechanically construct an LR(1) grammar which allows you to recover the original parse tree. (当然,对于语法不是正确的。)也就是说,如果您对一种语言具有LR( k )语法,则可以机械地构造LR(1)语法,以使您可以恢复原始的语法分析树。 This is not true of LL( k ); LL( k )并非如此; LL( k ) languages are a strict subset of LL( k +1) languages. LL( k )语言是LL( k +1)语言的严格子集。

  2. The test you propose will indeed let you decide whether a grammar is LR( k ) for some given k (or LL( k )). 您提出的测试确实可以让您确定某个给定的k(或LL( k ))的语法是否为LR( k )。 Unfortunately, there's no way of figuring out the smallest possible value of k other than the successive search you propose, and there is no guarantee that the search will ever terminate. 不幸的是,除了您建议的连续搜索之外,没有其他方法可以找出k的最小可能值,并且无法保证搜索将永远终止。

  3. Although the problem is hard (or impossible) in the general case, it can often be answered for specific grammars, by considering the possible valid successors of a grammar state which exhibits conflicts. 尽管在一般情况下该问题很难解决(或不可能解决),但通常可以通过考虑表现出冲突的语法状态的可能有效后继者来解决特定的语法问题。

In most real-world grammars, there will only be a few conflicts, so manual examination of conflicting states is possible. 在大多数现实世界语法中,只有少数冲突,因此可以手动检查冲突状态。 In general terms, one needs to figure out the path which led to the conflicting state, and the possible continuations. 一般而言,需要弄清楚导致冲突状态的路径以及可能的延续。 In many cases it will be clear that the parsing conflict could be resolved with slightly more lookahead. 在许多情况下,很明显,可以稍微提前一点解决解析冲突。

A large class of grammars where this will fail is the set of ambiguous grammars. 这将失败的一大类语法是模棱两可的语法集。 An ambiguous grammar cannot be LR(k) (or LL(k)) for any k . 对于任何k而言,歧义语法都不能为LR(k)(或LL(k))。 Again, the question of whether a grammar is ambiguous is not decidable but effective heuristics exist, some of which are included in commercial products. 同样,语法是否模棱两可的问题尚不确定,但存在有效的启发式方法,其中一些启发式方法包含在商业产品中。

Again, it is often easy to find ambiguities in real-world grammars, either by visual inspection (as above), or by feeding a lot of valid texts into a GLR parser (such as the one produced by bison) until an ambiguity is reported. 同样,通过目视检查(如上)或通过将大量有效文本输入到GLR解析器(如bison生成的文本)中,直到发现歧义,通常很容易在现实语法中发现歧义。 。 (Alternatively, you can enumerate valid texts from the grammar with a straight-forward algorithm, and see if a text appears twice in the enumeration.) (或者,您可以使用简单算法从语法中枚举有效文本,并查看文本是否在枚举中出现两次。)

Here are a couple of possibly relevant SO questions illustrating analysis techniques. 这是说明分析技术的几个可能相关的SO问题。 I'm sure there are more. 我敢肯定还有更多。

A yacc shift/reduce conflict on an unambiguous grammar 明确语法上的yacc移位/减少冲突

Bison reduce/reduce situation 野牛减少/减少情况

yacc shift-reduce for ambiguous lambda syntax yacc shift-reduce用于歧义lambda语法

How to understand and fix conflicts in PLY 如何理解和解决PLY中的冲突

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

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