The Gateway to Computer Science Excellence
+23 votes

To evaluate an expression without any embedded function calls

  1. One stack is enough
  2. Two stacks are needed
  3. As many stacks as the height of the expression tree are needed
  4. A Turing machine is needed in the general case
in Compiler Design by Veteran (52.3k points) | 2.5k views
In case of with embedded function calls, we need stack of size >=2, not exactly two, bcoz the functions can be recursive also.

2 Answers

+24 votes
Best answer
Expression without any calls in it $=> 1+2*3-4$

Expression with embedded calls $=> 1 + fun1(a,b,c) *fun2(3.4,58) - fun3(x,yz)$;

First we can convert Infix to Postfix using single stack (Using it as operator stack)

Then we can evaluate that expression using Single stack.

Correct Answer: $A$
by Boss (42.1k points)
edited by
how will you multiply using single stack ?
Does answer makes more sense now ? Amar ? I updated it .
what u think , embedded calls mean here ? I think, it mean like mul(a,b)   , add(5,6) .. mean operations itself..
Expression without any calls in it => 1+2*3-4

Expression with embedded calls => 1 + fun1(a,b,c) *fun2(3.4,58) - fun3(x,yz);
Ok, now it makes sense.. :)

And in general , when embedded calls allowed  ?   2 stacks ?
Why Turing machine is not needed, a TM can also perform required function i.e it can evaluate the expression  1+2*3-4 also, by Turing Thesis.

Is this the fact that, a stack is used for postfix evaluation of expression and also each function will have there own stack, so option A is correct?

How to decide here, a TM also does the job for us right?
What if questions says "WITH EMBEDDED FUNCTION CALLS"?

rahul sharma 5 : In that case Two stacks $First \ one \ works \ as \ Operator  \ stack$ and second one will be used to resolve function calls : 
Example : main(){                                     

int x=3,y=4,result;



addvalues(int v1,int v2)

return v1+v2;

functions                                          |  Operator    \
addvalues()                                        |    4              \
main()                                                 |3                 \                        7
Hello Anil

It's job of Pushdown automata (PDA) to evaluate an expression.

When you can do it using PDA. So in general case PDA is used not TM.


i did not understand how you built table? In table why there is 4 in addvalues and 3 in main?



For expression with embedded function many stacks required ??

2 or 1


@jatin khachane 1

already saxena0612, explained it... but still you are asking same,

didn't you read the comments before commenting ?

What would be answer "with embedded function calls"
@anchitjindal just read all the comments you will get what you want
any expression can be converted into Postfix or Prefix form.

Prefix and postfix evaluation can be done using a single stack.

For example : Expression ’10 2 8 * + 3 -‘ is given.
PUSH 10 in the stack.
PUSH 2 in the stack.
PUSH 8 in the stack.
When operator ‘*’ occurs, POP 2 and 8 from the stack.
PUSH 2 * 8 = 16 in the stack.
When operator ‘+’ occurs, POP 16 and 10 from the stack.
PUSH 10 * 16 = 26 in the stack.
PUSH 3 in the stack.
When operator ‘-‘ occurs, POP 26 and 3 from the stack.
PUSH 26 – 3 = 23 in the stack.
So, 23 is the answer obtained using single stack.

Thus, option (A) is correct.
$2$ stacks not needed?
one stack is enough..!
–1 vote
ans a)
by Loyal (5.2k points)
postfix .is done by one stack. computer like postfix evalution.. .so ans is (a)...

Related questions

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,833 questions
57,729 answers
107,861 users