Infix to Postfix Calculator
Convert infix notation expressions to postfix (Reverse Polish Notation) using the Shunting-Yard algorithm, then evaluate the result step by step.
About
Incorrect operator precedence parsing is the root cause of silent calculation errors in compilers, spreadsheets, and embedded firmware. The Shunting-Yard algorithm, published by Edsger Dijkstra in 1961, resolves this by converting human-readable infix notation (e.g., 3+4×2) into unambiguous postfix (Reverse Polish Notation): 3 4 2 × +. Postfix eliminates all parentheses and precedence ambiguity because the evaluation order is encoded directly in the token sequence. This tool implements the full Shunting-Yard conversion with step-by-step tracing, then evaluates the resulting postfix expression on a simple stack machine. It is restricted to single-digit operands (0 - 9) and the operators + − × / ^, matching the d8 interpreter constraint.
Limitation: fractional and negative literal operands are not supported. Division is floating-point; division by zero returns an error rather than infinity. Exponentiation is right-associative per mathematical convention (2^3^2 evaluates as 29 = 512, not 82 = 64). Pro tip: if your expression yields an unexpected result, inspect the step table to see exactly where precedence or associativity diverged from your assumption.
Formulas
The Shunting-Yard algorithm processes each token t of the infix expression left-to-right, maintaining an operator stack S and an output queue Q.
Postfix evaluation scans Q left-to-right. For each token: if operand, push to evaluation stack E. If operator op, pop operands b then a, compute a op b, push result. Final value remaining in E is the answer.
Operator precedence function prec(op):
Where Q = output queue, S = operator stack, E = evaluation stack, t = current token, op = operator, a = first operand (left), b = second operand (right).
Reference Data
| Operator | Symbol | Precedence | Associativity | Example Infix | Postfix | Result |
|---|---|---|---|---|---|---|
| Addition | + | 1 | Left | 3+4 | 3 4 + | 7 |
| Subtraction | − | 1 | Left | 9−5 | 9 5 − | 4 |
| Multiplication | × | 2 | Left | 3×4 | 3 4 × | 12 |
| Division | / | 2 | Left | 8/2 | 8 2 / | 4 |
| Exponentiation | ^ | 3 | Right | 2^3 | 2 3 ^ | 8 |
| Parentheses | ( ) | Override | N/A | (3+4)×2 | 3 4 + 2 × | 14 |
| Nested Parens | ( ( ) ) | Override | N/A | (1+(2×3)) | 1 2 3 × + | 7 |
| Right-Assoc Chain | ^ | 3 | Right | 2^3^2 | 2 3 2 ^ ^ | 512 |
| Left-Assoc Chain | − | 1 | Left | 9−3−2 | 9 3 − 2 − | 4 |
| Mixed Precedence | + × ^ | 1/2/3 | Mixed | 2+3×4^2 | 2 3 4 2 ^ × + | 50 |
| All Operators | All | All | Mixed | 1+2×3−4/2 | 1 2 3 × + 4 2 / − | 5 |
| Single Operand | None | N/A | N/A | 7 | 7 | 7 |