| Article Index |
|---|
| Postfix evaluation |
| Source Code |
| Documentation |
| Compatibility & References |
| All Pages |
Page 1 of 4
POSTFIX EVALUATION
Introduction :
Calculators employing reverse Polish notation use a stack structure to hold values. Expressions can be represented in prefix, postfix or infix notations. Conversion from one form of the expression to another form needs a stack. Many compilers use a stack for parsing the syntax of expressions, program blocks etc. before translating into low level code. Most of the programming languages are allowing them to be parsed with stack based machines. Consider the following expression ((1+2)*4)+ 3 can be written down like this in postfix notation with the advantage of no precedence rules and parentheses needed:
1 2 + 4 * 3 +
The expression is evaluated from the left to right using a stack:
-
push when encountering an operand and
-
pop two operands and evaluate the value when encountering an operation.
-
push the result
-
Input
Operation
Stack
1Push operand12Push operand1, 2+Add34Push operand3, 4*Multiply123Push operand12, 3+Add15
The final result, 15, lies on the top of the stack at the end of the calculation.
| < Prev | Next > |
|---|




