The Gateway to Computer Science Excellence
+3 votes
The minimum number of temporary variables created in 3 address code of the following expression are _____

Assume order of precedence from highest to lowest as: *,+ and - .Consider associativity for + and * are not important but - is left associative.
in Compiler Design by Active (3.9k points)
edited by | 261 views

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

2 Answers

+5 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 }
by Active (3.1k points)
selected ago by
Why are you repeating t1? Shouldn't the L.H.S be a unique variable?
Min No of register required=2

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

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

but it doesnot rewrite inside same temp variable 

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

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)
The answer is 2.
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
Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,737 questions
57,378 answers
105,314 users