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
Lexical analysis Source code is scanned; tokens are created (keywords, identifiers, operators, literals); comments and whitespace removed
Syntax analysis (parsing) Tokens are checked against the grammar of the language; a parse tree (abstract syntax tree) is built; syntax errors reported
Semantic analysis Checks meaning: type checking, undeclared variables, type mismatches, scope rules
Code generation Intermediate or target machine code is generated from the AST
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
Feature
Assembler
Compiler
Interpreter
Input language
Assembly
High-level
High-level
Output
Machine code file
Machine code file
No file — executes directly
Translation timing
Before execution
Before execution
During execution
Execution speed
Fast (machine code)
Fast (machine code)
Slower (translates each run)
Debugging
Difficult
All errors upfront
Stops at error line
Source code at runtime
Not needed
Not needed
Required
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]
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!
Term
Definition
🎯
Mini Test — 1.5.3 Translators
10 questions · 10 marks · 10 minutes
⏱ 10:00
Section A — Multiple Choice [5 marks]
Q1Which translator converts assembly language to machine code?
Q2Which is the FIRST stage of compilation?
Q3An interpreter executes source code:
Q4Which stage of compilation checks for type mismatches and undeclared variables?
Q5Compiled programs run faster than interpreted programs because:
Section B — Short Answer [5 marks]
Q6State one advantage of using an interpreter during program development.
Mark schemeEasier to debug — the interpreter stops at the line where an error occurs [1]; OR code can be run and tested immediately without a separate compilation step [1].
Q7Explain what tokens are and name two types of token created during lexical analysis.
Mark schemeTokens are the smallest meaningful units of source code [1]; any two from: keywords (reserved words), identifiers (variable/procedure names), operators (+, -, =), literals (constant values), delimiters (punctuation) [1 each, max 2].
Q8Explain why assembly language has a 1-to-1 relationship with machine code.
Mark schemeEach assembly mnemonic (e.g., LDD, ADD) corresponds exactly to one machine code instruction [1]; the assembler simply replaces the mnemonic with its binary opcode and resolves addresses — no complex translation is needed [1].
Q9State one advantage of a compiler for distributing software commercially.
Mark schemeThe compiled machine code can be distributed without the source code [1]; this protects the programmer's intellectual property since the source code is not accessible to end users [1].
Q10Describe what code optimisation achieves during compilation.
Mark schemeCode optimisation improves the generated machine code for speed and/or size [1]; examples: removing redundant calculations, loop unrolling, dead code elimination — making the program run faster or use less memory [1].