简体   繁体   English

这是LL(1)语法吗?

[英]Is this grammar LL(1)?

I have derived the following grammar: 我得到了以下语法:

S -> a | aT
T -> b | bR
R -> cb | cbR

I understand that in order for a grammar to be LL(1) it has to be non-ambiguous and right-recursive. 我知道,为了使语法成为LL(1),它必须是无歧义的和右递归的。 The problem is that I do not fully understand the concept of left-recursive and right-recursive grammars. 问题是我不完全理解左递归和右递归语法的概念。 I do not know whether or not the following grammar is right recursive. 我不知道以下语法是否正确递归。 I would really appreciate a simple explanation of the concept of left-recursive and right-recursive grammars, and if my grammar is LL(1). 我非常希望对左递归语法和右递归语法的概念做一个简单的解释,如果我的语法是LL(1)。

Many thanks. 非常感谢。

This grammar is not LL(1). 该语法不是LL(1)。 In an LL(1) parser, it should always be possible to determine which production to use next based on the current nonterminal symbol and the next token of the input. 在LL(1)解析器中,应该始终有可能基于当前的非终结符和输入的下一个标记来确定下一个要使用的生产。

Let's look at this production, for example: 让我们看一下这个产品,例如:

S → a | S→a | aT

Now, suppose that I told you that the current nonterminal symbol is S and the next symbol of input was an a. 现在,假设我告诉您当前的非终止符号为S,输入的下一个符号为a。 Could you determine which production to use? 您能确定要使用哪种产品? Unfortunately, without more context, you couldn't do so: perhaps you're suppose to use S → a, and perhaps you're supposed to use S → aT. 不幸的是,如果没有更多的上下文,您将无法这样做:也许您假设使用S→a,也许您应该使用S→aT。 Using similar reasoning, you can see that all the other productions have similar problems. 使用类似的推理,您可以看到所有其他产品都具有类似的问题。

This doesn't have anything to do with left or right recursion, but rather the fact that no two productions for the same nonterminal in an LL(1) grammar can have a nonempty common prefix. 这与左递归或右递归无关,而是这样的事实,即LL(1)语法中同一非终结符的任何两个产生式都不能具有非空的公共前缀。 In fact, a simple heuristic for checking if a grammar is not LL(1) is to see if you can find two production rules like this. 实际上,检查语法是否不是 LL(1)的一种简单启发式方法是看是否可以找到两个这样的生成规则。

Hope this helps! 希望这可以帮助!

The grammar has only a single recursive rule: the last one where R is the symbol on the left, and also appears on the right. 语法只有一个递归规则:最后一个规则,其中R是左侧的符号,也出现在右侧。 It is right-recursive because in the grammar rule, R is the rightmost symbol. 它是右递归的,因为在语法规则中,R是最右边的符号。 The rule refers to R, and that reference is rightmost. 该规则引用R,该引用最右边。

The language is LL(1). 语言是LL(1)。 How we know this is that we can easily construct a recursive descent parser that uses no backtracking and at most one token of lookahead. 我们所知道的是,我们可以轻松地构造不使用回溯且最多使用一个前瞻标记的递归下降解析器。

But such a parser would be based on a slightly modified version of the grammar. 但是这样的解析器将基于语法的略微修改版本。

For instance the two productions: S -> a and S -> a T could be merged into a single one that can be expressed by the EBNF S -> a [ T ] . 例如,两个产品: S -> aS -> a T可以合并为一个可以由EBNF S -> a [ T ] ( S derives a , followed by optional T ). S得出a ,后跟可选T )。 This rule can be handled by a single parsing function for recognizing S . 该规则可以由用于识别S的单个解析函数处理。

The function matches a and then looks for the optional T , which would be indicated by the next input symbol being b . 该函数与a匹配,然后寻找可选​​的T ,它将由下一个输入符号b

We can write an LL(1) grammar for this, along these lines: 我们可以按照以下方式为此编写LL(1)语法:

S -> a T_opt
T_opt -> b R_opt
T_opt -> <empty>
... et cetera

The optionality of T is handled explicitly, by making T (which we rename to T_opt ) capable of deriving the empty string, and then condensing to a single rule for S , so that we don't have two phrases that both start with a . 通过使T (我们将其重命名为T_opt )能够导出空字符串,然后将其压缩为S的单个规则,来显式处理T的可选性,这样我们就不会有两个都以a开头的短语。

So in summary, the language is LL(1), but the given grammar for it isn't. 因此,总而言之,语言是LL(1),但给定的语法不是。 Since the language is LL(1) it is possible to find another grammar which is LL(1), and that grammar is not far off from the given one. 由于语言为LL(1),因此可以找到另一种语法为LL(1),并且该语法与给定的语法相距不远。

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

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