Infix to Prefix Converter with Step-By-Step Conversion Tutorial

Infix to Prefix Converter Sign

This free online converter will convert a mathematical infix expression to a prefix expression (A.K.A., Polish Notation, or PN) using the stack method.

The Infix to Prefix Converter also attempts to handle negative numbers and multi-digit operands.

Plus, the converter's results also include the step-by-step, token-by-token processing used to complete the conversion. You can use these interactive tutorials to manually perform the infix to prefix conversions or check your practice conversions.

If you would like to evaluate an infix expression, please use the Order of Operations Calculator. Or, to evaluate a prefix expression, use the Prefix Evaluator. Or, to convert an infix expression to a postfix expression, use the Infix to Postfix Converter.

Read more ...

Also on this page:

Infix to Prefix Converter

Convert infix to prefix and see step-by-step how the conversion is made using the stack method.

Special Instructions

Learn More

Selected Data Record:

A Data Record is a set of calculator entries that are stored in your web browser's Local Storage. If a Data Record is currently selected in the "Data" tab, this line will list the name you gave to that data record. If no data record is selected, or you have no entries stored for this calculator, the line will display "None".

DataData recordData recordSelected data record: None
Example expressions:

Example infix expressions:

To see an example of how the Infix to Prefix Converter works, and what types of expressions the converter is set up to handle, select an infix expression from the drop-down menu. To clear the expression field to enter your own infix expression, select "Example Expressions" or click the "Reset" button.

Infix expression:

Infix expression:

Enter an infix expression that fits within the following guidelines:

  • No spaces between operands or operators.
  • Numbers, parenthesis, operators, and single letters only (no variables, e.g., 4y not allowed).
  • Contains only numbers, decimal points, letters, and these valid characters: ( ^ * / + - ).
  • Contains the same number of left and right parenthesis.
  • Numbers with a leading decimal point must be preceded by a zero (enter .5 as 0.5).
  • If a number (numeric operand) immediately precedes a left parenthesis (indicating multiplication), the calculator will attempt to insert a multiplication operator between the number and the left parenthesis (convert 2(2+1) to 2*(2+1) ). However, you might get more predictable results if you insert the multiplication operator when you enter the infix expression.
#
Prefix expression:

Prefix expression:

This line will display the prefix equivalent of the entered infix expression. Note that a prefix expression is also known as a Polish notation, or PN.

If you would like to save the current entries to the secure online database, tap or click on the Data tab, select "New Data Record", give the data record a name, then tap or click the Save button. To save changes to previously saved entries, simply tap the Save button. Please select and "Clear" any data records you no longer need.

Related Calculators

prefix evaluatorinfix to postfix converterpemdas calculator

Help and Tools

Learn

What infix is, what prefix is, and how to convert infix to prefix using stack.

What is an Infix Expression?

An Infix Expression (or Infix Notation) is characterized by a math expression wherein the operators are placed between operands, (as in 2 + 3).

In the case of infix expressions, parentheses (or brackets) must be used to indicate the order in which the author wants the operations to be executed. Otherwise, the person solving the problem is left to apply an order of operation convention (PEMDAS, BODMAS, etc.) to solve the expression.

What is a Prefix Expression?

As the name implies, a Prefix Expression (or Prefix Notation, or Polish Notation) is characterized by a math expression wherein the operators are placed before their operands (2 + 3 infix becomes + 2 3 prefix).

Since each prefix operator is evaluated from right to left, this eliminates the need for parenthesis. This is why prefix expressions are easier for computer algorithms to evaluate than infix expressions.

How to Convert Infix to Prefix Using Stack

In case you're not familiar, a stack is a collection or list wherein the last element added to the stack is always the first element to be removed.

Using a stack to temporarily store operators is necessary because as we are evaluating each operator token of the infix expression from left to right, we can't instantly know an operator's right-hand operand. Therefore we need to temporarily add (push) an operator to the stack and only remove (pop) it from the stack and append it to output once we know its right operand.

So now that you know what a stack is and why it is used, the next order of business is to assign precedence and associativity to each operator type based on an order of operations convention (PEMDAS or BODMAS). The only exception is, in the stack method parentheses don't have precedence because they don't exist in a prefix expression. Instead, parentheses are used to isolate parts of the infix notation that are to be executed independently of the operators outside of them. Any nested parentheses are evaluated prior to evaluating their encompassing parentheses.

OperatorSymbolPrecedenceAssociativity
Exponent^4Right to Left
Multiplication*3Left to Right
Division/3Left to Right
Addition+2Left to Right
Subtraction-2Left to Right

Now that you know what a stack is and have assigned precedence and associativity to each operator, the following are the steps to converting infix to prefix using stack.

