Apex › Systems & OS

How a calculator parses math (shunting-yard)

We write math infix (3 + 4 × 2), but a machine can’t evaluate that left-to-right. Dijkstra’s shunting-yard algorithm converts it to Reverse Polish (postfix, 3 4 2 × +) in one linear pass with an operator stack + an output queue — precedence and parentheses resolved purely by when operators leave the stack, no parse tree. Then a stack machine evaluates the RPN. Verified: the result matches an independent recursive-descent parser on every random expression (0 mismatches over 4828). Watch tokens shunt between the queue and stack, then reduce to the answer.

More in Systems & OS