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

消除左递归

时间:2019-11-15 22:22:48      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:psi   文法   nbsp   select   cup   first   sel   递归   follow   

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

 

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

 

(2)A → baBe | ?
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‘) = { e , # }

FOLLOW(B) = { d }

FOLLOW(B‘) = { d , # }

SELECT集:

SELECT(A → aA‘) = FIRST(aA‘) = { a }

SELECT(A‘ → ABe) = FIRST(ABe) = { a }

SELECT(C‘ → ε) = FIRST(ε) - { ε }∪FOLLOW(A‘) = { d , # }

SELECT(B → dB‘) = FIRST(dB‘) = { d }

SELECT(B‘ → bB‘) = FIRST(bB‘) = { b }

SELECT(B‘ → ε) = FIRST(ε) - {ε}∪FOLLOW(B‘) = { e }

(3) 

S → Aa | b

A → SB

B → ab

S → SBa | b

S → bS‘

S‘ → BaS‘ | ε

B → ab

 

FIRST集:

FIRST(S) = { b }

FIRST(S‘) = {a , ε}

FIRST(bS‘) = { a , ε }

FIRST(B) = { a }

FOLLOW集:

FOLLOW(S) = { # }

FOLLOW(S‘) = { # }

FOLLOW(B) = { a }

SELECT集:

SELECT(S→bS‘) = FIRST(bS‘) = { a , ε }

SELECT(S‘→BaS‘) = { a , b }

SELECT(S‘→ε) = { # }

SELECT(B→ab) = { a }

 

课堂练习:

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

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(A) = { p }

 FOLLOW(S) = { # }

SELECT集:

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

 SELECT(a) = FIRST(a) = { a }

 SELECT(ε) = FOLLOW(A) = { p }

 SELECT(cA) =  FIRST(cA) = { c }

 SELECT(aA) = FIRST(aA) = { a }

 

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

FIRST集:

FIRST(Ap) = { a , c }

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

 

消除左递归

标签:psi   文法   nbsp   select   cup   first   sel   递归   follow   

原文地址:https://www.cnblogs.com/fzwboke/p/11869641.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!