Test 2 Post Mortem CSCE 531 Compiler Construction • Topics • Questions and points • Grade Distribution • Answers April, 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)
Grade Distribution
LR(1) functions: Closure fig 4.38 • 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
S T T y T y T x T x T ε Augment with S’ S J0 = closure({[S’ ● S, $]}) = {[S’ ● S, $], [S ● T, $], [T ● y T y , $], [T ● x T x , $], [T ● , $], } J1 = GOTO(J0, S) = closure({[S’ S ●, $]}) = { [S’ S ●, $] } J2 = GOTO(J0, T) = closure({[S T ●, $]}) = { [S T ●, $] } J3 = GOTO(J0, y) = closure({[T y ● T y, $]}) (details on next page) J4 = GOTO(J0, x) = closure({[T x ● T x, $]}) (details on next page) 1. LR(1) Sets of Items
1. LR(1) Sets of Items continued • J3 = GOTO(J0, 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], • }
1. LR(1) Sets of Items continued • J4 = GOTO(J0, 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 J0 . • The GOTOs from J1 and J2 are empty so we proceed to J3 and then J4 .
1. Continued – See Figure
Question 2. LR(1) Parse Table action goto
Question 2. LR(1) Parse Table action goto
Question 3. LALR(1) Parse Table action goto
4. Do alternatingly • S do T M1 L1 N alternatingly M2 L2 until M3 B { • } • T ε { tmp = newtemp(); emit(tmp := 0;) $$=tmp; }
Do Alternatingly Picture 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) T57 := 0 • M1 • M2 • M3 • S do T M1 L1 N alternatingly M2 L2 until M3 B L1 L1.next N.next goto L2 L2.next B.true B B.false T57 := 1- T57 If t57 == 0 goto M1.quad goto M2.quad
4. Do alternatingly • S do T M1 L1 N alternatingly M2 L2 until M3 B { • backpatch(L1.next, M3.quad); • backpatch(L2.next, M3.quad); • backpatch(N.next, M3.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 • L1.next • L2.next • M1.quad • M2.quad • M3.quad • N.next • B.false • B.true • S.next
4. Most Illustrative Error Award • S do T M1 L1 N alternatingly M2 L2 until M3 B { • … • if ( t != 0) { • backpatch ( B.false, M2.quad ); • } else { • backpatch ( B.false, M5.quad ); • } • t = 1 – t ; • … • } What’s dynamic? What’s static?
Question 5 Booleans • Add Markers • easy • 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 • Attributes needed • Usual suspects • T needs T.place
Parse Tree • Parse Tree for: • while t < s or r > s and s > t do • x = x + 1 ; • r = r/3 ; • endloop ; • y = r + s ; L • 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) L M S id = E S id + id while M1 B do M2 endloop L B1 or M1 B2 L M S id LT id B1 and M1 B2 id GT id id GT id
Pre-Annotation Comments • Attributes • B.t • B.f • id.place • M.quad • S.next • L.next • E.place • Now we will annotate each node with the value of its attributes L L M S id = E S id + id while M1 B do M2 endloop L B1 or M1 B2 L M S id LT id B1 and M1 B2 id GT id id GT id
Semi-Annotated Parse Tree • Notes • I left off the M1.quads. • It is just too crowded. B.T ={41, 45} B.F ={44, 46} B.T ={45} B.F ={44, 46} B.T ={43} B.F ={44} B.T ={41} B.F ={42} B B1 or M1 B2 B.T ={45} B.F ={46} id.place =“t” id.place =“t” id LT id B1 and M1 B2 id GT id id GT id id.place =“s” id.place =“r” id.place =“s” id.place =“s”
func: • pushl %ebp ___save old frame pointer • movl %esp, %ebp ___set ebp to point to “old ebp” • subl $8, %esp __allocate space for 8 bytes of local variables • movl $0, -4(%ebp) __set local1 = 0 • movl 12(%ebp), %eax __move arg2 into eax • movl 8(%ebp), %edx __move arg1 into edx • subl %eax, %edx __arg1-arg2 now in edx • movl %edx, %eax __also in eax • movl %eax, -8(%ebp) __also stored in local2 • cmpl $0, 16(%ebp) __compare arg3 with 0 • jne .L3 __if not equal goto L3 • movl 12(%ebp), %eax __move arg2 to eax • addl 8(%ebp), %eax __ add arg1 to eax
movl %eax, -4(%ebp) ___local1 = eax • .L3: • movl -8(%ebp), %eax ___move local2 to eax • addl -4(%ebp), %eax ____ add local1 to eax • movl %ebp, %esp ___delete everything on the stack below the frame pointer • pop %ebp ___ restore previous frame pointer • ret
What I should have also asked !