A2 · Practice questions · The stages of compilation

Stage by stage.

Six original Cambridge-style questions on translation software and the stages of compilation, with mark-scheme solutions.

Original questions All questions on this page are original work, written in the Cambridge AS & A Level style. They are not from past papers. They test the same concepts and skills the syllabus rewards.
Keep these straight

Four stages, one table.

01
Explain
[3 marks]

Explain how an interpreter can execute a program without producing a translated version of it.

  • The interpreter reads and analyses one statement at a time (lexical and syntax analysis). ✓
  • It then executes the statement by calling its own pre-written machine code routines that carry out that type of statement. ✓
  • No object code / executable file is produced; each statement is analysed again every time it is executed. ✓
02
Explain
[4 marks]

Describe what happens during the lexical analysis stage of compilation.

  • White space and comments are removed. ✓
  • The source code is split into tokens (keywords, identifiers, operators, constants). ✓
  • Keywords are looked up in a keyword table and replaced by tokens. ✓
  • Identifiers and constants are entered into the symbol table; illegal characters / identifiers are reported as errors. ✓
03
Explain
[3 marks]

Describe what happens during the syntax analysis stage of compilation.

  • The sequence of tokens is checked against the grammar rules of the language (e.g. defined in BNF). ✓
  • A parse tree is built for each valid statement. ✓
  • Syntax errors such as a missing keyword or bracket are reported (as a list of all errors). ✓
04
Apply
[3 marks]

State the stage of compilation at which each error would be detected: (a) the identifier Cost# contains a character that is not allowed (b) a FOR loop has no NEXT (c) the statement Total ← * 3 has two operators together.

  • (a) Lexical analysis. ✓
  • (b) Syntax analysis. ✓
  • (c) Syntax analysis. ✓
05
Explain
[3 marks]

Explain the purpose of the optimisation stage and give two examples of optimisation.

  • Purpose: to make the object code execute in less time and/or use less memory, without changing what the program does. ✓
  • Example: remove redundant instructions, such as reloading a value already in the accumulator. ✓
  • Example: move a calculation that does not change out of a loop / evaluate constant expressions at compile time / remove code that can never be executed. ✓
06
Analysis
[2 marks]

The symbol table is used by more than one stage of compilation. Explain how it is used by lexical analysis and by code generation.

  • Lexical analysis adds each new identifier and constant to the symbol table (and replaces it in the token stream with a reference to its entry). ✓
  • Code generation uses the symbol table to find the data type of each identifier and to allocate / look up its memory address for the object code. ✓

Mark this once you have attempted all six and checked your working. It records a Practiced badge on the topic and adds a one-time bonus. Revealing the solutions alone does not count.