Advertisement
Advertisement
DEVELOPER

Polish Notation Converter — infix, prefix and postfix

Convert an arithmetic expression between ordinary infix, prefix Polish notation and postfix reverse Polish notation, and evaluate it at the same time.

Prefix and postfix need spaces between tokens, because 42 and 4 2 mean different things and nothing else can separate them.
Numbers, the operators + - * / ^ and %, and round brackets in infix. A minus directly before a number or bracket is treated as negation.
The other two forms and the evaluated value appear underneath whichever you choose here.
Converted expression
 
 
0
Evaluated value
0
Operands
0
Operators
0
Maximum stack depth
Tip: prefix and postfix need no brackets and no precedence rules at all. The order of the tokens carries the structure, which is exactly why a machine finds them easier to work with than the notation people write by hand.
Advertisement

The Polish notation converter above takes an arithmetic expression in any of the three notations and gives you the other two, together with its value. It parses the expression into a tree first and then prints that tree three ways, which is why the conversions always agree with each other and with the arithmetic.

Arb Digital built it around a real parser rather than a token shuffle because the shortcuts break in exactly the places that matter. Precedence, associativity, unary minus and nested brackets are where naive converters go wrong, and they are also the cases people come to a converter to check. Getting them right is the whole job.

What This Polish Notation Converter Does

Prefix notation, where the operator comes before its operands, was introduced by the Polish logician Jan Łukasiewicz. The Stanford Encyclopedia of Philosophy's article on Jan Łukasiewicz describes his parenthesis-free notation, which used letters such as C for implication and N for negation and allowed an extremely compressed linear notation for proofs. The arithmetic version is the same idea with familiar operators, and reversing it, so the operator follows its operands, gives postfix or reverse Polish notation.

The tool supports addition, subtraction, multiplication, division, exponentiation and the remainder operator, along with round brackets and negative numbers in infix. It reports the value, the number of operands and operators, and the maximum stack depth needed to evaluate the postfix form, which is a genuinely useful number if you are sizing a stack machine or reasoning about a compiler's register pressure.

Both alternative notations are unambiguous without brackets. That is their defining property: the position of each token fully determines the structure, so precedence rules and parentheses become unnecessary rather than merely optional. Infix has neither property, which is why it needs both.

How to Use It

  1. Tell the tool which notation you are typing. The same characters mean different things in each, and "3 4 +" is meaningless as infix while "3 + 4" is meaningless as postfix.
  2. Separate tokens with spaces in prefix and postfix. There are no brackets to mark boundaries, so without spaces there is no way to tell 42 from 4 followed by 2.
  3. Use brackets freely in infix. They are parsed properly and disappear from the prefix and postfix output, which is the clearest demonstration of what those notations buy.
  4. Check the evaluated value. If it is not what you expected, the expression is not saying what you thought, and the fully bracketed infix output shows exactly how it was read.
  5. Watch the stack depth when comparing two ways of writing the same calculation. Reordering operands can reduce it without changing the answer.

The Formula: How the Conversion Works

Infix is parsed with the shunting-yard method: operands go straight to the output, operators go onto a stack and are popped to the output whenever an operator of higher or equal precedence is already sitting there, and brackets control when the stack is flushed. The output of that process is postfix, and building a tree from postfix is a single left-to-right pass with a stack of subtrees.

Once a tree exists, all three notations are just three traversals of it. Prefix is a preorder walk, visiting the operator then its children. Postfix is a postorder walk, visiting the children then the operator. The NIST Dictionary of Algorithms and Data Structures lists postfix traversal as a synonym for postorder traversal, which is exactly the relationship: reverse Polish notation is the postorder listing of an expression tree. Infix is an inorder walk with brackets inserted wherever a child binds more loosely than its parent.

Work the default. The expression is 3 + 4 * 2 / (1 - 5). Multiplication and division bind more tightly than addition, and they associate left to right, so this reads as 3 + ((4 * 2) / (1 - 5)). Four times two is eight, one minus five is negative four, eight divided by negative four is negative two, and three plus negative two is one. In postfix that structure is 3 4 2 * 1 5 - / +, and in prefix it is + 3 / * 4 2 - 1 5. Neither needs a single bracket, and both evaluate to 1.

Advertisement

Associativity Is Where Converters Go Wrong

Most operators associate to the left, so 8 / 4 / 2 means (8 / 4) / 2, which is 1, rather than 8 / (4 / 2), which is 4. Exponentiation is the exception: it associates to the right, so 2 ^ 3 ^ 2 means 2 ^ (3 ^ 2), which is 2 to the ninth, or 512, and not (2 ^ 3) ^ 2, which is 64.

A converter that ignores that difference produces a postfix string which is internally consistent and silently computes the wrong number. The second preset above sets up exactly this case so you can see the tree the tool builds and check it against your own expectation. It is worth doing once, because the discrepancy only shows up in the value and never in the shape of the output.

Unary minus is the other trap. In 3 - -4 the second minus is negation rather than subtraction, and they have different precedence and a different number of operands. This tool treats a minus as negation when it appears at the start of an expression, immediately after another operator, or immediately after an opening bracket, which is the standard rule, and prints it as a separate negation node rather than folding it into the number.

