Presentation is loading. Please wait.

Presentation is loading. Please wait.

Test 2 Post Mortem Topics Questions and points Grade Distribution Answers April, 2006 CSCE 531 Compiler Construction.

Similar presentations


Presentation on theme: "Test 2 Post Mortem Topics Questions and points Grade Distribution Answers April, 2006 CSCE 531 Compiler Construction."— Presentation transcript:

1 Test 2 Post Mortem Topics Questions and points Grade Distribution Answers April, 2006 CSCE 531 Compiler Construction

2 – 2 – CSCE 531 Spring 2006 Questions and Point Evaluations 1. LR(1) sets of items (14)  LR(1) Parse Table (12)  LALR(1) Parse Table (8)  Do alternatingly (16)  Boolean (20) Markers Grammar extensions Attributes Quadruples  Annotated Parse Tree (14)  Activation Records (14)

3 – 3 – CSCE 531 Spring 2006 Grade Distribution

4 – 4 – CSCE 531 Spring 2006 LR(1) functions: Closure fig 4.38 Closure ( I ) Add I to Closure ( I ) Add I to Closure ( I )repeat for each item [A  α Bβ, a] in Closure ( I ) for each production B  γ and each token b in FIRST*(βa) add [B  γ, b] to closure( I ) if it is new until no new items can be added to closure( I ) Note error in text