First, reverse the order of the infix expression. For example, if the infix expression is 4*3+(5/2), the reverse would be )2/5(+3*4.

Next, working from left to right on the reversed infix expression, one token at a time, determine whether or not the current token is an operand, an operator, or an opening or closing parenthesis.

If the token is an operand, append it to the output. Operands always appear in the same order in the output as they do in the infix expression.

If the token is a closing parenthesis, push it to the stack. This marks the beginning of an expression that should be evaluated separately.

If the token is an opening parenthesis, until a closing parenthesis is encountered at the top of the stack, pop each operator from the stack and append to the output. This marks the end of the operands and operators located within the current set of parentheses.

If the token is an operator and it has greater precedence than the operator on the top of the stack, push the token to the stack.

If the token is an operator and it has lower precedence than the operator on the top of the stack, until the operator on top of the stack has lower or equal precedence than the token, or until the stack is empty, pop each operator from the stack and append them to the output. Then push the current token to the stack.

If the token is an operator and it has the same precedence as the operator on the top of the stack, and the operator on top of the stack is left-to-right associative, push the token to the stack.

If the token is an operator and it has the same precedence as the operator on the top of the stack, but the operator on top of the stack is right-to-left associative, until the operator on top of the stack has lower or equal precedence than the token and is left-to-right associative, or until the stack is empty, pop each operator from the stack and append them to the output. Then push the current token to the stack.

Once all of the infix tokens have been evaluated, pop any remaining operators from the stack, one at a time, and append each of them to the output expression.

Finally, reverse the order of the output expression to get the prefix expression.

Infix to Prefix Conversion Examples

The following three infix-to-prefix examples each give a step-by-step illustration of how the rules stated in the previous section are applied on a character-by-character basis.

Example #1: A+B*C+D

Reverse Infix Expression:

D + C * B + A

Scan tokens one at a time from left to right.

CharacterStackOuput
DD
Since D is an operand, print to output.
++D
Since + is an operator, and the stack is empty, push it to the stack.
C+D C
Since C is an operand, print to output.
*+ *D C
Since * is an operator, and it has higher precedence than the + at the top of the stack, push * to the stack.
B+ *D C B
Since B is an operand, print to output.
++ +D C B *
Since + is an operator, and it has lower precedence than the * at the top of the stack, pop * from the stack and append it to the output, and then push + to the stack.
A+ +D C B * A
Since A is an operand, print to output.
Scanning complete.
Pop remaining 2 operators from stack, one at a time, and append to output.
+D C B * A +
D C B * A + +
Reverse final output.
Prefix:+ + A * B C D
Example #2: 5*(6^2-2)

Reverse Infix Expression:

) 2 - 2 ^ 6 ( * 5

Scan tokens one at a time from left to right.

CharacterStackOuput
))
Since ) is a closing parenthesis, push it to the stack.
2)2
Since 2 is an operand, print to output.
-) -2
Since - is an operator, and it has higher precedence than the ) at the top of the stack, push - to the stack.
2) -2 2
Since 2 is an operand, print to output.
^) - ^2 2
Since ^ is an operator, and it has higher precedence than the - at the top of the stack, push ^ to the stack.
6) - ^2 2 6
Since 6 is an operand, print to output.
(2 2 6 ^ -
Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop ^ from the stack and append it to the output, pop - from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.
**2 2 6 ^ -
Since * is an operator, and the stack is empty, push it to the stack.
5*2 2 6 ^ - 5
Since 5 is an operand, print to output.
Scanning complete.
Pop remaining operator from stack and append to output.
2 2 6 ^ - 5 *
Reverse final output.
Prefix:* 5 - ^ 6 2 2
Example #3: 2*(1+(4*(2+1)+3))

Reverse Infix Expression:

) ) 3 + ) 1 + 2 ( * 4 ( + 1 ( * 2

Scan tokens one at a time from left to right.

CharacterStackOuput
))
Since ) is a closing parenthesis, push it to the stack.
)) )
Since ) is a closing parenthesis, push it to the stack.
3) )3
Since 3 is an operand, print to output.
+) ) +3
Since + is an operator, and it has higher precedence than the ) at the top of the stack, push + to the stack.
)) ) + )3
Since ) is a closing parenthesis, push it to the stack.
1) ) + )3 1
Since 1 is an operand, print to output.
+) ) + ) +3 1
Since + is an operator, and it has higher precedence than the ) at the top of the stack, push + to the stack.
2) ) + ) +3 1 2
Since 2 is an operand, print to output.
() ) +3 1 2 +
Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop + from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.
*) ) + *3 1 2 +
Since * is an operator, and it has higher precedence than the + at the top of the stack, push * to the stack.
4) ) + *3 1 2 + 4
Since 4 is an operand, print to output.
()3 1 2 + 4 * +
Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop * from the stack and append it to the output, pop + from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.
+) +3 1 2 + 4 * +
Since + is an operator, and it has higher precedence than the ) at the top of the stack, push + to the stack.
1) +3 1 2 + 4 * + 1
Since 1 is an operand, print to output.
(3 1 2 + 4 * + 1 +
Since ( is an opening parenthesis, pop from the stack until a closing parenthesis is encountered. So pop + from the stack and append it to the output, and then pop the closing parenthesis from the stack and discard both parenthesis.
**3 1 2 + 4 * + 1 +
Since * is an operator, and the stack is empty, push it to the stack.
2*3 1 2 + 4 * + 1 + 2
Since 2 is an operand, print to output.
Scanning complete.
Pop remaining operator from stack and append to output.
3 1 2 + 4 * + 1 + 2 *
Reverse final output.
Prefix:* 2 + 1 + * 4 + 2 1 3

Adjust Calculator Width:

Move the slider to left and right to adjust the calculator width. Note that the Help and Tools panel will be hidden when the calculator is too wide to fit both on the screen. Moving the slider to the left will bring the instructions and tools panel back into view.

Also note that some calculators will reformat to accommodate the screen size as you make the calculator wider or narrower. If the calculator is narrow, columns of entry rows will be converted to a vertical entry form, whereas a wider calculator will display columns of entry rows, and the entry fields will be smaller in size ... since they will not need to be "thumb friendly".

Show/Hide Popup Keypads:

Select Show or Hide to show or hide the popup keypad icons located next to numeric entry fields. These are generally only needed for mobile devices that don't have decimal points in their numeric keypads. So if you are on a desktop, you may find the calculator to be more user-friendly and less cluttered without them.

Stick/Unstick Tools:

Select Stick or Unstick to stick or unstick the help and tools panel. Selecting "Stick" will keep the panel in view while scrolling the calculator vertically. If you find that annoying, select "Unstick" to keep the panel in a stationary position.

If the tools panel becomes "Unstuck" on its own, try clicking "Unstick" and then "Stick" to re-stick the panel.