SLIDE 1
CSZone.co.uk
Click to advance · Arrow keys also work
CAIE 9618 · Paper 3 · Topic 3.4.1

Language
Translators

Compiler Stages · Interpreters · Assemblers · Linkers · Loaders

CSZone Cambridge International AS & A Level Computer Science 9618
Compiler vs Interpreter

Translating High-Level Languages

Compiler
Translates entire source code to machine code before execution
Produces a standalone executable — no translator needed to run
All syntax errors reported together after full translation
Resulting code runs faster (already machine code)
Translation takes time — longer build cycle
Used by: C, C++, Rust, Go, Pascal
Interpreter
Translates and executes source code one statement at a time
Translator must be present every time the program runs
Stops at first error — easier to debug interactively
Slower execution — re-translates on every run
Easier to develop and test code iteratively
Used by: Python, JavaScript, Ruby, PHP (traditional)
Compiler Stages

Inside the Compilation Process

A compiler is not a single process — it has multiple ordered phases:
1. Lexical Analysis
Source code tokenised — keywords, identifiers, literals, operators → token stream. Comments and whitespace removed.
2. Syntax Analysis
Token stream checked against grammar rules. Builds parse tree (AST). Syntax errors reported here.
3. Semantic Analysis
Checks meaning: type checking, scope checking, undeclared variables. Symbol table used.
4. Code Generation
Intermediate code or machine code generated from the AST.
5. Code Optimisation
Redundant code removed, loop unrolling, constant folding — without changing functionality.
Assembler, Linker & Loader

From Assembly to Running Program

After compilation, additional tools process the code before execution:
Assembler
Translates assembly language (mnemonics like LDA, STO, ADD) to machine code (binary instruction set). One-to-one correspondence: each assembly instruction becomes one machine instruction. Two-pass assembly: first pass builds symbol table, second pass generates code.
Linker
Combines multiple object files and library files into a single executable. Resolves external references (e.g. function calls to library routines). Static linking: library code copied into executable. Dynamic linking: executable references DLLs at runtime.
Loader
Loads the executable into RAM when the program is run. Adjusts absolute addresses to match actual memory locations (relocation). Passes control to program entry point.
Exam Practice

Cambridge-style questions

Question 1
A student is debugging a newly-written program. Explain why using an interpreter rather than a compiler may be more suitable during the development phase. [3]
1
An interpreter translates and executes one statement at a time — it stops immediately when it encounters an error, displaying which line and statement caused the problem, making errors easier to locate and fix.
1
No build/compile step is needed — the student can run the program immediately after making changes, allowing faster iterative testing.
1
A compiler would report all syntax errors at once after translating the entire program — this is less helpful during early development when the programmer wants to fix one error at a time.
Common Mistakes

Don't lose easy marks

1
Saying "interpreter is always better for debugging" — the question will ask about a specific context. Interpreters are better during development; compilers produce faster executables for distribution. Answer in context.
2
Confusing syntax analysis and semantic analysis — syntax analysis checks the grammar/structure (semicolons, brackets, keyword order). Semantic analysis checks meaning (type mismatches, undeclared identifiers, scope errors). "Using a variable before declaring it" = semantic error, not syntax.
3
Confusing linker and loader — linker combines object files + libraries into an executable (before running). Loader puts the executable into RAM when you run it. They are separate steps: link (create .exe) then load (run .exe).
Topic Summary — 3.4.1

What You Need to Know

COMPILER
All at once → standalone executable. Stages: lexical → syntax → semantic → code gen → optimisation. Faster execution. All errors at once.
INTERPRETER
One statement at a time. Stops at first error. Translator always needed. Slower but better for iterative development.
ASSEMBLER · LINKER · LOADER
Assembler: assembly → machine code, 1:1, two-pass.
Linker: combines object files + libraries → executable (static DLL copy, dynamic DLL at runtime).
Loader: puts executable in RAM, relocates addresses, starts execution.
CSZone

Next Video

3.4.2
VM for Intermediary Code
Bytecode · JIT · Portable Code · Cross-Platform
Head to CSZone.co.uk for the complete worksheet, quiz, and interactive tools