验证语法是否强大 LL(2)
Sudkamp 的《Languages and Machines》的问题 19.5 要求读者验证语法
G : S' -> S##
S -> aSa | bSb | λ
是否强LL(2)
。变量 S
的 FIRST
和 FOLLOW
集是使用算法 19.5.1(第 583 页,第三版)计算的:
FIRST(2)(S) = {λ,aa,bb,ab,ba}
FOLLOW(2)(S) = {##,a#,b#,aa,bb,ab,ba}
很明显由于规则 S ->
,它产生由 S
规则,S
规则的 length-2 先行设置不会对 S
的 length-2 先行设置进行分区。 λFOLLOW(2)(S)
组成的长度为 2 的先行集合:
LA(2)(S) = {##,a#,b#,aa,bb,ab,ba}
LA(2)(S -> aSa) = {a#,aa,ab}
LA(2)(S -> bSb) = {b#,bb,ba}
LA(2)(S -> λ) = {##,a#,b#,aa,bb,ab,ba}
现在我可能在计算 时犯了错误>FIRST
、FOLLOW
或 LA(2)
设置为 G
。然而,我相当有信心我已经正确执行了算法。特别是,我可以回到他们的定义:
FIRST(2)(S) = trunc(2)({x : S =>* x AND x IN Σ*})
= trunc(2)({uu^R : u IN {a,b}^*})
= {λ,aa,bb,ab,ba}
FOLLOW(2)(S) = trunc(2)({x : S' =>* uSv AND x IN FIRST(2)(v)})
= trunc(2)({x : x IN FIRST(2)({a,b}^*{##})})
= trunc(2)({##,a#,b#,aa,bb,ab,ba})
= {##,a#,b#,aa,bb,ab,ba}
现在的问题是:为什么语法强 LL(2)
。如果 S
规则的 length-2 前瞻集未对 S
的 length-2 Lookahead 集进行分区,则语法不应为强LL(2)
。但我无法得出书中所期望的结论。我不明白什么?
Problem 19.5 of Sudkamp's Languages and Machines asks the reader to verify that the grammar
G : S' -> S##
S -> aSa | bSb | λ
is strong LL(2)
. The FIRST
and FOLLOW
sets for the variable S
are computed using Algorithm 19.5.1 (p. 583, 3rd ed.):
FIRST(2)(S) = {λ,aa,bb,ab,ba}
FOLLOW(2)(S) = {##,a#,b#,aa,bb,ab,ba}
It is clear that the length-2 lookahead sets for the S
rules will not partition the length-2 lookahead set for S
, due to the rule S -> λ
, which gives rise to the length-2 lookahead set consisting of FOLLOW(2)(S)
:
LA(2)(S) = {##,a#,b#,aa,bb,ab,ba}
LA(2)(S -> aSa) = {a#,aa,ab}
LA(2)(S -> bSb) = {b#,bb,ba}
LA(2)(S -> λ) = {##,a#,b#,aa,bb,ab,ba}
Now it is possible that I have made an error in the computation of the FIRST
, FOLLOW
, or LA(2)
sets for G
. However, I'm fairly confident that I have executed the algorithm correctly. In particular, I can revert to their definitions:
FIRST(2)(S) = trunc(2)({x : S =>* x AND x IN Σ*})
= trunc(2)({uu^R : u IN {a,b}^*})
= {λ,aa,bb,ab,ba}
FOLLOW(2)(S) = trunc(2)({x : S' =>* uSv AND x IN FIRST(2)(v)})
= trunc(2)({x : x IN FIRST(2)({a,b}^*{##})})
= trunc(2)({##,a#,b#,aa,bb,ab,ba})
= {##,a#,b#,aa,bb,ab,ba}
Now the question is: why is the grammar strong LL(2)
. If the length-2 lookahead sets for the S
rules do not partition the length-2 lookahead set for S
, then the grammar should not be strong LL(2)
. But I can't reach the conclusion expected by the book. What am I not understanding?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个解决方案。上面给出的语法
G
并不强LL(2)
。要了解这一点,请回忆一下强LL(k)
语法的定义。对于某些k > ,语法
如果,只要有两个最左推导G
是LL(k)
。 0,其中
ui,wi IN Σ*
对于i IN {1,2}
和|z| = k,然后 x = y。考虑上面语法
G
中的以下最左推导:这些推导满足强
LL(2)
语法的定义条件。然而,λ \= aSa
,因此G
并不强LL(2)
。显然,我们可以构建许多最左推导来证明
G
不是强LL(2)
。但还有其他几个原因导致G
不强LL(2)
。例如,很明显,确定性下推自动机无法识别 G,因为无法确定何时开始从堆栈中删除元素。Here is a solution. The grammar
G
given above is not strongLL(2)
. To see this, recall the definition of a strongLL(k)
grammar. A grammarG
isLL(k)
for somek > 0
if, whenever there are two leftmost derivationswhere
ui,wi IN Σ*
fori IN {1,2}
, and|z| = k
, thenx = y
. Consider the following leftmost derivations in the grammarG
above:The derivations satisfy the conditions of the definition of a strong
LL(2)
grammar. However,λ \= aSa
, and consequentlyG
is not strongLL(2)
.Clearly we can build many leftmost derivations that demonstrate that
G
is not strongLL(2)
. But there are several other reasons thatG
is not strongLL(2)
. For instance, it is obvious thatG
cannot be recongized by a deterministic pushdown automata, because there is no way to determine when to begin removing elements from the stack.