如何判断一个文法是LL(1)、LR(0)还是SLR(1)?
如何识别文法是 LL(1)、LR(0) 还是 SLR(1)?
任何人都可以使用这个例子或任何其他例子来解释它吗?
X → Yz |一个
Y → bZ | ε
Z → ε
How do you identify whether a grammar is LL(1), LR(0), or SLR(1)?
Can anyone please explain it using this example, or any other example?
X → Yz | a
Y → bZ | ε
Z → ε
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
要检查语法是否为 LL(1),一种选择是构造 LL(1) 解析表并检查是否有任何冲突。这些冲突可以是
让我们通过为每个非终结符构建 FIRST 和 FOLLOW 集来尝试一下您的语法。在这里,我们得到:
我们还知道 FOLLOW 集合是
由此,我们可以构建以下 LL(1) 解析表:
由于我们可以构建没有冲突的解析表,因此语法为 LL(1)。
为了检查语法是 LR(0) 还是 SLR(1),我们首先为该语法构建所有 LR(0) 配置集。在这种情况下,假设 X 是起始符号,我们得到以下结果:
由此,我们可以看到语法不是 LR(0),因为状态 (1) 中存在移位/归约冲突。具体来说,因为我们有移位项 X → .a 和 Y → .,所以我们无法判断是移动 a 还是减少空字符串。更一般地说,没有 ε-产生式的文法是 LR(0)。
然而,这个文法可能是SLR(1)。为了看到这一点,我们使用特定非终结符的前瞻集来增强每次归约。这返回了这组 SLR(1) 配置集:
状态 (1) 中的移位/归约冲突已被消除,因为我们仅在前瞻为 z 时进行归约,这不会与任何其他项冲突。
To check if a grammar is LL(1), one option is to construct the LL(1) parsing table and check for any conflicts. These conflicts can be
Let's try this on your grammar by building the FIRST and FOLLOW sets for each of the nonterminals. Here, we get that
We also have that the FOLLOW sets are
From this, we can build the following LL(1) parsing table:
Since we can build this parsing table with no conflicts, the grammar is LL(1).
To check if a grammar is LR(0) or SLR(1), we begin by building up all of the LR(0) configurating sets for the grammar. In this case, assuming that X is your start symbol, we get the following:
From this, we can see that the grammar is not LR(0) because there is a shift/reduce conflicts in state (1). Specifically, because we have the shift item X → .a and Y → ., we can't tell whether to shift the a or reduce the empty string. More generally, no grammar with ε-productions is LR(0).
However, this grammar might be SLR(1). To see this, we augment each reduction with the lookahead set for the particular nonterminals. This gives back this set of SLR(1) configurating sets:
The shift/reduce conflict in state (1) has been eliminated because we only reduce when the lookahead is z, which doesn't conflict with any of the other items.
如果没有 FIRST/FIRST 冲突,也没有 FIRST/FOLLOW 冲突,则您的语法是 LL(1)。
FIRST/FIRST 冲突的示例:
仅通过查看第一个输入符号“a”,您无法知道是否应用产生式
S -> 。 Xb
或S -> Yc
,因为“a”位于 X 和 Y 的第一个集合中。FIRST/FOLLOW 冲突的示例:
仅看到第一个输入符号“f”,您无法决定是否应用产生式 <代码>A-> fe 或
A -> ε
,因为“f”既在 A 的 FIRST 集合中,又在 A 的 FOLLOW 集合中(A 可以解析为 ε/empty,B 解析为 f)。请注意,如果您没有 epsilon 产生式,则不会出现 FIRST/FOLLOW 冲突。
If you have no FIRST/FIRST conflicts and no FIRST/FOLLOW conflicts, your grammar is LL(1).
An example of a FIRST/FIRST conflict:
By seeing only the first input symbol "a", you cannot know whether to apply the production
S -> Xb
orS -> Yc
, because "a" is in the FIRST set of both X and Y.An example of a FIRST/FOLLOW conflict:
By seeing only the first input symbol "f", you cannot decide whether to apply the production
A -> fe
orA -> ε
, because "f" is in both the FIRST set of A and the FOLLOW set of A (A can be parsed as ε/empty and B as f).Notice that if you have no epsilon-productions you cannot have a FIRST/FOLLOW conflict.
简单回答:如果关联的 LL(1) 解析表的每个表项中至多有一个产生式,则称该文法是 LL(1)。
由于 [S,b] 包含两个产生式,因此对于选择哪个规则会产生混乱。因此它不是 LL(1)。
一些简单的检查来查看语法是否为 LL(1)。
检查 1:语法不应保留递归。
示例:E --> E+T。不是 LL(1),因为它是左递归。
检查2:语法应该是左因子分解的。
检查3:语法不应含糊不清。
Simple answer:A grammar is said to be an LL(1),if the associated LL(1) parsing table has atmost one production in each table entry.
As [S,b] contains two Productions there is a confusion as to which rule to choose.So it is not LL(1).
Some simple checks to see whether a grammar is LL(1) or not.
Check 1: The Grammar should not be left Recursive.
Example: E --> E+T. is not LL(1) because it is Left recursive.
Check 2: The Grammar should be Left Factored.
Check 3:The Grammar should not be ambiguous.
LL(1) 语法是上下文无关的无歧义语法,可以由 LL(1) 解析器解析。
在LL(1)中,
对于最左推导。 1 代表每次使用一个输入符号
步。
对于检查语法是 LL(1),您可以绘制预测解析表。如果你在表中发现任何多个条目,那么你可以说语法不是 LL(1)。
他们也是检查语法是否为 LL(1) 的捷径。
快捷技术
LL(1) grammar is Context free unambiguous grammar which can be parsed by LL(1) parsers.
In LL(1)
for Left Most Derivation. 1 stands for using one input symbol at each
step.
For Checking grammar is LL(1) you can draw predictive parsing table. And if you find any multiple entries in table then you can say grammar is not LL(1).
Their is also short cut to check if the grammar is LL(1) or not .
Shortcut Technique
通过这两个步骤,我们可以检查它是否为 LL(1)。
双方都必须满意。
1.如果我们有产生式:A->a1|a2|a3|a4|.....|an。
那么,First(a(i)) 交集 First(a(j)) 必须是 phi(空集)[a(i)-a 下标 i.]
2.对于每个非终结符 'A',如果 First(A)包含 epsilon
那么 First(A) 交集 Follow(A) 必须是 phi(空集)。
With these two steps we can check if it LL(1) or not.
Both of them have to be satisfied.
1.If we have the production:A->a1|a2|a3|a4|.....|an.
Then,First(a(i)) intersection First(a(j)) must be phi(empty set)[a(i)-a subscript i.]
2.For every non terminal 'A',if First(A) contains epsilon
Then First(A) intersection Follow(A) must be phi(empty set).