作业十----消除左递归


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

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

     E -> E+T | T

     T -> T*F | F

     F -> (E) | i

FIRST集

FIRST(TE')={(,i }

FIRST(+TE')={ + }

FIRST(ε)={ ε }

FIRST(FT')={(,i }

FIRST(*FT')={ * } 

FIRST((E))={( }

FIRST((E)|i)={ 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))={(  }

SELECT(F->i)={ i }

分析符号串 i*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'|ε

1、 FIRST集:

First(aA') = {a}

First(ABe) = {A}

First(ε) = {ε}

First(dB') = {d}

First(bB') = {b}

First(ε) = {ε}

 

2、 FOLLOW集:

Follow(A) = {Be}

Follow(A') = {#}

Follow(B) = {e}

Follow(B') = {#}

 

3、 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

2、 FIRST集:

First(SBa) = {S}

First(b) = {b}

First(bS') = {b}

First(BaS') = {B}

First(ε) = {ε}

First(ab) = {a}

 

2、 FOLLOW集:

Follow(S) = {B}

Follow(S') = {#}

Follow(B) = {a}

 

3、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}

 

课堂练习:

求以下文法的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(S)→{#}

FOLLOW(A)→{p}

 

SELECT集:

SELECT(S→Ap)={a,c,p}

SELECT(A→a)={a}

SELECT(A→ε)={p}

SELECT(A→cA)={c}

SELECT(A→aA)={a}

 

S->Ap
S->Bq
A->a
A->cA
B->b
B->dB

 

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

FIRST集:

FIRST(a)→{a}

FIRST(b)→{b}

FIRST(cA)→{c}

FIRST(dB)→{d}

FIRST(Bq)→{b,d}

FIRST(Ap)→{a,c}

 

FOLLOW集:

FOLLOW(S)→{#}

FOLLOW(A)→{p}

FOLLOW(B)→{q}

 

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}

 

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