Convert an infix expression to Reverse Polish notation (postfix), with a table showing token by token when each operator is pushed onto the stack and when it is popped. The tool also shows Polish notation (prefix) and the syntax tree, and if you give the variables values, you can watch the expression being evaluated with a stack. You can also convert a postfix expression back to infix with the fewest parentheses needed.
Favorites and recently used calculators are stored only in this browser (localStorage).
Result
Reverse Polish notation (postfix)
–
Polish notation (prefix)
–
Infix (fewest parentheses)
–
Value
–
Syntax tree
Conversion steps (shunting-yard algorithm)
Evaluation with a stack
^ has the highest precedence, followed by × ÷, with + − the lowest. + − × ÷ of equal precedence are evaluated left to right, and ^ right to left. ÷ gives a decimal result even when the division is not exact.
See it as a graph
Other inputs stay at their current values while the chosen input varies from 1/4 to 4 times its current value. The red dot is the current value.
What Reverse Polish notation is
The usual way of writing A + B, with the operator between its two operands, is called infix notation. Writing the operator after its operands, as in A B +, is Reverse Polish notation (postfix), and writing it before them, as in + A B, is Polish notation (prefix). In Reverse Polish notation the order of evaluation is uniquely determined without operator precedence or parentheses. For example, (A + B) × (C − D) becomes A B + C D − ×, and the parentheses disappear. Because of this property, it is used in calculators, programming language implementations and the instruction sequences of stack machines.
Evaluating with a stack
A Reverse Polish expression can be evaluated by reading it from left to right with just one stack. A number or variable is pushed onto the stack; an operator pops the top two values, computes the result and pushes it. Note that of the two values popped, the one popped first (the one that was on top) is the right operand. For 5 1 2 + 4 × + 3 −, the calculations are 1 + 2 = 3, 3 × 4 = 12, 5 + 12 = 17 and 17 − 3 = 14, and the 14 left on the stack is the answer. Choose an example and follow the changes to the stack in the evaluation table.
Converting from infix: the shunting-yard algorithm
To convert an infix expression to Reverse Polish notation, use the shunting-yard algorithm devised by Dijkstra. Read the expression from left to right and send numbers and variables straight to the output. For an operator, first output any operators on top of the stack with equal or higher precedence, then push it (for right-associative operators such as exponentiation, output only those with higher precedence). Push an opening parenthesis, and when a closing parenthesis arrives, output all operators down to the opening parenthesis. Finally, output the operators left on the stack. In A + B × C, × has higher precedence, so it is pushed on top of +, giving A B C × +.
Relation to the syntax tree
An expression can be represented as a syntax tree (a binary tree) whose nodes are operators and whose leaves are numbers and variables. Traversing this tree in the order left child, right child, node (post-order) gives Reverse Polish notation; node, left child, right child (pre-order) gives Polish notation; and left child, node, right child (in-order) gives infix notation. A compiler parses the expressions in source code into an abstract syntax tree and walks it to emit stack machine instructions or machine code. Exams often ask you to give the Reverse Polish form of an expression or tree, or to name a tree traversal order.
Push 3 and 4, then + pops them and pushes 3 + 4 = 7. Push 2, then * pushes 7 × 2 = 14. Push 5, then − subtracts the later value 5 from the earlier value 14, giving 14 − 5 = 9. In postfix notation the order of operations is fixed by the sequence itself, so no parentheses are needed; this expression corresponds to (3 + 4) × 2 − 5 in ordinary notation. −9 results from swapping the operands of the subtraction, and 6 from reading it as 3 + 4 × 2 − 5 without parentheses.
AThe root is a + node whose right child is a * node representing 3 * 4
BThe root is a * node whose left child is a + node representing 2 + 3
CThe five nodes 2, 3, 4, + and * all sit directly under the root
Show answer and explanation
Answer: A. The root is a + node whose right child is a * node representing 3 * 4
Multiplication has higher precedence than addition, so the expression means 2 + (3 * 4). In an AST, operations computed first sit lower, so the root is +, its left child is 2 and its right child is a * node with children 3 and 4. Evaluating bottom-up computes 3 * 4 = 12 first, then 2 + 12 = 14. A * root corresponds to (2 + 3) * 4, and a flat list cannot express evaluation order.
AIt keeps everything from the source, including parentheses and separators, as nodes
BIt omits symbols irrelevant to meaning and expresses operator precedence through its shape
CIt corresponds one-to-one with CPU instructions and can be executed directly
Show answer and explanation
Answer: B. It omits symbols irrelevant to meaning and expresses operator precedence through its shape
Parsing following the grammar rules yields a tree that includes parentheses and separators (a concrete syntax tree), which carries much information later phases do not need. An AST keeps only the operations and statements needed for meaning; in (a + b) * c, for example, the addition node sits under the multiplication node, so the order of evaluation is clear without parentheses. Code corresponding to CPU instructions is generated in later phases.
ARemoving whitespace and comments from the characters and splitting them into words
BFinding frequently executed parts and converting them to machine code at run time
CChecking the token sequence against the grammar and building the program structure as a tree
Show answer and explanation
Answer: C. Checking the token sequence against the grammar and building the program structure as a tree
Parsing checks whether the token sequence produced by lexical analysis follows the language grammar and builds structures, such as an if statement's condition and body or the operations inside an expression, into a tree (a syntax tree). Grammar errors such as a missing semicolon or mismatched parentheses are found here. Splitting into words while removing whitespace is lexical analysis, and converting to machine code at run time is JIT compilation.
FAQ
What is A − B − C in Reverse Polish notation?
A B − C −. The − operator is evaluated left to right (it is left-associative), so the expression means (A − B) − C. A − (B − C) would be A B C − −, which gives a different result.
Why does Reverse Polish notation need no parentheses?
Each operator always applies to the two values just before it (or to the results computed so far), so which values are combined is determined by the written order alone. In infix notation, the same sequence can be evaluated in different orders depending on precedence and parentheses.
Can expressions with a sign, such as −A, be converted?
This tool supports only operators that take two values (binary operators). Write a minus sign as (0 − A). Real language implementations treat unary minus as a separate operator.
How is this tested in exams?
IT certification exams such as Japan's Fundamental Information Technology Engineer Examination often ask you to choose the Reverse Polish form of an infix expression, to compute the value of a Reverse Polish expression, or to answer questions about syntax tree traversal. Choose an example and compare the steps in the table with your own.