1 / 17

5 章 习题

5 章 习题. 1 、设有文法 G : S→AaB | a A→AB | B→bB | 试消除空产生式。 解 :β={A,B} 消除 ε 产生式得: (1) S→AaB | a (2) A→AB (3) B→bB. 再扩充下列产生式: (4) S→aB (5) S→Aa (6) A→B (7) B→b. 2 、设有文法 G[P]:P→D;S D→int:i | D:D S→i | i:=E | i:S | goto i | if E then S | if E then S else S

landry
Download Presentation

5 章 习题

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 5章 习题 1、设有文法G: S→AaB|a A→AB| B→bB| 试消除空产生式。 解:β={A,B} 消除ε产生式得: (1) S→AaB|a (2) A→AB (3) B→bB

  2. 再扩充下列产生式: (4) S→aB (5) S→Aa (6) A→B (7) B→b

  3. 2、设有文法G[P]:P→D;S D→int:i|D:D S→i|i:=E|i:S|goto i |if E then S |if E then S else S |while E do S |begin L end|ε L→S|L;S E→i|(E)|Eεi 试对每一产生式求SELECT(P)

  4. 解: (1) select(P)=first(D)={int} (2) select(D)={int} (3) select(S)={i,goto,if,while, begin}∪follow(S) follow(S) ={else}∪follow(P)∪follow(L) ={else}∪{#}∪{end,;} 所以select(S)={i,goto,if,while, begin,else,#,end,;}

  5. (4)select(L)=first(S)∪first(L) =first(S) ={i,goto,if,while,begin} ∪follow(S) ={i,goto,if,while,begin,else,#,end,;} 注:first(L)同select(L) (5) select(E)={i,( }

  6. 3、试判断下列文法是否为LL(1)文法: G1:A→aBC,B→a,C→a G2: A→aABe,B→Bb,B→b G3: A→aABe,A→Ba,B→dB,B→ 解: (1)对A,B,C都只有一个产生式 所以,G1为LL(1)文法。

  7. (2)因为select(B→Bb)∩select(B→b) ={b}∩{b}={b}≠Ф, 所以,G2为非LL(1)文法。 (3)select(A→aABe)∩select(A→Ba) ={a}∩{d,#}=Ф select(B→dB)∩select(B→ ) ={d}∩{a,e}=Ф 所以,G3是LL(1)文法。

  8. 4、设有文法G: Z→bMb M→a|(L L→Ma) (1)试写出文法G的LL矩阵。 (3)试写出b(aa)b的LL分析过程。 解:select(Z→bMb)={b} select(M→a|(L)={a,(} select(L→Ma))=first(M)={a,(}

  9. b a ( Z bMb M a (L L Ma) Ma) 预测分析表:

  10. b a ( ) # Z bM,N M ε,N L,N L )aM,P )aM,P b ε,P ) ε,P a ε,P # ok LL矩阵:

  11. 分析式 输入流 矩阵元素 #Z b(aa)b# LL(Z,b)=(bM,N) #bM (aa)b# LL(M,()=(L,N) #bL aa)b# LL(L,a)=( )aM,p) #b)aM aa)b# LL(M,a)=( ε,N) #b)a )b# LL(a,a)=( ε,P) #b) )b# LL( ),))=(ε,P) #b b# LL(b,b)=( ε,P) # # LL(#,#)=ok

  12. 5、对下面的文法G: E→TE’ E’→+E|ε T→FT’ T’→T|ε F→PF’ F’→*F’|ε P→(E)|A|B|∧ A→a B→b (1)计算这个文法的每个非终极符的First集和follow集。 (2)证明这个文法是LL(1)的 (3)构造他的转换矩阵。

  13. 解:first(E)=first(T)=first(F) =first(P)={(,a,b,∧} first(E’)={+,ε} first(T’)=first(T)∪{ε} ={(,a,b,∧,ε} first(F’)= {*,ε} follow(E)=follow(E’)∪{ }}∪{#}={),#} follow(E’)= follow(E)= {),#} follow(T) = first(E’)-ε∪follow(E)∪follow(T’) ={+,),#}

  14. follow(T’)=follow(T)={+, ), #} follow(F)=first(T’)- ε∪follow(T) ={(,a,b,∧,+,),#} follow(F’)=follow(F) ={(,a,b,∧,+,),#} follow(P)=first(F’) -ε∪follow(F) ={*,(,a,b,∧,+, ),#}

  15. select(E→TE’)=first(T)={(,a,b,∧} select(E’→+E)={+} select(E’→ε)=follow(E’)={),#} select(T→FT’)=first(F)={(,a,b,∧} select(T’→T)=first(T)={(,a,b,∧} select(T’→ε)=follow(T’)={+,),#} select(F→PF’)=first(P)={(,a,b,∧} select(F’→F’)={*} select(F’→ε)=follow(F’) ={(,a,b,∧,+,),#} select(P→(E))={(}

  16. select(P→a)={a} select(P→b)={b} select(P→∧)={∧} 相同字符选择集的交集为φ.所以为LL(1)文法。

  17. + * ( a b ) ∧ # EE’T,P E’T,P E’T,P E’T,P E’ E,N ε,P ε,P T T’F,P T’F,P T’F,P T’F,P T’ ε,P T, P T, P T, P ε,P T, P ε,P F F’P,P F’P,P F’P,P F’P,P F’ ε,P F’,N ε,P ε,P ε,P ε,P ε,P ε,P P )E,N ε,N ε,N ε,N ) ε,N # ok

More Related