# Polish Notation Converter

Created by Davide Borchia
Reviewed by Anna Szczepanek, PhD and Rijk de Wet
Last updated: Apr 06, 2022

There are other ways to write operations than the standard "symbols-between-numbers" fashion: with our Polish notation converter, you'll learn all about them!

is a matter of notations and conventions, symbols and representations of abstract concepts on a blackboard. From a very young age, we learn the basics of this — from the idea of a number (without breaking your brain in the process, can you explain why 7 means... 7?), doing long division in primary school, up to university, where a curvy line starts to represent integration. Where is the connection?

The order in which we write every operation is one of these conventions: the one everyone knows is called infix notation. But are you sure that's the best way to write equations?

Let's find out! Keep on reading to learn more about Polish notation and reverse Polish notation. Here we will teach you:

• What the infix notation is;
• The elements of an arithmetic notation;
• What the Polish notation is;
• The two types of Polish notation;
• How to convert between notations; and
• How to use our Polish notation converter.

## Arithmetical and logical notations

To write, compute, or communicate mathematical operations or statements, we need to introduce their building blocks. We are specifically talking about:

• Operands: numbers (e.g., $1$, $137$, $e$, $\pi$) or letters representing unknowns (e.g., $x$, $a$, $n$);
• Operators: everything that acts on operands (e.g., $+$, $-$, $\times$, $\div$).

Operators follow specific rules and properties that let us use them in the correct order.

• Precedence indicates which operator has to be applied first.
• Grouping uses brackets to indicate that certain operations must be performed directly without following precedence.
• Associativity determines the direction in which operators act in case of equal precedence and absence of brackets.

Let's see them with some examples. Multiplication has higher precedence than addition. It means that in the operation $3+8\times2$, we must compute the multiplication first:

$3+\textcolor{red}{8\times2}=3+\textcolor{red}{16}=19$

The precedence of the basic arithmetic operators is, from the highest to the lowest:

Now it's clear that in the case of operations with equal precedence (or when the order has to be changed), additional rules are required. Brackets prioritize certain parts of the operation, insulating them from the standard precedence order.

For example, consider $8\times4+2$. Following the standard precedence rules, we would compute it as $\textcolor{red}{8\times4}+2 = \textcolor{red}{32}+2+34$. If we need to compute the addition first, we need to add brackets around the interested operands: $8\times\textcolor{blue}{(4+2)}=8\times\textcolor{blue}{6}=48$.

Notice how we can also write the first, precedence-ruled expression: $8\times4+2 = (8\times4)+2$. In this case, brackets are not necessary, but are implied to be surrounding the operands in order of precedence.

The last rule is used when two or more operators with the same precedence appear in the same expression. In which order should they be evaluated? It is necessary to introduce the concept of associativity — note that this is not the same thing as the property of operations like the distributive property!

Take the expression $7-4+2$. Addition and subtraction have the same precedence, and according to how we place brackets, we can get two different results: $(7-4)+2 = 5$ or $7-(4+2)=1$.

Which one is correct? The answer is "it depends". Traditionally, all of the binary operators (acting on two operands, like multiplication, addition, and so on) are left-associative, which means that the operators are grouped from the left. The other situation is right-associative, and the operators are enclosed in brackets from the right.

## What is the infix notation?

The traditional way to write operations is called infix notation. The operators lie between the operands, like in $4+1$ where the $+$ lies between the $4$ and the $1$. To understand which operand belongs to which operator, it is necessary to use associativity and precedence rules. Brackets are often necessary to ensure the correct evaluation of the operation.

## What is the Polish notation?

In 1924, a Polish logician Jan Łukasiewicz invented a different way to write operations, making brackets and precedence rules unnecessary. This notation, called Polish notation, sees the operators and the operands grouped in a way that avoids interposing the former with the latter if they are parts of the same operation.

For now, it may sound unclear, but stay here because it is easier than you expect!

Remember that operators are not necessarily all together, and they can still appear between operands; however, as the calculation proceeds, they eventually disappear.

🔎 The Polish notation is thought to be easier and faster to use, but this is not yet scientifically proved: it may just be that it requires a more shortened version of the expression rather than an actual reduction of the difficulty.

## Types of Polish notation

The Polish notation, also known as prefix notation, proved its value in logic as an alternative to the infix notation, as the operators now appear in front of the relative operands. In the '50s, with the diffusion of informatics and computers, another kind of notation arose.

The postfix, or reverse Polish notation (so-called because the operators follow their operands) is in almost every aspect similar to the Polish notation. This way of writing an arithmetic expression moves the operators toward the end of the expression, grouping the operands on the left side of their operators. Such an implementation is faster on a computer since it requires less memory, and efficiently uses the stack: we will see later how popping and pushing on it is fundamental in the conversion and calculation of Polish notation expressions.

