27-Back Patching-18-09-2024
27-Back Patching-18-09-2024
100: if x < 100 goto ____ 100: if x < 100 goto 106
101: goto ____ 101: goto 102
102: if y > 200 goto ____ 102: if y > 200 goto 104
103: goto ____ 103: goto 107
104: if x!=y goto 106
104: if x!=y goto ____
105: goto 107
105: goto ____ 106: true
106: true 107: false
107: false
Annotated parse tree
S denotes a statement
S → if (B) S | if (B) S else S | while (B) S | {L} | A
L a list
L → LS | S
A an argument
B a boolean
• The attributes of S is S.nextlist which will backpatch list for jumps to the next
statement after S.
• The attributes of L is L.nextlist which will backpatch list for jumps to the next
statement after L
Semantic rules for control flow statements to incorporate backpatching
Production Semantic Action
SA { S.nextlist := nil }
S begin L end { S.nextlist := L.nextlist }
S if E then M S1 { backpatch(E.truelist, M.quad);
S.nextlist := merge(E.falselist, S1 .nextlist) }
S if E then M S1 N { backpatch(E.truelist, M1 .quad);
else M2 S2 backpatch(E.falselist, M2 .quad);
S.nextlist := merge(S1 .nextlist, merge(N.nextlist, S2 .nextlist)) }
S while M1 E do M2 { backpatch(S1 ,nextlist, M1 .quad);
S1 backpatch(E.truelist, M2 .quad);
S.nextlist := E.falselist;
emit(„goto _‟) }
L L1 ; M S { backpatch(L1 .nextlist, M.quad);
L.nextlist := S.nextlist; }
LS { L.nextlist := S.nextlist; }
N ε { N.nextlist := makelist(nextquad());
emit(„goto _‟) }
SA
• This production is a termination production and hence there is no need for a
backpatch
S begin L end
• Both S and L has a nextlist attribute and they are set to the same.
• The statements between „begin‟ and „end‟ are run only once.
S if E then M S1
• The variable M produces ε.
• If the expression is false, then the statement S1 need to be skipped.
• If expression is true, then S1 should be executed.
• In both the scenarios, the statement that is available outside S1 need to be
continued.
• To carry out this, the falselist of E and the nextlist of S1 are merged and that is
assigned as S‟s nextlist.
• If expression is true then the statement S1 is to be executed.
• To incorporate this we backpatch the truelist of the expression to S1 which is
done with the help of M.quad
N ε
• The goto is to skip S2 which is part of if-then-else and go to the statement
following S2.
S if E then M S1 N else M2 S2
• The expression is evaluated and if it is true S1 is to be executed and S2 if it is false S1
is to be executed.
• This is implemented by backpatching the truelist and falselist to M1.quad and
M2.quad which is the beginning of statements S1, S2 respectively.
• After executing S1, S2 need to be skipped and the statement which is available after S
needs to be executed.
• After executing S2 by skipping S1, we need to execute the statement outside the body
of S. To incorporate this we use the symbol N to skip S2.
• We assign the nextlist of S as S1‟s next and S2‟s next
S while M1 E do M2 S1
• The variable M2 helps to go to go to statement S1 if the expression is true.
• If the expression is false then we need to go to the statement following S1 which is
done as the nextlist of S the same as E‟s falselist.
• To incorporate continuation of the loop, M1 is used which is to come back after
finishing S1.
• The goto is to loop again to execute the expression E
7. Generate three address code for the following code
prod = 0 ;
i=1;
The three-address code for the given
do
program segment is given below:
{
prod = prod + a[ i ] x b[ i ] ;
i=i+1; 1. prod = 0
} while (i <= 10) ; 2. i = 1
3. t1 = 4 x i
4. t2 = a[t1]
5. t3 = 4 x i
6. t4 = b[t3]
7. t5 = t2 x t4
8. t6 = t5 + prod
9. prod = T6
10. t7 = i + 1
11. i = t7
12. if (i <= 10) goto (3)