Вы находитесь на странице: 1из 2

Application of Stack : Evaluation of Arithmetic Expressions

Observation :
We read an arithmetic expression left to right focusing on the operators. We keep on moving from left
to right as long as we get operators in increasing order of precedence. At the moment we find that the
next operator is of lower precedence than the current one, we apply the current operator on its appropriate
operands (this operator is thus removed from the list). So previous to current operator becomes current
operator now. We again see if the next operator has less precedence than the (new) current, and if so we
perform the current operator and thus further simplify the expression. We keep on doing so until we find
that the precedence of the next operator is more than the current one. When it happens, we start moving
from left to right from the point we stopped. This is the way to evaluate the arithmetic expression. Care-
fully observe the method above and try to see how a suitable data structure can be used to implement the
algorithm. One crucial observation here is : we process the latest operator and operands first. Of course one
crucial point is we need priority of all operators.

Operator priority
+,- 1
*,/ 2
^ 3
\$ 0

Number-STACK : N-stack
OPERATOR-STACK : O-stack

Simple-case-Method
push(O-stack,’\$’);
While (true) do
x <- next token;
if x is number push(x,N-stack);
else
{ if (x = ’#’)
{ While(not Isempty(O-stack))
{
o <- POP(O-stack);
Execute(o);
}
}
else
{ while(PRIORITY(TOP(O-stack) >= PRIORITY(x))
{ o <- POP(O-stack);
Execute(o); // POP two numbers from Nstack and apply operation y on them
// and place the result back into N-stack
}
}
}

How can we extend it to incorporate


1. associativity of operators 2+32̂4̂
2. parentheses

One may try to come up with adhoc tricks to incorporate these extensions.
But we would like to extend the above solution to a more general one.

1
2*(3+4)-5 is different from 2*3+4-5
We must insert ’(’ into the stack : why ?
Reason: to evaluate parenthesis before any other operator preceding it. =¿
this implies when ’(’ is incoming operator its priority should be higher than any other operator present at
top of the stack

Once we inserted ’(’ into the stack how do we process the operators that we encounter in this parenthesis.
For example how do we evaluate 2*(3+4*5)-66*3 ?
The process has to be the same as if we are starting a new arithmetic expression. So we need to treat ’(’ as
$ now.
So the priority of ’(’ in the stack has to be smaller than any other incoming operator.

What about ’)’ : we need to evaluate the complete expression in the parenthesis before any other operator,
so we need to treat ”)” like ”#”. So, on seeing ’)’ we run a while loop and apply the operators until we see
the matching ’)’.
Solution : keep two types of priorities for each operator :
INS-priority = in stack priority
INC-priority = in coming priority

INS priority INC priority


+,- 2 2
*,/ 3 3
^ 4 5
( 1 6
\$ 0 -

Algorithm :
---------------------------------------
While (true) do
x <- next token;
if x is number push(x,N-stack);
else
{
case x = ’#’ : While(not Isempty(O-stack))
{
o <- POP(O-stack);
Execute(o);
break;
}

case x = ’)’ : While(TOP(O-stack)<> ’(’) do


{
o <- POP(O-stack);
Execute(o);
}

otherwise : while(PRIORITY(TOP(O-stack) >= PRIORITY(x))


{ o <- POP(O-stack);
Execute(o); // POP two numbers from Nstack and
// apply operation o on them,
// and place the result back into N-stack
}
Push(x,O-stack);
}

Вам также может понравиться