11.13消除左递归
Posted azan1999
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了11.13消除左递归相关的知识,希望对你有一定的参考价值。
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
分析符号串 i*i+i :
FIRST集:
FIRST(E) = { ( , i }
FIRST(E’) = {+ , ε }
FIRST(T) = { ( , i }
FIRST(T’) = { * , ε }
FIRST(F) = { ( , i }
FOLLOW集:
FOLLOW(E) = { ) , # }
FOLLOW(E’) = { ) , # }
FOLLOW(T) = { + , ) ,#}
FOLLOW(T’) = {+ , ) ,#}
FOLLOW(F) = {* , + , ) ,#}
SELECT集:
SELECT (E -> TE’) = FIRST(E) = { ( , 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集
答:
(2)
A -> aABe | a
B -> Bb | d
文法改写:
A -> aA’
A’ -> ABe | ε
B -> dB’
B’ -> bB’ | ε
FIRST集:
FIRST(A) = { a }
FIRST(A’) = { a , ε }
FIRST(B) = { d }
FIRST(B’) = { 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’ -> ε) = FIRST(ε) - {ε} U FOLLOW(A’) = FOLLOW(A’) = { d , # }
SELECT(B -> dB’) = FIRST(dB’) = { d }
SELECT(B’ -> bB’) = FIRST(bB’) = {b }
SELECT(B’ -> ε) = FIRST(ε) - {ε} U FOLLOW(B’) = FOLLOW(B’) = { e }
(3)
S -> SBa | b
B -> ab
文法改写:
S -> bS’
S’ -> BaS’ | ε
B -> ab
FIRST集:
FIRST(S) = { b }
FIRST(S’) = { a , ε }
FIRST(B) = { 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集。
(1)
S->Ap
A->a | ε
A->cA
A->aA
答:
FIRST集:
FIRST(S) = { 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 }
(2)
S->Ap
S->Bq
A->a
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 }
以上是关于11.13消除左递归的主要内容,如果未能解决你的问题,请参考以下文章