Postfix Algorithms
Expressions where the operator follows the operand to which it is applied are in postfix form. This is also known as "reverse Polish notation."
A few examples...
1. 2 + 3 * 4 -> 2 3 4 * +
3. (2 + 3) * 4 -> 2 3 + 4 *
A subtlety is that these operations associate from left to right. So:
2 + 3 + 4 -> 2 3 + 4 + NOT 2 3 4 + +
Postfix is a way of enforcing precedence rules without using parenthesis.
To evaluate postfix expressions using a stack:
*When a number (operand) is seen push it on the stack.
*What an operator is seen, pop the stack twice, and apply the operand. Then push the result.
To convert infix to postfix:
First, use only +, *, (, )
*When a number (operand) is read, push it on the stack.
*If a ) is encountered, then pop the stack and output until a ( is found. Do not write the ( to output.
*If a +, *, or ( is found, then pop and output until the top of the stack is a symbol of lower priority. EXCEPT, never remove a ( except when processing a ). [So, + has lowest priority, * has next lowest priority, and ( has highest priority.] Then, push that operator onto the stack.
*At EOF, pop the stack until it is empty.