📘 Paper 3 · 3.4 Translation
3.4.1 Language Translators & Compilation
Cambridge 9618 · International A Level Computer Science · ~17 min read
Notes
Video
Slides
Quiz
Worksheet

Types of Language Translator

Source code written by programmers cannot run directly on a computer — it must be translated to machine code. There are three types of translator:

🔧 Compiler
  • Translates whole program at once
  • Produces a standalone executable
  • Fast execution — no translation at runtime
  • Source code not needed after compilation
  • Errors reported as a list after full scan
  • Examples: GCC (C/C++), javac (Java)
▶️ Interpreter
  • Translates and executes line by line
  • No executable produced — re-translates each run
  • Slower — translation overhead every execution
  • Source code needed every time program runs
  • Errors reported immediately at failing line
  • Examples: Python (CPython), early BASIC
⚙️ Assembler
  • Translates assembly language → machine code
  • Near one-to-one mapping: mnemonic → opcode
  • Very fast translation — simple substitution
  • Output is machine-specific object code
  • No control flow analysis needed
  • Examples: NASM, MASM, GNU as
PropertyCompilerInterpreterAssembler
InputHigh-level source codeHigh-level source codeAssembly language
OutputExecutable / object codeNo output fileMachine code object file
Translation timeBefore execution (one-off)During execution (each run)Before execution
Execution speedFastSlowVery fast
Error reportingAfter full scan (list of errors)Stops at first errorAfter full scan
Source code needed at runtime?NoYesNo
DebuggingHarder — errors only after compileEasier — immediate feedbackVery hard

Stages of Compilation

A compiler translates source code through several distinct stages. Each stage transforms the code into a different representation, getting closer to machine code. For Cambridge 9618 you need to know: lexical analysis, syntax analysis, semantic analysis, and code generation.

1
Lexical Analysis (Tokenisation)
Source code is read character by character. Whitespace and comments are removed. Meaningful units called tokens are identified — each token has a type (keyword, identifier, operator, literal, punctuation) and a value.

A symbol table is built — a data structure storing identifiers (variable names, function names) with their attributes (type, scope, memory address).
Source: x = 3 + 5
Tokens: [IDENTIFIER:x] [OP:=] [INT:3] [OP:+] [INT:5]
2
Syntax Analysis (Parsing)
Tokens are checked against the language's grammar rules. An Abstract Syntax Tree (AST) or parse tree is built representing the hierarchical structure of the program.

Syntax errors are detected here — e.g. a missing bracket, a malformed statement. This stage checks form/structure but not meaning.
AST for x = 3 + 5:
ASSIGN → [x] [ADD → [3] [5]]
3
Semantic Analysis
The AST is checked for meaning — it is possible to be syntactically correct but semantically wrong. Checks include:
  • Type checking: are operands compatible? (e.g. adding a string to an integer)
  • Undeclared variables: is x declared before use?
  • Scope: is a variable accessible in this context?
  • Function calls: correct number and type of arguments?
x = "hello" + 5 → passes syntax but fails semantic (type error)
4
Code Generation
The AST is traversed and translated to target machine code (or an intermediate code like bytecode). The compiler selects appropriate CPU instructions, allocates registers and memory addresses from the symbol table, and produces executable binary output.
x = 3 + 5 →
MOV R1, #3 ; MOV R2, #5 ; ADD R3, R1, R2 ; STR R3, [x]
5
Code Optimisation (sometimes included)
The generated code is analysed and improved to run faster or use less memory. Techniques include removing unreachable code, simplifying constant expressions (3+58 at compile time), and loop unrolling.
x = 3 + 5 → compiler directly generates: MOV x, #8

Lexical Analysis — Token Types

The lexical analyser (lexer) converts raw source text into a stream of tokens. Each token is classified:

Lexical analysis example
Source code:   if (x > 10) { count = count + 1; }
↓ Tokenised into:
if · KEYWORD ( · PUNCTUATION x · IDENTIFIER > · OPERATOR 10 · LITERAL ) · PUNCTUATION { · PUNCTUATION count · IDENTIFIER = · OPERATOR count · IDENTIFIER + · OPERATOR 1 · LITERAL ; · PUNCTUATION } · PUNCTUATION

Symbol Table

During lexical analysis, a symbol table is built and updated. It stores all identifiers encountered in the source code along with their properties:

NameTypeScopeMemory address
xINTEGERglobal0x0A00
countINTEGERglobal0x0A04
mainFUNCTIONglobal0x1000

The symbol table is consulted throughout compilation — semantic analysis uses it for type checking and scope resolution; code generation uses it for memory addresses.

Assembler — Assembly to Machine Code

Assembly language uses mnemonics — short human-readable codes representing machine instructions. An assembler performs a near one-to-one translation of mnemonics to binary opcodes.

; Assembly language (LMC example) LDA 05 010 00101 (Load from address 05) ADD 06 001 00110 (Add from address 06) STA 07 011 00111 (Store to address 07) HLT 000 00000 (Halt)
Cambridge 9618 exam tip: Know ALL four compilation stages clearly: (1) Lexical analysis — tokenisation, removes whitespace/comments, builds symbol table; (2) Syntax analysis — checks grammar, builds parse tree/AST, detects syntax errors; (3) Semantic analysis — checks meaning: type compatibility, undeclared variables, scope, argument counts; (4) Code generation — translates AST to machine code using symbol table. For translator comparison: compiler = whole program → executable, fast runtime, no source needed; interpreter = line by line, re-translates every run, slower, easier to debug; assembler = assembly → machine code, near one-to-one. Know that syntax analysis checks structure but NOT meaning (semantic errors need a separate stage).
⚠️ Common Mistakes
  • Saying syntax analysis checks for type errors — type errors are semantic errors; syntax analysis only checks grammar/structure (e.g. brackets match)
  • Confusing lexical errors and syntax errors — a lexical error is an invalid character/token (like £ in code); a syntax error is a valid token in the wrong position (like missing bracket)
  • Saying an interpreter is faster than a compiler — interpreters are SLOWER at runtime; they must re-translate code every execution whereas compiled code just runs
  • Saying assemblers handle high-level languages — assemblers only translate assembly language (mnemonics) to machine code; compilers/interpreters handle high-level languages
  • Forgetting the symbol table — it is built during lexical analysis and used throughout all later stages; forgetting it loses marks in "describe lexical analysis" questions
