让G
成为一种语法:
S -> aBa
B -> bB | \epsilon
其中\epsilon
表示空字符串。
在计算FIRST
和FOLLOW
之后,有没有办法告诉G
是LL(1)
而不诉诸解析表?
答案 0 :(得分:4)
在计算FIRST
变量的FOLLOW
和G
集后,您可以为{{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
等小型语法计算FIRST
和FOLLOW
相比,这通常更容易,也更乏味。
我没有方便的书,因此在某些定义或计算中可能会出错。但这就是我如何处理这个问题。计算G
和FIRST
集得出:
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
。