Free Polish Notation Converter

a + b+ a b

Enter an expression to convert or calculate

Supported: +, -, *, /, ^, ( )

Mathematical expressions can be structured in several ways. The most familiar form, infix notation, positions every operator between its two operands (e.g., 3+43 + 4). This system relies on explicit rules—operator precedence, associativity, and parentheses—to specify the evaluation order. In contrast, Polish notation (prefix) and Reverse Polish Notation (postfix) remove those dependencies by placing the operator before or after its operands, respectively. A dedicated Polish Notation Converter acts as both an Infix to Postfix Converter, Infix to Prefix Converter, Prefix to Infix Converter, and Postfix to Infix Converter, while also functioning as a Reverse Polish Notation Calculator that can evaluate expressions in either notation. Powered by the Shunting Yard Algorithm Calculator, it offers a complete online solution for exploring these three notation systems.

Building Blocks of Mathematical Expressions

Every arithmetic expression consists of operands (numeric values or literal symbols such as xx or nn) and operators (+,−,×,÷,∧+, -, \times, \div, \wedge). In infix notation, the order in which operators are applied is determined by:

  • Precedence: A ranking among operators. Exponentiation (∧\wedge) is evaluated first, followed by multiplication and division, and finally addition and subtraction. For instance, in 3+8×23 + 8 \times 2, the multiplication is performed before the addition, yielding 3+16=193 + 16 = 19.
  • Associativity: When two operators share the same precedence, associativity decides the grouping. Almost all common binary operators are left-associative, meaning they group from left to right. For example, 7−4+27 - 4 + 2 is evaluated as (7−4)+2=5(7 - 4) + 2 = 5, not 7−(4+2)=17 - (4 + 2) = 1. Exponentiation is right-associative, so 2^3^4 is evaluated as 2^(3^4).
  • Parentheses (grouping): Parentheses override the natural precedence and associativity, forcing a particular sub‑expression to be evaluated first. Adding parentheses to the earlier example, (3+8)×2(3 + 8) \times 2 gives 11×2=2211 \times 2 = 22.

Without these rules, an infix expression would be ambiguous. Polish notations were developed precisely to eliminate this ambiguity.

Polish Notation (Prefix) and Reverse Polish Notation (Postfix)

Introduced by the Polish logician Jan Łukasiewicz in the 1920s, prefix notation writes the operator before its operands. The infix expression 4+54 + 5 becomes +  4  5+ \;4\;5. A more complex expression such as 3+4×5−63 + 4 \times 5 - 6 translates to −  +  3  ×  4  5  6- \;+\;3\;\times\;4\;5\;6. Every operator appears immediately before the two operands it acts upon, so parentheses are unnecessary.

Reverse Polish Notation (postfix), which gained popularity with early computers, places the operator after its operands. The same infix expression becomes 3  4  5  ×  +  6  −3\;4\;5\;\times\;+\;6\;-. Both prefix and postfix can be evaluated using a stack: operands are pushed, and when an operator is encountered, it pops the required number of operands, applies the operation, and pushes the result.

The table below compares the three notations using the expression A+B×CA + B \times C (where ×\times has higher precedence than ++):

NotationRepresentationRequires Precedence Rules?Requires Parentheses?
InfixA+B×CA + B \times CYesSometimes
Prefix+  A  ×  B  C+ \;A\;\times\;B\;CNoNo
PostfixA  B  C  ×  +A\;B\;C\;\times\;+NoNo

Operator Precedence and Associativity Details

The following table summarizes the precedence levels and associativity for the supported operators:

OperatorPrecedence LevelAssociativity
∧\wedge (exponentiation)3 (highest)Right-to-left
×,÷\times, \div2Left-to-right
+,−+, -1 (lowest)Left-to-right

These rules play a crucial role when converting expressions, especially in the shunting‑yard algorithm.

Conversion Algorithms

Shunting‑Yard Algorithm: Infix to Postfix

The shunting‑yard algorithm, invented by Edsger Dijkstra, is the standard procedure for converting infix to postfix. It processes tokens from left to right using an output queue and an operator stack.

  • Operand: Append to output.
  • Operator (O1): While there is an operator O2 on top of the stack whose precedence is greater than that of O1, or equal to O1 when O1 is left‑associative (the usual case), pop O2 to output. For right‑associative operators (e.g., exponentiation), pop only when precedence is strictly greater, not equal. Then push O1 onto the stack.
  • Left parenthesis: Push onto the stack.
  • Right parenthesis: Pop operators from the stack to output until a left parenthesis is encountered. Discard both parentheses.

After the end of the input, pop any remaining operators to output.

Example: Convert (6+2)×(5−3)(6 + 2) \times (5 - 3) to postfix.

