Expression Evaluation

Humans write expressions like A + B * C, with operators sitting between operands and precedence rules deciding what happens first. A computer evaluating an expression directly would have to work out precedence and parentheses on the fly, every single time. Because of that, expressions are almost always converted into a different form first, one that removes the ambiguity, and only then evaluated.

Three ways to write the same expression

Definition

Infix notation places an operator between its operands (A + B). Prefix notation (also called Polish notation) places the operator before its operands (+ A B). Postfix notation (also called Reverse Polish notation) places the operator after its operands (A B +).

All three represent the exact same computation, just written in a different order:

Infix:    A + B * C
Prefix:   + A * B C
Postfix:  A B C * +

Infix is what humans read and write naturally, but it needs parentheses and precedence rules to stay unambiguous, A + B * C only means "multiply first" because of a rule we agreed on separately, not because the expression itself says so. Prefix and postfix both remove that dependency: the position of each operator relative to its operands is enough, on its own, to say which sub-expression it applies to, in whichever order the scan happens (prefix reads right-to-left with a stack, postfix reads left-to-right). That is exactly why compilers and calculators convert to one of these forms before evaluating.

Why postfix is the common choice

Prefix and postfix are equally valid, unambiguous, parenthesis-free notations, evaluating either just means scanning in the matching direction (right-to-left for prefix, left-to-right for postfix) with a stack. Postfix tends to be the one taught and used most, mainly because a left-to-right scan matches how text is naturally read and processed, and because converting from infix (which is also read left-to-right) to postfix is a more direct, single-pass translation than converting to prefix.

The full landscape of conversions

Given three notations, there are six possible directions to convert between them, plus evaluating each notation directly:

Infix   <-> Prefix
Infix   <-> Postfix
Prefix  <-> Postfix
 
Evaluate Prefix directly
Evaluate Postfix directly

Every one of these follows the same underlying idea, a stack temporarily holding operators or operands until the right moment to combine them, but the exact bookkeeping differs by direction. This page focuses on the two steps used most often together: converting infix to postfix, then evaluating that postfix expression, each covered on its own page next.

Covered in depth here

Infix to Postfix
Postfix Evaluation

Quick recall

  • The same expression can be written in infix, prefix, or postfix notation, all mathematically equivalent, differing only in where the operator sits relative to its operands.
  • Infix needs precedence rules and parentheses to stay unambiguous; prefix and postfix don't, the token order alone fixes the meaning.
  • There are six possible conversions between the three notations, plus direct evaluation of prefix or postfix, all built on the same stack-based idea.
  • Infix to Postfix conversion, followed by Postfix Evaluation, is the most common pairing in practice and the one covered in depth next.