一个文法G是LL(1)的,当且仅当G的任意两个不同的产生式A -> α | β 满足下面的条件:
举个小栗子
文法G[E]:
(1) FIRST集合(这里只求非终结符号的FIRST集合)
FIRST(E) = { ( , a , b , ∩ }
FIRST(T) = { ( , a , b , ∩ }
FIRST(F) = { ( , a , b , ∩ };
FIRST§ = { ( , a , b , ∩ };
FIRST(E’) = { + , ε };
FIRST(T’) = { ( , a , b , ∩ , ε };
FIRST(F’) = { * , ε };
(2) FOLLOW集合
FOLLOW(E) = FOLLOW(E’) + { ) ,$}
FOLLOW(E’) = FOLLOW(E) = { ) ,$ }
FOLLOW(T) = FIRST(E’) / ε +FOLLOW(T’) = { + , ) , $ }
FOLLOW(T’) = FOLLOW(T) = { + , ) , $ }
FOLLOW(F) = FIRST(T’) / ε +FOLLOW(T) = { ( , a , b , ∩ , + , ) ,$ }
FOLLOW(F’) = FOLLOW(F) = { ( , a , b , ∩ , + , ) ,$ }
FOLLOW§ = FIRST(F’) / ε + FOLLOW(F) = {* , ( , a , b , ∩ , + , ), $ }
注:关于FIRST集合和FOLLOW集合的计算,请参照这篇博客
编译原理之计算FIRST集合和FOLLOW集合
(3) 证明是LL(1)文法
对于E’-> +E’| ε ,( FIRST(+E’) = { + } ) ∩ ( FIRST( ε ) = { ε } )= ∅
对于T’-> T| ε ,( FIRST(T) = { ( , a , b , ∩ } ) ∩ ( FIRST( ε ) = { ε } )= ∅
对于F’ -> *F’| ε ,( FIRST(*F’) = { * } ) ∩ ( FIRST( ε ) = { ε } )= ∅
对于P -> (E) | a | b | ∩ ,( FIRST( (E) ) = { ( } ) ∩ ( FIRST( a ) = { a } ) ∩ ( FIRST( b ) = { b } ) ∩ ( FIRST( ∩ ) = { ∩ } )= ∅
对于E’-> +E| ε , (FIRST(+E) = { + }) ∩ (FOLLOW(E’) = { ) ,$ }) = ∅
对于T’-> T| ε , (FIRST(T) =( , a , b , ∩) ∩ (FOLLOW(T’) = {+ , ) , $ }) = ∅
对于F’ -> *F’| ε , (FIRST(*F’) = { * }) ∩ (FOLLOW(F’) = { ( , a , b , ∩ , + , ) ,$ }) = ∅
作者:Jane_96
来源:CSDN
原文:https://blog.csdn.net/Jane_96/article/details/79896085
版权声明:本文为博主原创文章,转载请附上博文链接!