Prashnikaप्रश्निका
Code
About

Infix to postfix converter

Convert an infix expression to postfix (Reverse Polish) or prefix notation the way ISC Computer Science marks it, with the stack after every symbol - or evaluate a postfix expression step by step.

How to use it

  1. 1Choose what to do: infix to postfix, infix to prefix, or evaluate a postfix expression.
  2. 2Type the expression, e.g. (A + B) * C - D / E, or tap an example.
  3. 3Press Convert and read the stack table, symbol by symbol.
What to do

Operators: + - * / % and ^ (power). Brackets ( ) [ ] { } work. Operands can be letters, names or numbers.

Worked example

(A + B) * C - D / E

This is the kind of answer you get. Put these values in the boxes or type your own.

Answer

Postfix
AB+C*DE/-
Prefix
-*+ABC/DE

Stack table

Scan left to right. An operand goes straight to the output. An operator first pops every operator of higher or equal precedence (^ is right to left, so an equal ^ stays), then is pushed. ( is pushed; ) pops up to its (. At the end the stack is emptied.

Symbol scannedStackPostfix expressionAction
((Push '('
A(AOperand: add it to the output
+(+APush '+'
B(+ABOperand: add it to the output
)(empty)AB+Pop + to the output, then remove the '('
**AB+Push '*'
C*AB+COperand: add it to the output
--AB+C*Pop * (higher or equal precedence), then push '-'
D-AB+C*DOperand: add it to the output
/-/AB+C*DPush '/'
E-/AB+C*DEOperand: add it to the output
end(empty)AB+C*DE/-End: pop /, - to the output

About the Infix to postfix converter

What it does

Converts an infix expression to postfix (Reverse Polish) or prefix notation and shows the stack after every symbol, the table ISC Computer Science expects in an answer. It can also evaluate a postfix expression, with the stack after each step.

The rules it follows

  • An operand goes straight to the output.
  • An operator first pops every operator on the stack with higher or equal precedence, then is pushed. ^ goes right to left, so an equal ^ stays on the stack.
  • ( is pushed; ) pops operators until the matching (.
  • At the end the stack is emptied into the output.
  • Precedence, highest first: ^, then * / %, then + and −.
  • For prefix, the expression is reversed (swapping the brackets), converted with the same stack method, and the result reversed.

Evaluating postfix

Scan left to right. Push each operand; at an operator pop two values (the second one popped is the left operand), work it out and push the result. The one value left at the end is the answer.

Common questions

Why is the second value popped the left operand?

The first value popped is the one pushed last, which is the right operand. For 8 2 − the stack holds 8 and then 2; popping gives 2 first, so the result is 8 − 2 = 6, not 2 − 8.

What is the postfix form of A + B * C?

ABC*+. The * has higher precedence than +, so B * C is worked out first and + comes last.

Do I need spaces between the letters?

No: AB+C* works as A B + C *. Use spaces between numbers (5 6 2 + *) and between names of more than one letter.

Practise on real ISC questions

More Computer Science tools

All study tools →