《程序设计语言编译原理第3版课后答案(20页).doc》由会员分享,可在线阅读,更多相关《程序设计语言编译原理第3版课后答案(20页).doc(20页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、-程序设计语言编译原理第3版课后答案-第 20 页第二章P36-6(1)是09组成的数字串(2)最左推导:最右推导:P36-7G(S)P36-8文法:最左推导:最右推导:语法树:/*P36-9句子iiiei有两个语法树:P36-10P36-11L1: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
2、1 1 0 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
3、0最小化: 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调至下一个输入符号er
4、ror:是出错诊察程序(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
6、or sym= 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 errorendp
7、rocedure 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)#归
10、22#(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 S11100 a432 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,5,
12、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(,b)
13、= =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 E.ty
15、pe := 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)a+b*(c+d/e)abcde/+*+-a+b*(-c+d)if (x+y)*z =0 then (a+b)c else abcxy+z*0
16、= 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)(5)(6)四元式序列:(1) +, a
17、, b, (2) +, c, d, (3) +, a, b, (4) +, , c, 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-(10)+D) i:=E*(-iA-B-C(11)+D) i:=E*(-EA-B-C(,C,-, )(
18、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:=A84T3:=4*T1Tn:=T2T3 /这一步是多余的T4:= i + jT5:=B4T6:=4*T4T7:=T
19、5T6T8:= i * 20T8:=T8+jT9:=A84T10:=4*T8T11:=T9T10T12:= i + jT13:=D4T14:=4*T12T15:= T13T14T16:=T11+T15T17:=C4T18:=4*T16T19:=T17T18T20:=T7+T19Tn:=T20P2186100. (jnz, A, -, 0)101. (j, -, -, 102)102. (jnz, B, -, 104)103. (j, -, -, 0)104. (jnz, C, -, 103)105. (j, -, -, 106)106. (jnz, D, -, 104) -假链链首107. (
20、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); if p nil thenI.place := pelse error M.quad := nextquad方法
21、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.qua
22、d:=nextquad;F.final:=FINAL;backpatch(S1.nextlist, nextquad)p:=nextquad+2;emit(j, F.place, F.final , p );S.nextlist := merge(F.nextlist, makelist(nextquad);emit(j,);emit(succ, F.place , -, F.place);emit(j, F.quad);第九章P2709(1) 传名即当过程调用时,其作用相当于把被调用段的过程体抄到调用出现处,但必须将其中出现的任一形式参数都代之以相应的实在参数。A:=2; B:=3; A:=
23、A+1; A:=A+(A+B); print A; A=9 (2) 传地址即当程序控制转入被调用段后,被调用段首先把实在参数抄进相应的形式参数的形式单元中,过程体对形参的任何引用或赋值都被处理成对形式单元的间接访问。当被调用段工作完毕返回时,形式单元(都是指示器)所指的实参单元就持有所希望的值。A:=2;B:=3;T:=A+B把T,A,A的地址抄进已知单元J1,J2,J3x:J1;y:=J2;z:=J3 /把实参地址抄进形式单元,且J2=J3Y:=y+1 Z:=z+x / Y:对y的间接访问 Z:对z的间接访问print AA=8(3) 得结果每个形参均对应两个单元,第一个存放实参地址,第二个存放实参值,在过程体中对形参