ToolSphere

Infix to Postfix and Prefix

Computer science

Convert arithmetic expressions to postfix and prefix with a shunting-yard stack trace, expression tree and postfix evaluation, including unary minus.

Type an expression to see its postfix, prefix and numeric result.

Postfix: 2 2 ^ u- 3 4 1 - * + · Value: 5

Use explicit operators. ^ means exponentiation and associates right. Powers bind more tightly than unary minus: -2^2 means -(2^2). u- and u+ mark unary signs in the output.

Copy conversion

Postfix (reverse Polish notation)

Operators follow their operands; prefix puts each operator first.

2 2 ^ u- 3 4 1 - * +

Prefix (Polish notation)

+ u- ^ 2 2 * 3 - 4 1

Numeric evaluation

5

Evaluation uses finite JavaScript numbers. % is remainder; decimal results have floating-point precision.

Expression tree

Scroll inside the diagram to see all branches. Full labels are also available in the node tooltips.

+, unary −, ^, 2, 2, *, 3, -, 4, 1++unary −unary −^^2222**33--4411

Shunting-yard stack trace

Token / actionOperator stackOutput queue
- push unary u-u-empty
2 append operandu-2
^ push binary operatoru- ^2
2 append operandu- ^2 2
+ pop higher precedence or left-associative equal precedenceu-2 2 ^
+ pop higher precedence or left-associative equal precedenceempty2 2 ^ u-
+ push binary operator+2 2 ^ u-
3 append operand+2 2 ^ u- 3
* push binary operator+ *2 2 ^ u- 3
( push opening parenthesis+ * (2 2 ^ u- 3
4 append operand+ * (2 2 ^ u- 3 4
- push binary operator+ * ( -2 2 ^ u- 3 4
1 append operand+ * ( -2 2 ^ u- 3 4 1
) pop operator before closing parenthesis+ * (2 2 ^ u- 3 4 1 -
) discard matching opening parenthesis+ *2 2 ^ u- 3 4 1 -
end drain operator stack+2 2 ^ u- 3 4 1 - *
end drain operator stackempty2 2 ^ u- 3 4 1 - * +

Postfix evaluation trace

TokenBefore stackAfter stack
2[][2]
2[2][2, 2]
^[2, 2][4]
u-[4][-4]
3[-4][-4, 3]
4[-4, 3][-4, 3, 4]
1[-4, 3, 4][-4, 3, 4, 1]
-[-4, 3, 4, 1][-4, 3, 3]
*[-4, 3, 3][-4, 9]
+[-4, 9][5]

On this browser only

Give feedback

Did this tool help?

Tell us what worked, what didn’t, or what you’d like next.

Opens Google Forms in a new tab with this tool’s name filled in. No sign-in or email required. Feedback is optional.

How to use Infix to Postfix and Prefix

  1. 1Edit the loaded numeric expression, or choose the symbolic or right-associative power example.
  2. 2Use explicit multiplication, supported arithmetic operators and matching parentheses.
  3. 3Read the postfix and prefix outputs; u- and u+ represent unary signs.
  4. 4Follow the operator-stack/output trace and expression tree to check grouping.
  5. 5For a numeric expression, inspect the value and evaluation stack, then copy the conversion.

About Infix to Postfix and Prefix

Convert an expression while keeping the working visible

This infix-to-postfix and prefix converter shows how an arithmetic expression becomes reverse Polish notation and Polish notation. It uses the shunting-yard algorithm for postfix, builds an expression tree from that result and reads the tree in preorder for prefix. A numeric expression is also evaluated from its postfix form, with a separate value-stack trace. The loaded example produces a result immediately, and every stage updates as you edit.

The input is an expression, not an assignment or complete program. It accepts numeric operands, ASCII identifier names, parentheses and the arithmetic operators +, −, *, /, % and ^. Multiplication must be written explicitly. Function calls, commas, array syntax and implicit multiplication are outside this grammar. A malformed expression produces a readable error rather than a conversion that only looks plausible.

Read the precedence and associativity conventions

Powers bind most tightly and associate to the right. Unary signs bind below exponentiation but above multiplication, division and remainder. The latter operators share a precedence level and associate left, followed by addition and subtraction at their shared left-associative level. Parentheses override ordinary grouping.

This convention makes unary minus visible instead of treating a leading minus as part of every numeric token. In the generated comparison, -2^2 evaluates to -4, while (-2)^2 evaluates to 4. The power chain 2^3^2 evaluates to 512, because its right-hand power is grouped first. These values come from the same library that supplies this page's conversion and evaluation traces.

A unary sign on the right side of a power also needs careful stack handling. The example 2^-3 evaluates to 0.125; reading the sign must not prematurely emit the waiting exponent operator before its right operand exists. The converter handles unary operators separately when pushing them, then applies the normal precedence rules when a binary operator arrives.

Understand the shunting-yard trace

An operand goes directly to the output queue. An opening parenthesis goes onto the operator stack. A closing parenthesis emits waiting operators until its matching opening parenthesis is found, then discards that opening marker. Parentheses determine grouping but do not appear in the final postfix expression.

For a binary operator, emit a stacked operator when it has higher precedence, or equal precedence when the incoming operator associates left. Equal-precedence powers are not popped in that step because exponentiation associates right. After the last operand, drain the remaining operators. An unmatched parenthesis or a missing operand stops conversion instead of being silently discarded.

The trace records each push, emitted operator and discarded parenthesis with the current stack and output queue. Stack order is displayed from bottom to top. Reading only the final string can hide an associativity mistake; the intermediate rows show exactly when an operator was delayed or emitted.

A complete numeric worked example

The loaded expression is:

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

Its generated postfix form is:

2 2 ^ u- 3 4 1 - * +

Its generated prefix form is:

+ u- ^ 2 2 * 3 - 4 1

The postfix evaluator gives 5. The u- marker is unary minus and consumes one operand; binary subtraction consumes two. u+ is the corresponding unary-plus marker. Keeping these distinct is necessary when a leading sign appears beside an exponent or another operator. Names such as neg and pos remain ordinary identifiers rather than colliding with internal unary-operation names.

Build prefix notation from the expression tree

Postfix already encodes the operation dependencies. Push a leaf for each operand. At a unary operator, pop one child and attach it. At a binary operator, pop the right child and then the left child, retaining that order for subtraction, division and powers. The process must finish with exactly one tree.

The tree's root operator is visited before its children to produce prefix notation. Its leaves keep the original operand spellings, including multi-character variable names and numeric exponent notation. Spaces separate output tokens so identifiers are not confused with a run of single-letter operands. The SVG diagram is generated from the same tree; node titles retain full labels when a long name is shortened visually.

Evaluate postfix only when the operands are numeric

For numeric postfix, push each number. A unary operator replaces its operand with a result. A binary operator pops the right value and then the left value before applying the operation. Each step shows the numeric stack before and after the token. Finishing with several values or trying an operator without enough operands is an invalid evaluation.

Expressions containing identifiers still get postfix, prefix and a tree, but no values are invented for their names. This page does not provide a variable-binding editor or assume that a symbol means a built-in mathematical constant. Use the symbolic example to focus on conversion without mixing it with a numerical substitution exercise.

Evaluation uses finite JavaScript numbers rather than arbitrary-precision or exact rational arithmetic. Decimal results may show binary floating-point rounding. The percent operator is remainder, not a promise of a nonnegative modular result for negative inputs. Division or remainder by zero and nonfinite or out-of-real-domain results produce an evaluation error. A valid conversion remains visible even when numeric evaluation fails.

Avoid common exam mistakes

Do not use the same marker for unary minus and binary subtraction. Do not pop an equal-precedence power as though it were left-associative. When evaluating postfix, preserve the order in which the two operands were popped; reversing them changes division, subtraction and exponentiation. Parentheses belong to the conversion process and should not survive in the completed Polish forms.

Check the expected grouping before comparing printed strings. Different unary-operation spellings can represent the same tree, while a superficially similar operator order can represent a different expression. This converter documents its u-/u+ notation and power convention so copied results have an explicit interpretation.

Use the three-address code generator to see another intermediate representation of expressions, or the compiler phases walkthrough to connect syntax trees with type analysis and code generation. The compiler lab groups the related tools. Copy conversion saves the current expression, both Polish forms and its numeric result or evaluation explanation.

Guides and tutorials

Frequently asked questions

What is postfix notation?

It places an operator after its operands. The operand order is retained, while grouping is represented by operator position instead of parentheses.

What is prefix notation?

It places an operator before its operands. This tool produces it by reading the generated expression tree in preorder.

Does ^ mean exponentiation or XOR?

It means exponentiation on this page and associates right. The input is an arithmetic-expression dialect, not a C expression where the same symbol commonly denotes XOR.

How is unary minus written in the output?

The u- marker consumes one operand. Binary subtraction remains -, so the two operations are unambiguous in postfix and prefix.

Why are -2^2 and (-2)^2 different?

Exponentiation binds more tightly than an unparenthesized unary minus under the documented convention. Parentheses make the negated value the base before exponentiation.

Can I convert expressions with variable names?

Yes. Identifiers retain their names in postfix, prefix and the tree. Numeric evaluation is omitted because the page does not assign values to them.

Can I omit the multiplication sign?

No. Write an explicit operator between adjacent operands or before a parenthesized factor. Function calls and implicit multiplication are not supported by this grammar.

Why is the decimal value not always exact?

Evaluation uses JavaScript numbers with binary floating-point precision. Conversion keeps the token spelling, but numerical evaluation is not exact rational arithmetic.

Does % always return a positive modulo?

No. It is the numeric remainder operator in this model. Negative-input behaviour should not be confused with a separately defined nonnegative mathematical modulo.

Why is a conversion shown beside an evaluation error?

The expression can have valid syntax while its numeric operation is undefined or nonfinite. The valid Polish forms and tree remain useful even when no finite value is displayed.

Have a suggestion for Infix to Postfix and Prefix?Share your feedback ↗