.LEFT MARGIN 8 .RIGHT MARGIN 72 The following algorithm evaluates an algebraic expression in two steps. The expression is first converted to a reverse Polish notation expression and then the reverse Polish expression is evaluated. Step one converts an algebraic expression (infix notation) to a reverse Polish expression (postfix notation). The algebraic expression is first broken up into tokens which are passed to the conversion algorithm which reassembles them into a reverse Polish expression. The tokens are of two types, operands (values) and operators (+,-,*,/,etc). The reverse polish expression is built in a FIFO queue (called the "Intermediate Queue") which is used by step two. The conversion algorithm also uses a stack called the "Operator Stack". Step two evaluates the reverse Polish expression and generate an results. This process requires a stack to hold operands called the "Operand Stack". Each token is read, one at a time, from the "Intermediate Queue". If the token is an operand it is pushed onto the the "Operand Stack". If the token is an operator the operation is performed on the operands on the top of the stack and the results pushed onto the stack. When there are no more tokens in the queue the results will be on the top of the stack. .LITERAL Converting And Algebraic Expression To Reverse Polish Notation 1. Scan the algebraic expression form left to right breaking up into tokens. Pass the tokens to the following steps. 2. If the token is a operand (value), add it to the "Intermediate Queue". 3. if the token is an operator (+,-,*,/,etc.) do one of the following: (a) Look at the operator on the top of the "Operator Stack" (TOS). If the TOS has an equal or greater precedence that the token, pop the TOS and add it to the "Intermediate Queue". Got to step (a). (b) If the TOS has a precedence less than the token, push the token onto the "Operator Stack". (c) When the token is a left parenthesis push it onto the "Operator Stack". (d) If the token is a right parenthesis discard it. then pop operators one at a time from the "Operator Stack" and add them to the "Intermediate Queue". Continue this until a left parenthesis is found, which is discarded. 4. When the scan is complete, add any operators left on the "Operator Stack" to the "Intermediate Queue". Evaluating An Expression In Reverse Polish Notation 1. Scan the expression from left to right breaking it into tokens which are passed to the following steps. 2. Push any tokens that are operands onto the "Operand Stack". 3. If a token is a operator the appropriate number of operands are removed from the top of the stack and the operation preformed. The results is pushed onto the operand stack. 4. When the scan is complete the results of the expression is on the top of the stack. .END LITERAL