Reverse Polish Notation

RPN writes the operator after its operands — 3 4 + instead of 3 + 4. That single change removes brackets and precedence entirely. Here is how it works, and a converter for it.

Try:

What Reverse Polish Notation is

Reverse Polish Notation — RPN, also called postfix notation — writes the operator after its operands. Where ordinary arithmetic says 3 + 4, RPN says 3 4 +.

That one change removes two things from the language: brackets and precedence rules. Neither is needed, because the order in which the symbols appear already determines the order in which the operations happen.

The same expressions in ordinary notation and RPN
Ordinary (infix)Reverse Polish (postfix)Value
3 + 43 4 +7
3 + 4 * 23 4 2 * +11
(3 + 4) * 23 4 + 2 *14
5 - 2 - 15 2 - 1 -2
5 - (2 - 1)5 2 1 - -4

Why it needs no brackets

Look at rows 2 and 3 above. In infix, 3 + 4 * 2 and (3 + 4) * 2 contain the same symbols in the same order — only the brackets tell them apart, and only because we have agreed that * binds tighter than +.

In RPN they are 3 4 2 * + and 3 4 + 2 *. The symbols are in a different order, and that order alone says which operation happens first. There is nothing left for a bracket to disambiguate.

The same is true of precedence

An RPN expression has exactly one reading. You never have to know that ^ outranks *, or that - groups left to right, because the writer already committed to a grouping when they chose the symbol order.

Converting infix to RPN

Use Dijkstra's shunting-yard algorithm. Scan left to right; send operands straight to the output; hold operators on a stack until an operator of lower or equal precedence arrives, then release the waiting ones. Brackets are pushed as markers and discarded when their partner shows up.

The converter at the top of this page runs exactly that and shows the stack after every token. The full rule set, pseudocode and complexity analysis are on the conversion algorithm page, and there is an animated walkthrough if you would rather watch it happen.

Reading an RPN expression

Go left to right. Say "push" for every number and "apply" for every operator, taking the two most recent values. If you can get to the end without backtracking, the expression is valid.

15 7 1 1 + - / 3 * 2 1 1 + + -

  15 7 1 1        push four numbers          15 7 1 1
  +               1 + 1 = 2                  15 7 2
  -               7 - 2 = 5                  15 5
  /               15 / 5 = 3                 3
  3               push 3                     3 3
  *               3 * 3 = 9                  9
  2 1 1           push three numbers         9 2 1 1
  +               1 + 1 = 2                  9 2 2
  +               2 + 2 = 4                  9 4
  -               9 - 4 = 5                  5

  result 5        ordinary notation: ((15 / (7 - (1 + 1))) * 3) - (2 + (1 + 1))

That expression is the standard worked example for RPN, and you can paste it into the calculator to watch the stack move.

Where the name comes from

In the 1920s the Polish logician Jan Łukasiewicz showed that writing the operator before its operands removes the need for brackets. That became known as Polish notation — what we now call prefix.

Reversing it — operator after the operands — gives the same bracket-free property but suits a stack far better, because operands arrive before the operation that consumes them. Hence Reverse Polish. The form was developed for computing in the late 1950s and popularised by Hewlett-Packard, whose HP-35 in 1972 made RPN familiar to a generation of engineers.

Where RPN is used

Real systems built on postfix evaluation
SystemHow RPN appears
HP calculatorsKeyed directly — enter operands, then the operation. No brackets, no equals key.
ForthThe whole language is postfix: 3 4 + is valid Forth.
PostScriptPage descriptions are postfix programs executed on a stack.
Java Virtual MachineBytecode is stack-based — iload_1 iload_2 iadd is a b +.
CPythonCompiles to stack bytecode: LOAD_FAST, LOAD_FAST, BINARY_ADD.
Compilers generallyExpressions are converted once, then evaluated or emitted from the postfix form.

Infix to Reverse Polish Notation examples

Infix to RPN conversion examples with answers
InfixRPNWhy
A + BA B +The base case
A + B * CA B C * +* binds tighter, so it is applied first
(A + B) * CA B + C *The bracket forces the addition first
A - B - CA B - C -Left associative
A - (B - C)A B C - -The bracket overrides the grouping
a ^ b ^ ca b c ^ ^Right associative, so the rightmost power resolves first
(a + b) * (c - d)a b + c d - *Each group flushes independently
3 + 4 * 2 / (1 - 5) ^ 23 4 2 * 1 5 - 2 ^ / +Everything at once

Frequently asked questions

What is Reverse Polish Notation?

Reverse Polish Notation, or RPN, is a way of writing expressions where the operator comes after its operands: 3 4 + instead of 3 + 4. Because the position of each symbol fixes the order of evaluation, RPN needs no brackets and no precedence rules at all.

Is RPN the same as postfix notation?

Yes, they are two names for the same thing. Postfix is the descriptive name; Reverse Polish Notation is the historical one, coined because it reverses the Polish notation of Jan Lukasiewicz, who put the operator first.

How do you convert infix to Reverse Polish Notation?

With Dijkstra's shunting-yard algorithm. Scan the infix expression left to right, send operands straight to the output, and hold operators on a stack until an operator of lower or equal precedence arrives, at which point the waiting ones are released. Brackets are pushed as markers and discarded when their partner arrives.

Why does RPN not need brackets?

Because the grouping is already carried by the order of the symbols. In infix, A + B * C and (A + B) * C are made of the same symbols in the same order and only brackets distinguish them. In RPN they are A B C * + and A B + C *, which differ in symbol order, so nothing extra is needed.

Where is RPN actually used?

HP calculators from the HP-35 onwards, the Forth and PostScript languages, and the bytecode of stack machines such as the Java Virtual Machine and CPython. In all of them, execution is push operands, apply operator, push result.

How do you read an RPN expression aloud?

Work left to right and say 'push' for each operand and 'apply' for each operator. For 3 4 + 2 *: push 3, push 4, add them to get 7, push 2, multiply to get 14. If you can do that without backtracking, the expression is valid.

What is the advantage of RPN on a calculator?

No brackets to key in and no equals key. Each intermediate result stays on the stack ready for the next operation, so long calculations need fewer keystrokes and there is never an ambiguity about what the machine will do next.