Pro Lesson
This lesson is part of the Cambridge 9618 Pro track. Upgrade to access all 82 lessons, worksheets, quizzes and mini tests.
Upgrade to Pro →
💻 Paper 1 · 1.5 System Software
1.5.3 Translators: Compilers, Interpreters and Assemblers
Cambridge 9618 · International A Level Computer Science · ~13 min read
Notes
Video
Slides
Quiz
Worksheet

Why Translators are Needed

Computers can only execute machine code — binary instructions specific to the CPU's instruction set. Programmers write code in high-level languages (Python, Java, C++) or assembly language, which are human-readable but not directly executable. Translators convert source code into machine code (or an intermediate form).

Assembler
Assembly language
Machine code
Compiler
High-level source code
Machine code object file
Interpreter
High-level source code
Executes line-by-line

Assembler

An assembler translates assembly language into machine code. Assembly language uses mnemonics (e.g., LDD, ADD, CMP) that are a 1-to-1 mapping to machine code instructions. The assembler replaces each mnemonic with its binary opcode and resolves symbolic addresses and labels into actual memory addresses.

  • One assembly instruction → one machine code instruction (1-to-1 relationship)
  • Produces an object file of machine code
  • Assembly language is processor-specific — code written for one CPU won't run on a different architecture

Compiler

A compiler translates an entire high-level language program into machine code in one go (before execution). The resulting machine code (object file) can then be run directly by the CPU, without the compiler being present.

Stages of Compilation

  1. Lexical analysis Source code is scanned; tokens are created (keywords, identifiers, operators, literals); comments and whitespace removed
  2. Syntax analysis (parsing) Tokens are checked against the grammar of the language; a parse tree (abstract syntax tree) is built; syntax errors reported
  3. Semantic analysis Checks meaning: type checking, undeclared variables, type mismatches, scope rules
  4. Code generation Intermediate or target machine code is generated from the AST
  5. Code optimisation Generated code is improved for speed or size (e.g., removing redundant calculations, loop optimisation)

Interpreter

An interpreter translates and executes source code one statement at a time. It does not produce a separate machine code output file — each line is decoded and run immediately. If an error occurs, execution stops at that line.

✅ Compiler advantages
  • Faster execution — code already compiled to machine code
  • Source code is not needed at runtime (distributes compiled executable)
  • Optimisation possible during compilation
  • All errors reported before any code runs
✅ Interpreter advantages
  • Easier to debug — stops at exact error line
  • Allows interactive testing — run code immediately without a separate compile step
  • Portable — same source code runs on any platform with the interpreter
  • Better for development and scripting

Comparison Table

FeatureAssemblerCompilerInterpreter
Input languageAssemblyHigh-levelHigh-level
OutputMachine code fileMachine code fileNo file — executes directly
Translation timingBefore executionBefore executionDuring execution
Execution speedFast (machine code)Fast (machine code)Slower (translates each run)
DebuggingDifficultAll errors upfrontStops at error line
Source code at runtimeNot neededNot neededRequired

Tokens (Lexical Analysis)

During lexical analysis, the compiler identifies and classifies tokens: the smallest meaningful units of the source code:

  • Keywords: reserved words (e.g., IF, FOR, WHILE)
  • Identifiers: variable and procedure names
  • Operators: +, -, *, /, =, <, >
  • Literals: constant values (e.g., 42, "hello")
  • Delimiters: punctuation (e.g., ;, (, ))
Exam tip: Cambridge questions often ask you to compare compiler and interpreter, or describe the stages of compilation. For comparison questions: compiler = faster execution, no source code at runtime, all errors upfront; interpreter = easier debugging, portable, slower. For compilation stages, the five key stages are lexical analysis, syntax analysis, semantic analysis, code generation, optimisation.
⚠️ Common Mistakes
  • Saying compilers are "better" — they're better for deployment (speed, no source code needed); interpreters are better for development (easier debugging)
  • Confusing assembler with compiler — assembler works with assembly language (low-level, 1-to-1 mapping); compiler works with high-level languages (many-to-1 mapping)
  • Saying interpreters produce an object file — they don't; they execute line by line without creating a separate output file
  • Missing "semantic analysis" from compilation stages — common to list only 4 of the 5 stages; semantic checks meaning (types, scope), not just syntax
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 1.5.3 Translators

7 questions · instantly marked · Cambridge 9618 standard

Q1Explain why translators are needed for high-level language programs.[2]
✅ Mark scheme
The CPU can only execute machine code (binary) [1]; high-level language programs must be translated into machine code before or during execution [1].
Q2Describe the difference between an assembler and a compiler.[4]
✅ Mark scheme
Assembler translates assembly language into machine code; there is a 1-to-1 relationship between assembly mnemonics and machine code instructions [1+1]; Compiler translates a high-level language program (many statements) into machine code; many source statements → many machine code instructions [1]; both produce machine code output files that can be executed without the translator present [1].
Q3List five stages of compilation in the correct order.[5]
✅ Mark scheme
1. Lexical analysis [1]; 2. Syntax analysis (parsing) [1]; 3. Semantic analysis [1]; 4. Code generation [1]; 5. Code optimisation [1].
Q4Describe what happens during lexical analysis and state what a token is.[3]
✅ Mark scheme
Lexical analysis scans the source code character by character and identifies tokens [1]; tokens are the smallest meaningful units: keywords, identifiers, operators, literals, delimiters [1]; comments and whitespace are removed [1].
Q5Give two advantages of a compiler over an interpreter, and two advantages of an interpreter over a compiler.[4]
✅ Mark scheme
Compiler advantages (any 2): faster execution of compiled code; source code not needed at runtime; all errors reported upfront; code can be optimised [1 each]; Interpreter advantages (any 2): easier to debug — stops at exact error line; code can be run immediately without a compile step; portable — same source runs on any platform with the interpreter [1 each].
Q6A student claims that an interpreter "compiles code faster". Explain why this claim is incorrect.[3]
✅ Mark scheme
An interpreter does not compile — it translates and executes source code line-by-line during runtime without producing a separate object file [1]; compiled programs run faster than interpreted ones because the machine code is already prepared before execution [1]; interpreters retranslate code every time the program runs, making execution slower overall [1].
Q7Explain what semantic analysis checks during compilation, and why it is a separate stage from syntax analysis.[3]
✅ Mark scheme
Semantic analysis checks the meaning of the code [1]; examples: type checking (e.g., assigning a string to an integer variable), use of undeclared variables, incorrect number of function arguments [1]; it is separate from syntax analysis because code can be syntactically correct (grammatically valid) but semantically invalid — e.g., adding an integer to a boolean is syntactically valid but may be semantically wrong [1].
Q8Compare compilers and interpreters. State one advantage of using an interpreter during software development, one advantage of distributing compiled code to end users, and explain why compiled programs typically execute faster than interpreted programs.[5]
✅ Mark scheme
Interpreter advantage during development: errors reported immediately line by line, making debugging faster without a full compilation step — 1 mark; compiler advantage for distribution: compiled executable can run without the interpreter being installed on the user's machine — 1 mark; additionally, compiled code can be distributed without exposing source code — 1 mark; compiled programs are faster because translation to machine code is done once in advance, whereas an interpreter translates each instruction at runtime — 1 mark; (additional valid comparison) — 1 mark.
Topic Quiz
Question 1 of 12
You scored
out of 12
Card 1 of 9
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 1.5.3 Translators

10 questions · 10 marks · 10 minutes

← 1.5.2 Utility Software
28 of 82 · Cambridge 9618
1.5.4 High vs Low Level →