LL(1)语法验证

时间:2011-06-18 15:53:06

标签: parsing compiler-construction grammar

G成为一种语法:

S -> aBa
B -> bB | \epsilon

其中\epsilon表示空字符串。

在计算FIRSTFOLLOW之后,有没有办法告诉GLL(1)而不诉诸解析表?

1 个答案:

答案 0 :(得分:4)

在计算FIRST变量的FOLLOWG集后,您可以为{{1}的变量和规则计算长度1前瞻集LA(1) }}。如果满足以下条件,G就会很强G

LL(1)

或者,您可以在不计算LA(1)(A -> wi) partition LA(1)(A) for each variable A such that A -> wi is a rule. G集的情况下,从强LL(1)语法的定义中证明LL(k)FIRST。与FOLLOW等小型语法计算FIRSTFOLLOW相比,这通常更容易,也更乏味。

我没有方便的书,因此在某些定义或计算中可能会出错。但这就是我如何处理这个问题。计算GFIRST集得出:

FOLLOW

计算变量和规则的长度1前瞻集给出:

FIRST(1)(S) = trunc(1)({x : S =>* x AND x IN Σ*})
            = trunc(1)({ab^na : n >= 0})
            = {a}

FIRST(1)(B) = trunc(1)({x : B =>* x AND x IN Σ*})
            = trunc(1)({b^n : n >= 0})
            = {ε,b}

FOLLOW(1)(S) = trunc(1)({x : S =>* uSv AND x IN FIRST(1)(v)})
             = trunc(1)({x : x IN FIRST(1)(ε)})
             = trunc(1)(FIRST(1)(ε))
             = {ε}

FOLLOW(1)(B) = trunc(1)({x : S =>* uBv AND x IN FIRST(1)(v)})
             = trunc(1)({x : x IN FIRST(1)(a)})
             = trunc(1)(FIRST(1)(a))
             = {a}

由于LA(1)(S) = trunc(1)(FIRST(1)(S)FOLLOW(1)(S)) = trunc(1)({a}{ε}) = trunc(1){a} = {a} LA(1)(B) = trunc(1)(FIRST(1)(B)FOLLOW(1)(B)) = trunc(1)({ε,b}{a}) = trunc(1){a,b} = {a,b} LA(1)(S -> aBa) = trunc(1)(FIRST(1)(a)FIRST(1)(B)FIRST(1)(a)FOLLOW(1)(S)) = trunc(1){a} = {a} LA(1)(B -> bB) = trunc(1)(FIRST(1)(b)FIRST(1)(B)) = trunc(1){b} = {b} LA(1)(B -> ε) = trunc(1)(FIRST(1)(ε)FOLLOW(1)(b)) = trunc(1)({ε}{a}) = {a} LA(1)(B -> ε)分区LA(1)(B -> bB)LA(1)(B)分区LA(1)(S -> aBa)LA(1)(S)很强G