Skip to content
Cambridge AS & A Level9618

Translation Software

Computer Science 9618 Chapter Notes

Last updated November 2026

ShareWhatsAppPost

Translation Software notes as a printable PDF.

Read online free. Download a watermarked copy with a free account.

Read the notes

The full Translation Software notes as text: skim, search, and jump between subtopics.

~13 min read

1. Interpreters

An interpreter translates and executes a program one statement at a time. It does not produce a translated version of the program, so no executable file is made.

It stops when an error is found. The error can be corrected in real time and the program continues from that point. Only one error is displayed at a time, and a partly written program can be tested on its own.

A compiler translates all the code at the same time and creates an executable file. Some high-level languages are partially compiled and partially interpreted. Partially compiled programs can be used on different platforms, as they are interpreted when run.

Examiner insight

“Many of the responses simply described the operation of an interpreter without any comparison to a compiler.” “Some candidates applied descriptions more appropriate to the role of a compiler to this question, and others wrote long answers that did not address the whole question.”

Worked example 14 marks

A programmer uses both a compiler and an interpreter to translate a program written in a high-level language.

Describe the advantages of using the interpreter compared to the compiler to translate the program.

  1. 1

    The interpreter is easier to debug with: it translates line by line and stops when an error is found, whereas the compiler translates all the program at the same time [1]

  2. 2

    It reports one error at a time [1]

  3. 3

    The error can be corrected in real time and the program restarts at the same point, whereas with a compiler the program has to be corrected, recompiled and re-run [1]

  4. 4

    A partially completed program can be translated and tested on its own, but a compiler cannot translate a partial program [1]

2. Lexical analysis and syntax analysis

The stages of compilation include lexical analysis, syntax analysis, code generation and optimisation. The first two break up and check the source code.

Lexical analysis converts the high-level source code into a sequence of tokens. It removes unnecessary white space and comments and creates a symbol table. The tokens are sent to the parser.

Syntax analysis checks that the tokens conform to the rules of the programming language (its grammar). It reports syntax errors and produces a parse tree.

Examiner insight

“Most candidates correctly identified that syntax analysis reports syntax errors or checks that the code matches the grammar of the language. Relatively few candidates gave completely correct answers.”

Worked example 12 marks

Outline the purpose of lexical analysis during the compilation of a program.

  1. 1

    To convert the high-level source code into a sequence of tokens [1]

  2. 2

    … that can be sent to the parser for syntax analysis [1]

3. Code generation and optimisation

Code generation converts an intermediate representation of the source code into an executable form. Compilers produce executable or object code files that can be run without the source code.

Optimisation improves the code so that it uses the minimum resources: CPU time, memory or storage, and program execution time.

It can identify and remove redundant code, simplify expressions or reorder the code.

Worked example 12 marks

Outline the process of optimisation during the compilation of a program.

  1. 1

    To identify and remove redundant code, simplify expressions or reorder the code [1]

  2. 2

    … so that storage size, memory use, power consumption or program execution time is minimised [1]

4. Backus-Naur Form and syntax diagrams

The grammar of a language can be expressed using syntax diagrams or Backus-Naur Form (BNF). A syntax diagram shows a rule as its name and boxes joined by arrows; BNF writes the same rules as text.

In BNF each rule defines a name in angle brackets using `::=`. A bar `|` separates alternatives, so a part used one or two times is written `<digit>|<digit><digit>`. A rule that uses its own name allows any number of repeats.

```

<digit> ::= 0|1|2|3|4|5|6|7|8|9 <integer> ::= <digit> | <digit><integer> ```

Examiner insight

“Most candidates identified the fact that the variable 9SW was invalid and UWY was valid, with candidates who also gave clear reasons for why this was the case achieving the marks.”

Worked example 13 marks

A language defines a name using these BNF rules.

```

<letter> ::= a|b|c <name> ::= <letter> | <letter><name> ```

