Infix to Postfix Converter
An infix to postfix converter rewrites A+B*C as ABC*+, with every operator after its operands. It uses the shunting-yard algorithm and lists each push and pop of the operator stack.
Sources: Wikipedia: Shunting yard algorithm Updated
Instructions: Enter an infix expression using letters (A-Z) or numbers (0-9) as operands and operators (+, -, *, /, ^). Use parentheses for grouping.
Examples: A+B*C, (A+B)*C-D, 3+4*2/(1-5)^2
Step-by-Step Conversion Process:
Operator Precedence:
- ^ (Exponentiation) - Highest precedence
- * / (Multiplication, Division)
- + - (Addition, Subtraction) - Lowest precedence
Postfix, also called reverse Polish notation, needs no brackets: a stack reads it from left to right and always knows what to do next. The converter does the conversion with Edsger Dijkstra's shunting-yard algorithm, which keeps an output list and a stack of waiting operators.
How to convert infix to postfix: A+B*C step by step
- Read A. Operands go straight to the output: A.
- Read +. The stack is empty, so push + onto it.
- Read B. Output: A B.
- Read *. It has higher precedence than the + on top of the stack, so nothing is popped and * goes on top: the stack is + *.
- Read C. Output: A B C.
- At the end of the input, pop the stack from the top: first *, then +. Output: A B C * +.
Read the result back to check it. ABC*+ multiplies B and C first and then adds A, which is the order that A+B*C asks for.
Infix to postfix rules: precedence and associativity
| Operator | Precedence | Associativity |
|---|---|---|
| ^ | 3 (highest) | Right: A^B^C = A^(B^C) |
| * and / | 2 | Left: A/B*C = (A/B)*C |
| + and − | 1 (lowest) | Left: A−B+C = (A−B)+C |
When an operator arrives, the converter first pops every operator on the stack with higher precedence, or with the same precedence when the new operator is left-associative. Then it pushes the new operator.
A left bracket is pushed as a marker. A right bracket pops operators to the output until it reaches that marker, and both brackets are dropped.
Infix and postfix expressions compared
These are the examples behind the buttons in the converter. The converter prints the result without spaces; the spaces here only make the tokens easier to read.
| Infix | Postfix |
|---|---|
| A+B*C | A B C * + |
| (A+B)*C | A B + C * |
| A+B*C−D/E | A B C * + D E / − |
| (A+B)*(C−D) | A B + C D − * |
| A^B^C | A B C ^ ^ |
| 3+4*2/(1−5)^2 | 3 4 2 * 1 5 − 2 ^ / + |
How to evaluate a postfix expression with a stack
Take 3 4 2 * 1 5 − 2 ^ / + from the last row. Push numbers; each operator pops two numbers and pushes its result.
- Push 3, 4 and 2. The * pops 4 and 2 and pushes 8.
- Push 1 and 5. The − pops them and pushes 1 − 5 = −4.
- Push 2. The ^ pops −4 and 2 and pushes (−4)² = 16.
- The / pops 8 and 16 and pushes 8 ÷ 16 = 0.5.
- The + pops 3 and 0.5 and pushes 3.5, the answer.
Working 3+4*2/(1−5)^2 in the usual order gives the same 3.5. The powers step is the one the exponent calculator does, and the % operator that many programming languages add to this list is covered by the modulus calculator.
What the converter checks first
Before converting, it checks that the brackets balance, that no two operators stand next to each other (as in A++B) and that the expression does not end with an operator. Each problem gets its own message instead of a wrong answer.
Operands are single letters or whole numbers. Converting the numbers themselves into binary or hexadecimal, as a compiler would, is a job for the base converter.
Infix to Postfix Converter: FAQ
What is postfix notation?
A way of writing expressions with each operator after the two values it works on. A + B becomes A B +, and A + B × C becomes A B C × +. Postfix needs no brackets and no precedence rules, because the order of the symbols already fixes the order of the operations.
How do you evaluate a postfix expression?
Read it from left to right with a stack. Push each number. When you reach an operator, pop the top two numbers, apply the operator to them and push the result. The one number left at the end is the answer.
Why does A^B^C become ABC^^?
Exponents are right-associative: A^B^C means A^(B^C). When the second ^ arrives, the converter leaves the first one on the stack instead of popping it, so the B^C part is done first.
What can I type into the converter?
Single letters or whole numbers as operands, the operators + − * / and ^, and round brackets. The converter rejects unbalanced brackets, two operators in a row and an expression that ends with an operator.
Sources
- Wikipedia: Shunting yard algorithm — the algorithm, its operator stack and the precedence and associativity rules (checked October 16, 2026)
- Wikipedia: Reverse Polish notation — postfix notation and how a stack evaluates it (checked October 16, 2026)