Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Postfix Expression Evaluation

 Postfix expression is also known as Reverse Polish Notation(RPN). In RPN the


operators follow their operands.

1. Read the element.


2. If element is an operand then: Push the element into the stack.
3. If element is an operator then : Pop two operands from the stack. Evaluate
expression formed by two operand and the operator. Push the result of
expression in the stack end.
4. If no more elements then: Pop the result Else Goto step 1.

Infix to postfix conversion :

Use a loop to read the character one by one from a Infix String of characters
(strings) representing an infix expression.

( Scan the Infix Expression from left to right.)

For each character do the following in the loop:

 When the character x is an operand


 Add it to the end of the Postfix String of character (strings) that is used
to store the corresponding postfix expression
 When the character x is a left parenthesis “(”
 Push the character x on to the stack of character.
 When the character x is a right parenthesis “)”
 Repeatedly pop a character y from stack and append to Postfix String
until “(“ is encountered in stack. Then pop “(“ from stack.
 (both parentheses are discarded in this process).
 If stack is already empty before finding a “(“, that expression is not a
valid expression.
 When the character x is an operator
 If the character x is an operator and has a higher precedence than
the symbol at the top of the stack, push it onto the stack.
 If the character x is an operator and the precedence is lower than
or equal to the precedence of the operator at the top of the stack,
one element of the stack is popped to the Postfix Expression;
repeat this step (i.e. 2) with the new top element on the stack.
Finally, push the scanned character onto the stack.
 After all characters are scanned, the stack is popped to the Postfix Expression
until the stack is empty.
A * (B + C * D) + E becomes A B C D * + * E +

current symbol operator stack postfix string

1 A A

2 * * A

3 ( *( A

4 B *( AB

5 + *(+ AB

6 C *(+ ABC

7 * *(+* ABC

8 D *(+* ABCD

9 ) * ABCD*+

10 + + ABCD*+*

11 E + ABCD*+*E

12 ABCD*+*E+
A summary of the rules follows:
1. Print operands as they arrive.
2. If the stack is empty or contains a left parenthesis on top, push the incoming
operator onto the stack.
3. If the incoming symbol is a left parenthesis, push it on the stack.
4. If the incoming symbol is a right parenthesis, pop the stack and print the operators
until you see a left parenthesis. Discard the pair of parentheses.
5. If the incoming symbol has higher precedence than the top of the stack, push it on
the stack.
6. If the incoming symbol has equal precedence with the top of the stack, use
association. If the association is left to right, pop and print the top of the stack and
then push the incoming operator. If the association is right to left, push the incoming
operator.
7. If the incoming symbol has lower precedence than the symbol on the top of the
stack, pop the stack and print the top operator. Then test the incoming operator against
the new top of stack.
8. At the end of the expression, pop and print all operators on the stack. (No
parentheses should remain.)
Current
Expression Stack Output Comment
Symbol

Initial
A/B^C-D NULL – Initially Stack is Empty
State

/B^C-D A NULL A Print Operand

B^C-D / / A Push Operator Onto Stack

^C-D B / AB Print Operand

Push Operator Onto Stack because Priority of ^ is


C-D ^ /^ AB greater than Current Topmost Symbol of Stack i.e
‘/’

-D C /^ ABC Print Operand

Step 1 : Now ‘^’ Has Higher Priority than


Incoming Operator So We have to Pop Topmost
D – / ABC^ Element .
Step 2 : Remove Topmost Operator From Stack
and Print it

Step 1 : Now ‘/’ is topmost Element of Stack Has


Higher Priority than Incoming Operator So We
D – NULL ABC^/ have to Pop Topmost Element again.
Step 2 : Remove Topmost Operator From Stack
and Print it

Step 1 : Now Stack Becomes Empty and We can


D – – ABC^/
Push Operand Onto Stack

NULL D – ABC^/D Print Operand

Expression Scanning Ends but we have still one


NULL NULL – ABC^/D-
more element in stack so pop it and display it

You might also like