Polish Notation (Prefix) Calculator

Evaluate prefix expressions and convert between infix and prefix notation with step-by-step solutions.

Enter Expression

Use spaces between operators and operands. Operators: + - * / ^ %

Result

Evaluation Result
23
Prefix Expression + 3 * 4 5
Infix Equivalent 3 + (4 * 5)

Evaluation Steps

+ 3 * 4 5 = 23

What is Polish Notation?

Polish notation, also known as prefix notation, is a mathematical notation in which operators precede their operands. It was invented by the Polish logician Jan Lukasiewicz in 1924 and is used extensively in computer science for expression parsing and evaluation.

Unlike infix notation (the standard way we write math), prefix notation eliminates the need for parentheses to indicate order of operations. The position of the operator itself determines how operands are grouped.

Notation Comparison

Infix Notation

Standard mathematical notation where the operator is between operands.

3 + 4 * 5 = 23

Prefix (Polish) Notation

Operator comes before operands. No parentheses needed.

+ 3 * 4 5 = 23

Postfix (Reverse Polish)

Operator comes after operands. Used in stack-based calculators.

3 4 5 * + = 23

How to Evaluate Prefix Expressions

To evaluate a prefix expression, read from right to left. When you encounter an operator, apply it to the next two operands. Alternatively, use a stack-based approach: push operands onto a stack, and when an operator is encountered, pop operands and apply the operation.

Algorithm (Right-to-Left Stack)

  1. Read the expression from right to left.
  2. If the token is an operand, push it onto the stack.
  3. If the token is an operator, pop two operands, apply the operator, and push the result.
  4. The final value on the stack is the result.

Applications

  • Compilers: Expression trees and intermediate code generation.
  • Lisp Programming: Lisp uses prefix notation natively for all operations.
  • Logic: Formal logic systems and proof assistants.
  • Calculator Design: Simplifies parsing by removing ambiguity.