Using it
Type an expression with letters or numbers, the operators + − * / % ^ and brackets. The output is the postfix form, and the steps show what happens to each token: straight to the output, or onto the operator stack. If the expression is all numbers, the Value row works it out. Set Convert to to Prefix for Polish notation.
Swap the boxes to go back from postfix (or prefix, with the setting) to infix. Brackets are only added where they're needed.
The shunting-yard algorithm
Edsger Dijkstra published the method in 1961. Read the tokens left to right. A number or variable goes straight to the output. An operator first pops any operators off the stack that bind as tightly or more tightly, then goes on the stack itself. ( goes on the stack; ) pops everything back to the matching (. At the end, pop whatever is left.
The one subtlety is ^, which groups from the right: 2 ^ 3 ^ 2 means 2 ^ (3 ^ 2) = 512, so a ^ doesn't pop another ^. Every other operator here groups from the left.
Why postfix
Postfix needs no brackets and no precedence rules. To evaluate it, push each number onto a stack, and when an operator comes, pop two numbers, apply it, and push the answer. That's how stack-based calculators and many interpreters work, and why HP calculators used reverse Polish notation for years. It's called Polish after the logician Jan Łukasiewicz, who wrote operators before their operands; postfix puts them after.
Questions
What is A + B * C in postfix?
A B C * +. Multiplication binds more tightly, so B * C is done first.
How do I convert infix to prefix?
Reverse the expression, swap ( and ), convert that to postfix, then reverse the result. The steps here show it.
Does it handle negative numbers?
Not as a leading minus sign, such as −3. Write 0 − 3 instead.
Sources
Added . What's new






