1.将以下文法消除左递归,分析符号串 i*i+i 。

   并分别求FIRST集、FOLLOW集,和SELECT集

     E -> E+T | T

     T -> T*F | F

     F -> (E) | i

 解:消除左递归:

(1) E->TE'

     E'->+TE'|ε

(2) T->FT'

      T'->*FT'|ε

 (3) F->(E)|i

① FIRST集:

First(TE') = {T}

First(+TE') = {+}

First(ε) = {ε}

First(FT') = {F}

First(*FT') = {*}

First(ε) = {ε}

First((E)) = {( }

First(i) = {i}

② FOLLOW集:

Follow(E) = {)}

Follow(E') = {#}

Follow(T) = {E'}

Follow(T') = {#}

Follow(F) = {#}

③ SELECT集:

Select(E->TE') = First(TE') = {T}

Select(E'->+TE') = First(+TE') = {+}

Select(E'->ε) = (First(ε) = {ε})∪Follow(E') = {)}

Select(T->FT') = First(FT') = {F}

Select(T'->*FT') = First(+TE') = {*}

Select(T'->ε) = (First(ε) = {ε})∪Follow(T') = {#}

Select( F->(E)) = First((E)) = {( }

Select( F->i) = First(i) = {i}

 

 

 

 

 

 

 

2.P101练习7(2)(3)文法改写,并分别求FIRST集、FOLLOW集,和SELECT集

 (2) A->aABe|a

      B->Bb|d

提取左公因子:A->aA'      A'->ABe|ε

消除左递归:B->dB'          B'->bB'|ε

① FIRST集:

First(aA') = {a}

First(ABe) = {A}

First(ε) = {ε}

First(dB') = {d}

First(bB') = {b}

First(ε) = {ε}

② FOLLOW集:

Follow(A) = {Be}

Follow(A') = {#}

Follow(B) = {e}

Follow(B') = {#}

③ SELECT集:

Select(A->aA') = First(aA') = {a}

Select(A'->ABe) = First(ABe) = {A}

Select(A'->ε) = First(ε) = {ε}∪Follow(A') = {#}

Select(B->dB') = First(dB') = {d}

Select(B'->bB') = First(bB') = {b}

Select(B'->ε) = First(ε) = {ε}∪Follow(B') = {#}

(3) S->Aa|b

     A->SB

     B->ab

A->SB代入S->Aa|b可得:S->SBa|b

消除左递归:S->bS'      S'->BaS'|ε     B->ab

① FIRST集:

First(SBa) = {S}

First(b) = {b}

First(bS') = {b}

First(BaS') = {B}

First(ε) = {ε}

First(ab) = {a}

② FOLLOW集:

Follow(S) = {B}

Follow(S') = {#}

Follow(B) = {a}

③ SELECT集:

Select(S->SBa) = First(SBa) = {S}

Select(S->b) = First(b) = {b}

Select(S->bS') = First(bS') = {b}

Select(S'->BaS') = First(BaS') = {B}

Select(S->ε) = First(ε) = {ε}∪Follow(S') = {#}

Select(B->ab) = First(ab) = {a}

 

 

 

 

 

 

 

 

 

 

3.课堂练习:

求以下文法的FIRST集、FOLLOW集和SELECT集。

S->Ap
A->a |ε
A->cA

A->aA

① FIRST集:

First(S) = First(Ap) = {a,c,p}

First(a) = {a}

First(ε) = {ε}

First(cA) = {c}

First(aA) = {a}

② FOLLOW集:

Follow(S) = {#}

Follow(A) = {p}

 ③ SELECT集:

Select(S->Ap)= First(Ap) = {a,c,p}

Select(A->a)= First(a) = {a}

Select(A->ε)= First(ε) = {ε}∪Follow(A) = {p}

Select(A->cA)= First(cA) = {c}

Select(A->aA)= First(aA) = {a}

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(S) = {#}

Follow(A) = {p}

Follow(B) = {q}

③ SELECT集:

Select(S->Ap) = Fisrt(Ap) = {A}

Select(S->Bq) = Fisrt(Bq) = {B}

Select(A->a) = Fisrt(a) = {a}

Select(A->cA) = Fisrt(cA) = {c}

Select(B->b) = Fisrt(b) = {b}

Select(B->dB) = Fisrt(dB) = {d}

 

posted on 2019-11-13 11:08  跛豪丶  阅读(244)  评论(0编辑  收藏  举报