Cambridge A2 Level Computer Science 9618 · 16.2 Translation Software

Q38 · 9618/31/O/N/25 Q8

Worked solution from the System Software Paper 3 Advanced Theory booklet, Part 2 of 2.

Q389618/31/O/N/25original Q8Structured · 8 marks
Question 8 from 9618/31/O/N/25

Worked solution

(a)

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]

(b)

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]

(c)

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.

StepStack (top first)
push d5
push a6, 5
push b12, 6, 5
+ (6 + 12)18, 5
* (5 * 18)90
push c15, 90
push a6, 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]

KEY IDEARPN is evaluated with a stack: push values, and for each operator pop two, work out the result and push it back.
TOTAL 8 marks

Official mark scheme

Mark scheme for 9618/31/O/N/25 question 8
VIDEO
▶ 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.