Skip to content
Math

Polish Notation Converter - Infix, Prefix, and Postfix

Convert a mathematical expression between infix (the familiar A + B form), prefix (Polish notation, where the operator comes first: + A B), and postfix (Reverse Polish Notation, where the operator comes last: A B +). Numeric expressions are also evaluated. Enter your expression, choose the input notation, and all four outputs update instantly.

Your details

Choose the notation your expression is already written in.
Use + - * / ^ and parentheses. Operands can be numbers or single letters (A-Z).
Infix
( A + B ) * C

Familiar left-to-right form with parentheses where needed

Prefix (Polish)* + A B C
Postfix (RPN)A B + C *
Numeric resultVariables present - enter numbers to evaluate

Converted "(A + B) * C" to all three notations.

  • Prefix form: "* + A B C" - operator first, no parentheses needed because arity is always two.
  • Postfix / RPN form: "A B + C *" - operator last, evaluated left-to-right with a simple stack.
  • Prefix and postfix forms are unambiguous without parentheses because operator precedence is encoded in position.

Next stepPostfix (RPN) is used by most scientific calculators and compilers. Prefix (Polish) is common in Lisp-family languages and expression trees.

Conversion examples reference table

Infix expressionPrefix (Polish)Postfix (RPN)
A + B+ A BA B +
A + B * C+ A * B CA B C * +
(A + B) * C* + A B CA B + C *
A * B + C * D+ * A B * C DA B * C D * +
(A + B) * (C - D)* + A B - C DA B + C D - *
A ^ B ^ C^ A ^ B CA B C ^ ^
3 + 4 * 2+ 3 * 4 23 4 2 * +
(3 + 4) * 2* + 3 4 23 4 + 2 *

All examples above use standard operator precedence: ^ before * and /, then + and -.

What is Polish notation?

Polish notation (also called prefix notation) was invented by the Polish logician Jan Lukasiewicz in 1924 as a way to write logical and mathematical expressions without parentheses. Instead of placing the operator between its two operands as in standard infix notation (A + B), the operator comes first (+ A B). Because the number of operands for every operator is fixed (always two for binary operators), the expression is completely unambiguous without any parentheses or rules about precedence. Postfix notation, also called Reverse Polish Notation (RPN), follows the same idea but puts the operator after its operands (A B +). Both forms are widely used in computer science: compilers convert infix code into abstract syntax trees that resemble prefix notation, while stack-based virtual machines and RPN calculators evaluate postfix expressions directly.

How the Shunting-yard algorithm works

Infix-to-postfix conversion uses Edsger Dijkstra's Shunting-yard algorithm (1961). It scans the expression left to right, maintaining an operator stack and an output queue. Operands go straight to the output. When an operator is encountered, operators already on the stack that have higher or equal precedence (for left-associative operators) are popped to the output first, then the new operator is pushed. Parentheses control grouping: a left parenthesis is pushed, and a right parenthesis causes operators to be popped until the matching left parenthesis is found. After all tokens are scanned, remaining operators are popped to the output. The result is a postfix expression that evaluates correctly using a simple stack. Prefix conversion uses the same algorithm on a reversed token list, then reverses the output.

Operator precedence and associativity

Precedence determines which operator binds tighter when two operators compete for the same operand. This calculator uses the standard hierarchy: exponentiation (^) has the highest precedence, then multiplication and division (* and /), then addition and subtraction (+ and -). Associativity matters when two operators have equal precedence: addition, subtraction, multiplication, and division are left-associative (A - B - C means (A - B) - C), while exponentiation is right-associative (A ^ B ^ C means A ^ (B ^ C)). The Shunting-yard algorithm handles both correctly, and the reference table below shows how the same expression changes form across all three notations.

Evaluating prefix and postfix expressions

Postfix evaluation is straightforward: scan the tokens left to right. When you see an operand, push it onto a stack. When you see an operator, pop two operands, apply the operator, and push the result. After scanning all tokens, the stack holds the final answer. Prefix evaluation is similar but scans right to left (or uses recursive descent scanning left to right). For example, evaluating the postfix expression "3 4 2 * +" proceeds: push 3, push 4, push 2, see *, pop 2 and 4 to get 4*2=8 and push 8, see +, pop 8 and 3 to get 3+8=11, done. This calculator performs this evaluation automatically when all operands are numbers.

Notation quick-reference

NotationExampleOperator positionCommon use
InfixA + B * CBetween operandsHuman math, most programming languages
Prefix+ A * B CBefore operandsLisp / Scheme, expression trees, compilers
PostfixA B C * +After operandsRPN calculators, stack machines, PostScript

How the same expression A + B * C is written in each notation, along with where each is used.

Frequently asked questions

What is the difference between Polish notation and Reverse Polish Notation?

Polish notation (prefix) places the operator before its operands: + A B means A + B. Reverse Polish Notation (postfix or RPN) places the operator after its operands: A B + also means A + B. Both eliminate the need for parentheses because the position of the operator uniquely determines which operands it applies to. Polish notation was invented by Jan Lukasiewicz; RPN is the form used by HP calculators, the PostScript page-description language, and most stack-based virtual machines.

Why do prefix and postfix expressions not need parentheses?

In infix notation, parentheses and precedence rules are needed to tell you which operator applies to which operands when operators are adjacent. In prefix and postfix notation, the operator always applies to the next fixed number of operands (two for binary operators), so the grouping is implicit in the order of tokens. The expression * + A B C in prefix unambiguously means multiply the result of (A + B) by C; there is no other reading.

How do I convert an infix expression to postfix by hand?

Use the Shunting-yard algorithm: (1) Scan tokens left to right. (2) If the token is an operand, write it to the output. (3) If the token is an operator, pop operators from a stack to the output while they have greater or equal precedence (for left-associative operators), then push the new operator. (4) If the token is a left parenthesis, push it. (5) If it is a right parenthesis, pop to the output until you reach the matching left parenthesis. (6) After all tokens, pop the remaining stack to the output. The result is the postfix expression.

Can this converter handle expressions with variables like A and B?

Yes. You can use any single capital letter (A-Z) as a variable. Conversion between notations works identically for variables and numbers. Numeric evaluation is only performed when every operand in the expression is a number; if any variable is present, the evaluated field will say so.

What operators does this converter support?

The converter supports addition (+), subtraction (-), multiplication (*), division (/), and exponentiation (^), along with parentheses for grouping in infix expressions. Exponentiation is right-associative (A ^ B ^ C is treated as A ^ (B ^ C)). All other operators follow left-to-right associativity with the standard precedence order: ^ first, then * and /, then + and -.

Where is Polish notation used in real life?

Prefix notation is used in Lisp and Scheme programming languages, where all expressions are written as (operator arg1 arg2). Compilers internally convert infix source code into abstract syntax trees, which are a tree form of prefix notation. Postfix (RPN) is used by older HP scientific calculators, the PostScript and PDF page description languages, the Java bytecode format, Forth programming, and the CPython interpreter, all of which use a stack to evaluate expressions without any operator precedence parsing at runtime.

Sources

Written by Dr. Rajiv Menon, PhD Applied Mathematician · Bengaluru, India

Applied mathematician bridging algebraic theory and computational tools for students, engineers, and everyday problem-solvers.

Search 3,500+ calculators

Loading search…