The Prefix Processor

You can imagine a prefix processor that operates in a similar manner to the postfix processor that we met in the last section. It is a little more complicated than the postfix processor because the operators come up front rather than afterwards. It sounds as if all you need to do when you find an operator is to take the appropriate number of operands off the input stream, do the calculation, and put the answer back on the stack. This presents problems when the operands are themselves expressions.

We can get round this problem by giving our processor two separate stacks - one for operators and one for operands (the numbers which the operators work on). The processor knows that each of the operators that we have looked at so far takes two operands, so whenever there are two operands on the operand stack and one operator on the operator stack, then the operation can be carried out.

You probably didn't understand a word of that, so an example is needed to clear the matter up.



Go back

Main Menu