4.6.3 Translating Infix to Postfix

Suppose, rather than evaluating an infix expression, a programmer wished to translate it and produce the corresponding postfix expression as output. The reason is that postfix expressions can be executed directly, whereas infix expressions are tricky to evaluate. Moreover, if the translation can be done, then the postfix expression may easily be embellished to produce a machine language program for the evaluation of the original infix expression.

The following modification of the infix evaluation algorithm results in such a translation.

1. Eliminate the operand stack. Instead, any pushing onto the operand stack should be changed to outputting the same value.

2. Instead of applying operators to operands, simply output the operators.