5 – 5 – CSCE 531 Spring 2006 1. LR(1) Sets of Items S  T T  y T y T  x T x T  ε Augment with S’  S J 0 = closure({[S’  ● S, $]}) = {[S’  ● S, $], = {[S’  ● S, $], [S  ● T, $], [T  ● y T y, $], [T  ● x T x, $], [T  ●, $], } J 1 = GOTO(J 0, S) = closure({[S’  S ●, $]}) = closure({[S’  S ●, $]}) = { [S’  S ●, $] } = { [S’  S ●, $] } J 2 = GOTO(J 0, T) = closure({[S  T ●, $]}) = closure({[S  T ●, $]}) = { [S  T ●, $] } = { [S  T ●, $] } J 3 = GOTO(J 0, y) = closure({[T  y ● T y, $]}) = closure({[T  y ● T y, $]}) (details on next page) (details on next page) J 4 = GOTO(J 0, x) = closure({[T  x ● T x, $]}) = closure({[T  x ● T x, $]}) (details on next page) (details on next page)

6 – 6 – CSCE 531 Spring 2006 1. LR(1) Sets of Items continued J 3 = GOTO(J 0, y) = closure({[T  y ● T y, $]}) = closure({[T  y ● T y, $]}) = { [T  y ● T y, $], First*(y$) = {y} [T  ● y T y, y], [T  ● x T x, y], [T  ●, y], }

7 – 7 – CSCE 531 Spring 2006 1. LR(1) Sets of Items continued J 4 = GOTO(J 0, x) = closure({[T  x ● T x, $]}) = closure({[T  x ● T x, $]}) = { [T  x ● T x, $], First*(x$) = {x} [T  ● y T y, x], [T  ● x T x, x], [T  ●, x], } Now that handles all of the GOTOs from J 0. The GOTOs from J 1 and J 2 are empty so we proceed to J 3 and then J 4.

8 – 8 – CSCE 531 Spring 2006 1. Continued – See Figure

9 – 9 – CSCE 531 Spring 2006 Question 2. LR(1) Parse Table Stateyx$ST 03412 1accept 2r1 3 s7 / r4 s85 4s11 s12 / r4 9 5s6 6r2 7 s7 / r4 s813 8s11 s12 / r4 17 9s10 10r3 action goto

10 – 10 – CSCE 531 Spring 2006 Question 2. LR(1) Parse Table Stateyx$ST 11 s7 / r4 s815 1219 13s14 14r2 15s16 16r2 17s18 18r3 19s20 20r3 action goto

11 – 11 – CSCE 531 Spring 2006 Question 3. LALR(1) Parse Table Stateyx$ST 03412 1accept 2r1 3_7_11 s7 / r4 s85 4_8_12s11 s12 / r4 9 5_13_15s6 6_14_16r2 9_17_19s10 10_18_20r3 action goto

12 – 12 – CSCE 531 Spring 2006 4. Do alternatingly S  do T M 1 L 1 N alternatingly M 2 L 2 until M 3 B{ } T  ε{ tmp = newtemp(); emit(tmp := 0;) $$=tmp; }

13 – 13 – CSCE 531 Spring 2006 Do Alternatingly Picture M1M2M3 S  do T M 1 L 1 N alternatingly M 2 L 2 until M 3 B goto T57 := 0 T57 := 1- T57 If t57 == 0 goto M1.quad goto M2.quad B L2 L1 L1.next L2.next B.true B.false N.next Common errors -5 backpatch(B.false, M2.quad); -2 backpatch(B.true, xxx); -7 backpatch(B.false, …); twice -5 wrong markers -10 static/dynamic problem (slide after the next slide)

14 – 14 – CSCE 531 Spring 2006 4. Do alternatingly S  do T M 1 L 1 N alternatingly M 2 L 2 until M 3 B{ backpatch(L 1.next, M 3.quad); backpatch(L 2.next, M 3.quad); backpatch(N.next, M 3.quad); backpatch(B.false, nextquad); emit( T57 := 1 - T57); emit( if t57 == 0 goto M1.quad); emit( goto M2.quad); S.next = B.true;} Attribute Checklist 1.L1.next 2.L2.next 3.M1.quad 4.M2.quad 5.M3.quad 6.N.next 7.B.false 8.B.true 9.S.next

15 – 15 – CSCE 531 Spring 2006 4. Most Illustrative Error Award S  do T M 1 L 1 N alternatingly M 2 L 2 until M 3 B{ … if ( t != 0) { backpatch ( B.false, M 2.quad ); } else { backpatch ( B.false, M 5.quad ); backpatch ( B.false, M 5.quad );} t = 1 – t ; …} What’s dynamic? What’s static?

16 – 16 – CSCE 531 Spring 2006 Question 5 Booleans  Add Markers easy  Extend the grammar sufficiently B  id GT id // to handle r > s B  id LT intconst // to handle r < 0 B  E relop E with E  id | intconst and relop  LT | GT | EQ is better  Attributes needed Usual suspects T needs T.place

17 – 17 – CSCE 531 Spring 2006 Parse Tree L LMS S whileM1M1 BdoM2M2 L LMS endloop B1B1 orM1M1 B2B2 B1B1 andM1M1 B2B2 idGTid GTid LTid Parse Tree for: while t s and s > t do x = x + 1 ; r = r/3 ; endloop ; y = r + s ; id=E + Notes To allow it to fit:  I left out the semicolons  I left out the body of the loop. Also I assume that the reduction of the while will emit(goto, M1) Also I assume that the reduction of the while will emit(goto, M1)

18 – 18 – CSCE 531 Spring 2006 QuadNoOpcodeLeftRightResultTarget 41IF-LTts 42GOTO 43IF-GTrs 44GOTO 45IF-GTst 46GOTO 47ADDx1T0 48ASSIGNT0x 49DIVr3T1 50ASSIGNT1r 51GOTO41 52ADDrsT2 53ASSIGNT2y 54

19 – 19 – CSCE 531 Spring 2006 Pre-Annotation Comments L LMS S whileM1M1 BdoM2M2 L LMS endloop B1B1 orM1M1 B2B2 B1B1 andM1M1 B2B2 idGTid GTid LTid =E + Attributes B.t B.t B.f B.f id.place id.place M.quad M.quad S.next S.next L.next L.next E.place E.place Now we will annotate each node with the value of its attributes

20 – 20 – CSCE 531 Spring 2006 Semi-Annotated Parse Tree B B1B1 orM1M1 B2B2 B1B1 andM1M1 B2B2 idGTid GTid LTid Notes I left off the M1.quads. It is just too crowded. id.place =“t” id.place =“s” id.place =“t” id.place =“r”id.place =“s” B.T ={41} B.F ={42} B.T ={43} B.F ={44} B.T ={45} B.F ={46} B.T ={45} B.F ={44, 46} B.T ={41, 45} B.F ={44, 46}

21 – 21 – CSCE 531 Spring 2006 func: pushl %ebp ___save old frame pointer pushl %ebp ___save old frame pointer movl %esp, %ebp___set ebp to point to “old ebp” movl %esp, %ebp___set ebp to point to “old ebp” subl $8, %esp __allocate space for 8 bytes of local variables subl $8, %esp __allocate space for 8 bytes of local variables movl $0, -4(%ebp) __set local1 = 0 movl $0, -4(%ebp) __set local1 = 0 movl 12(%ebp), %eax __move arg2 into eax movl 12(%ebp), %eax __move arg2 into eax movl 8(%ebp), %edx __move arg1 into edx movl 8(%ebp), %edx __move arg1 into edx subl %eax, %edx __arg1-arg2 now in edx subl %eax, %edx __arg1-arg2 now in edx movl %edx, %eax __also in eax movl %edx, %eax __also in eax movl %eax, -8(%ebp) __also stored in local2 movl %eax, -8(%ebp) __also stored in local2 cmpl $0, 16(%ebp) __compare arg3 with 0 cmpl $0, 16(%ebp) __compare arg3 with 0 jne.L3 __if not equal goto L3 jne.L3 __if not equal goto L3 movl 12(%ebp), %eax __move arg2 to eax movl 12(%ebp), %eax __move arg2 to eax addl 8(%ebp), %eax __ add arg1 to eax addl 8(%ebp), %eax __ add arg1 to eax

22 – 22 – CSCE 531 Spring 2006 movl %eax, -4(%ebp) ___local1 = eax movl %eax, -4(%ebp) ___local1 = eax.L3: movl -8(%ebp), %eax ___move local2 to eax movl -8(%ebp), %eax ___move local2 to eax addl -4(%ebp), %eax ____ add local1 to eax addl -4(%ebp), %eax ____ add local1 to eax movl %ebp, %esp ___delete everything on the stack below the frame pointer movl %ebp, %esp ___delete everything on the stack below the frame pointer pop %ebp ___ restore previous frame pointer pop %ebp ___ restore previous frame pointer ret ret

23 – 23 – CSCE 531 Spring 2006 What I should have also asked !


Download ppt "Test 2 Post Mortem Topics Questions and points Grade Distribution Answers April, 2006 CSCE 531 Compiler Construction."

Similar presentations


Ads by Google