| | | | | |

Evaluating Postfix Expressions -- Algorithm

  • Postfix expressions: operands precede operator
  • Tokens: atomics of expressions, either operator or operand
  • Evaluation algorithm:
    • Use stack of tokens
    • Repeat
    • Read token
      • If operand, push onto stack
      • If operator
        • pop operands off stack
        • evaluate operator on operands
        • push result onto stack
    • Until expression is read
    • Return top of stack
  • Most CPUs have hardware support for this algorithm
  • Translation from infix to postfix also uses a stack (software)

| | Top of Page | 5. Abstract Data Types: Stacks and Queues - 14 of 22