The syllabus for AS & A‑Level Computer Science (Core 1) contains several topics under the heading Translation Software. RPN is only one part of this wider picture.
| Stage | Key ideas |
|---|---|
| Lexical analysis | Tokenising source code – identifiers, literals, operators, delimiters. |
| Parsing | Building a parse tree / abstract syntax tree (AST) using a grammar (e.g. BNF). Detecting syntax errors. |
| Syntax‑directed translation | Walking the AST to produce an intermediate representation – often post‑fix (RPN). |
| Code generation | Turning the intermediate form into target‑machine instructions or byte‑code. |
| Optimisation (optional) | Eliminating redundant operations, constant folding, dead‑code removal. |
| Interpreter vs. compiler | Interpreters evaluate the intermediate form directly; compilers translate it to native code. |
All of the above stages are examined in the exam. The notes below concentrate on the post‑fix (RPN) representation and its evaluation, but the introductory roadmap shows how it connects to the rest of the translation pipeline.
a + b × c → RPN a b c × +operand₁ operand₂ operator.Dijkstra’s algorithm converts a fully parenthesised or un‑parenthesised infix expression into RPN while respecting precedence and associativity.
pop op₂ to the output.
+, –, ×, ÷, %) are left‑associative. Example: a – b – c → a b – c –.^) are not in the core syllabus, but if required they are treated as having lower precedence when the same operator is on the stack.Infix expression: ( 3 + 4 ) × 5 – 6 ÷ 2
| Token | Action | Output (post‑fix) | Stack (top → bottom) |
|---|---|---|---|
| ( | push | ( | |
| 3 | output | 3 | ( |
| + | push | 3 | + ( |
| 4 | output | 3 4 | + ( |
| ) | pop until ‘(’ | 3 4 + | |
| × | push | 3 4 + | × |
| 5 | output | 3 4 + 5 | × |
| – | pop higher precedence (×) → output, then push – | 3 4 + 5 × | – |
| 6 | output | 3 4 + 5 × 6 | – |
| ÷ | push (same precedence, left‑assoc → pop – first) | 3 4 + 5 × 6 – | ÷ |
| 2 | output | 3 4 + 5 × 6 – 2 | ÷ |
| End | pop remaining operators | 3 4 + 5 × 6 2 ÷ – |
Resulting RPN: 3 4 + 5 × 6 2 ÷ –
The evaluation uses a single stack. For each token:
function evaluateRPN(tokens):
stack ← empty list
for token in tokens:
if token is a number:
stack.push(token)
else if token is a binary operator:
// right operand is popped first
operand2 ← stack.pop()
operand1 ← stack.pop()
result ← applyOperator(token, operand1, operand2)
stack.push(result)
else:
raise Error("Invalid token")
if stack.size() ≠ 1:
raise Error("Malformed expression")
return stack.pop()
Extension notes (not required for the exam):
sin or max can be handled by storing the arity with the token.In an exam answer a brief comment such as “If the stack under‑flows the expression is malformed” demonstrates understanding and gains marks.
| Token | Action | Stack (top → bottom) |
|---|---|---|
| 3 | push | 3 |
| 4 | push | 4, 3 |
| + | pop 4,3 → 3+4=7, push 7 | 7 |
| 5 | push | 5, 7 |
| × | pop 5,7 → 7×5=35, push 35 | 35 |
| 6 | push | 6, 35 |
| 2 | push | 2, 6, 35 |
| ÷ | pop 2,6 → 6÷2=3, push 3 | 3, 35 |
| – | pop 3,35 → 35–3=32, push 32 | 32 |
Final result: 32
3 4 + with 7 before code generation).| Concept | AO1 – Knowledge | AO2 – Application | AO3 – Design/Implementation |
|---|---|---|---|
| Definition of RPN | State that operators follow their operands. | Identify the RPN form of a given infix expression. | Explain why RPN eliminates the need for parentheses. |
| Conversion (Shunting‑Yard) | List the five algorithmic steps and the role of precedence/associativity. | Convert an infix expression to RPN, showing the stack at each step. | Write (or comment on) a short routine that implements the algorithm. |
| Evaluation using a stack | Describe push/pop actions for operands and operators. | Trace a supplied RPN expression, recording the stack after each token. | Provide pseudocode (or a real program fragment) that evaluates RPN. |
| Error handling | Identify the three common malformed‑expression errors. | Comment on what would happen if an under‑flow occurs. | Include a simple error‑check in the evaluation routine. |
| Context within translation software | Recall the stages: lexical analysis → parsing → RPN → code generation. | Explain how RPN simplifies code generation for a stack‑based target. | Design a mini‑compiler that translates arithmetic statements to RPN and then to a hypothetical stack machine. |
Write a small program (Python, Java or C++) that reads a line containing an arithmetic expression in infix form, converts it to RPN using the Shunting‑Yard algorithm, and then evaluates the RPN. Include:

Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources, past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.