编译原理课后习题答案(第三版)

内容发布更新时间 : 2024/11/3 0:23:46星期一 下面是文章的全部内容请认真阅读。

精品文档

GO(I0,A)={ S?A?S,S??AS,S??b,A??SA,A??a}=I4 GO(I3,a)={ A?a?}=I1 GO(I3,b)={ S?b?}=I2

GO(I3,S)={ A?S?A,S??AS,S??b,A??SA,A??a}=I5

GO(I3,A)={ A?SA?,S?A?S,S??AS,S??b,A??SA,A??a}=I6 GO(I4,a)={ A?a?}=I1 GO(I4,b)={ S?b?}=I2

GO(I4,S)={ S?AS?,A?S?A,S??AS,S??b,A??SA,A??a}=I7 GO(I4,A)={ S?A?S,S??AS,S??b,A??SA,A??a}=I4 GO(I5,a)={ A?a?}=I1 GO(I5,b)={ S?b?}=I2

GO(I5,S)={ A?S?A,S??AS,S??b,A??SA,A??a}=I5

GO(I5,A)={ A?SA?,S?A?S,S??AS,S??b,A??SA,A??a}=I6 GO(I6,a)={ A?a?}=I1 GO(I6,b)={ S?b?}=I2

GO(I6,S)={ S?AS?,A?S?A,S??AS,S??b,A??SA,A??a}=I7 GO(I6,A)={ S?A?S,S??AS,S??b,A??SA,A??a}=I4 GO(I7,a)={ A?a?}=I1 GO(I7,b)={ S?b?}=I2

GO(I7,S)={ A?S?A,S??AS,S??b,A??SA,A??a}=I5

GO(I7,A)={ A?SA?,S?A?S,S??AS,S??b,A??SA,A??a}=I6 项目集规范族为C={I1,I2,I3,I4,I5,I6,I7}

(3)不是SLR文法

状态3,6,7有移进归约冲突

状态3:FOLLOW(S’)={#}不包含a,b

状态6:FOLLOW(S)={#,a,b}包含a,b,;移进归约冲突无法消解 状态7:FOLLOW(A)={a,b}包含a,b;移进归约冲突消解 所以不是SLR文法。

(4) 构造例如LR(1)项目集规范族 见下图: 对于状态5,因为包含项目[A?AS? a/b],所以遇到搜索符号a或b时,应该用A?AS归约。又因为状态5包含项目[A??a a/b],所以遇到搜索符号a时,应该移进。因此存在“移进-归约”矛盾,所以这个文法不是LR(1)文法。

.

精品文档

b b b 1: 5: 8: S??S? #A?SA? a/bS?A?S a/b A A?S?A a/bS?A?S a/bS??AS a/b A A A??SA a/bS??AS a/bS??b a/b A??a a/bS??b a/bA??SA a/b S??AS a/bA??SA a/bA??a a/b S S??b a/bA??a a/b a a S S a S 3: 0: 3: a a A a A?a? a/b A S???S # S??AS #/a/b9: 6: S??b #/a/b S?AS? a/bA?S?A a/b S A??SA a/bA?S?A a/bA??SA a/b b 4: A??SA a/b A??a a/bS?b? #/a/bA??a a/b A??a a/b S??AS a/b S S??AS a/bS??b a/b A b S??b a/b a a S b b 2: 7: S?AS? #/a/bS?A?S #/a/b A?S?A a/bS??AS #/a/b A??SA a/bS??b #/a/b10: S b A??a a/bS?b? a/b A??SA a/b S??AS a/bA??a a/b S??b a/b A A 5: .

精品文档

第六章

/********************第六章会有点难

P164–5

(1)

E?E1+T {if (E1.type = int) and (T.type = int ) then E.type := int else E.type := real} E?T {E.type := T.type} T?num.num {T.type := real} T?num {T.type := int} (2)

P164–7

S?L1|L2

{S.val:=L1.val+(L2.val/2L2.length)}

S?L {S.val:=L.val}

L?L1B {L.val:=2*L1.val + B.val; L.length:=L1.length+1} L?B {L.val:=B.c;

L.length :=1} B?0 {B.c:=0} B?1 {B.c:=1} ***********************/

第七章

P217–1

a*(-b+c) a+b*(c+d/e) -a+b*(-c+d)

ab@c+* abcde/+*+ a@bc@d+*+

?A??(C??D) A?CD????

AB?C@D?? AB?CD@E???

(A?B)?(?C?D)

(A?B)?(C??D?E)

if (x+y)*z =0 then (a+b)↑c else a↑b↑c xy+z*0= ab+c↑abc↑↑ ¥ 或 xy+z*0= P1 jez ab+c↑ P2 jump abc↑↑

P1 P2

.

精品文档

P217–3

-(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) (2) (3) (4) (5) (6) (7)

+, a, b, T1 @, T1, -, T2 +, c, d, T3 *, T2, T3, T4 +, a, b, T5 +, T5, c, T6 -, T4, T6, T7

P218–4

自下而上分析过程中把赋值句翻译成四元式的步骤: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:=i A-B

(5) *(-C+D) i:=E A-B

.

精品文档

(6) (7) (8) (9) (10) (11) (12) (13) (14) *(-C+D) i:=E (-C+D) i:=E* -C+D) i:=E*( C+D) i:=E*(- +D) i:=E*(-i +D) +D) D) ) i:=E*(-E i:=E*(E i:=E*(E+

A-B A-B- A-B-- A-B--- A-B---C A-B---C A-B--T A-B--T-

11(@,C,-, T)

1 i:=E*(E+i A-B--T-D

(15) ) i:=E*(E+E A-B--T1(16) ) i:=E(E A-B--T1-D (17) i:=E*(E) A-B--T2(18) i:=E+E 2- A-B-T(19)

2 i:=E

A-T

(20) 3A

产生的四元式: (@,C,-, T1) (+,T,D,T)

(*,B,1T22,T3) (:=,T3,-,A)

P218–5

/****************

设A :10*20,B、C、D:20,宽度为w=4 则T1:= i * 20 T1:=T1+j T2:=A–84 T3:=4*T1

Tn:=T2[T3] //这一步是多余的 T4:= i + j T5:=B–4 T6:=4*T4 T7:=T5[T6] T8:= i * 20 T8:=T8+j T9:=A–84 T10:=4*T8 T11:=T9[T10] T12:= i + j T13:=D–4 T14:=4*T12 T15:= T13[T14] T16:=T11+T15 T17:=C–4

.

(+,T1,D,T2)

(*,B,T,T(:=,T23)

3,-,A)

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4 ceshi