Show that `cab` is a valid `<name>`.

  1. 1

    `c` is a `<letter>`, so `cab` fits `<letter><name>` if `ab` is a `<name>` [1]

  2. 2

    `a` is a `<letter>`, so `ab` fits `<letter><name>` if `b` is a `<name>` [1]

  3. 3

    `b` is a `<letter>`, so `b` is a `<name>` by the first alternative. Therefore `cab` is valid [1]

5. Evaluating Reverse Polish Notation

In Reverse Polish Notation (RPN) the operator comes after its operands, so `a b -` means a - b. RPN is unambiguous and is read from left to right, with no brackets.

To evaluate it, use a stack. Read each item in turn. An operand is pushed. An operator pops the top two values, applies the operator and pushes the result.

The first value popped is the right-hand operand. When the expression ends, the stack holds one value: the answer.

Examiner insight

“Some candidates were able to explain why Reverse Polish Notation (RPN) is used to carry out expressions, for example, in terms of its unambiguous representation, or that expressions are read from left to right.” “The strongest answers described the RPN process in a general fashion that could be applied to any RPN expression.”

Worked example 14 marks

The RPN expression:

`d a b + * c a - /`

is to be evaluated, where a = 6, b = 12, c = 15 and d = 5.

Show the changing contents of the stack as the RPN expression is evaluated.

  1. 1

    Push `d`, `a`, `b`. Stack (bottom to top): 5, 6, 12 [1]

  2. 2

    `+`: pop 12 and 6, push 18. Stack: 5, 18 [1]

  3. 3

    `*`: pop 18 and 5, push 5 × 18 = 90. Stack: 90 [1]

  4. 4

    Push `c` and `a`: 90, 15, 6. `-` gives 15 - 6 = 9, so 90, 9. `/` gives 90 ÷ 9 = 10. Stack: 10 [1]

6. Converting between infix and RPN

Infix puts the operator between its operands. To convert to RPN, find the order of the operations: brackets first, then `*` and `/`, then `+` and `-`, from left to right.

Write each operation as operand, operand, operator. A finished part then becomes an operand of the next operation. For example, `(2 - 6) * (13 + 7) / 5` becomes `2 6 - 13 7 + * 5 /`.

To convert RPN back to infix, build each result in brackets as it is formed.

Examiner insight

“A common error was for candidates to leave out the brackets in their infix expression, therefore making their solution incorrect.”

Worked example 13 marks

Write the Reverse Polish Notation (RPN) for the given infix expression:

`((6 + 12) / (16 – 10)) * 18`

  1. 1

    `6 12 +` for the bracket (6 + 12) [1]

  2. 2

    `16 10 -` for (16 - 10), then `/` to divide: `6 12 + 16 10 - /` [1]

  3. 3

    Then `18 *`. Final answer: `6 12 + 16 10 - / 18 *` [1]

Recap

  • An interpreter translates and executes one statement at a time and produces no translated version.
  • It stops at an error, which is corrected in real time. A compiler creates an executable file.
  • Lexical analysis converts source code into tokens and removes white space and comments.
  • Syntax analysis checks the tokens against the grammar, reports syntax errors and produces a parse tree.
  • Code generation produces executable form. Optimisation minimises CPU time, memory and execution time.
  • BNF: names in angle brackets, `::=` defines, `|` gives alternatives, a rule using its own name repeats.
  • RPN puts the operator after its operands. Evaluate with a stack: push operands, pop two for an operator, push the result.

End-of-chapter exercise