💡 We will use the pairs of terms "prefix" – "Polish notation" and "postfix" – "reverse Polish notation" interchangeably across the text.
Just remember:

• Polish notation = Prefix notation; and
• Reverse Polish notation = Postfix notation.

## Some examples of Polish notation expressions

Are you ready for an expression written in Polish notation?

$-+3\times4\ 5\ 6$

Does it look strange? Maybe a little. Let's see how to evaluate it. First, look at where numbers group, on the right. Take the ones closest to the operator $\times$:

$-+3\textcolor{red}{\times4\ 5}\ 6$

In Polish notation, the expression $\times\ 4\ 5$ is the same as $4\times5$ in infix notation. The first step is then:

$-+3\textcolor{red}{\times4\ 5}\ 6=-+3\ \textcolor{red}{20}\ 6$

Now all of the operators and operands are grouped with their kin, and we can easily proceed with the same rule we just saw in action.

$\begin{split} & -+\ 3\ 20\ 6\\ =& -\textcolor{blue}{+\ 3\ 20}\ 6 \\ =& -\textcolor{blue}{23}\ 6 \\ =& \textcolor{green}{-23\ 6} \\ =&\ \textcolor{green}{17} \end{split}$

So how would we write the same expression in infix notation? Easy — it's $3+4\times5-6$. It may look a bit more complicated, even if more familiar and regular. But how to evaluate it?

First thing, we have to know where to start. From the left? From the right? In this case, we start from the left. However, we first evaluate the second operation rather than the initial one since the precedence of the multiplication is higher than the one of the sum.

We have $3+\textcolor{red}{4\times5}-6$, which gives $3+\textcolor{red}{20}-6$.

And now, since all the operators have the same precedence, we proceed from left to right: $\textcolor{blue}{3+20}-6 = \textcolor{blue}{23}-6$, and then becomes $\textcolor{green}{23-6}=\textcolor{green}{17}$.

As you can see, the infix notation requires more rules than the Polish notation: knowing the precedence, scanning the expression in its entirety before executing, and checking for brackets.

## How to convert from and to the prefix and postfix notations

#### Conversion from the infix notation to the postfix notation

When you have an expression written using the infix notation and you need to convert it to postfix notation, you can follow a simple algorithm called the shunting-yard algorithm. The Dutch computer scientist Edsger W. Dijkstra first proposed it in 1961.

Assuming the input is a string containing both operators and operands in the general form $A+B\times(C-D)$, for example, and that there are two "arrays" wherein we store the various elements, an output array, and an operator stack.

Here is the pseudocode for the conversion:

• Read the first element of the input string:

• If it's a number, then push it on the output.
• If it's an operator O1, then:
• While there is an operator O2 at the top of the operator stack with greater or equal precedence than O1, pop O2 from the operator stack and push it on the output.
• Push O1 on the operator stack.
• If it is a left bracket (, then push it on the operator stack.
• If it is a right bracket ), then:
• While the last element of the operator stack is a left bracket, pop the operators from the operator stack and push them on the output.
• Pop the left bracket.
• Repeat until the input is empty. Then, if the operator stack is not empty, pop all of the content of the operator stack and push it on the output.

#### Conversion from the infix notation to the prefix notation

The conversion from the infix to the Polish notation is a bit more tricky, and there is no named algorithm for the procedure.

In a way less elegant manner, the steps are:

• Reverse the infix expression, making particular attention to the brackets (we will see this later in an example).
• Apply the shunting yard algorithm, but changing the condition on the precedence of the operators from greater or equal to just greater.
• Reverse the newly found quasi-postfix expression to find the prefix expression.

Converting from the Polish notations to the infix notation is almost more effortless. The only difficulty arises in choosing when to include or omit brackets. We will skip that step in the following procedure so as not to make it too heavy, but we will tell you how to do that later.

First, let's check how to convert from prefix to infix.

