Polish Notation Converter & RPN Evaluator

Convert between infix, prefix (Polish), and postfix (Reverse Polish) notation. Evaluate RPN expressions with step-by-step stack traces.

Polish Notation Converter & Evaluator

Use spaces between all tokens: ( 3 + 4 ) * 2
Input Tokens
3 + 4 * 2
5 tokens parsed
Postfix (RPN)
3 4 2 * +
Reverse Polish Notation โ€” operators follow operands
Evaluated Result
11.000000
Numeric evaluation of the expression
Notation Type
Postfix (RPN)
Used by HP calculators and Forth
Tokens Count
5
Total number of tokens in the expression

Conversion Steps (Shunting-Yard Algorithm)

#ActionStackOutput
1Push 3 to output[]3
2Push op '+' to stack[+]3
3Push 4 to output[+]3 4
4Push op '*' to stack[+, *]3 4
5Push 2 to output[+, *]3 4 2
6Pop remaining[+]3 4 2 *
7Pop remaining[]3 4 2 * +

RPN Evaluation Trace

#ActionStack
1Push 3[3.0000]
2Push 4[3.0000, 4.0000]
3Push 2[3.0000, 4.0000, 2.0000]
44 * 2 = 8[3.0000, 8.0000]
53 + 8 = 11[11.0000]

Operator Precedence & Associativity

OperatorPrecedenceAssociativityExample
^3 (highest)Right2^3^2 = 2^(3^2) = 512
* /2Left6/3*2 = (6/3)*2 = 4
+ โˆ’1 (lowest)Left3โˆ’1+2 = (3โˆ’1)+2 = 4
Planning notes, formulas, and examples

About the Polish Notation Converter & RPN Evaluator

Polish notation (prefix notation), invented by Jan ลukasiewicz in 1924, writes operators before their operands: + 3 4 instead of 3 + 4. Reverse Polish notation (RPN, or postfix) puts operators after: 3 4 +. Both eliminate the need for parentheses and operator-precedence rules, making them ideal for stack-based computation.

This converter converts between standard infix notation and both Polish/Reverse Polish forms using Dijkstra's shunting-yard algorithm. It also evaluates any RPN expression and shows the full stack trace so you can follow each push and pop. Whether you are learning about expression parsing in a data structures course, debugging a Forth program, or simply curious about how HP calculators work, this converter makes the process transparent.

All five arithmetic operators are supported (+, โˆ’, ร—, รท, ^) with correct precedence and associativity. The step-by-step tables show the operator stack and output queue at every stage of the conversion, and the evaluation trace shows the numeric stack at every computation step.

When This Page Helps

Expression parsing is a fundamental topic in computer science โ€” it appears in compilers, interpreters, calculators, and spreadsheet engines. Understanding how infix expressions are converted to postfix and evaluated on a stack is essential knowledge for any programmer.

This converter makes the abstract algorithm concrete. By watching the stack and output queues change step by step, concepts like operator precedence, associativity, and stack-based evaluation become intuitive rather than memorized.

How to Use the Inputs

  1. Select a mode: Infix โ†’ Postfix, Infix โ†’ Prefix, Evaluate RPN, or Postfix โ†’ Infix.
  2. Enter your expression with spaces between every token (numbers, operators, parentheses).
  3. Use presets for classic textbook examples.
  4. Read the converted notation and evaluated result in the output cards.
  5. Examine the Shunting-Yard Algorithm table to see how conversion works step by step.
  6. Review the RPN Evaluation Trace to follow the stack during computation.
  7. Check the precedence reference table for operator priority rules.
Formula used
Shunting-Yard Algorithm: scan tokens left-to-right; push numbers to output; push operators to stack (popping higher-precedence operators to output first); push "(" to stack; on ")" pop to output until "(". Evaluate RPN: push numbers to stack; on operator, pop two operands, compute, push result.

Example Calculation

Result: Postfix: 3 4 2 * +, Result: 11

The * has higher precedence than +, so 4 2 * is evaluated first (= 8), then 3 + 8 = 11.

Tips & Best Practices

  • Always space-separate every token: "( 3 + 4 )" not "(3+4)".
  • Start with simple expressions (3 + 4 * 2) before adding parentheses.
  • Try right-associative exponentiation: 2 ^ 3 ^ 2 should give 512, not 64.
  • Use the Evaluate RPN mode to manually check answers by tracing the stack.
  • Compare the infix and postfix forms to see where parentheses are implicit in RPN.
  • The Postfix โ†’ Infix mode shows how to reconstruct parenthesized infix from RPN.

History of Polish Notation

Polish notation was invented by the Polish logician Jan ลukasiewicz in 1924 as a way to write logical formulas without parentheses. Charles Hamblin and others later developed Reverse Polish Notation in the 1950s for computer science applications. Burroughs Corporation built the first stack-based computer (B5000, 1961) using RPN internally, and Hewlett-Packard popularized RPN calculators starting with the HP-35 in 1972.

The Shunting-Yard Algorithm

Edsger Dijkstra described this algorithm in 1961. It converts infix notation to postfix in O(n) time using a single operator stack. The name comes from the analogy with a railroad shunting yard, where cars are sorted from one track to another. The algorithm processes tokens left-to-right: numbers go directly to output; operators are pushed to the stack after popping all higher-or-equal-precedence operators; left parentheses are pushed; right parentheses pop the stack to output until a matching left parenthesis is found.

Stack-Based Evaluation

Evaluating a postfix expression is beautifully simple: scan left to right; push numbers onto a stack; when an operator is encountered, pop two values, apply the operator, and push the result. The final value on the stack is the answer. This simplicity is why postfix notation is used in virtual machines (Java bytecode, Python bytecode), printer languages (PostScript), and embedded systems.

Sources & Methodology

Last updated:

Frequently Asked Questions

  • RPN (postfix) places operators after their operands: 3 4 + means 3 + 4. It requires no parentheses and is easy to evaluate with a stack.