第十次作业:消除左递归

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

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

     E -> E+T | T

     T -> T*F | F

     F -> (E) | i

 解:

E->TE'

E'->+TE'|ε

T->FT'

T'->*FT'|ε

F->(E)|i

分析符号串 i*i+i:

 

FIRST集:

FIRST(TE') = {(,i}

FIRST(+TE') = {+}

FIRST(FT') = { ( , i }

FIRST(*FT') = { * }

FIRST((E) ) = { ( }

FIRST(i) = { i }

 

FOLLOW集:

FOLLOW(E) = { ) }

FOLLOW(E') = { # }

FOLLOW(T) = { # }

FOLLOW(T') = { # }

FOLLOW(F) = { # }

 

SELECT集:

SELECT(E→TE') = { ( , i }

SELECT(E'→+TE') = { + }

SELECT(E'→ε) = (FIRST(ε)-{ ε }) U FOLLOW(E') = FOLLOW(E') = { # }

SELECT(T→FT') = { T' }

SELECT(T'→*FT')={ * }

SELECT(T'→ε) = (FIRST(ε)-{ ε }) U FOLLOW(T') = FOLLOW(T')={ # }

SELECT(F→(E)) = { ( }

SELECT(F→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}

 

FOLLOW集:

FOLLOW(A)={e}

FOLLOW(A')={#}

FOLLOW(B)={e}

FOLLOW(B')={#}

 

SELECT集:

SELECT(A->aA')=FIRST(aA')={a}

SELECT(A'->ABe)=FIRST(ABe)={a}

SELECT(A'->ε)=FIRST(ε)-{ε}UFOLLOW(A')=FOLLOW(A')={#}

SELECT(B->dB')=FIRST(dB')={d}

SELECT(B'->bB')=FIRST(bB')={b}

SELECT(B'->ε)=FIRST(ε)-{ε}UFOLLOW(B')=FOLLOW(B')={#}

 (3)

S->Aa | b

A->SB

B->ab

文法改写:

S->bS'

S'->BaS' | ε

B->ab 

 

FIRST集:

FIRST(bS')={b}

FIRST(BaS')={a}

FIRST(ε)={ε}

FIRST(ab)={a}

 

FOLLOW集:

FOLLOW(S)={#}

FOLLOW(S')={#}

FOLLOW(B)={a}

 

SELECT集:

SELECT(S->bS')=FIRST(bS')={b}

SELECT(S'->BaS')=FIRST(BaS')={a}

SELECT(S'->ε)=FIRST(ε)-{ε}UFOLLOW(S')=FOLLOW(S')={#}

SELECT(B->ab)=FIRST(ab)={a}

 

课堂练习:

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

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

A->aA

 

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

解:

First(Ap)={a,c,p}      FOLLOW(A)={P}     SELECT(S->Ap)=FIRST(Ap)

First(a)={a}         FOLLOW(S)={#}     SELECT(A->a)=FIRST(a)

First(ε)={ε}                     SELECT(A->ε)=FIRST(ε)

First(cA)={c}                     SELECT(A->cA)=FIRST(cA)

First(aA)={a}                     SELECT(A->aA)=FIRST(aA)

 

First(Ap)={a,c,p}      FOLLOW(A)={P}     SELECT(S->Ap)=FIRST(Ap)

First(a)={a}         FOLLOW(S)={#}     SELECT(A->a)=FIRST(a)

First(b)={b}         FOLLOW(B)={q}      SELECT(S->Bq)=FIRST(Bq)

First(cA)={c}                     SELECT(A->cA)=FIRST(cA)

First(dB)={d}                     SELECT(B->dB)=FIRST(dB)

First(Bq)={b,d,q}                    SELECT(B->b)=FIRST(b)

posted @ 2019-11-15 20:08  菠蘿啤  阅读(156)  评论(0编辑  收藏  举报