• Take a prefix expression. All of the symbols are stacked at the left of their operands. Scan the expression from the right until you find an operator.
• If you find an operator before you pass two operands, there is an error in your expression.
• Now take the last two operands you encountered, place the operator between them (see that it's an infix expression?), and wrap it in brackets. That operation will be considered a single composite operand from now on.
• Keep scanning the expression until you find the next operator, and place it between the last two operands.
• Proceed until the expression is completely read.

#### Conversions back to the infix notation

The procedure to convert from postfix to infix is similar.

• Take the postfix expression, and scan it from left to right.
• When you meet an operator, take the last two operands you scanned and place the operator between them.
• If you didn't see two operands before the operator, you are not scanning a correct expression.
• Wrap the resulting infix expression between brackets.
• Proceed to scan the expression and build infix blocks until you run out of operators on the right side.

#### Bracket placement

We told you that we would explain how to correctly place brackets. It's better to leave this to a computer, but here is the explanation if you want to go on.

When you meet an operator while scanning the expression, you have to check the precedence of the operands associated with it. Then wrap the operand in brackets (NOT the expression you will obtain by placing the newly scanned operator in between them) only if the operator's precedence is higher or equal than the associated to the operand.

This is pretty easy if the operand is a number: we can say that it has infinite precedence, and therefore it is never closed between brackets.

The case of single operators is again pretty easy. However, when you have a composite operand (containing two or more operators), it is necessary to check all of them and select the lowest priority.

Here are some examples:

• For $3,\ +,\ 5$, both operands (on the left and right side of $+$) are numbers — no brackets here. The result is $3+5$.

• For $3+5,\ \times,\ 2/7$, the precedence of $+$ is lower than that of $\times$; hence, we need brackets. The precedence of the operators on the right side, $\times$ and $/$ is the same: no brackets. The end result is $(3+5)\times2/7$.

Last one! Multiple operators:

• In $3+4\times2,\times,4$, the right side is a number, so no brackets there. On the left side, we find both a $+$ and a $\times$. The lowest precedence ($+$) wins. The current operator is $\times$, so we have the same situation as before: brackets! The outcome is $(3+4\times2)\times4$.

The conversion from the prefix notation to the infix one is straightforward, and only requires you to reverse the expression before applying the algorithm we just saw!

⚠️ If a set of brackets already encloses an operation, ignore its operator while computing the lowest precedence!

## Examples of conversion from infix to the Polish notations (and vice-versa)

Now it's time to see how to apply all of those algorithms. We'll do just four examples, and then we are sure you will be able to write everything in Polish notation!

Let's convert from infix to postfix first. Here is our expression:

$(3+4)\times(7-2)$

It's time for the shunting yard algorithm. We start reading it from the left.

First, we meet a left bracket; we push it on the stack. The number 3 is pushed on the output.

$\text{output} = [\textcolor{red}{3}] \\ \text{stack} = [\ \textcolor{red}{(}\ ]$

We then encounter the $+$ operator. The stack doesn't contain other operators, so we push it directly there. You can easily do it on a piece of paper — or if you are good with remembering things, use your memory.

$\text{output} = [3]\\ \text{stack} = [\ (\ , \textcolor{red}{+}\ ]$

Let's continue. Next is a number — push it on the output.

$\text{output} = [3,\textcolor{red}{4}]\\ \text{stack} = [\ (\ ,+\ ]$

We've reached the right bracket: we need to pop operators from the stack until we meet the paired left bracket.

$\text{output} = [3,4,\textcolor{red}{+}\ ]\\ \text{stack} = [\ ]$

Remember to pop also the left bracket. Now we read $\times$. The operator stack is empty, so we push it there.

$\text{output} = [3,4,+\ ]\\ \text{stack} = [\textcolor{red}{\times}]$

Keep on scanning. We meet another left bracket. Now you know how it works:

$\text{output} = [3,4,+]\\ \text{stack} = [\times,\ \textcolor{red}{(}\ ]$

Here comes another operation. The $-$ is pushed on the stack, the numbers on the output. Just a few steps left!

$\text{output} = [3,4,+,\textcolor{red}{7,2}]\\ \text{stack} = [\times,\ (\ ,\ \textcolor{red}{-}]$

At last, the right bracket. We pop the minus sign and the left bracket from the stack. Remember the bracket!

$\text{output} = [3,4,+,7,2,\textcolor{red}{-}]\\ \text{stack} = [\times]$

Now the expression is over, but the $\times$ is still on the stack. Pop it and push it on the output, and you will get a new, shiny, reverse Polish notation expression: $3\ 4+7\ 2-\times$.

Next episode: the conversion to prefix notation.

We chose an expression that will show you how it differs from the postfix notation conversion: $3+2-7\times1$

First step, reverse it!

$3+2-7\times1 \Rightarrow 1\times7-2+3$

Now, follow these steps: we will speak up only if it's needed.

$\text{output} = [1,7]\\ \text{stack} = [\times ]\\ [0.5em] \text{output} = [1,7,\times]\\ \text{stack} = [-] \\ [0.5em] \text{output} = [1,7,\times,2]\\ \text{stack} = [-]$

Yes, now we need to talk! The next symbol we meet is a $+$, but look, there is a symbol with the same precedence on the top of the stack: $-$! Here, the only modification to the shunting yard algorithm comes into play, and since the operators are popped only if their precedence is greater (and not equal) to the one on the top of the stack, we simply push $+$ there:

$\text{output} = [1,7,\times,2,3]\\ \text{stack} = [-\ ,+]$

The expression is over, and we get this almost postfix result:

$1\ 7\times\ 2\ 3+-$

We reverse it, and here is our prefix expression!

$-+3\ 2\times\ 7\ 1$

🔎 Feeding the reversed infix expression in the unmodified shunting yard algorithm would have returned $1\ 7\times2- 3+$ — quite different from the result we obtained earlier!

The conversion back to infix notation is even easier. We will give you an example only for the one from postfix notation. The other one is straightforward.

The expression we take is a little bit more complex than the one we showed before. Ready? Here it is: $3\ 2+7\times6\ 4+/$.

We read it from the left. You will see both a right and a left stack containing the operators, one for each side of the operand we consider at each "building" step. If an operand is a number, then you will see $\text{null}$ in the stack. It will remember you that you won't need brackets there!

$\text{output} = [3,2]\\ \text{left stack} = [\text{null}]\\ \text{right stack} = [\text{null}]$

We read the operator $+$, so we build the expression $3+2$, pop everything we used from the output, and then push the result. Remember to empty the stacks.

$\text{output} = [3+2]\\ \text{left stack} = [\ ]\\ \text{right stack} = [\ ]$

Now we read another number, and then a symbol, $\times$. Let's see what happens:

$\text{output} = [3+2,7]\\ \text{left stack} = [+]\\ \text{right stack} = [\text{null}]$

Now in the left stack we have a $+$. Its precedence is smaller than the one of $\times$, so we put some bracket around that operand. Then we clean the stacks, and rearrange the output:

$\text{output} = [(3+2)\times7]\\ \text{left stack} = [\ ]\\ \text{right stack} = [\ ]$

$\text{output} = [(3+2)\times7,6,4]\\ \text{left stack} = [\ ]\\ \text{right stack} = [\ ]\\ \textcolor{red}{\text{\text{Reading}}\ +}\\ \text{output} = [(3+2)\times7,6+4]\\ \text{left stack} = [\text{null}]\\ \text{right stack} = [\text{null}]\\ \textcolor{red}{\text{Reading}\ /}\\ \text{output} = [(3+2)\times7,6+4]\\ \text{left stack} = [\times]\\ \text{right stack} = [+]\\ [1em] \text{output} = [(3+2)\times7/(6+4)]\\ \text{left stack} = [\ ]\\ \text{right stack} = [\ ]\\$

In the last step, we had to ignore the $+$ since a set of brackets encloses it, and then we added a new set only on the right side: the left side had the same precedence, and we ignored it.

The resulting expression in infix notation is then $(3+2)\times7/(6+4)$.

## How to use our Polish notation converter?

We offer you a calculator that you can use in many modes. But be careful; it is not that easy to use, and you cannot always detect your errors!

First thing, choose what you want to do: you can either convert an expression in the four modes we explained above or calculate the result of a Polish notation expression.

If you choose convert, then you have to select one of the four modes of conversion:

• From infix to prefix notation;
• From infix to postfix notation;
• From prefix to infix notation; or
• From postfix to infix notation.

Now write the expression! Remember that you can input the four operators +, −, * and /.

⚠️ Our Polish notation converter can read only positive numbers, either integers or decimal. If you try to input a negative number, bad things... won't happen: you will just get a really bad result!

If you decide to convert back from the Polish notation to the infix notation, we need to teach you how to input the expression. Since you can't use spaces, you need to separate pairs of adjacent numbers through a couple of periods: .. — but be careful, as their position changes according to the desired conversion.

• If you are converting from the prefix notation, then the two periods must be inserted before the number, like so: +..5..7.
• If you are converting from the postfix notation, then you must add the periods at the end of each number: 7..6..*

The other choice you had would bring you to the calculation mode. Here you can insert a Polish notation or a reverse Polish notation expression and see the result!

⚠️ You don't have to choose the type of Polish notation in which you are writing the expression, because our calculator can detect it independently. But remember to add the periods!

If you need to check the result of an infix expression, select advanced mode, and switch to infix notation. Remember that you can input operations only with the symbols +, −, *, and /.

## Why use our Polish notation converter?

Now that you know what is the Polish notation, how to read it, convert it from infix notation and back, and you can calculate any expression written that way, you may wonder, "do I need this?".

Probably not. This strange way of writing operations may not be your cup of tea. Some calculators used reverse Polish notation in the past years, but now they are limited to a restricted group of enthusiasts. It remains mostly a curiosity and a beautiful example of how being open-minded about other possibilities can create interesting and curious things.

Enjoy our Polish notation converter!

## FAQ

### What is the Polish notation?

The Polish notation is a different way to write arithmetic operations. There are two types of Polish notations: one in which operators are grouped on the left of the operations, and one in which the operators are grouped on the right.

### Is Polish notation better?

The question is still open! Some say that using the Polish notation leads to faster and more efficient calculations, but the scientific community hasn't decided yet.

However, it may be better for you, so try it out!