TokenActionOutputStack
((push onto stack(
66output6(
++push onto stack6( +
22output6 2( +
))pop until (6 2 +
×\timespush onto stack6 2 +×
((push6 2 +× (
55output6 2 + 5× (
−-push6 2 + 5× ( -
33output6 2 + 5 3× ( -
))pop until (6 2 + 5 3 −×
Endpop ×6 2 + 5 3 − ×

The result is 6  2  +  5  3  −  ×6\;2\;+\;5\;3\;-\;\times.

Infix to Prefix

Converting infix to prefix is less direct but follows a similar pattern:

  1. Reverse the infix expression, swapping (( with )) and vice versa.
  2. Apply the shunting‑yard algorithm with a modified condition: pop operators only when they have strictly greater precedence than the incoming operator (do not pop for equal precedence). This modification mirrors the right‑associative pop rule applied to all operators.
  3. Reverse the resulting sequence to obtain the prefix notation.

Using the same expression, the reversed infix is )3−5(×)2+6()3-5(\times)2+6(; after applying the modified algorithm and reversing, the prefix expression becomes ×  +  6  2  −  5  3\times\;+\;6\;2\;-\;5\;3.

Converting Back to Infix

Restoring infix from a prefix or postfix expression involves reassembling the operator between its operands and adding parentheses where needed to preserve the original order.

For postfix to infix: scan from left to right. When you encounter an operator, combine the two preceding operands into an infix string with the operator between them, and wrap the result in parentheses if the operator being placed has lower precedence than any operator already inside the left or right operand. If an operand is a single number, it is considered to have infinite precedence and never requires parentheses.

Example: Convert 3  2  +  7  ×  6  4  +  /3\;2\;+\;7\;\times\;6\;4\;+\; / (postfix) to infix.

  • Scan 33 → operand.
  • Scan 22 → operand.
  • Scan ++ → combine 33 and 22 to (3+2)(3+2). Precedence of ++ is lower than the next operator, so parentheses are kept.
  • Scan 77 → operand.
  • Scan ×\times → combine (3+2)(3+2) and 77 to ((3+2)×7)((3+2)\times7). The operands are (3+2)(3+2) and 77. Since ++ in the left operand has lower precedence than ×\times, parentheses remain around the sum.
  • Scan 66 → operand.
  • Scan 44 → operand.
  • Scan ++ → combine 66 and 44 to (6+4)(6+4).
  • Scan // → combine ((3+2)×7)((3+2)\times7) and (6+4)(6+4) to (((3+2)×7)/(6+4))(((3+2)\times7)/(6+4)). The precedence of ×\times and // are equal; no extra parentheses are needed inside. The final result is (3+2)×7/(6+4)(3+2)\times7/(6+4).

This procedure works similarly for prefix (scan from right to left and combine operands on the right side).

Practical Usage of the Polish Notation Converter

The online Polish Notation Converter offers two primary modes:

  1. Convert Mode – Choose one of the four conversions (infix ↔ prefix/postfix). Enter your expression according to the following guidelines:
    • For infix expressions, use standard notation with parentheses if needed (e.g., (3+4)∗2(3+4)*2).
    • For prefix or postfix expressions, separate every token by a single space. Example prefix: +34+ 3 4; example postfix: 34+3 4 +.
  2. Calculate Mode – Input any Polish notation expression (prefix or postfix) without specifying its type. The tool automatically detects the notation and returns the evaluated result.

The calculator supports the operators +,−,×,÷+, -, \times, \div, and exponentiation ∧\wedge. It can handle both integer and decimal operands.

Why Explore Different Notations?

Although infix notation is deeply ingrained in everyday mathematics, Polish and Reverse Polish notations offer a refreshing perspective on expression evaluation. Historically, some handheld calculators used postfix to simplify internal computation, and even today, stack‑based virtual machines interpret postfix instructions. Understanding how to convert between these notations enhances your grasp of parsing techniques and algorithm design—skills that are valuable in programming and computer science.

Whether you are a student learning about expression evaluation, a developer implementing a calculator, or a curious mind exploring alternative mathematical conventions, this Polish Notation Calculator Online provides a practical and interactive way to experiment with infix, prefix, and postfix conversions.

FAQ

1. What input format does the converter require for prefix or postfix expressions?

Tokens (numbers and operators) must be separated by a single space. For example, enter "+ 3 4" for prefix or "3 4 +" for postfix. Infix expressions can be entered in standard mathematical notation with parentheses as needed.

2. How does associativity affect the shunting-yard algorithm?

For left-associative operators, when the current operator has equal precedence to the operator on top of the stack, the stack operator is popped. For right-associative operators (like exponentiation), the stack operator is popped only when its precedence is strictly greater—equal precedence does not trigger a pop.

3. Can the converter handle exponentiation (power) operations?

Yes, the calculator supports the exponentiation operator (^). In the conversion and calculation modes, exponentiation is treated with the highest precedence and right‑associativity.

4. How are parentheses treated when converting infix to postfix?

Left parentheses are pushed onto the operator stack. When a right parenthesis is encountered, all operators are popped to the output until a matching left parenthesis is found; both parentheses are then discarded.

5. What is the difference between converting infix to postfix and infix to prefix?

Infix to postfix uses the standard shunting-yard algorithm where operators with equal or greater precedence (for left-associative) are popped. Infix to prefix requires reversing the input, applying the algorithm with a stricter pop condition (only strictly greater precedence), and then reversing the output.

How to Use

  1. Select 'Convert' or 'Calculate' mode using the toggle.
  2. Choose the conversion direction or enter a Polish notation expression with spaces between tokens.
  3. View the converted expression or calculated result instantly as you type.