test 2 post mortem topics questions and points grade distribution answers april, 2006 csce 531...
Post on 21-Dec-2015
222 views
TRANSCRIPT
Test 2 Post MortemTest 2 Post Mortem
Topics Topics Questions and points Grade Distribution Answers
April, 2006
CSCE 531 Compiler Construction
– 2 – CSCE 531 Spring 2006
Questions and Point EvaluationsQuestions and Point Evaluations
1. LR(1) sets of items (14)1. LR(1) sets of items (14)
2.2. LR(1) Parse Table (12)LR(1) Parse Table (12)
3.3. LALR(1) Parse Table (8)LALR(1) Parse Table (8)
4.4. Do alternatingly (16)Do alternatingly (16)
5.5. Boolean (20)Boolean (20) Markers Grammar extensions Attributes Quadruples
6.6. Annotated Parse Tree (14)Annotated Parse Tree (14)
7.7. Activation Records (14)Activation Records (14)
– 4 – CSCE 531 Spring 2006
LR(1) functions: Closure fig 4.38LR(1) functions: Closure fig 4.38
Closure (Closure (II))
Add Add II to Closure ( to Closure (II))
repeatrepeat
for each item [Afor each item [Aα α •• BBββ, a] in Closure (, a] in Closure (II) )
for each production Bfor each production B γγ and and
each token b in FIRST*(each token b in FIRST*(ββa)a)
add [Badd [B •• γγ , b] to closure( , b] to closure(II) if it is new) if it is new
until no new items can be added to closure(until no new items can be added to closure(II) )
Note error in textNote error in text
– 5 – CSCE 531 Spring 2006
1. LR(1) Sets of Items1. LR(1) Sets of Items
S S T T
T T y T yy T y
T T x T xx T x
T T εε
Augment with S’ Augment with S’ S S
JJ00 = closure({[S’ = closure({[S’ ● S, $]}) ● S, $]}) = {[S’ = {[S’ ● S, $], ● S, $],
[S[S ● T, $], ● T, $],[T[T ● y T y , $], ● y T y , $],[T[T ● x T x , $], ● x T x , $],[T[T ● , $], ● , $],
}}
JJ11 = GOTO(J = GOTO(J00, S) , S)
= closure({[S’ = closure({[S’ S ●, $]}) S ●, $]})
= { [S’ = { [S’ S ●, $] } S ●, $] }
JJ22 = GOTO(J = GOTO(J00, T) , T)
= closure({[S = closure({[S T ●, $]}) T ●, $]})
= { [S = { [S T ●, $] } T ●, $] }
JJ33 = GOTO(J = GOTO(J00, y) , y)
= closure({[T = closure({[T y ● T y, $]}) y ● T y, $]})
(details on next page)(details on next page)
JJ44 = GOTO(J = GOTO(J00, x) , x)
= closure({[T = closure({[T x ● T x, $]}) x ● T x, $]})
(details on next page)(details on next page)
– 6 – CSCE 531 Spring 2006
1. LR(1) Sets of Items continued1. LR(1) Sets of Items continued
JJ33 = GOTO(J = GOTO(J00, y) , y)
= closure({[T = closure({[T y ● T y, $]}) y ● T y, $]})
= { [T = { [T y ● T y , $], y ● T y , $], First*(y$) = {y}First*(y$) = {y}
[T [T ● y T y , y], ● y T y , y],
[T [T ● x T x , y], ● x T x , y],
[T [T ● , y], ● , y],
}}
– 7 – CSCE 531 Spring 2006
1. LR(1) Sets of Items continued1. LR(1) Sets of Items continued
JJ44 = GOTO(J = GOTO(J00, x) , x)
= closure({[T = closure({[T x ● T x, $]}) x ● T x, $]})
= { [T = { [T x ● T x , $], x ● T x , $], First*(x$) = {x}First*(x$) = {x}
[T [T ● y T y , x], ● y T y , x],
[T [T ● x T x , x], ● x T x , x],
[T [T ● , x], ● , x],
}}
Now that handles all of the GOTOs from JNow that handles all of the GOTOs from J00 . .
The GOTOs from JThe GOTOs from J11 and J and J22 are empty so we are empty so we proceed to Jproceed to J33 and then J and then J44 . .
– 9 – CSCE 531 Spring 2006
Question 2. LR(1) Parse TableQuestion 2. LR(1) Parse Table
StateState yy xx $$ SS TT
00 33 44 11 22
11 acceptaccept
22 r1r1
33 s7 / r4s7 / r4 s8s8 55
44 s11s11 s12 / r4s12 / r4 99
55 s6s6
66 r2r2
77 s7 / r4s7 / r4 s8s8 1313
88 s11s11 s12 / r4s12 / r4 1717
99 s10s10
1010 r3r3
action goto
– 10 – CSCE 531 Spring 2006
Question 2. LR(1) Parse TableQuestion 2. LR(1) Parse Table
StateState yy xx $$ SS TT
1111 s7 / r4s7 / r4 s8s8 1515
1212 1919
1313 s14s14
1414 r2r2
1515 s16s16
1616 r2r2
1717 s18s18
1818 r3r3
1919 s20s20
2020 r3r3
action goto
– 11 – CSCE 531 Spring 2006
Question 3. LALR(1) Parse TableQuestion 3. LALR(1) Parse Table
StateState yy xx $$ SS TT
00 33 44 11 22
11 acceptaccept
22 r1r1
3_7_113_7_11 s7 / r4s7 / r4 s8s8 55
4_8_124_8_12 s11s11 s12 / r4s12 / r4 99
5_13_155_13_15 s6s6
6_14_166_14_16 r2r2
9_17_199_17_19 s10s10
10_18_2010_18_20 r3r3
action goto
– 12 – CSCE 531 Spring 2006
4. Do alternatingly4. Do alternatingly
S S do T M do T M11 L L11 N alternatingly M N alternatingly M22 L L22 until M until M33 B B {{
}}
TT εε { tmp = newtemp(); emit(tmp := 0;) ${ tmp = newtemp(); emit(tmp := 0;) $$=tmp; }$=tmp; }
– 13 – CSCE 531 Spring 2006
Do Alternatingly PictureDo Alternatingly Picture
M1M1
M2M2
M3M3
S S do T M do T M11 L L11 N alternatingly M N alternatingly M22 L L22 until M until M33 B B
goto
T57 := 0
T57 := 1- T57If t57 == 0 goto M1.quadgoto 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 – CSCE 531 Spring 2006
4. Do alternatingly4. Do alternatingly
S S do T M do T M11 L L11 N alternatingly M N alternatingly M22 L L22 until M until M33 B B {{
backpatch(Lbackpatch(L11.next, M.next, M33.quad);.quad);
backpatch(Lbackpatch(L22.next, M.next, M33.quad);.quad);
backpatch(N.next, Mbackpatch(N.next, M33.quad);.quad);
backpatch(B.false, nextquad);backpatch(B.false, nextquad);
emit( T57 := 1 - T57);
emit( if t57 == 0 goto M1.quad);
emit( goto M2.quad);
S.next = B.true;
}}
Attribute Checklist1.1. L1.nextL1.next2.2. L2.nextL2.next3.3. M1.quadM1.quad4.4. M2.quadM2.quad5.5. M3.quadM3.quad6.6. N.nextN.next7.7. B.falseB.false8.8. B.trueB.true9.9. S.nextS.next
– 15 – CSCE 531 Spring 2006
4. Most Illustrative Error Award4. Most Illustrative Error Award
S S do T M do T M11 L L11 N alternatingly M N alternatingly M22 L L22 until M until M33 B B {{
……
if ( t != 0) {if ( t != 0) {
backpatch ( B.false, Mbackpatch ( B.false, M22.quad );.quad );
} else {} else {
backpatch ( B.false, Mbackpatch ( B.false, M55.quad );.quad );
}}
t = 1 – t ;t = 1 – t ;
……
}}
What’s dynamic?What’s static?
– 16 – CSCE 531 Spring 2006
Question 5 BooleansQuestion 5 Booleans
a.a. Add MarkersAdd Markers easy
b.b. Extend the grammar sufficiently Extend the grammar sufficiently Bid GT id // to handle r > s Bid LT intconst // to handle r < 0 B E relop E with Eid | intconst and relopLT | GT | EQ
is better
c.c. Attributes neededAttributes needed Usual suspects T needs T.place
– 17 – CSCE 531 Spring 2006
Parse TreeParse Tree
L
L M S
S
while M1B do M2 L
L M S
endloop
B1or M1 B2
B1and M1 B2
id GT idid GT id
id LT id
Parse Tree for:Parse Tree for:
while t < s orwhile t < s or r > s and s > t do r > s and s > t do
x = x + 1 ;x = x + 1 ;
r = r/3 ;r = r/3 ;
endloop ;endloop ;
y = r + s ;y = r + s ;
id = E
id + id
NotesNotes
To allow it to fit:To allow it to fit:
1.1. I left out the I left out the semicolonssemicolons
2.2. I left out the I left out the body of the body of the loop.loop.
Also I assume Also I assume that the that the reduction of reduction of the while will the while will emit(goto, M1)emit(goto, M1)
– 18 – CSCE 531 Spring 2006
QuadNoQuadNo OpcodeOpcode LeftLeft RightRight ResultResult TargetTarget
4141 IF-LTIF-LT tt ss
4242 GOTOGOTO
4343 IF-GTIF-GT rr ss
4444 GOTOGOTO
4545 IF-GTIF-GT ss tt
4646 GOTOGOTO
4747 ADDADD xx 11 T0T0
4848 ASSIGNASSIGN T0T0 xx
4949 DIVDIV rr 33 T1T1
5050 ASSIGNASSIGN T1T1 rr
5151 GOTOGOTO 4141
5252 ADDADD rr ss T2T2
5353 ASSIGNASSIGN T2T2 yy
5454
– 19 – CSCE 531 Spring 2006
Pre-Annotation Comments Pre-Annotation Comments
L
L M S
S
while M1B do M2 L
L M S
endloop
B1or M1 B2
B1and M1 B2
id GT idid GT id
id LT id
id = E
id + id
AttributesAttributes B.tB.t B.fB.f id.placeid.place M.quadM.quad S.nextS.next L.nextL.next E.placeE.place
Now we will Now we will annotate annotate each node each node with the with the value of its value of its attributesattributes
– 20 – CSCE 531 Spring 2006
Semi-Annotated Parse TreeSemi-Annotated Parse Tree
B
B1or M1 B2
B1and M1 B2
id GT idid GT id
id LT id
NotesNotes
I left off the I left off the M1.quads.M1.quads.
It is just too It is just too crowded.crowded.
id.place =“t”
id.place =“s”
id.place =“t”
id.place =“r” id.place =“s” 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 – CSCE 531 Spring 2006
func:func:
pushl %ebp pushl %ebp ___save old frame pointer___save old frame pointer
movl %esp, %ebpmovl %esp, %ebp ___set ebp to point to “old ebp”___set ebp to point to “old ebp”
subl $8, %esp subl $8, %esp __allocate space for 8 bytes of local __allocate space for 8 bytes of local variablesvariables
movl $0, -4(%ebp) movl $0, -4(%ebp) __set local1 = 0__set local1 = 0
movl 12(%ebp), %eax movl 12(%ebp), %eax __move arg2 into eax__move arg2 into eax
movl 8(%ebp), %edx movl 8(%ebp), %edx __move arg1 into edx__move arg1 into edx
subl %eax, %edx subl %eax, %edx __arg1-arg2 now in edx__arg1-arg2 now in edx
movl %edx, %eax movl %edx, %eax __also in eax__also in eax
movl %eax, -8(%ebp) movl %eax, -8(%ebp) __also stored in local2__also stored in local2
cmpl $0, 16(%ebp) cmpl $0, 16(%ebp) __compare arg3 with 0__compare arg3 with 0
jne .L3 jne .L3 __if not equal goto L3__if not equal goto L3
movl 12(%ebp), %eax movl 12(%ebp), %eax __move arg2 to eax__move arg2 to eax
addl 8(%ebp), %eax addl 8(%ebp), %eax __ add arg1 to eax __ add arg1 to eax
– 22 – CSCE 531 Spring 2006
movl %eax, -4(%ebp) ___local1 = eaxmovl %eax, -4(%ebp) ___local1 = eax
.L3:.L3:
movl -8(%ebp), %eax ___move local2 to eaxmovl -8(%ebp), %eax ___move local2 to eax
addl -4(%ebp), %eax ____ add local1 to eaxaddl -4(%ebp), %eax ____ add local1 to eax
movl %ebp, %esp ___delete everything on the stack below the movl %ebp, %esp ___delete everything on the stack below the frame pointerframe pointer
pop %ebp ___ restore previous frame pointerpop %ebp ___ restore previous frame pointer
retret