《编译基本知识第三版课后答案解析.doc》由会员分享,可在线阅读,更多相关《编译基本知识第三版课后答案解析.doc(27页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、,编译 原理 课后题答案第二章P36-6(1)是09组成的数字串(2)最左推导:最右推导:P36-7G(S)P36-8文法:最左推导:最右推导:语法树:/*/P36-9句子iiiei有两个语法树:P36-10/*/P36-11/*L1:L2:L3:L4:*/第三章习题参考答案P647(1)XY X1234Y5 0 1 1 0 1 1确定化:01X1,2,31,2,32,32,3,42,32,32,3,42,3,42,3,52,3,42,3,52,32,3,4,Y2,3,4,Y2,3,52,3,4, 0320 1 01 0 0 1 1 0654 0 1 0 1 1 1最小化: 002 1 1 0
2、 0 1 0543 0 1 0 1 1 1P648(1) (2)(3)P6412(a) a10 a,b a确定化:ab00,110,10,1110给状态编号:ab012112203333 a10 a a b b b32 b a最小化: a a210 b b a b(b)032 b b a a b a a b541 b a a a已经确定化了,进行最小化最小化:021 b b a a b aP6414 (1) 010 1 0(2):YX 2 0 1Y1X 0确定化:01X,1,Y1,Y21,Y1,Y221,Y给状态编号:01012112213333 010 0 1 032 1 1 1 0最小化:
3、 0310 1 1 1 0 0第四章P811(1) 按照T,S的顺序消除左递归递归子程序:procedure S;beginif sym=a or sym= then abvanceelse if sym=( then beginadvance;T;if sym=) then advance;else error; endelse errorend;procedure T;beginS;end;procedure ;beginif sym=, then beginadvance;S;endend;其中:sym:是输入串指针IP所指的符号 advance:是把IP调至下一个输入符号error:是
4、出错诊察程序(2)FIRST(S)=a,(FIRST(T)=a,(FIRST()=,FOLLOW(S)=),#FOLLOW(T)=)FOLLOW()=)预测分析表a(),#ST是LL(1)文法P812文法:(1)FIRST(E)=(,a,b,FIRST(E)=+,FIRST(T)=(,a,b,FIRST(T)=(,a,b,FIRST(F)=(,a,b,FIRST(F)=*,FIRST(P)=(,a,b,FOLLOW(E)=#,)FOLLOW(E)=#,)FOLLOW(T)=+,),#FOLLOW(T)=+,),#FOLLOW(F)=(,a,b,+,),#FOLLOW(F)=(,a,b,+,),
5、#FOLLOW(P)=*,(,a,b,+,),#(2)考虑下列产生式:FIRST(+E)FIRST()=+=FIRST(+E)FOLLOW(E)=+#,)=FIRST(T)FIRST()=(,a,b,=FIRST(T)FOLLOW(T)=(,a,b,+,),#=FIRST(*F)FIRST()=*=FIRST(*F)FOLLOW(F)=*(,a,b,+,),#=FIRST(E)FIRST(a) FIRST(b) FIRST()=所以,该文法式LL(1)文法.(3)+*()ab#EETTFFP(4)procedure E;beginif sym=( or sym=a or sym=b or sy
6、m= then begin T; E end else errorendprocedure E;beginif sym=+ then begin advance; E end else if sym) and sym# then errorendprocedure T;beginif sym=( or sym=a or sym=b or sym= then begin F; T end else errorendprocedure T;beginif sym=( or sym=a or sym=b or sym= then T else if sym=* then errorendproced
7、ure F;beginif sym=( or sym=a or sym=b or sym= then begin P; F end else errorendprocedure F;beginif sym=* then begin advance; F endendprocedure P;beginif sym=a or sym=b or sym= then advance else if sym=( thenbeginadvance; E;if sym=) then advance else errorendelse errorend;P813/*(1) 是,满足三个条件。(2) 不是,对于
8、A不满足条件3。(3) 不是,A、B均不满足条件3。(4) 是,满足三个条件。*/第五章P1331短语: E+T*F, T*F,直接短语: T*F句柄: T*FP1332文法:(1)最左推导:最右推导:(2)(a,a),(a),a)(S,a),(a),a)(T,a),(a),a)(T,S),(a),a)(T),(a),a)(S,(a),a)(T,(a),a)(T,S,(a),a)(T,(a),a)(T,(S),a)(T,(T),a)(T,S),a)(T),a)(S,a)(T,S)(T)S“移进-归约”过程:步骤栈输入串动作0#(a,a),(a),a)#预备1#(a,a),(a),a)#进2#(
9、a,a),(a),a)#进3#(a,a),(a),a)#进4#(a,a),(a),a)#进5#(S,a),(a),a)#归6#(T,a),(a),a)#归 7#(T,a),(a),a)#进8#(T,a),(a),a)#进9#(T,S),(a),a)#归10#(T),(a),a)#归11#(T),(a),a)#进12#(S,(a),a)#归13#(T,(a),a)#归 14#(T,(a),a)#进15#(T,(a),a)#进16#(T,S,(a),a)#归17#(T,(a),a)#归18#(T,(a),a)#进19#(T,(a),a)#进20#(T,(a),a)#进21#(T,(S),a)#归2
10、2#(T,(T),a)#归23#(T,(T),a)#进24#(T,S),a)#归25#(T),a)#归26#(T),a)#进27#(S,a)#归28#(T,a)#归29#(T,a)#进30#(T,a)#进31#(T,S)#归32#(T)#归33#(T)#进34#S#归P1333(1) FIRSTVT(S)=a,(FIRSTVT(T)=,a,(LASTVT(S)=a,)LASTVT(T)=,a,)(2)a(),a(=,是算符文法,并且是算符优先文法(3)优先函数a(),f44244g55523 (4) 栈输入字符串动作#(a,(a,a))#预备#(a, (a,a)#进#(a, (a,a)#进#(
11、t, (a,a)#归#(t,(a,a))#进#(t,(a,a)#进#(t,(a,a)#进#(t,(t,a)#归#(t,(t,a)#进#(t,(t,a)#进#(t,(t,s)#归#(t,(t)#归#(t,(t)#进#(t,s)#归#(t)#归#(t)#进# s#归successP1345(1)0.1.2.3.4.5.6.7.8.9.10.11.(2)1987 S A S 11100 a 432 A S d 56确定化:SAab0,2,5,7,101,2,5,7,8,102,3,5,7,101161,2,5,7,8,102,5,7,8,102,3,5,7,9,101162,3,5,7,102,4,
12、5,7,8,102,3,5,7,101162,5,7,8,102,5,7,8,102,3,5,7,9,101162,3,5,7,9,102,4,5,7,8,102,3,5,7,101162,4,5,7,8,102,5,7,8,102,3,5,7,9,10116116 A S3:5:6: S A a b S a A S b S A b a A4:0:7: A S b a a b b a2:1: DFA构造LR(0)项目集规范族也可以用GO函数来计算得到。所得到的项目集规范族与上图中的项目集一样:=,GO(,a)= =GO(,b)= =GO(,S)= ,=GO(,A)= ,=GO(,a)= =GO
13、(,b)= =GO(,S)= ,=GO(,A)= ,=GO(,a)= =GO(,b)= =GO(,S)= ,=GO(,A)= ,=GO(,a)= =GO(,b)= =GO(,S)= ,=GO(,A)= ,=GO(,a)= =GO(,b)= =GO(,S)= ,=GO(,A)= ,=GO(,a)= =GO(,b)= =GO(,S)= ,=GO(,A)= ,=项目集规范族为C=,(3)不是SLR文法状态3,6,7有移进归约冲突状态3:FOLLOW(S)=#不包含a,b状态6:FOLLOW(S)=#,a,b包含a,b,;移进归约冲突无法消解状态7:FOLLOW(A)=a,b包含a,b;移进归约冲突消
14、解所以不是SLR文法。(4) 构造例如LR(1)项目集规范族见下图:对于状态5,因为包含项目,所以遇到搜索符号a或b时,应该用归约。又因为状态5包含项目,所以遇到搜索符号a时,应该移进。因此存在“移进-归约”矛盾,所以这个文法不是LR(1)文法。 b b b8:1:5: A A A S a a S3: S a S3:0: a a A a A6:9: 4: S b S A b a a S b b7:2:10: S b A A5:第六章/*第六章会有点难P1645(1)EE1T if (E1.type = int) and (T.type = int )then E.type := intelse
15、 E.type := realETE.type := T.typeTnum.num T.type := realTnumT.type := int(2)P1647SL1|L2S.val:=L1.val+(L2.val/2)SLS.val:=L.valLL1BL.val:=2*L1.val + B.val; L.length:=L1.length+1LBL.val:=B.c; L.length :=1B0 B.c:=0B1B.c:=1*/第七章P2171a*(-b+c)abc+*a+b*(c+d/e)abcde/+*+-a+b*(-c+d)abcd+*+if (x+y)*z =0 then (a
16、+b)c else abcxy+z*0= ab+cabc ¥或 xy+z*0= P1 jez ab+c P2 jump abc P1 P2P2173-(a+b)*(c+d)-(a+b+c)的三元式序列:(1) +, a, b(2) , (1), -(3) +, c, d(4) *, (2), (3)(5) +, a, b(6) +, (5), c(7) -, (4), (6)间接三元式序列:三元式表:(1) +, a, b(2) , (1), -(3) +, c, d(4) *, (2), (3)(5) +, (1), c(6) -, (4), (5)间接码表:(1)(2)(3)(4)(1)(
17、5)(6)四元式序列:(1) +, a, b, (2) , , -, (3) +, c, d, (4) *, , , (5) +, a, b, (6) +, , c, (7) -, , , P2184自下而上分析过程中把赋值句翻译成四元式的步骤:A:=B*(-C+D)步骤输入串栈 PLACE四元式(1)A:=B*(-C+D)(2):=B*(-C+D)i A(3)B*(-C+D)i:= A-(4)*(-C+D)i:=iA-B(5)*(-C+D)i:=EA-B(6)*(-C+D)i:=EA-B(7)(-C+D)i:=E*A-B-(8)-C+D)i:=E*(A-B-(9)C+D)i:=E*(-A-B
18、-(10)+D) i:=E*(-iA-B-C(11)+D) i:=E*(-EA-B-C(,C,-, )(12)+D) i:=E*(EA-B-(13)D) i:=E*(E+A-B-(14) i:=E*(E+iA-B-D(15)i:=E*(E+EA-B-D(+,D,)(16)i:=E(EA-B-(17)i:=E*(E)A-B-(18)i:=E+EA-B-(*,B,)(19)i:=EA-(:=,-,A)(20) A产生的四元式:(,C,-, )(+,D,)(*,B,)(:=,-,A)P2185/*设A :10*20,B、C、D:20,宽度为w4 则T1:= i * 20T1:=T1+jT2:=A84
19、T3:=4*T1Tn:=T2T3 /这一步是多余的T4:= i + jT5:=B4T6:=4*T4T7:=T5T6T8:= i * 20T8:=T8+jT9:=A84T10:=4*T8T11:=T9T10T12:= i + jT13:=D4T14:=4*T12T15:= T13T14T16:=T11+T15T17:=C4T18:=4*T16T19:=T17T18T20:=T7+T19Tn:=T20*/P2186100. (jnz, A, -, 0)101. (j, -, -, 102)102. (jnz, B, -, 104)103. (j, -, -, 0)104. (jnz, C, -,
20、103)105. (j, -, -, 106)106. (jnz, D, -, 104) -假链链首107. (j, -, -, 100) -真链链首假链:106,104,103真链:107,100P2187100. (j, A, C, 102)101. (j, -, -, 0)102. (j,E1.place ,E2.place ,0);emit(I.Place :=E1.place);F.truelist := makelist(nextquad);emit(j,-,-,-);F.place := I.place;F.end := E2.place;p:=lookup(id.name);
21、if p nil thenI.place := pelse error M.quad := nextquad*/方法2:S for id:=E1 to E2 do S1S F S1F for id:=E1 to E2 do doINITIAL=NEWTEMP;emit(:=,E1.PLACE, -, INITIAL);FINAL=NEWTEMP;emit(:=,E2.PLACE, -, FINAL);p:= nextquad+2;emit(j, INITIAL , FINAL , p);F.nextlist:=makelist(nextquad);emit(j,);F.place:=lookup(id.name);if F.placenil thenemit(F.place := INITIAL)F.quad:=nextquad;F.final:=FINAL;backpatch(S1.nextlist, nextquad)p:=nextquad+2;emit(