Polish Notation Converter (Prefix, Infix, Postfix)
Convert an expression between prefix (Polish), infix, and postfix (Reverse Polish) notation with the parsing steps shown.
Reviewed by Manoj Kumar, Mathematics Educator
Formula
Shunting Yard: scan tokens, use operator stack and output queue with precedence rules
The Shunting Yard algorithm processes infix tokens left to right. Numbers go to output. Operators are pushed to a stack after popping higher-precedence operators to output. Parentheses control grouping. The result is a postfix expression that can be evaluated with a simple stack.
Worked Examples
Example 1: Converting (3 + 4) * 2 - 5
Problem:Convert the infix expression (3 + 4) * 2 - 5 to both prefix (Polish) and postfix (Reverse Polish) notation.
Solution:Infix: ( 3 + 4 ) * 2 - 5\n\nShunting Yard steps for postfix:\n1. Push 3 to output\n2. Push ( to stack\n3. Push 3, then +, then 4\n4. ) encountered: pop + to output\n5. * pushed to stack\n6. Push 2 to output\n7. - has lower precedence: pop * to output, push -\n8. Push 5 to output\n9. Pop remaining: -\n\nPostfix (RPN): 3 4 + 2 * 5 -\nPrefix (PN): - * + 3 4 2 5\n\nEvaluation: 3+4=7, 7*2=14, 14-5=9
Result:Prefix: - * + 3 4 2 5 | Postfix: 3 4 + 2 * 5 - | Result: 9
Example 2: Converting 2 ^ 3 ^ 2 (Right Associative)
Problem:Convert 2 ^ 3 ^ 2 noting that exponentiation is right-associative.
Solution:Infix: 2 ^ 3 ^ 2\nRight-associative means: 2 ^ (3 ^ 2) = 2 ^ 9 = 512\nNot: (2 ^ 3) ^ 2 = 8 ^ 2 = 64\n\nPostfix: 2 3 2 ^ ^\nPrefix: ^ 2 ^ 3 2\n\nEvaluation: 3^2=9, 2^9=512
Result:Prefix: ^ 2 ^ 3 2 | Postfix: 2 3 2 ^ ^ | Result: 512
Frequently Asked Questions
What is Polish notation and why was it invented?
Polish notation, also called prefix notation, was invented by the Polish logician Jan Lukasiewicz in 1924 to simplify propositional logic expressions. In this notation, operators are placed before their operands rather than between them. For example, the infix expression 3 + 4 becomes + 3 4 in prefix notation. The key advantage is that Polish notation completely eliminates the need for parentheses and operator precedence rules because the order of operations is unambiguous from the expression structure itself. This makes expressions easier to parse programmatically and reduces potential ambiguity in mathematical and logical statements. Polish notation laid the groundwork for computer science expression evaluation and is still used in certain programming contexts today.
What is Reverse Polish Notation (postfix) and where is it used?
Reverse Polish Notation, commonly abbreviated RPN, places operators after their operands. The expression 3 + 4 becomes 3 4 + in RPN. This notation became famous through Hewlett-Packard calculators starting in the 1960s, where users would enter operands first, then press the operator key. RPN is particularly efficient for stack-based evaluation because you simply push operands onto a stack and apply operators to the top elements. Many programming languages and virtual machines use postfix notation internally. The Java Virtual Machine, for instance, uses a stack-based architecture that essentially processes bytecode in postfix order. RPN is also used in the PostScript page description language and in many embedded systems where memory efficiency is critical.
How does the Shunting Yard algorithm convert infix to postfix?
The Shunting Yard algorithm, invented by Edsger Dijkstra in 1961, converts infix expressions to postfix notation using two data structures: an output queue and an operator stack. When reading tokens left to right, numbers go directly to the output queue. When an operator is encountered, operators on the stack with higher or equal precedence (respecting associativity) are popped to the output queue before the new operator is pushed onto the stack. Left parentheses are pushed onto the stack, and when a right parenthesis appears, operators are popped to the output until the matching left parenthesis is found. After all tokens are processed, remaining operators on the stack are popped to the output. The algorithm runs in linear time relative to the number of tokens, making it highly efficient for expression parsing.
How do you evaluate a postfix (RPN) expression step by step?
Evaluating a postfix expression uses a simple stack-based algorithm. Read tokens from left to right. If the token is a number, push it onto the stack. If the token is an operator, pop the top two values from the stack, apply the operator with the first popped value as the right operand and the second as the left operand, then push the result back onto the stack. After processing all tokens, the single remaining value on the stack is the final result. For example, evaluating 3 4 + 2 * works as follows: push 3, push 4, see + so pop 4 and 3 and push 7, push 2, see * so pop 2 and 7 and push 14. The final answer is 14. This algorithm is elegant because it requires no parentheses and no precedence checking during evaluation.
References
Reviewed by Manoj Kumar, Mathematics Educator ยท Editorial policy