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
Property
Compiler
Interpreter
Assembler
Input
High-level source code
High-level source code
Assembly language
Output
Executable / object code
No output file
Machine code object file
Translation time
Before execution (one-off)
During execution (each run)
Before execution
Execution speed
Fast
Slow
Very fast
Error reporting
After full scan (list of errors)
Stops at first error
After full scan
Source code needed at runtime?
No
Yes
No
Debugging
Harder — errors only after compile
Easier — immediate feedback
Very 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).
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.
The generated code is analysed and improved to run faster or use less memory. Techniques include removing unreachable code, simplifying constant expressions (3+5 → 8 at compile time), and loop unrolling.
During lexical analysis, a symbol table is built and updated. It stores all identifiers encountered in the source code along with their properties:
Name
Type
Scope
Memory address
x
INTEGER
global
0x0A00
count
INTEGER
global
0x0A04
main
FUNCTION
global
0x1000
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!
Term
Definition
🎯
Mini Test — 3.4.1 Language Translators
10 questions · 10 marks · 10 minutes
⏱ 10:00
Section A — Multiple Choice [5 marks]
Q1Which stage of compilation builds the symbol table?
Q2A programmer writes: x = "hello" + 42. This passes syntax analysis. What type of error is it?
Q3What does an assembler translate?
Q4What does the syntax analysis stage produce?
Q5Which is an advantage of an interpreter over a compiler for development?
Section B — Short Answer [5 marks]
Q6List the four main stages of compilation in order.
Mark schemeLexical analysis [1]; Syntax analysis [1]; Semantic analysis [1]; Code generation [1]. Accept code optimisation as a fifth valid stage.
Q7Describe what a token is and give two examples of token types.
Mark schemeA token is a meaningful unit of source code produced during lexical analysis — a classified chunk of text (word, symbol, number) that cannot be broken down further [1]; any two of: keyword (e.g. if, while, for), identifier (variable or function name), operator (+, -, =, >), literal (numeric or string value), punctuation (brackets, semicolons) [1].
Q8Explain why a compiled program runs faster than the same program run using an interpreter.
Mark schemeA compiled program is already translated to native machine code before execution — at runtime the CPU executes binary instructions directly with no translation overhead [1]; an interpreter must translate each line of source code to machine code during execution — every time the program runs, all the translation work is repeated, adding overhead to every statement executed [1].
Q9State two items of information stored in a symbol table.
Mark schemeAny two from: identifier name [1]; data type (e.g. integer, string) [1]; scope (e.g. local, global) [1]; memory address [1]; value (for constants) [1].
Q10A student argues that syntax analysis and semantic analysis could be combined into one stage. Evaluate this claim.
Mark schemeThe claim is not ideal: syntax analysis checks only structural correctness against grammar rules — it builds the parse tree [1]; semantic analysis checks meaning using the parse tree and symbol table — it can only happen after the parse tree is built [1]; combining them is technically possible but less efficient — a statement can be syntactically correct and require full semantic analysis even if it would fail; keeping them separate allows cleaner error diagnosis (syntax vs. semantic error), and semantic analysis depends on the output (parse tree) of syntax analysis [1].