Infix to Postfix Conversion
Infix to Postfix Conversion
Not only can a stack be used to evaluate a postfix expression, but we can also use a
stack to convert an expression in standard form (otherwise known as infix) into postfix.
We will concentrate on a small version of the general problem by allowing only the
operators +, *, (,), and insisting on the usual precedence rules. We will further assume
that the expression is legal. Suppose we want to convert the infix expression
a+b*c+(d*e+f)*g
into postfix. A correct answer is a b c * + d e * f + g * +.
When an operand is read, it is immediately placed onto the output. Operators are not
immediately output, so they must be saved somewhere. The correct thing to do is to
place operators that have been seen, but not placed on the output, onto the stack. We
will also stack left parentheses when they are encountered. We start with an initially
empty stack.
If we see a right parenthesis, then we pop the stack, writing symbols until we encounter
a (corresponding) left parenthesis, which is popped but not output.
If we see any other symbol (+, *, (), then we pop entries from the stack until we find an
entry of lower priority. One exception is that we never remove a ( from the stack except
when processing a ). For the purposes of this operation, + has lowest priority and (
highest. When the popping is done, we push the operator onto the stack.
Finally, if we read the end of input, we pop the stack until it is empty, writing symbols
onto the output.
The idea of this algorithm is that when an operator is seen, it is placed on the stack. The
stack represents pending operators. However, some of the operators on the stack that
have high precedence are now known to be completed and should be popped, as they
will no longer be pending. Thus prior to placing the operator on the stack, operators that
are on the stack, and which are to be completed prior to the current operator, are
popped. This is illustrated in the following table:
Stack When
Third
Operator Is
Expression Processed Action
To see how this algorithm performs, we will convert the long infix expression above
into its postfix form. First, the symbol a is read, so it is passed through to the output.
Then + is read and pushed onto the stack. Next b is read and passed through to the
output.
The state of affairs at this juncture is as follows:
+ ab
Stack Output
Next, a * is read. The top entry on the operator stack has lower precedence than *, so
nothing is output and * is put on the stack. Next, c is read and output. Thus far, we have
*
+ abc
Stack Output
The next symbol is a +. Checking the stack, we find that we will pop a * and place it on
the output; pop the other +, which is not of lower but equal priority, on the stack; and
then push the +.
+ abc*+
Stack Output
The next symbol read is a (. Being of highest precedence, this is placed on the stack.
Then d is read and output.
(
+ abc*+d
Stack Output
We continue by reading a *. Since open parentheses do not get removed except when a
closed parenthesis is being processed, there is no output. Next, e is read and output.
*
(
+ abc*+de
Stack Output
The next symbol read is a +. We pop and output * and then push +. Then we read
and output f.
+
(
+ abc*+de*f
Stack Output
+ abc*+de*f+
Stack
Output
We read a * next; it is pushed onto the stack. Then g is read and output.
*
+ abc*+de*f+g
Stack Output
The input is now empty, so we pop and output symbols from the stack until it is
empty.
abc*+de*f+g*+
Stack Output
As before, this conversion requires only O(N) time and works in one pass through
the input. We can add subtraction and division to this repertoire by assigning
subtraction and addition equal priority and multiplication and division equal
priority. A subtle point is that the expression a - b - c will be converted to a b - c
- and not a b c - -. Our algorithm does the right thing, because these operators
associate from left to right.