TACBool1

advertisement
Boolean Function :
# Encode the boolean function as arithmetic function.
# Transform it into normal control statement as is done in the following
example :
Boolean expression :: a or b and not c
Three address sequence for above boolean expression is :
t1 := not c
t2 := b and t1
t3 := a or t2
A relational expression such as a<b is equivalent to the conditional statement if a<b then 1
else 0, which can be translated into the three-address code sequence. Let us start numbering from
100, then the resulting sequence is :
100 :
101 :
102 :
103 :
104 :
if a<b goto (nextstat+3 =103)
t := 0
goto (nextstat+2 = 104)
t:=1
...........
The nextstat gives the index of the next three-address statement in the output sequence, and
emit increments nextstat after producing each three-address statement.
In the boolean expressions 1 is used to denote true and 0 to denote false.
We will consider the boolean expressions generated by the following grammar :
E --> E or E | E and E | not E | id1 relop id2 | true | false | (E)
Now, the translation scheme using the numerical representations as cited above for boolean
expressions is as follows :
E --> E1 or E2
{ E.place = newtemp();
emit(E.place = E1.place 'or' E2.place) }
E --> E1 and E2
{ E.place = newtemp();
emit(E.place = E1.place 'and' E2.place) }
E --> not E1
{ E.place = newtemp();
emit(E.place = 'not' E.place) }
E --> (E1)
{ E.place = E1.place }
E --> id1 relop id2
{ E.place = newtemp();
emit( 'if' id1 relop id2 'goto' (nextstat+3)) ;
emit ( E.place = '0');
emit ( 'goto' (nextstat+2));
E --> true
{ E.place = newtemp();
emit ( E.place = '1') }
E --> false
{ E.place = newtemp();
emit ( E.place ='0') }
_____________________________________________________________________________
Let the boolean expression be :
a<b or c<d and e<f
Parse tree for this expression is :
E
______________________|_________
|
|
|
E
or
E
____ _|_____
_________|_______
|
|
|
|
|
|
a
<
b
E
and
E
___|___
___|____
| | |
| |
|
c < d
e < f
Translation statements for above boolean expressions are :
100 : if (a<b) goto 103
101 :
102 :
103 :
104 :
105 :
106 :
107 :
108 :
109 :
110 :
111 :
112 :
113 :
t1 = 0
goto 104
t1 = 1
if (c<d) goto 107
t2 = 0
goto 108
t2 = 1
if ( e< f) goto 111
t3 = 0
goto 112
t3 = 1
t4 = t2 and t3
t5 = t1 or t4
Control Statements :
S --> if E then S
S --> if E then S1 else S2
S --> while E then S1
Code for above 3 statements will be in this form :
a) S --> if E then S
1.) E.code
E.true : 2.) S1.code
E.false : 3.) S.next
............
b) S --> if E then S1 else S2
1.) E.code
2.) S1.code
3.) goto S.next
E.false : 4.) S2.code
...........
E.true :
c) S --> while E then S1
S.begin : 1.) E.code
E.true : 2.) S1.code
3.) goto S.begin
E.false : 4.) ...........
Flow of Control :
1.) S --> if E then S1
{ E.true = newtemp();
E.false = S.next;
S1.next = S.next;
S.code = E.code || gen(E.true ) || S1.code
}
2.) S --> if E then S1 else S2
{ E.true = newtemp();
E.false = newtemp();
S1.next = S.next();
S2.next = S.next();
S.code = E.code || gen (E.true) || S1.code || goto ( S1.next) ||
gen ( E.false) || S2.code
}
3.) S --> while E then S1
{ S.begin = newtemp();
E.true = newtemp();
E.false = S.next;
S1.next = S.begin;
S.code = gen(S.begin) || E.code || gen ( E.true ) || S1.code
|| gen ( 'goto' S.begin)
}
Control Flow Translation of Boolean Expression :
Following are the productions and corresponding Semantic Rules :
1.) E --> E1 or E2
Semantic Rules :
E1.true = E.true;
E1.false = newlabel;
E2.true = E.true ;
E2.false = E.false ;
E.code = E1.code || gen(E1.false) || E2.code
2.) E --> E1 and E2
Rules :
E1.true = newlabel;
E1.false = E.false ;
E2.true = E.true ;
E2.false = E.false ;
E.code = E1.code || gen ( E1.true) || E2.code
3.) E --> not E1
Rules :
E1.true = E.false ;
E1.false = E.true ;
E.code = E1.code
4.) E --> (E1)
Rules :
E1.true = E.true;
E1.false = E.false ;
E.code = E1.code
5.) E --> true
Rules :
gen ('goto' E.true )
6.) E --> false
Rules :
gen ( 'goto' E.false)
7.) E --> id1 relop id2
Rules :
gen ( 'if' id1 relop id2 'goto' E.true)
gen ( 'goto' E.false )
Example :
1.)Consider the expression :
a < b or c < d and e < f
3-address code produced will be :
if ( a<b) goto L.true
goto L1
L1 : if( c<d) goto L2
goto L.false
L2 : if ( e<f) goto L.true
goto L.false
2.) Consider the expression :
while a < b do
if c < d then
x=y+z
else
x = y-z
3-address code produced will be :
E.true = L1
E.false = L.false
S.begin = if (a < b ) goto L1
goto L.false
L1 :
if ( c < d) then goto L2
E.true = L2
E.false = L3
goto L3
L2 :
x=y+z
goto S.begin
L3 :
x=y-z
goto S.begin
Submitted By :
Arpit Jain
03CS3009
Download