Why Machines Prefer Postfix

Evaluating postfix needs one stack and no lookahead. Read tokens left to right; push operands, and when you meet an operator pop the operands it needs, apply it and push the result. When the tokens run out the answer is the only thing left on the stack. There is no precedence table, no bracket matching and no backtracking.

That is why stack machines and many virtual machines use it as their instruction order, why some calculators adopted reverse Polish entry, and why compilers convert infix to a tree and then emit code in postorder. It is also why the maximum stack depth is a meaningful figure: it is the number of intermediate values that have to be held at once, which maps directly onto register pressure in generated code.

Prefix has the same bracket-free property and is read most naturally from the right, or with a recursive descent from the left. It is the form Lisp made famous, where an operator followed by its arguments inside brackets is prefix notation with delimiters added so that operators can take a variable number of arguments. The brackets in Lisp are not for precedence, which prefix does not need, but for arity, which it cannot otherwise express.

What This Page Does Not Do

The boundary matters, so here it is plainly. This tool converts and evaluates a numeric arithmetic expression. It does not do algebra: there are no variables, nothing is simplified or rearranged, and no equation is solved. If you need a value from a formula with functions and constants, the scientific calculator is the right page, and for roots of a polynomial the quadratic equation solver and the system of equations calculator handle those specific forms.

It also does not check whether an expression is well-formed in any deeper sense than parsing it. A prefix string with too few operands, a postfix string with too many, or an unmatched bracket all produce a written message naming the problem rather than a number, which is the honest behaviour, but a syntactically valid expression that does not compute what you intended will convert perfectly and give you the wrong answer confidently.

Division by zero returns infinity rather than an error, following the floating-point behaviour the arithmetic actually has, and the tool says so in the result line rather than hiding it. A remainder with a negative left operand follows the same convention as most programming languages, taking the sign of the dividend, which differs from the mathematical modulo you may be expecting; the modulo calculator covers that difference in detail.

Need a website that loads fast and actually works?

Arb Digital builds free tools like this one because useful pages earn attention. If you want tools, calculators or content built for your own audience, we can help.

Browse All Free Tools Talk to Arb Digital

Common Mistakes to Avoid

  • Omitting spaces in prefix or postfix — without brackets, spaces are the only thing separating one number from the next.
  • Assuming exponentiation associates to the left — 2 ^ 3 ^ 2 is 512, not 64, and a converter that gets this wrong looks perfectly plausible.
  • Confusing negation with subtraction — a minus after an operator or an opening bracket takes one operand, not two.
  • Reading prefix left to right and evaluating as you go — the operands come after the operator, so evaluation has to recurse or read from the right.
  • Expecting brackets in the output — prefix and postfix never need them, and a converter that emits them has not really converted anything.

Related Free Tools From Arb Digital

For evaluating formulas with functions and constants, use the scientific calculator, and for specific algebraic forms the quadratic equation solver or the system of equations calculator. The modulo calculator covers remainder conventions, the number base converter rewrites the operands in another radix, and the bitwise calculator handles the operators this page deliberately leaves out. For code structure rather than expression structure, see the cyclomatic complexity calculator, and browse the rest in the free online tools hub.

Frequently Asked Questions

What is the difference between Polish and reverse Polish notation?

In Polish, or prefix, notation the operator comes before its operands, so addition of three and four is written + 3 4. In reverse Polish, or postfix, the operator follows them, giving 3 4 +. Both are unambiguous without brackets; they are mirror images of the same idea.

Why do prefix and postfix need no brackets?

Because the position of each token fully determines which operands belong to which operator. Infix needs brackets and precedence rules precisely because writing the operator between its operands leaves the grouping undetermined, and something has to resolve it.

Why must I put spaces between tokens?

Because there are no brackets to mark where one token ends and the next begins. Without a space, 42 could be the number forty-two or the numbers four and two, and nothing in the notation distinguishes them.

How is exponentiation handled?

It associates to the right, so 2 ^ 3 ^ 2 is read as 2 raised to the power of 3 squared, giving 512. Every other operator here associates to the left. Converters that apply left associativity everywhere produce output that looks correct and computes the wrong value.

What does the maximum stack depth tell me?

It is the largest number of intermediate values that must be held at once while evaluating the postfix form. It maps directly onto register pressure in generated code, and rewriting an expression can sometimes reduce it without changing the answer.

Can I use variables or functions?

No. This page converts and evaluates numeric arithmetic expressions only. There is no algebra, nothing is simplified, and no equation is solved. A calculator page is the right tool for formulas with functions and constants.

How is a minus sign interpreted?

As negation when it appears at the start of the expression, straight after another operator, or straight after an opening bracket. Everywhere else it is subtraction. Negation takes one operand and appears as its own node in the converted output.

Is reverse Polish notation the same as a postorder traversal?

Yes. Build the expression tree and list it in postorder, visiting each operator's children before the operator, and the result is exactly the postfix string. Prefix is the preorder listing of the same tree, and infix is the inorder listing with brackets added where needed.

Advertisement
Advertisement

Take it further