Worked solution from the System Software Paper 3 Advanced Theory booklet, Part 2 of 2.
Write any two of these:
To turn the high-level source code into a sequence of tokens [1]
To pass the tokens on to the parser for syntax analysis, or to create a symbol table, or to remove white space and comments [1]
Work out the brackets first, then the multiply and the divide from left to right. (2 – 6) gives 2 6 –. (13 + 7) gives 13 7 +.
2 6 – 13 7 + [1]
Complete answer: 2 6 – 13 7 + * 5 / [1]
Read the RPN left to right. Push a value for a letter. For an operator, pop two values and push the result. Values: d = 5, a = 6, b = 12, c = 15. Each row shows the stack, with the top first.
| Step | Stack (top first) |
|---|---|
| push d | 5 |
| push a | 6, 5 |
| push b | 12, 6, 5 |
| + (6 + 12) | 18, 5 |
| * (5 * 18) | 90 |
| push c | 15, 90 |
| push a | 6, 15, 90 |
| - (15 – 6) | 9, 90 |
| / (90 / 9) | 10 |
Stacks for d, a, b and + correct [1]
Stack after * correct (90) [1]
Stacks for c, a and - correct [1]
Final stack after / holds 10 [1]
▶ Whole paper
Video solution of the whole paper · by Zafar Ali Khan (ZAK). Tap the picture to open it on YouTube.
Solutions follow the official Cambridge mark schemes; marks are shown in grey. Videos are from other YouTube channels. Paper codes read component / series / year: M/J = May/June, O/N = October/November. Past-paper questions © UCLES, reproduced for classroom practice.