Show understanding of how Reverse Polish Notation (RPN) can be used to carry out the evaluation of expressions.
What is Reverse Polish Notation?
RPN, also known as postfix notation, writes the operator after its operands.
An expression such as \$a + b \times c\$ in infix form becomes \$a\;b\;c\;\times\;+\$ in RPN.
Why Use RPN?
No need for parentheses – the order of evaluation is implicit.
Evaluation can be performed using a simple stack algorithm.
Many calculators and some compilers use RPN for efficiency.
Converting Infix to Postfix (RPN)
The Shunting‑Yard algorithm (invented by Dijkstra) is commonly used. The key rules are:
Read the infix expression from left to right.
When an operand is encountered, output it immediately.
When an operator is encountered, pop operators from the stack to the output while they have higher or equal precedence (and are left‑associative). Then push the new operator.
When a left parenthesis ‘(’ is encountered, push it onto the stack.
When a right parenthesis ‘)’ is encountered, pop and output operators until the matching ‘(’ is removed.
After the whole expression has been read, pop any remaining operators to the output.