《例LR分析方法-构造LR项目集规范族.doc》由会员分享,可在线阅读,更多相关《例LR分析方法-构造LR项目集规范族.doc(4页珍藏版)》请在金锄头文库上搜索。
1、例 4-12:LR分析器举例设如下LR(1)文法的LALR 分析表已给定如下,试分析输入串bab的合法性: 1) S B B2) B a B3) B bLALR 分 析 表状态 动作表 状态转移表 a b # S B 0 s3 s4 1 2 1 acc 2 s3 s4 5 3 s3 s4 6 4 r3 r3 r3 5 r1 6 r2 r2 r2 解:分析过程如下符号栈 状态 输入 动作说明# 0 bab# action(0,b) =s4#b 04 ab# action(4,a) = Bb goto(0,B) =2#B 02 ab# action(2,a) =s3#Ba 023 b# actio
2、n(3,b) =s4#Bab 0234 # action(4,#) = Bb goto(3,B) =6#BaB 0236 # action(6,#) = BaB goto(2,B) =5#BB 025 # action(5,#) = SBB goto(0,S) =1#S 01 # action(1,#) = accept 例: LR(0)项目集规范族构造举例(参见教材P106 图5.7)I0=closure(SE) =SE ,EaA,EbBI1=GO(I0 ,E)=closure(SE) =SEI2=GO(I0 ,a)=closure(EaA) =EaA,AcA, AdI3=GO(I0 ,b)
3、=closure(EbB) =EbB,BcB, BdI4=GO(I2 ,c)=closure(AcA) =AcA ,AcA, Ad I6=GO(I2 ,A)=closure(EaA) =EaA例:对教材P111中例5.11拓广文法,构造LR(0)项目集规范族:解:I0=closure(SE) =SE ,EE+T,ET,TT*F,TF,F(E),F iI1=GO(I0 ,E)=closure(SE,EE+T)=SE,EE+TI2=GO(I0 ,T)=closure(ET, TT *F)=ET,TT *FI3=GO(I0 ,F)=closure(TF)= TFI4=GO(I0 ,( )=closu
4、re(F(E) =F(E),EE+T,ET,TT*F,TF,F(E),FiI5=GO(I0 ,i )=closure(Fi)= Fi得:C=I0, I1 , I2, I3 , I4 , I5I6=GO(I1 ,+)=closure(EE+T) = EE+T, TT*F,TF,F(E),F iI7=GO(I2,*)=closure(TT*F) =TT* F,F(E),F iI8=GO(I4 ,E)=closure(E(E),EE+T) =E(E), EE+T GO(I4,T)=closure(ET ,TT*F) = ET ,TT*F= I2 GO(I4 ,F)=closure(TF)= TF=
5、I3 GO(I4,( )=closure(F(E) =F(E),EE+T,ET,TT*F,TF,F(E),Fi= I4 GO(I4,i )=closure(Fi)= Fi= I5I9=GO(I6,T)=closure(EE+T , TT*F) = EE+T, TT*FGO(I6 ,F)=closure(TF)= TF= I3 GO(I6,( )=closure(F(E)= I4 GO(I6,i )=closure(Fi)= Fi= I5I10=GO(I7,F)=closure(TT*F )= TT*F GO(I7,( )=closure(F(E)= I4 GO(I7,i )=closure(Fi)= Fi= I5I11=GO(I8,) )=closure(F(E) )= F(E) GO(I8,+)=closure(EE+T)= EE+T, TT*F,TF,F(E),F i=I6 GO(I9,*)=closure(TT*F) =TT*F,F(E),F i=I7得:C=I0, I1 , I2, I11