Infix to Prefix Converter

Convert any infix expression to prefix (Polish notation) and see every step — the reversal, the stack pass, and the final flip. Prefix to infix and prefix to postfix are here too.

Try:

Prefix to infix and postfix

Try:

The three-step method

There is no separate prefix algorithm to learn. Prefix is the mirror of postfix, so you turn the problem into one you can already solve, solve it, and mirror the answer back.

  1. Reverse the infix expression, swapping the brackets. Every ( becomes ) and every ) becomes (. Miss the swap and every bracketed group breaks.
  2. Run the shunting-yard algorithm, with the associativity test flipped. A left-associative operator now pops only on strictly higher precedence; a right-associative one pops on equal precedence too. This is the only change to the algorithm itself.
  3. Reverse the output. The result is the prefix expression.
Watch it happen

The converter above shows the reversed input on its own row, then traces the middle pass token by token. Read the final output row backwards and you have the prefix answer.

The one comparison that changes

On the forward (postfix) pass, an incoming left-associative operator pops an equal-precedence operator off the stack. On the reversed pass it must not — because the expression is backwards, so "equal precedence, group left" has become "equal precedence, group right".

How the popping condition differs between the two directions
SituationInfix → postfixInfix → prefix (reversed pass)
Top has higher precedencePopPop
Top has equal precedence, operator is left associativePopDo not pop
Top has equal precedence, operator is right associativeDo not popPop
Top has lower precedenceDo not popDo not pop

Get this wrong and A - B - C converts to - A - B C, which means A - (B - C). The correct answer is - - A B C.

Worked example: A + B * C

infix       A + B * C

step 1      reverse it              C * B + A
step 2      shunting-yard pass      output: C B * A +
step 3      reverse the output      + A * B C

prefix      + A * B C

Sanity check: + A * B C reads as "add A to the product of B and C", which is exactly what A + B * C means. Multiplication still binds tighter, and no brackets were needed to say so.

Infix to prefix using a stack

The stack does the same job it does in the forward direction: it holds operators that cannot be placed yet. Operands go straight to the output; an operator waits until the algorithm can prove nothing stronger is coming.

The six rules from the stack walkthrough apply unchanged — apart from the flipped comparison above, and the fact that you are feeding the stack a reversed expression. Everything about push, pop, peek and the bracket marker is identical.

Prefix to postfix conversion

Going from prefix to postfix needs no precedence rules at all, because both notations are already unambiguous. Scan the prefix expression right to left with a stack of part-built expressions:

  • Operand: push it.
  • Operator: pop two expressions — the first popped is the left operand — join them as left right operator, and push the result.
  • One expression remains: that is the postfix form.

So * + A B C becomes A B + C *. The second tool above does this and shows the stack at each step.

Infix to prefix examples with answers

Infix, prefix and postfix side by side
InfixPrefix (Polish)Postfix (RPN)Note
A + B+ A BA B +Mirror images
A + B * C+ A * B CA B C * +* binds tighter
(A + B) * C* + A B CA B + C *Brackets vanish in both
A - B - C- - A B CA B - C -Left associative
A ^ B ^ C^ A ^ B CA B C ^ ^Right associative
A / B / C/ / A B CA B / C /Left associative
(a + b) * (c - d)* + a b - c da b + c d - *Two groups
A + B * C - D / E- + A * B C / D EA B C * + D E / -Mixed precedence

Evaluating a prefix expression

Scan right to left with a value stack. Push each operand. On an operator, pop two values, apply it with the first popped as the left operand, and push the result. One value is left at the end.

+ 2 * 3 4        scanning right to left

  4              push 4                stack: 4
  3              push 3                stack: 4 3
  *              pop 3, pop 4 -> 12    stack: 12
  2              push 2                stack: 12 2
  +              pop 2, pop 12 -> 14   stack: 14

  result 14

Note the operand order: for prefix the first value popped is the left operand, the opposite of postfix evaluation.

Frequently asked questions

How do you convert infix to prefix?

Reverse the infix expression, swapping every opening bracket for a closing one and vice versa. Run the shunting-yard algorithm over the reversed expression, but flip the associativity test so left-associative operators pop only on strictly higher precedence. Reverse the output and you have the prefix form.

Why do you reverse the expression twice?

Prefix is the mirror image of postfix. Reversing turns the problem into one the ordinary postfix algorithm already solves, and reversing the answer turns it back. It is the cheapest correct method — the alternative is writing a second, separate algorithm.

What changes in the algorithm besides the reversal?

One comparison. On the forward pass a left-associative operator pops an equal-precedence operator off the stack; on the reversed pass it must not. Right-associative operators swap the other way. Miss this and A - B - C comes out as - A - B C instead of - - A B C.

Is prefix the same as Polish notation?

Yes. Prefix notation and Polish notation are the same thing — the operator written before its operands, named after the Polish logician Jan Lukasiewicz. Postfix is Reverse Polish Notation, the mirror of it.

How do you convert prefix to postfix?

Scan the prefix expression right to left with a stack. Push operands. On an operator, pop two expressions — the first popped is the left operand — join them as left right operator, and push the result. The second tool on this page does exactly that.

How is a prefix expression evaluated?

Scan right to left. Push each operand. When you hit an operator, pop two values, apply the operator with the first popped as the left operand, and push the result. One value remains and that is the answer.

Do brackets ever appear in prefix notation?

No. Like postfix, prefix is unambiguous from the symbol order alone, so brackets are never needed and never produced. If your prefix expression contains brackets, something has gone wrong.