1. 文法 G(S):
(1)S -> AB
(2)A ->Da|ε
(3)B -> cC
(4)C -> aADC |ε
(5)D -> b|ε
验证文法 G(S)是不是 LL(1)文法?
First(Da)={b,a} Follow(A)={c,b,a,#} SELECT(A->Da)={b,a}
First(ε)={ε} Follow(C)={#} SELECT(A->ε)={c,b,a,#}
First(aADC)={a} Follow(D)={a,#} SELECT(C->aADC)={a}
First(b)={b} select(C->ε)={#}
select(D->b)={b}
select(D->c)={a,#}
因为:
SELECT(A->Da)∩SELECT(A->ε)!=空
SELECT(C->aADC)∩SELECT(C->ε)=空
SELECT(D->b)∩SELECT(D->ε)=空
所以G(S)不是LL(1)文法。
2.法消除左递归之后的表达式文法是否是LL(1)文法?
Select(E' -> +TE') = First(+TE') = {+}
Select(E' -> ε) = (First(ε)-{ε})∪Follow(E') = {),#}
Select(T' -> *FT') = First(*FT') = {*}
Select(T' -> ε) = (First(ε)-{ε})∪Follow(T') = {#,+,)}
Select(F -> (E)) = First((E)) = {(}
Select(F -> i ) = First(i) = {i}
因为:Select(E' -> +TE') ∩ Select(E' -> ε) = ∅
Select(T' -> *FT') ∩ Select(T' -> ε) = ∅ Select(F -> (E)) ∩ Select(F -> i ) = ∅
所以:文法G‘(s)是LL(1)文法。
3.接2,如果是LL(1)文法,写出它的递归下降语法分析程序代码。
E()
{T();
E'();
}
E'()
T()
T'()
F()