1 / 23

8 章 习题

8 章 习题. 1. 设有表达式 A*(B*C-A)≤B+C∧D a. 试写出逆波兰式中间代码。 b. 试写出三元式中间代码。 c. 试写出树中间代码。 d. 试写出四元式中间代码。. 解: a.ABC*A-*BC+≤D∧ 两种形式. b. ①(*, B, C) ②(-,①, A) ③(*, A,②) ④(+, B, C) ⑤(≤,③,④) ⑥(∧,⑤,D). 6. c. ∧. 5. ≤. D. +. 3. 4. *. C. B. -. A. 2. *. A. 1. C. B.

jalia
Download Presentation

8 章 习题

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. 8章 习题 1.设有表达式A*(B*C-A)≤B+C∧D a.试写出逆波兰式中间代码。 b.试写出三元式中间代码。 c.试写出树中间代码。 d.试写出四元式中间代码。 解:a.ABC*A-*BC+≤D∧ 两种形式

  2. b. ①(*, B, C) ②(-,①, A) ③(*, A,②) ④(+, B, C) ⑤(≤,③,④) ⑥(∧,⑤,D)

  3. 6 c. ∧ 5 ≤ D + 3 4 * C B - A 2 * A 1 C B

  4. d. 1.(*, B, C, T1) 2.(-, T1, A, T2) 3.(*, A, T2, T3) 4.(+, B, C, T4) 5.(≤,T3, T4, T5) 6.(∧, D, T5, T6)

  5. 2.试写出下列语句的四元式中间代码. a.IF x>0 THEN x:=0 ELSE x:=1 b.WHILE x>0 DO x:=x-1 c.IF x>0 THEN x:=x-1 ELSE IF x<0 THEN x:=x+1 ELSE x:=1 d.WHILE x>0 DO WHILE y>0 DO BEGIN y:=y-x;x:=x-1 END

  6. 解:a. 1. (>, x , 0 , T1) 2. (then, T1, ——, ——) 3. (:=, 0, ——, x ) 4. (else, ——, ——, ——) 5. (:=, 1, ——, x ) 6. (ifend, ——, ——,——)

  7. b. 1.(while,——,——,——) 2.( >, x , 0 ,T1) 3.(do , T1,——,——) 4.(——, x, 1,T2) 5.(:=, T2,——,x) 6.(whend,——,——,——)

  8. c. 1.(>, x , 0 , T1) 2.(then, T1, —, —) 3.(—, x , 1 , T2) 4.(:=, T2, —, x ) 5.(else, —, —, —) 6.(<, x , 0 , T3) 7.(then, T3, —, —) 8.(+, x , 1 , T4) 9.(:=, T4, —, x )

  9. 10.( else, —, —, —) 11.( := , 1 , —, x ) 12.(ifend, —, —, —) 13.(ifend, —, —, —)

  10. d. 1.(while, —, —, —) 2.( > , x , 0 ,T1) 3.( do , T1, —, —) 4.(while, —, —, —) 5.( > , y , 0 ,T2) 6.( do , T2, —, —) 7.( — , y ,x , T3) 8.( := , T3,—, y ) 9.( — , x , 1 ,T4)

  11. 10.( := , T4, —,x ) 11.(whend, —, —, —) 12.(whend, —, —, —)

  12. 3.试写出下列赋值语句的四元式中间代码: a.A[i+1]:=A[i-1]+A[i] b. R.f:=R.x+a[i] c.B[i].x:=R1.D[j].y d. A[A[i]]:=R2.R.f e.D[i+1][j]:=D[i+1][j]+1 其中各数组的各界类型为1..10

  13. 1.( +, i, 1, T1) • 2.(—, T1, 1, T2) • 3.( *, T2, L, T3) • 4.([], A, T3, T4) (A[i+1]) • 5.( -, i, 1, T5) • 6.(—, T5, 1, T6) • 7.( *, T6, L, T7) (A[i-1]) • 8.([], A, T7, T8) • 9.(—, i, 1, T9) • 10.( *, T9, 5, T10)

  14. 11.([], A, T10, T11) (A[i]) 12.( +, T7, T11, T12) 13.(:=, T12, —, T13)

  15. b. 1.(. , R, offf, T1) (R.f) 2.(. , R, offx, T2) (R.x) 3.(- , i, 1, T3) 4.( *, T3, 5, T4) 5.([], A, T4, T5) (A[i]) 6.( +, T2, T5, T6) 7.(:=, T6, —, T7)

  16. c.1.(-, i, 1, T1) 2.(*, T1, l, T2) B[i] 3.([ ], B, T2, T3) 4.(., T3, offx, T4) 5.(., R1, offD[j].y, T5) 6.( -, j, 1, T6) 7.(*, T6, l, T7) D[j] 8.([ ], D, T7, T8) 9.(., T8, offy, T9) 10.(:=, T9, _, T4)

  17. d.1.(-, i, 1, T1) 2.(*, T1, l, T2) 3.([ ], A, T2, T3) 4.(-, T3, 1, T4) 5.(*, T4, l, T5) 6.([ ], A, T5, T6) 7.(。, R2, offR.f, T7) 8.(。, R, offf, T8) 9.(:=, T8, -, T6)

  18. e.1.(+, i, 1, T1) 2.(-, T1, 1, T2) 3.(*, T2, l, T3) 4.([ ], D, T3, T4) 5.(-, j, 1, T5) 6.(*, T5, l, T6) 7.([ ], T4, T6, T7) 8.(+, T7, 1, T8) 9.(:=, T8, -, T7)

  19. 4.试写出下列过程语句的四元式中间代码: a.f(X*(X+1),i+1) b.f(g(X-1), X) c.f(g(h(X),g(X)) 其中f,g,h的形参均为赋值形参变量。

  20. 解:a. 1.( +, x, 1, T1) 2.( *, x, T1, T2) 3.( +, i, 1, T3) 4.( act, T2, 1, 4) 5.( act, T3, 1, 5) 6.(call, f, —,—)

  21. b. 1.( - , x, 1, T1) 2.(act, T1, 1, 4 ) g(x-1) 3.(call, g, _, T2) 4.(act, T2, 1, 4 ) 5.(act, x, 1, 5 ) 6.(call, f, _, _ )

  22. c.1.(act, x, 1, 4 ) 2.(call, h, _,T1) h(x) 3.(act, T1, 1, 4 ) 4.(call, g, _, T2) g(h(x)) 5.(act, x, 1, 4 ) 6.(call, g, _, T3) g(x) 7.(act, T2, 1, 4 ) 8.(act, T3, 1, 5 ) 9.(call, f, _, _ )

  23. 5.设有表达式 A-(A*B-D*(A-B*D))/A 写出生成三元式的过程。 解: ⑴ (*, A, B) ⑵ (*, B, D) ⑶ (-, A, ⑵) ⑷ (*, D, ⑶) ⑸ (-, ⑴, ⑷) ⑹ (1, ⑸, A) ⑺ (-, A, ⑹)

More Related