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
FRIST集: FRIST( TE' ) -> { ( , i }
FRIST( +TE' ) -> { + }
FRIST( ε ) -> { ε }
FRIST( FT' ) -> { F }
FRIST( *FT' ) -> { * }
FRIST( ε ) -> { ε }
FRIST( (E) ) -> { ( }
FRIST( i ) -> { i }
FOLLOW集: FOLLOW( E ) -> { ) }
FOLLOW( E' ) -> { # }
FOLLOW( F ) -> { * }
FOLLOW( T ) -> { + }
FOLLOW( T' ) -> { * }
SELECT集: SELECT( E -> TE' ) -> { ( , i }
SELECT( E' -> +TE' ) -> { + }
SELECT( E' -> ε ) -> { # }
SELECT( T -> FT' ) -> { F }
SELECT( T' -> *FT' ) -> { * }
SELECT( T' -> ε ) -> { * }
SELECT( F -> (E) ) -> { ( }
SELECT( F -> i ) -> { i }
分析符号串 i * i + i
2.P101练习7(2)(3)文法改写,并分别求FIRST集、FOLLOW集,和SELECT集
(2) A -> aA'
A' -> aA'Be | ε
B -> dB'
B' -> bB' | ε
FIRST集: FIRST( aA' ) -> { a }
FIRST( aA'Be ) -> { a}
FIRST( ε ) -> { ε }
FIRST( dB' ) -> { d }
FIRST( bB' ) -> { b }
FIRST( ε ) -> { ε }
FOLLOW集: FOLLOW( A ) -> { # }
FOLLOW( A' ) -> { e }
FOLLOW( B ) -> { e }
FOLLOW( B' ) -> { b , # }
SELECT集: SELECT( A -> aA' ) -> { a }
SELECT( A' -> aA'Be ) -> { a }
SELECT( A' -> ε ) -> { e }
SELECT( B -> dB' ) -> { d }
SELECT( B' -> bB' ) -> { b }
SELECT( B' -> ε ) -> { b , # }
(3) S -> Aa | b -> Saba | b
S -> bS'
S' -> abaS'
FIRST集: FIRST( bS' ) -> { b }
FIRST( abaS' ) -> { a }
FOLLOW集: FOLLOW( S ) -> { # }
FOLLOW( S' ) -> { # }
SELECT集: SELECT( S -> bS' ) -> { b }
SELECT( S' -> abaS' ) -> { a }
课堂练习:
求以下文法的FIRST集、FOLLOW集和SELECT集。
S->Ap
A->a | ε
A->cA
A->aA
解:FIRST( Ap ) = { a,c,p }
FIRST( a ) = { a }
FIRST( ε ) = { ε }
FIRST( cA ) = { c }
FIRST( aA ) = { a }
FOLLOW( S ) = { # }
FOLLOW( A ) = { p }
SELECT( S -> Ap ) -> { a,c,p }
SELECT( S -> a ) -> { a }
SELECT( S -> ε ) -> { p }
SELECT( S -> cA ) -> { c }
SELECT( S -> aA ) -> { a }
S->Ap
S->Bq
A->a
A->cA
B->b
B->dB
解:FIRST( Ap ) = { a,c }
FIRST( Bq ) = { b,d }
FIRST( a ) = { a }
FIRST( cA ) = { c }
FIRST( b ) = { b }
FIRST( dB ) = { d }
FOLLOW( S ) = { # }
FOLLOW( A ) = { p }
FOLLOW( b ) = { q }
SELECT( S -> Ap ) -> { a,c }
SELECT( S -> Bq ) -> { b,d }
SELECT( S -> a ) -> { a }
SELECT( S -> cA ) -> { c }
SELECT( S -> b ) -> { b }
SELECT( S -> dB ) -> { d }