消除左递归

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

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

     E -> E+T | T

     T -> T*F | F

     F -> (E) | i

 

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

 

 

课堂练习:

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

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

A->aA

 

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

 

1、

E->TE’

E’->+TE’|ε

T->FT’

T’->*FT’|ε

F->(E)|i

 

解:First(E) = {(,i}

  First(E’) = {+,ε}

       First(T) = ((,i)

       First(T') = {*,ε}

       First(F) = {(,i}

 

 

解:FOLLOW(E) = {#,)}

  FOLLOW(E') = {#,)}

       FOLLOW(T) = {+,#,)}

  FOLLOW(T') = {+,#,)}

       FOLLOW(F) = {+,*,#,)}

解:

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

SELECT(E->+TE') = {+}

SELECT(E'->ε) = {),#}

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

SELECT(T'->*FT') = {*}

SELECT(T'->ε) = {(,i,#}

SELECT(F->(E)) = {(}

SELECT(F->I) = {i}

 消除左递归_第1张图片

 2、(2)A->aA'

A'->ABe|ε

B->Bb|d

解:First(A) = {a}

First(A') = {a,ε}

First(B) = {d,b}

 

解:FOLLOW(B) = {e,b}

FOLLOW(A) = {a}

 

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

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

SELECT(A'->ε) = {a}

SELECT(B->Bb) = {b}

SELECT(B->d) = {d}

因为A'的产生式有交集a,所以不是LL(1)文法

 

2、(3)

将A、B带入S

S->Saba|b

 

消除:S->bS'

S'->abaS'|ε

解:

First(S) = {b}

First(S') = {a,ε}

 

解:无FOLLOW

 

解:

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

SELECT(S'->abaS') = {a,ε}

SELECT(S->ε) = {ε}

因为S'的产生式有交集ε,所以不是LL(1)文法

 

 

 

 

 

 

 3、

解:First(Ap) = {a,c,p}

First(a) = {a}

First(ε) = {ε}

First(cA) = {c}

First(aA) = {a}

 

解:

  FOLLOW(A) = {p}

 

解:SELECT(S->Ap) = {a }

SELECT(A->a) = {a}

SELECT(A->ε) = {p}

SELECT(A->cA) = {c}

SELECT(A->aA) = {a}

 

 4、

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

  FOLLOW(A) = {p}

  FOLLOW(B) = {q}

 

 解: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}

你可能感兴趣的:(消除左递归)