243 views
The minimum number of temporary variables created in 3 address code of the following expression are _____
a+b*c+d-e-a+b*c

Assume order of precedence from highest to lowest as: *,+ and - .Consider associativity for + and * are not important but - is left associative.

edited | 243 views
+1

(((a+(b*c))+d)-e)-(a+(b*c))    [ as * have highest precedence so should be done first , then + should be dne ad then -                                                                             and as associativity of + is not importnat so i m taking + as left associative]

t1 = b * c;

t2 = a + t1;

t1 = t2 + d;

t1 = t1 - e;

t1 = t1 - t2;             so minimum 2 temporary variables are needed {t1 , t2 }
by Active (3k points)
edited
0
Why are you repeating t1? Shouldn't the L.H.S be a unique variable?
+5
Min No of register required=2

Min no of tem variable required=5
0
No ,  need not becoz here if we store even in t1 then also no problem as we dnt need t1 again later
0

temp variables created by the compiler as needed to keep the number of operands down

but it doesnot rewrite inside same temp variable

0
How to know when the temporary variables can be repeated on the LHS?
0 Ullman page 364

+1 vote
Question mentioned the statement  : a+b*c+d-e-a+b*c

"Temp variables created by the compiler as needed to keep the number of operands down"

Three Address Code represented as below :

Precedence is * is highest, so it is evaluated first.

t1 = b * c

t2 = a + t1

t3 = t2 + d

t4 = t3 - e

t5 = t4 -t2

So minimum 5 temp variables needed.
by Active (1.7k points)
0