第十次作业-消除左递归

1.将以下文法消除左递归,求FIRST集、FOLLOW集,并分析符号串 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))={ ( }

First(i)={ i }

First(ε)={ε}

First(*FT')={*}

First(FT')={ (,i }

First(+TE')={+}

First(TE')={ (,i }

 

FOLLOW集:

Follow(E)={ ) }

Follow(E')={#}

Follow(T)={+,ε}

Follow(T')={#}

Follow(F)={ * ,ε}

 

SELECT集:

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

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

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

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

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

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

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

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

 

 分析符号串 i*i+i

 

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

题目:

(2)A->aABe|a

    B->Bb|d

(3)S->Aa|b

    A->SB

    B->ab

 解:(2) A->aA'

A'->aA'Be|ε

B->dB'

B'->bB'|ε

FIRST集:

First(aA')={a}

First(aA'Be)={a}

First(dB')={d}

First(bB')={b}

First(ε)={ε}

 

FOLLOW集:

Follow(A)={#}

Follow(A')={e}

Follow(B)={e}

Follow(B')={#}

 

SELECT集:

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

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

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

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

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

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

 

(3)先将A、B代入得:S->Saba|b

S->bS'

S'->abaS'

 

FIRST集:

First(abaS')={a}

First(bS')={b}

 

FOLLOW集:

Follow(S)={#}

Follow(S')={#}

 

SELECT集:

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

Select(S'->abaS')=First(abaS')={a}

 

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

(1)S->Ap
A->a |ε
A->cA

A->aA

FIRST集:

First(A)={a,c,ε}

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}

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

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

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

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

 

(2)S->Ap
S->Bq
A->a
A->cA
B->b
B->dB

FIRST集:

First(B)={b,d}

First(A)={a,c}

First(Ap)={a,c}

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)=First(Ap)={a,c}

Select(S->Bq)=First(Bq)={b,d}

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

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

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

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

你可能感兴趣的:(第十次作业-消除左递归)