Evaluation of Postfix Expression-Concept and Example
1.
Dr.Mary Jacob
Department ofComputer Science
Kristu Jayanti College(Autonomous),Bengaluru
Evaluation of Postfix Expression
2.
A postfix expression(also called Reverse Polish Notation) is a
mathematical notation in which operators follow their operands.
Example:
Infix: A + B
Postfix: A B +
What is a Postfix Expression?
3.
Why Postfix?
No needfor parentheses to define operator precedence.
Easy to evaluate using a stack-based approach.
4.
Algorithm to Evaluatea Postfix Expression
Initialize an empty stack.
Scan the postfix expression from left to right.
For each token in the expression:
1.If the token is an operand, push it onto the stack.
2.If the token is an operator (+, −, ×, ÷):
Ø Pop two operands from the stack (second popped =
operand1, first popped = operand2).
Ø Apply the operator: result = operand1 operator operand2
Ø Push the result back onto the stack.
After the expression is fully scanned, the value left in the stack
is the final result.