Web4College

Infix to Prefix Converter

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

Expression converter

Examples:
+ A * B C

Visualization

Watch this interactive visualization convert your infix expression to prefix step by step. Follow how it reverses tokens, swaps parentheses, builds postfix with an operator stack and reverses the result into prefix. Play, pause or step forward and backward to inspect each change.

Variables, numbers, + โˆ’ * / ^ and parentheses. Up to 32 tokens; use explicit multiplication. Unary signs and functions are unsupported.

Infix to prefix ยท Reverse method
Space plays or pauses. Arrow keys step. Ctrl plus arrows changes chapter. Shift plus greater or less changes speed. Escape releases focus.
InfixA+B*CReversed infixC*B+A

Step-by-Step Conversion ยท Reverse Method

Follow these steps to convert your infix expression to prefix notation using operator precedence and a stack. Reverse the tokens and swap parentheses, convert to postfix with reversed associativity, then reverse the postfix tokens to get prefix.

  1. Reverse and swap parentheses

    C * B + A

  2. Convert to postfix

    C B * A +

  3. Reverse to get prefix

    + A * B C

Conversion methods

Four common ways to convert infix to prefix.

  1. Reverse method

    Used here

    Reverse tokens and swap parentheses, convert to postfix, then reverse to prefix.

  2. Direct stack conversion

    Use operator and expression stacks to build prefix directly, following operator rules.

  3. Expression tree

    Build a tree, then visit each operator before its left and right operands.

  4. Recursive parsing

    Parse nested expressions by precedence, combining each operator with its operands.

Important terms to revise

Infix

The operator sits between its operands.

Example

A + B puts the operator between A and B.

Prefix

The operator comes before its operands.

Example

+ A B puts the operator first.

Postfix

The operator comes after its operands.

Example

A B + means add A and B.

Operator

A symbol that specifies an operation.

Example

+ adds; * multiplies.

Operand

A value or variable an operator acts on.

Example

In A + 5, A and 5 are operands.

Precedence

Which operator binds more tightly.

Example

In A + B * C, multiply B and C first.

Associativity

How equal-priority operators group.

Example

A - B - C groups as (A - B) - C.

Stack

Stores items in last-in, first-out order.

Example

Push +, then *; pop * first.