Test yourself on the whole chapter. Work through these before moving on.

  1. State what is meant by an interpreter executing a program without producing a translated version of it.1 mark
    Answer
    1. It translates and executes the program one statement at a time, and no translated file (executable or object code) is created.

    Trap: Describing a compiler, which translates all the code at the same time, instead of an interpreter.

  2. A programmer is still writing a program. Two sections are not yet written. Explain one reason why an interpreter is the appropriate translator at this stage.2 marks
    Answer
    1. A partly written program can be translated and tested on its own [1].
    2. … so the finished sections can be tested without the missing ones, and if one section does not work the others can still be tested [1].

    Trap: “It makes debugging easier” is too vague. Say how, and tie it to the coding stage.

  3. Name the stage of compilation that converts source code into a sequence of tokens. State one thing it removes from the code.2 marks
    Answer
    1. Lexical analysis [1].
    2. It removes white space (or comments) [1].

    Trap: Syntax analysis works on the tokens afterwards. It does not create them.

  4. A programmer types the line `total ← 5 +` and nothing follows the plus sign. Every symbol in the line is a valid token. Name the compilation stage that finds the error. Explain why that stage and not the earlier one finds it.3 marks
    Answer
    1. Syntax analysis [1].
    2. Each token is valid on its own, so lexical analysis finds nothing wrong [1].
    3. The tokens do not conform to the grammar of the language (an operator needs a second operand), so a syntax error is reported [1].

    Trap: Choosing lexical analysis because the error is “in the symbols”.

  5. Describe the purpose of code generation and the purpose of optimisation in the compilation of a program.3 marks
    Answer
    1. Code generation converts an intermediate representation of the source code into an executable form [1].
    2. Optimisation improves the code [1].
    3. … so that it uses the minimum CPU time, memory or storage, and runs in the shortest time [1].

    Trap: Swapping the two stages. Optimisation does not produce the executable form.

  6. Evaluate the RPN expression `8 2 - 3 *`. Show the changing contents of the stack.3 marks
    Answer
    1. Push 8 and 2. `-`: pop 2 then 8, push 8 - 2 = 6. Stack: 6 [1].
    2. Push 3. Stack: 6, 3 [1].
    3. `*`: pop 3 and 6, push 6 × 3 = 18. Answer: 18 [1].

    Trap: Working out 2 - 8 = -6: the first value popped is the right-hand operand.

  7. Write the RPN for the infix expression `(7 + 5) * 2`.2 marks
    Answer
    1. Bracket first: `7 5 +` [1].
    2. Then multiply by 2: `2 *`, giving `7 5 + 2 *` [1].

    Trap: Writing `7 5 2 * +` ignores the brackets and gives 17.

  8. A product code is one letter from A, B or C followed by one or more digits, each 0 or 1. Write BNF rules that define a `<code>`.4 marks
    Answer
    1. ``` <letter> ::= A|B|C <bit> ::= 0|1 <bits> ::= <bit> | <bit><bits> <code> ::= <letter><bits> ```
    2. `<letter>` with the three alternatives [1].
    3. `<bit>` with the two alternatives [1].
    4. `<bits>` uses its own name, so one or more digits are allowed [1].
    5. `<code>` joins the letter to the digits, letter first [1].

    Trap: Writing `<bit><bit>` only gives a fixed length. A repeating part needs a rule that uses its own name.

  9. A car park checks permit numbers. A permit number is exactly two letters followed by one, two or three digits. The only letters used are P, Q and R. The only digits used are 1, 2, 3 and 4. (a) State whether each permit number is valid or invalid and give a reason for your choice in each case: `QR314`, `P42`, `RP4132`. (b) Write BNF rules that define a `<permit>`. (c) A programmer writes `<digits> ::= <digit> | <digit><digits>`. Give a permit number this rule wrongly accepts and explain why.7 marks
    Answer
    1. (a)
    2. `QR314` is valid: two letters followed by three digits, all from the allowed sets [1].
    3. `P42` is invalid: the second character must be a letter, and 4 is a digit [1].
    4. `RP4132` is invalid: it has four digits and no more than three are allowed [1].
    5. (b)
    6. ``` <letter> ::= P|Q|R <digit> ::= 1|2|3|4 <digits> ::= <digit>|<digit><digit>|<digit><digit><digit> <permit> ::= <letter><letter><digits> ```
    7. `<letter>` and `<digit>` with the allowed values [1].
    8. `<digits>` gives exactly one, two or three digits, with no rule that uses its own name [1].
    9. `<permit>` has exactly two letters, then the digits [1].
    10. (c)
    11. For example `PQ1234` [1]: the rule uses its own name, so it allows any number of digits, not at most three.

    Trap: Using a rule that repeats itself for the digits: it accepts any number of digits, when examiners credited only rules allowing at least one and no more than three.

  10. The RPN expression `p q r - * s + t /` is to be evaluated, where p = 4, q = 3, r = 11, s = 2 and t = 5. (a) Write the expression in infix form. (b) Show the changing contents of the stack as the RPN expression is evaluated.7 marks
    Answer
    1. (a)
    2. `q r -` gives `(q - r)`; with `p *` this is `p * (q - r)` [1].
    3. Adding `s` gives `p * (q - r) + s` [1].
    4. Dividing the whole by `t`: `(p * (q - r) + s) / t`, with brackets [1].
    5. (b)
    6. Push 4, 3, 11. `-`: pop 11 then 3, push 3 - 11 = -8. Stack: 4, -8 [1].
    7. `*`: push 4 × (-8) = -32. Push 2, `+`: push -32 + 2 = -30 [1].
    8. Push 5. `/`: pop 5 then -30, push -30 ÷ 5 = -6 [1].
    9. Answer: -6. Check in infix: (4 × (3 - 11) + 2) ÷ 5 = -30 ÷ 5 = -6 [1].

    Trap: Leaving out the brackets in the infix form, which examiners reported as a common error, or reversing the operands of `-` and `/`.

