Web4College

Infix to Prefix Converter

Convert an infix expression to prefix notation and follow each conversion step.

Test animation

Infix Postfix

Watch tokens move into the stack and output. Play it through or explore one step at a time.

Variables, numbers, + โˆ’ * / % ^ and parentheses. Up to 32 tokens; use explicit multiplication.

Try:
Operand โ†’ output Operator โ†’ stack โ†’ outputPrecedence: ^ > * / % > + โˆ’
Viz PlayerInfix to postfixClick to select
INFIX INPUTread left โ†’ rightA+B*CCURRENT TOKEN1. Read the next token2. Apply the rule below3. Move it to its placeOPERATOR STACKemptyLast in, first outPOSTFIX OUTPUTOperands and popped operators arrive here โ†’A+B*C
0

Ready to convert

Read left to right. Operands go to output; operators wait on the stack.

Step 0 / 14

Space play / pause ยท โ† โ†’ stepยท Scroll over the player to step ยท Esc deselect

Output so farโ€”
Infix Prefix

Use numbers or variable names, +, -, *, /, ^ and parentheses. Write multiplication explicitly: 2 * x. Unary minus and functions are not supported.

Prefix result

+ A * B C

Step-by-Step Conversion ยท Stack-Based

Read left to right with an operator stack and an expression stack. To reduce an operator, pop the right fragment, then the left, and push operator + left + right. Stacks below run from bottom to top; | separates expression fragments.

Follow the two stacks. On narrow screens, scroll horizontally to see every column.
TokenActionOperator stackExpression stack
APush operand A onto the expression stack.โ€”A
+Push operator + after resolving higher-priority operations.+A
BPush operand B onto the expression stack.+A | B
*Push operator * after resolving higher-priority operations.+ *A | B
CPush operand C onto the expression stack.+ *A | B | C
EndPop *, then right and left fragments; push * B C.+A | * B C
EndPop +, then right and left fragments; push + A * B C.โ€”+ A * B C

Visual Breakdown

Read the tree in prefix order: visit the operator first, then its left subtree, then its right subtree. The numbers on the nodes show that reading order. Expand the diagram to pan and zoom.

Prefix reading order: + A * B C

Examples

InfixPrefix
A + B * C+ A * B C
(A + B) * C* + A B C
A - B - C- - A B C
A ^ B ^ C^ A ^ B C
price * quantity + tax+ * price quantity tax

Key Terms

TermLabelDetails
InfixInput notationThe operator sits between its operands.

For example, A + B uses infix notation. Parentheses and precedence determine which operation comes first.

PrefixOutput notationThe operator comes before its operands.
PrecedenceOperator priorityWhich operator binds more tightly.
AssociativityGrouping ruleHow equal-priority operators group.

Frequently Asked Questions