验证语法是否强大 LL(2)

发布于 2024-11-16 03:04:26 字数 1379 浏览 3 评论 0原文

Sudkamp 的《Languages and Machines》的问题 19.5 要求读者验证语法

G : S' -> S##
    S  -> aSa | bSb | λ

是否强LL(2)。变量 SFIRSTFOLLOW 集是使用算法 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}

现在我可能在计算 时犯了错误>FIRSTFOLLOWLA(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

相守太难 2024-11-23 03:04:26

这是一个解决方案。上面给出的语法G并不强LL(2)。要了解这一点,请回忆一下强 LL(k) 语法的定义。对于某些k > ,语法GLL(k)。 0 如果,只要有两个最左推导

S =>* u1Av1 => u1xv1 =>* uzw1          S =>* u2Av2 => u2yv2 =>* u2zw2

,其中 ui,wi IN Σ* 对于 i IN {1,2}|z| = k,然后 x = y。考虑上面语法 G 中的以下最左推导:

S =>* aaSaa##  (u1 = aa, v1 = aa##)    S =>* baSab##   (u2 = ba, v2 = ab##)
  =>1 aaaa##   (x = λ)                   =>1 baaSaab## (y = aSa)
  =>* aaaA##   (z = aa, w1 = aa##)       =>* baaaab##  (z = aa, w2 = ab##)

这些推导满足强 LL(2) 语法的定义条件。然而,λ \= aSa,因此G 并不强LL(2)

显然,我们可以构建许多最左推导来证明 G 不是强LL(2)。但还有其他几个原因导致G 不强LL(2)。例如,很明显,确定性下推自动机无法识别 G,因为无法确定何时开始从堆栈中删除元素。

Here is a solution. The grammar G given above is not strong LL(2). To see this, recall the definition of a strong LL(k) grammar. A grammar G is LL(k) for some k > 0 if, whenever there are two leftmost derivations

S =>* u1Av1 => u1xv1 =>* uzw1          S =>* u2Av2 => u2yv2 =>* u2zw2

where ui,wi IN Σ* for i IN {1,2}, and |z| = k, then x = y. Consider the following leftmost derivations in the grammar G above:

S =>* aaSaa##  (u1 = aa, v1 = aa##)    S =>* baSab##   (u2 = ba, v2 = ab##)
  =>1 aaaa##   (x = λ)                   =>1 baaSaab## (y = aSa)
  =>* aaaA##   (z = aa, w1 = aa##)       =>* baaaab##  (z = aa, w2 = ab##)

The derivations satisfy the conditions of the definition of a strong LL(2) grammar. However, λ \= aSa, and consequently G is not strong LL(2).

Clearly we can build many leftmost derivations that demonstrate that G is not strong LL(2). But there are several other reasons that G is not strong LL(2). For instance, it is obvious that G cannot be recongized by a deterministic pushdown automata, because there is no way to determine when to begin removing elements from the stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文