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)=
follow(e')=
follow(t)=
follow(t')=
follow(f)=
select集:
select(e→te')=
select(e'→+te')=
select(e'→ε)=
select(t→ft')=
select(t'→*ft')=
select(t'→ε)=
select(f→(e))=
select(f→i)=
分析符號串 i*i+i :
e->t+e'->ft'+e'->i*t'+e'->i*ft'+e'->i*f+e'->i*i+e'->i*i+t+e'->i*i+t->i*i+f->i*i+i
2.p101練習7(2)(3)
(2)a->aabe|a
b->bb|d
消除左遞迴和提取公共因子
a->aa'|a
a'->abe
b->bb'|d
b'->bb'|ε
first集:
first(a)→
first(a')→
first(b)→
first(b')→
follow集:
follow(a)→
follow(a')→
follow(b)→
follow(b')→
select集:
select(a→aa')=
select(a'→abe)=
select(a'→ε)=
select(b→db')=
select(b'→bb')=
select(b'→ε)=
(3)s->aa|b
a->sb
b->ab
消除左遞迴:
s→aa|b→sba|b→bs'
s'→bas'|ε
b→ab
first集:
first(s)→
first(s')→
first(b)→
follow集:
follow(s)→
follow(s')→
follow(b)→
select集:
select(s→bs')=
select(s'→bas')=
select(s'→ε)=
select(b→ab)=
3.求first集
s->ap
a->a |ε
a->ca
a->aa
first集:
first(ap)→
first(a)→
first(ε)→
first(ca)→
first(aa)→
follow集:
follow(s)→
follow(a)→
select集:
select(s→ap)=
select(a→a)=
select(a→ε)=
select(a→ca)=
select(a→aa)=
4.求first集
s->ap
s->bq
a->a
a->ca
b->b
b->db
first集:
first(a)→
first(b)→
first(ca)→
first(db)→
first(bq)→
first(ap)→
follow集:
follow(s)→
follow(a)→
follow(b)→
select集:
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 並分別求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...