消除左遞迴

2022-08-31 20:39:08 字數 1987 閱讀 3326

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->i | (e)

first(te')=

first(+te')=

first(ε)=

first(ft')=

first(*ft')=

first(i)=

first((e))=

follow(e)=

follow(e')=

follow(t)=

follow(t')=

follow(f)=

select(te')=

select(+te')=

select(ε)=

select(ft')=

select(*ft')=

select(i)=

select(f)=

2.p101練習7(2)(3)文法改寫,並分別求first集、follow集,和select集

(2)a->aa'

a'->abe | ε

b->db'

b'->bb' | ε

first( aa')=

first( abe)=

first( ε)=

first(db')=

first(bb')=

follow(a)=

follow(a')=

follow(b)=

follow(b')=

select( a->aa')=

select( a'->abe)=

select( a'->ε)=

select(b->db')=

select(b'->bb')=

select(b'->ε)=

(3)s->bs'

s'->bas' | ε

b->ab 

first(bs')=

first(bas')=

first(ε)=

first(ab)=

follow(s)=

follow(s')=

follow(b)=

select(s->bs')=

select(s'->bas')=

select(s'->ε)=

select(b->ab)=

課堂練習:

求以下文法的first集、follow集和select集。

(1)s->ap

a->a |ε

a->ca

a->aa

first(ap)=

first(a)=

first(ε)=

first(ca)=

first(aa)=

follow(s)=

follow(a)=

select(s->ap)=

select(a->a)=

select(a->ε)=

select(a->ca)=

select(a->aa)=

(2)s->ap

s->bq

a->a

a->ca

b->b

b->db

first(ap)=

first(bq)=

first(a)=

first(c)=

first(b)=

first(db)=

follow(s)=

follow(a)=

follow(b)=

select(s->ap)=

select(s->bq)=

select(a->a)=

select(a->ca)=

select(b->b)=

select(b->db)=

消除左遞迴

參考關健young的部落格,左遞迴形式為 1 a a a vn,v 2 a b b a a b vn,v 稱其為左遞迴。其中可以採取修改左遞迴為右遞迴 a a 其中 非空,不以a打頭 可寫為 a a a a 即 a a 1 a 2 a m 1 2 n 其中,i 1 i m 均不為空,j 1 j n ...

消除左遞迴

first集的求法 對於文法g的任一符號串 x1x2 xn可按下列步驟構造其first 集合 1 置first 2 將first x1 中的一切非 符號加進first 3 若 first x1 將first x2 中的一切非 符號加進first 若 first x1 和first x2 將first...

消除左遞迴

1.將以下文法消除左遞迴,並分析符號串 i i i e e t t t t f f f e i 消除左遞迴 e t e e te t ft t ft f e i first集 first e first e first t first t first f follow集 follow e follo...