How to use the Postfix Expression Evaluator
- 1. Pick the notationPostfix (RPN) or prefix (Polish).
- 2. Type the expressionNumbers and operators separated by spaces.
- 3. EvaluateSee every token, the action and the stack.
How stack evaluation works
In postfix notation the operator comes after its operands, so 3 4 + means 3 + 4. There are no brackets and no precedence rules to worry about, which is why calculators and compilers like it.
- Read the tokens left to right.
- A number is pushed onto the stack.
- An operator pops the top two values, applies itself (the lower value is the left operand) and pushes the result.
- At the end, exactly one value should be left: the answer.
Prefix notation puts the operator first: + 3 4. It's evaluated the same way but reading right to left, with the top of the stack as the left operand. Supported operators are + − * / % and ^ (power). If the stack runs out early or has values left over, you'll get a clear message saying what's wrong.
Worked example
Evaluate 5 1 2 + 4 * + 3 −:
- Push 5, push 1, push 2: [5, 1, 2].
- + pops 1 and 2 and pushes 3: [5, 3].
- Push 4: [5, 3, 4]. * pops 3 and 4 and pushes 12: [5, 12].
- + pops 5 and 12 and pushes 17: [17].
- Push 3, then − pops 17 and 3 and pushes 14: [14].
The answer is 14, the same as the infix expression 5 + (1 + 2) × 4 − 3. The equivalent prefix expression − + 5 * + 1 2 4 3 also gives 14; switch the notation to check. To turn an infix expression into postfix in the first place, use the Infix to Postfix converter.
Frequently asked questions
Do I need spaces between tokens?
Yes, so multi-digit numbers like 12 are read correctly. A single string of one-digit numbers and operators, like 23+4*, also works.
Which operand comes first for subtraction?
In postfix, the value pushed earlier is on the left: 10 3 − is 10 − 3 = 7.
Are decimals and negative numbers allowed?
Yes, like 2.5 or -4, written as separate tokens.
Is the postfix expression evaluator free to use?
Yes, the postfix expression evaluator is free and costs nothing. Your first evaluation shows a short form for your name and WhatsApp number one time; once that's done, this tool and every other Uncodemy tool open with no form.