in Compiler Design retagged by
2,263 views
4 votes
4 votes
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.
in Compiler Design retagged by
2.3k views

1 comment

@Sambhrant Maurya, Can you please add the question source ?

1
1

2 Answers

7 votes
7 votes
Best answer
(((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 }
selected by

4 Comments

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

but it doesnot rewrite inside same temp variable 

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

Ullman page 364

0
0
1 vote
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.

2 Comments

The answer is 2.
1
1
5 require if they mention SSA right

But here only mention 3 address code

So at left side variable can be repeat.

So only 2 variable are enough
1
1

Related questions

2 votes
2 votes
1 answer
1