Documentation on Postfix It is a long-standing tradition in mathematics to write the operator between the operands, as in X + Y, rather than after the operands , as in XY+. The form with the operator "in" between the operands is called infix notation. The form with the operator "after" the operands is called postfix notation. It is also called "reverse Polish" after the Polish logican Lukasjewicz, who investigated this notation in the 1950's. Postfix has a number of advantages over infix for expressing algebraic formulas. First, any formula can be expressed without parenthesis. Second, it is very convenient for evaluating formulas on computers with stacks. Third, infix operators have precedence. For example, we know that a * b + c means (a * b) + c and not a * (b + c), because multiplication has been defined to have precedence over addition. Postfix eliminates this nuisance. Several algorithms for converting infix formulas into postfix exist. The one given here is adapted from an idea of E. W. Dijkstra. Assume that a formula is composed of the following symbols: variables, the binary operators +, -, *, /, and ^ (which stands for exponentiation), and left and right parentheses. To mark the beginning and end of the formula, we will insert the symbol '#' before the first symbol and after the last symbol. (We can also assume that all variables are of the same length (one character) and that there are no blank spaces.) Figure 1 below shows a railroad track from New York to California, with a spur in the middle that heads towards Texas. Each symbol in the formula is represented by one railroad car. The train moves westward (to the left). When each car arrives at the switch, it must stop and ask if it should go to California directly or take a side trip to Texas. Cars containing variables always go directly to California and never to Texas. Cars containing other symbols must inquire about the contents of the nearest car on the Texas line before entering the switch. o s w i t _ _ _ _ _ _ _ _ c |A|-|*|-|(|-|B|-|+|-|C|-|)|-|#| h - - - - - - - - California --------------------*------------------------------------New York \ / \ / * | | _ | |#| | - | | Texas Figure 1 Page 2 Figure 2 below shows what happens, depending on the contents of the nearest car on the Texas line and the car at the switch. The first '#' always goes to Texas. (This is show in Figure 1.) The numbers in the chart refer to the following situations: 1. The car at the switch goes to Texas. 2. The most recent car on the Texas line turns around and goes to California. 3. Both the car at the switch and the most recent car on the Texas line are hijacked and disappear; that is, both are deleted. 4. Stop, the symbols now in California represent the postfix formula. 5. Stop, an error has occurred. The original formula was not correctly balanced. car at the switch # + - * / ( ) ^ M a o #| 4 | 1 | 1 | 1 | 1 | 1 | 5 | | o r n _______________________________| s r +| 2 | 2 | 2 | 1 | 1 | 1 | 2 | | t i T _______________________________ v e -| 2 | 2 | 2 | 1 | 1 | 1 | 2 | | r e x _______________________________ e d a *| 2 | 2 | 2 | 2 | 2 | 1 | 2 | | c s _______________________________ e c /| 2 | 2 | 2 | 2 | 2 | 1 | 2 | | n a s _______________________________ t r p (| 5 | 1 | 1 | 1 | 1 | 1 | 3 | | l u _______________________________ y r ^| | | | | | | | | _______________________________ Figure 2 After each action is taken, a new comparison is made between the car at switch, which may be the same car as in the previous comparison or it may be the next car, and the car that is now the last one on the Texas line. The process continues until step 4 is reached (or step 5 if an error has occurred. Notice that the Texas line is being used as a stack, with routing a car to Texas being a push operation, and turning a car already on the Texas line around and sending it to California being a pop operation. Note that the order of the variables in an infix and its corresponding postfix expression is the same. Only the order of the operands is changed. Figure 3 gives some examples of infix and the converted postfix expression. infix postfix A + B * C ABC*+ A * B + C AB*C+ A * B + C * D AB*CD*+ (A + B)/(C+D) AB+CD+/ A*B^C+D ABC^*D+ A * B / C AB*C/ ((A+B)*C+D)/(E+F+G) AB+C*D+EF+G+/ A^B^C ABC^^ Figure 3 Page 3 Postfix expressions are the ideal notation for evaluating formulas on a computer with a stack. Suppose we have the expression in infix of (A+B*C)/(D+E*B-F) where the values of A, B, C, D, E, and F are 8, 2, 5, 1, 3, and 4 respectively. The corresponding postfix expression for the infix formula is ABC*+DEB*+F-/. One evaluates the postfix expression by the following algorithm: 1. Look at the next symbol. If it is a variable, push its value onto the stack. 2. If the next symbol is an operator, pop the top two items off of the stack, perform the operator on the two symbols, by next_to_last operator last. 3. If there is still symbols left, go to step 1. 4. Terminate with the evaluation of the expression being at the top of the stack. For the expression of ABC*+DEB*+F-/, we would get: step 1 ABC*+DEB*+F-/ Push 8 2 BC*+DEB*+F-/ Push 2 3 C*+DEB*+F-/ Push 5 4 *+DEB*+F-/ Multiply 2 * 5 5 +DEB*+F-/ Add 8 + 10 6 DEB*+F-/ Push 1 7 EB*+F-/ Push 3 8 B*+F-/ Push 2 9 *+F-/ Multiply 3 * 2 10 +F-/ Add 1 + 6 11 F-/ Push 4 12 -/ Subtract 7 - 4 13 / Divide 18 / 3 The stack at each step above would look like: top->8 2 8 8 ->18 18 18 18 18 18 18 18 ->6 ->8 2 ->10 -> 1 1 1 1 -> 7 7 -> 3 ->5 -> 3 3 -> 6 -> 4 -> 2 step 1 2 3 4 5 6 7 8 9 10 11 12 13 A computer organized around a stack offers several advantages compared to multiregister machines. they are 1. short instructions, because many instructions have no addresses 2. formulas are easy to evaluate. 3. There is no need for complicated algorithms to optimize register use.