Say this, not that

Wording that loses the mark, against the wording the mark scheme credits.

Loses the markGets the mark
“an interpreter makes debugging easier”it translates line by line and stops when an error is found, so the error can be corrected in real time
“a compiler is faster”the compiler creates an executable file, so users do not need the translator to run the program
a description of how a compiler works, when asked why to use it for a finished programuse the compiler after the program is complete to create an executable file that can be tested repeatedly without retranslating
`a - b + c * c - a / d` as the infix of `a b - c + c a - * d /``(a - b + c) * (c - a) / d`, with the brackets kept

Frequently asked questions

What is the difference between an interpreter and a compiler?
An interpreter translates and executes a program one statement at a time, and no executable file is made. A compiler translates all the code at the same time and creates an executable file. The interpreter stops when an error is found, so the error can be corrected in real time and the program continues from that point.
What does lexical analysis do during compilation?
Lexical analysis converts the high-level source code into a sequence of tokens. It removes unnecessary white space and comments and creates a symbol table. The tokens are then sent to the parser for syntax analysis.
How does syntax analysis find an error that lexical analysis misses?
Syntax analysis checks that the tokens conform to the rules of the programming language, its grammar. Each token can be valid on its own, so lexical analysis finds nothing wrong. A line such as total with an operator and no second operand breaks the grammar, so syntax analysis reports a syntax error and produces a parse tree.
How do you write a Backus-Naur Form rule for a repeating part?
In BNF each rule defines a name in angle brackets using ::=, and a bar | separates alternatives. A rule that uses its own name allows any number of repeats, so <integer> ::= <digit> | <digit><integer> accepts one or more digits. A rule such as <digit><digit> only allows a fixed length.
How do you evaluate a Reverse Polish Notation expression using a stack?
Read each item in turn, from left to right. An operand is pushed onto the stack. An operator pops the top two values, applies the operator and pushes the result, and the first value popped is the right-hand operand. When the expression ends, the stack holds one value, the answer.
What mistake do students make when converting an infix expression to Reverse Polish Notation?
Students often leave out the brackets, which makes the expression incorrect. Brackets are worked first, so ((6 + 12) / (16 - 10)) * 18 becomes 6 12 + 16 10 - / 18 *. Writing 7 5 2 * + for (7 + 5) * 2 ignores the brackets and gives 17 instead of the correct answer.

Free notes are just the start.

Unlock every Workbook and Chapter at a Glance, and generate your own worksheets and predicted papers.

Explore plans

Related chapters