码迷,mamicode.com
首页 > 其他好文 > 详细

编译原理10 消除左递归

时间:2019-11-15 10:43:24      阅读:87      评论:0      收藏:0      [点我收藏+]

标签:练习   nbsp   div   文法   bsp   ilo   eps   字符串   不可   

 

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

 

 

分析字符串如下:

 技术图片

 

 

 

 

 

 

 

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’) = FIRST(TE’) = { ( , i }

SELECT(E’ -> +TE’) = FIRST(+TE’) = { + }

SELECT(E’ -> ε) = FIRST(ε) - {ε} U FOLLOW(E’) = FOLLOW(E’) = { ) , # }

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

SELECT(T’ -> *FT’) = FIRST(*FT’) = { * }

SELECT(T’ -> ε) = FIRST(ε) - {ε} U FOLLOW(T’) = FOLLOW(T’) = { + , ) ,# }

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

SELECT(F -> i) = FIRST(i) = { i }

 

 

 

 

 

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

 原题:

(1)A->aABe|a

B->Bb|d

 

消除左递归得:

 A -> aA’

 A’ -> ABe | ε

 B -> dB’

 B’ -> bB’ | ε

 

 FIRST集:

FIRST(aA‘)={a}

FIRST(ABe)= FIRST(aA’Be)={a}

FIRST(ε)={ε}

FIRST(dB‘)={d}

FIRST(bB‘)={b}

 

FOLLOW集:

FOLLOW(A)={d,#}

FOLLOW(A‘)={d,#}

FOLLOW(B)={e,#}

FOLLOW(B‘)={e,#}

 

SELECT集:

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

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

SELECT(A‘->ε) =FOLLOW(A‘)={d,#}

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

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

SELECT(B‘->ε) =FOLLOW(B‘)={e}

 

(3)S->Aa|b

A->SB

B->ab

 

S -> SBa | b

B -> ab

文法改写:

S -> bS’

S’ -> BaS’ | ε

B -> ab

 

 FIRST集:

FIRST(bS’) = { b }

FIRST(BaS’) = { a , ε }

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(ε) - {ε} U FOLLOW(S’) = FOLLOW(S’) = { # }

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

 

 

 

 

课堂练习:

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

S->Ap
A->a |ε  # 有ε空 可跳P
A->cA

A->aA

 

FIRST集:

FIRST(AP) = { a , c , p }

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

 

FOLLOW集:

FOLLOW(S) = { # }

FOLLOW(A) = { p }

 

SELECT集:

SELECT(S -> Ap) = FIRST(Ap) = { a , c , p }

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

SELECT(A -> ?) = FIRST(ε) - {ε} U FOLLOW(A) = FOLLOW(A) = { p }

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

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

 

 

 

S->Ap
S->Bq
A->a   #A无 ε不可跳P
A->cA
B->b
B->dB

 

FIRST集:

FIRST(S1) = FIRST(Ap) = { a , c }

FIRST(S2) = FIRST(Bq) = { b , d }

FIRST(A) = { a , c}

FIRST(B) = { b ,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 }

编译原理10 消除左递归

标签:练习   nbsp   div   文法   bsp   ilo   eps   字符串   不可   

原文地址:https://www.cnblogs.com/longlog/p/11864854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
分享档案
周排行
mamicode.com排行更多图片
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!