CEG's Linux User Group

  • Increase font size
  • Default font size
  • Decrease font size
Home Labs Postfix evaluation

Postfix evaluation

E-mail Print
User Rating: / 8
PoorBest 
Article Index
Postfix evaluation
Source Code
Documentation
Compatibility & References
All Pages

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:
  1. push when encountering an operand and
  2. pop two operands and evaluate the value when encountering an operation.
  3. push the result

Input

Operation

Stack

1
Push operand
1
2
Push operand
1, 2
+
Add
3
4
Push operand
3, 4
*
Multiply
12
3
Push operand
12, 3
+
Add
15
The final result, 15, lies on the top of the stack at the end of the calculation.



Last Updated on Saturday, 06 June 2009 19:52  
Please register or login to add your comments to this article.