140 likes | 324 Views
Backpatching II. Prepared by: Shreyas Ashok Karkhedkar Roll Number: 04CS3013. Backpatching II. Consider the following grammar: E → E 1 or ME 2 E → E 1 and ME 2 E → not E 1 E → (E 1 ) E → id 1 relop id 2 E → false E → true M → ε. Backpatching II.
E N D
Backpatching II Prepared by: Shreyas Ashok Karkhedkar Roll Number: 04CS3013
Backpatching II • Consider the following grammar: E → E1 or ME2 E → E1 and ME2 E → not E1 E → (E1) E → id1 relop id2 E → false E → true M → ε
Backpatching II • The corresponding semantic rules are given by: E → E1 or ME2 { Backpatch( E1.falselist,M.quad) E.truelist=merge(E2.truelist,E1.truelist) E.falselist=E2.falselist }
Backpatching II E → E1 and ME2 { Backpatch(E1.truelist,M.quad) E.falselist=merge(E1.falselist,E2.falselist) E.truelist=E2.truelist }
Backpatching II E → not E1 { E.truelist = E1.falselist E.falselist = E1.truelist }
Backpatching II E → (E1) { E.truelist = E1.truelist }
Backpatching II E → id1 relop id2 { E.truelist = makelist(nextquad) E.falselist = makelist(nextquad +1) emit( “if” id1 relop id2 “goto ____”) emit( “goto ____”) }
Backpatching II E → false { E.falselist = makelist(nextquad) emit( “goto ____”) } E → true { E.truelist = makelist(nextquad) emit( “goto ____”) }
Backpatching II M → ε { M.quad = nextquad }
Backpatching II • Example : Consider the string: a<b or c<d and e<f (Assuming that the grammar is left associative)
Backpatching II • The corresponding intermediate code is: 100: if ( a<b ) goto L3 goto L1 102: L1: if ( c<d ) goto L2 goto L4 104: L2: if ( e<f ) goto L3 goto L4 L3: ….. ….. L4: ….. …..
Backpatching II • The code after backpatching becomes: 100: if ( a<b ) goto ____ goto 102 102: L1: if ( c<d ) goto 104 goto ____ 104 L2: if ( e<f ) goto ____ goto ____ L3: ….. ….. L4: ….. ….. • This code is more similar to the machine code
Backpatching II • We define the function Backpatch (X , Y) as follows: • Backpatch (X , Y) : replace the line numbers listed in ‘X’ with the value ‘Y’ Note: We try to design the function Backpatch in such a way that ‘Y’ is unique for every ‘X’