The Gateway to Computer Science Excellence
+3 votes
216 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.
in Compiler Design by Active (3.4k points)
edited by | 216 views
+1

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

2 Answers

+4 votes
(((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 by
0
Why are you repeating t1? Shouldn't the L.H.S be a unique variable?
+4
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
The answer is 2.
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,647 questions
56,492 answers
195,439 comments
100,701 users