十 消除左遞迴

2022-09-07 00:21:20 字數 2918 閱讀 4950

1.將以下文法消除左遞迴,分析符號串 i*i+i 。

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')=

first(+te')=

first(ε)=

first(ft')=

first(*ft')=

first((e))=

first(i)=

follow集:

follow(e)=

follow(e')=

follow(t)=

follow(t')=

follow(f)=

select集:

select(e→te')=first(te')=

select(e'→+te')=first(+te')=

select(e'→ε)=(first(ε)-)∪follow(e')=

select(t→ft')=first(ft')=

select(t'→*ft')=first(*ft')=

select(t'→ε)=(first(ε)-)∪follow(t')=

select(f→(e))=first((e))=

select(f→i)=first(i)=

分析符號串i*i+i:

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

(2)a→aabe|a

b→bb|d

消除左遞迴:

a→ac'

a'→abe| ε

b→db'

b'→bb' | ε

first集:

first(aa')=

first(abe)=

first(ε)=

first(db')=

first(bb')=

follow集:

follow(a)=

follow(a')=

follow(b)=

follow(b')=

select集:

select(a→aa')=first(aa')=

select(a'→abe)=first(abe)=

select(a'→ε)=(first(ε)-)∪follow(a')=

select(b→db')=first(db')=

select(b'→bb')=first(bb')=

select(b'→ε)=(first(ε)-)∪follow(b')=

(3)s→aa|b

a→sb

b→ab

消除左遞迴:

s→bs'

s'→bas' | ε

b→ab

first集:

first(sba)=

first(b)=

first(bs')=

first(bas)=

first(ε)=

first(ab)=

follow集:

follow(s)=

follow(s')=

follow(b)=

select集:

select(s→sba)=first(sba)=

select(s→b)=first(b)=

select(s→bs')=first(bs')=

select(s'→bas')=first(bas')=

select(s'→ε)=(first(ε)-)∪follow(s')=

select(b→ab)=first(ab)=

課堂練習:

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

s->ap

a->a |ε

a->ca

a->aa

first集:

first(ap) =

first(a) =

first(ε) =

first(ca) =

first(aa) =

follow集:

follow(a) =

follow(s) =

select集:

select(s→ap) = first(ap) =

select(a) = first(a) =

select(ε) = follow(a) =

select(ca) =  first(ca) =

select(aa) = first(aa) =

s->ap

s->bq

a->a

a->ca

b->b

b->db

first集:

first(ap) =

first(bp) =

first(a) =

first(ca) =

first(b) =

first(db) =

follow集:

follow(s) =

follow(a) =

follow(b) =

select集:

select(s->ap) = first(ap) =

select(s->bq) = first(bq) =

select(a->a) = first(a) =

select(a->ca) = first(ca) =

select(b->b) = first(b) =

select(b->db) = first(db) =

作業十 消除左遞迴

提交作業 1.將以下文法消除左遞迴,分析符號串 i i i 並分別求first集 follow集,和select集 e e t t t t f f f e i first集 first te first te first first ft first ft first e first e i fol...

消除左遞迴

參考關健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...