第十次作业
1.将以下文法消除左递归,分析符号串 i*i+i 。
E -> E+T | T
T -> T*F | F
F -> (E) | i
解:
文法改写:
E->TE’
E’->+TE’|ε
T->FT’
T’->*FT’|ε
F->(E)|i
FIRST集:
FIRST(E) = { ( , i }
FIRST(E') = { + , ɛ }
FIRST(T) = { ( , i }
FIRST(T') = { * , ɛ }
FIRST(F) = { ( , i }
FOLLOW集:
FOLLOW(E) = { ) , # }
FOLLOW(E') = { ) , # }
FOLLOW(T) = { + , ) , # }
FOLLOW(T') = { + , ) , # }
FOLLOW(F) = { * , + , ) , # }
SELECT集:
SELECT(E -> TE') = { ( , i }
SELECT(E' -> +TE') = { + , ɛ }
SELECT(E' -> ɛ) = { ) , # }
SELECT(T -> FT') = { ( , i }
SELECT(T' -> *FT' ) ={ * , ɛ }
SELECT(T' -> ɛ) = { + , ) , # }
SELECT(F -> (E) ) = { ( , i }
SELECT(F -> i ) = { i }
分析符号串:
2.P101练习7(2)(3)文法改写。
(2)
A->aABe|a
B->Bb|d
解:
A -> aC
C -> ABe | ɛ
B -> dB'
B' -> bB' | ɛ
FIRST集:
FIRST(A) = { a }
FIRST(C) = { ABe , ɛ } = { e , ɛ }
FIRST(ɛ) = { ɛ }
FIRST(B) = { d }
FIRST(B') = { b ,ɛ }
FIRST(ɛ) = { ɛ }
FOLLOW集:
FOLLOW(A) = { d , # }
FOLLOW(B) = { e }
FOLLOW(B') = { e }
FOLLOW(C) = { d , # }
SELECT集:
SELECT(A -> aC) = { a }
SELECT(C -> ABe) = { e , ɛ }
SELECT(C -> ɛ) = { d , # }
SELECT(B -> dB') = { d }
SELECT(B' -> bB') ={ b ,ɛ }
SELECT(B' -> ɛ) ={ e }
(3)
S->Aa|b
A->SB
B->ab
解:
S -> SBa | b
S -> bS'
S' -> BaS' | ɛ
B -> ab
FIRST集:
FIRST(S) = { b }
FIRST(S') = { Ba , ɛ } = { a , ɛ }
FIRST(ɛ) = { ɛ }
FIRST(B) = { a }
FOLLOW集:
FOLLOW(S) = { # }
FOLLOW(S') = { # }
FOLLOW(B) = { b }
SELECT集:
SELECT(S -> bS') = { b }
SELECT(S' -> BaS' ) = { a , ɛ }
SELECT(S' -> ɛ) = { # }
SELECT(B -> ab) = { a }
课堂练习:
3.求以下文法的FIRST集、FOLLOW集和SELECT集。
(1)
S->Ap
A->a |ε
A->cA
A->aA
解:
FIRST集:
FIRST(Ap) = { a , c , p }
FIRST(a) = { a }
FIRST(ɛ) = { ɛ }
FIRST(cA) = { c }
FIRST(aA) = { a }
FOLLOW集:
FOLLOW(A) = { p }
FOLLOW(S) = { # }
SELECT集:
SELECT(S -> Ap) = { a , c , p }
SELECT(A -> a ) = { a }
SELECT(A -> ɛ) = { p }
SELECT(A -> cA) = { c }
SELECT(A -> Aa) ={ a }
(2)
S->Ap
S->Bq
A->a
A->cA
B->b
B->dB
解:
FIRST集:
FIRST(S1) = FIRST(Ap) = { a , c }
FIRST(S2) = FIRST(Bq) = { b , d }
FIRST(a) = { a }
FIRST(cA) = { c }
FIRST(b) = { b }
FIRST(dB) = { d }
FOLLOW集:
FOLLOW(A) = { p }
FOLLOW(B) = { q }
FOLLOW(S) = { # }
SELECT集:
SELECT(S -> Ap) = { a , c }
SELECT(S -> Bq ) = { b , d }
SELECT(A -> a) = { a }
SELECT(A -> cA) = { c }
SELECT(B -> b) ={ b }
SELECT(B -> dB) ={ d }