✅ Notes completed!
Video coming soon
Click slide or press arrow keys to navigate

Worksheet — 3.4.1 Language Translators

8 questions · Cambridge 9618 standard

Q1State two differences between a compiler and an interpreter.[2]
✅ Mark scheme
Any two from: a compiler translates the whole program at once; an interpreter translates line by line [1]; a compiler produces an executable that can be run without the source code; an interpreter requires source code every time it runs [1]; a compiled program runs faster at runtime; an interpreted program runs slower as translation happens each execution [1]; a compiler reports all errors after a complete scan; an interpreter stops at the first error encountered [1].
Q2Describe what happens during the lexical analysis stage of compilation. Include the role of the symbol table.[4]
✅ Mark scheme
Source code is read character by character and whitespace and comments are removed [1]; meaningful units called tokens are identified and classified by type (keyword, identifier, operator, literal, punctuation) [1]; a symbol table is created — a data structure that records all identifiers (variable names, function names) encountered in the source code [1]; the symbol table stores information about each identifier including its type, scope, and memory address — it is used by later stages such as semantic analysis and code generation [1].
Q3Explain why syntax analysis and semantic analysis are separate stages. Give an example of an error caught at each stage.[4]
✅ Mark scheme
Syntax analysis checks whether the structure of the code conforms to the grammar rules of the language — it builds a parse tree but does not check meaning [1]; example of syntax error: missing closing bracket, such as if (x > 5 { [1]; semantic analysis checks the meaning of the syntactically valid code — it uses the parse tree and symbol table to verify meaning and correctness [1]; example of semantic error: using a variable before declaring it, or adding an integer to a string (type mismatch) [1]. Code can be syntactically correct but semantically invalid — the stages must be separate to correctly identify which kind of error has occurred.
Q4State two checks performed during semantic analysis.[2]
✅ Mark scheme
Any two from: type checking — checking that operands and operations are compatible (e.g. not adding a string to an integer) [1]; checking that variables are declared before use [1]; checking that variable references are in scope [1]; checking that function calls pass the correct number and type of arguments [1].
Q5A software company develops a cross-platform application. Suggest and justify whether they should use a compiled or interpreted language.[3]
✅ Mark scheme
Interpreted language [1]; an interpreter can run the same source code on any platform that has the interpreter installed — the source code does not need to be recompiled for each operating system [1]; compiled programs produce platform-specific executables that must be separately compiled for each target OS/architecture [1]. Accept: use a language that compiles to portable bytecode (like Java/JVM) with valid justification about write-once run-anywhere.
Q6Explain the role of an assembler and how it differs from a compiler.[3]
✅ Mark scheme
An assembler translates assembly language (mnemonics) into machine code (binary) [1]; the translation is near one-to-one — each assembly mnemonic corresponds to one or a very small number of machine code instructions (no complex analysis needed) [1]; a compiler translates high-level language (with complex syntax, control structures, and abstractions) into machine code — this requires multiple stages including lexical analysis, syntax analysis, semantic analysis, and code generation; an assembler requires far less processing as assembly language is already very close to machine code [1].
Q7Describe the role of the lexical analyser and the parser in the compilation process. State what a token is, give two examples of tokens, and explain what a syntax error is and at which compilation stage it is detected.[5]
✅ Mark scheme
Lexical analyser: reads source code character by character and groups characters into tokens; removes whitespace, comments, and produces a token stream [1]; a token is a classified unit of source code e.g. keyword, identifier, operator, literal [1]; examples: keyword = IF / WHILE, identifier = variableName, operator = +, literal = 42 (any two valid examples) [1]; Parser: takes the token stream and checks it conforms to the grammar (syntax rules) of the language, typically building a parse tree or abstract syntax tree [1]; syntax error: a construct that violates the grammar rules (e.g. missing semicolon, unmatched bracket) — detected during parsing [1].
Q8Explain the difference between a compiler and an interpreter. State two advantages of compilation over interpretation for a commercial software product distributed to end users, and one advantage of interpretation that makes it useful during software development.[5]
✅ Mark scheme
Compiler: translates entire source code to machine code (or object code) in one pass; the resulting executable runs without needing the compiler again [1]; Interpreter: translates and executes source code line by line at runtime; the interpreter must be present every time the program runs [1]; Advantage 1 of compilation: faster execution — machine code runs directly on the CPU with no translation overhead at runtime [1]; Advantage 2 of compilation: source code protection — the compiled binary does not expose the original source code to the end user [1]; Advantage of interpretation for development: immediate error feedback — the interpreter executes and reports errors line by line without requiring a full compile cycle, speeding up the debug-edit cycle [1].
Topic Quiz
Question 1 of 10
You scored
out of 10
Card 1 of 10
Click to reveal definition
🎉
All cards reviewed!
TermDefinition
🎯

Mini Test — 3.4.1 Language Translators

10 questions · 10 marks · 10 minutes

← 3.3.2 Virtual Machines
60 of 82 · Cambridge 9618
3.4.2 VM for Intermediary Code →