🔒 Pro · Component 1 · 1.2.4 Types of Programming Language
1.2.4c Translators: Compilers, Interpreters and Assemblers
OCR H446 · A Level Computer Science · ~11 min read
Notes
—
Video
—
Slides
—
Worksheet
—
Quiz
What is a Translator?
A translator is a program that converts source code written in one language into another form (typically machine code or an intermediate form) so it can be executed by the CPU. There are three main types relevant to OCR H446: compilers, interpreters, and assemblers.
The Assembler
An assembler translates assembly language into machine code (binary). Each assembly instruction maps one-to-one to a machine code instruction. The assembler works in two passes:
First pass: scans the entire code to build a symbol table — a mapping of all label names to their memory addresses. This resolves forward references (labels used before they are defined).
Second pass: converts each mnemonic to its binary opcode and replaces label names with their actual binary addresses from the symbol table.
Output: an object file containing machine code, ready to be linked and executed.
The Compiler
A compiler translates an entire high-level language source program into machine code (or an intermediate language) all at once, before the program runs. The output is a standalone executable file.
Stages of Compilation
Lexical analysis: the source code is broken into tokens (individual meaningful units: keywords, identifiers, operators, literals). Comments and whitespace are removed. A symbol table is built recording variable names, types, and memory locations.
Syntax analysis (parsing): tokens are checked against the grammar rules of the language. A parse tree (or abstract syntax tree) is built representing the program's structure. Syntax errors are reported.
Semantic analysis: the parse tree is checked for meaning — e.g. type checking (can you add an integer to a string?), undeclared variables, incorrect function call arguments. Semantic errors are reported.
Code generation: machine code (or intermediate code) is generated from the syntax tree.
Code optimisation: the generated code is analysed and improved for efficiency — removing redundant instructions, optimising loops, reducing memory usage. Happens before or after code generation.
Advantages of a Compiler
The compiled executable runs very fast — no translation overhead at runtime.
The executable can be distributed without the source code — protects intellectual property.
All errors are found before the program runs — a complete list of errors is produced.
Code optimisation can significantly improve performance.
The compiled file can run on any compatible machine without needing the compiler installed.
Disadvantages of a Compiler
The compilation process takes time — development is slower because the programmer must compile before running.
The compiled executable is platform-specific — must recompile for each target OS/architecture.
Errors are only found after compilation of the whole program — large programs may take time to compile.
Debugging can be harder — the error messages relate to compiled code, not always the original line.
The Interpreter
An interpreter translates and executes high-level language source code line by line (or statement by statement) at runtime. Unlike a compiler, it does not produce a separate machine code file — it translates and immediately executes each statement, then moves to the next.
Advantages of an Interpreter
Errors are reported immediately at the line where they occur — easier for debugging during development.
No separate compilation step — faster to test and iterate on small programs during development.
Programs are portable — the same source code runs on any platform that has the interpreter installed.
Good for scripting, REPL environments (Read-Eval-Print Loop), and interactive development.
Disadvantages of an Interpreter
Slower execution — translation happens at runtime; each line must be re-translated every time it is executed (e.g. inside loops).
The source code must be present to run the program — intellectual property is exposed.
An interpreter must be installed on the target machine.
Errors are found only when the interpreter reaches the line — errors in rarely-executed branches may not be caught during testing.
Compiler vs Interpreter: Full Comparison
Feature
Compiler
Interpreter
Translation
Entire program translated before execution
Line-by-line during execution
Output
Standalone executable file (machine code)
No separate file produced
Execution speed
Fast (no translation at runtime)
Slower (translate as you run)
Error reporting
All errors listed after full compilation
Stops at first error — reports immediately
Source code needed to run?
No — executable is independent
Yes — source must be present
Portability
Executable is platform-specific
Source is portable (interpreter needed)
Development speed
Slower — must compile to test
Faster iteration — run immediately
Typical languages
C, C++, Java, Rust, Go
Python, JavaScript, Ruby, Bash
Just-In-Time (JIT) Compilation
Some modern language environments (e.g. Java's JVM, Python's PyPy, JavaScript V8 engine) use a hybrid approach called Just-In-Time (JIT) compilation:
The source code is first compiled to an intermediate bytecode (not full machine code).
At runtime, a JIT compiler identifies hot paths (frequently executed code) and compiles them to native machine code during execution.
This combines the portability of interpretation with the speed of compilation for frequently-run code.
Exam tip: Know the stages of compilation (lexical analysis, syntax analysis, semantic analysis, code generation, optimisation). Know the differences between compiler and interpreter clearly — specifically: compiler translates all at once before execution; interpreter translates line by line at execution time.
Exam tip: JIT compilation is relevant for OCR H446. Remember: intermediate bytecode → JVM/JIT → native code. The JIT identifies frequently-executed code (hot paths) and compiles them to machine code for speed.
⚠ Common Mistakes
Saying an interpreter produces an executable — it does not. Only a compiler produces a separate machine code file.
Saying a compiler is always better — interpreters are preferred for development/debugging, scripting, and portability. Each has advantages.
Confusing lexical and syntax analysis — lexical analysis breaks source code into tokens and removes whitespace/comments. Syntax analysis checks the grammar/structure using those tokens.
Forgetting that interpreted programs need the source code present at runtime — compiled programs do not (the binary is self-contained).
✓ Notes completed!
▶
Video coming soon
Click to advance · Arrow keys also work
Click slide or press arrow keys to navigate
✍
Worksheet — 1.2.4c Translators
8 questions · 20 marks · instantly marked
Q1Explain the difference between a compiler and an interpreter. In your answer, refer to when translation takes place and whether an output file is produced.[4 marks]
✓ Mark scheme
A compiler translates the entire source program into machine code before execution [1]; it produces a standalone executable file that can be run independently of the compiler [1]. An interpreter translates and executes the source code line-by-line at runtime [1]; it does not produce a separate machine code file — the source code must be present every time the program is run [1].
Q2Describe the stages of compilation in order. For each stage, state what the output is.[5 marks]
✓ Mark scheme
Lexical analysis: source code is broken into tokens; comments/whitespace removed; symbol table built [1]. Syntax analysis: tokens are checked against grammar rules; parse tree/abstract syntax tree is produced; syntax errors reported [1]. Semantic analysis: parse tree is checked for meaning — type checking, undeclared variables, parameter mismatches; semantic errors reported [1]. Code generation: machine code (or intermediate code) is produced from the syntax tree [1]. Code optimisation: the generated code is improved for efficiency — removing redundant instructions, optimising loops [1].
Q3Give two advantages of using a compiler over an interpreter for a finished commercial application.[2 marks]
✓ Mark scheme
Any two: the compiled executable runs faster — no translation overhead at runtime [1]; the source code does not need to be distributed — only the binary executable, protecting intellectual property [1]; the compiled executable can be distributed to users who do not have the compiler installed [1]; code optimisation can significantly improve performance of the final executable [1].
Q4Give two advantages of using an interpreter over a compiler during the development stage of a program.[2 marks]
✓ Mark scheme
Any two: errors are reported immediately at the exact line where they occur — faster debugging [1]; no compilation step needed — programmers can run and test code immediately, speeding up the development cycle [1]; interactive REPL environments allow line-by-line testing [1].
Q5Explain what lexical analysis does in the compilation process. What is a token and what is a symbol table?[3 marks]
✓ Mark scheme
Lexical analysis breaks the source code into tokens — the smallest meaningful units of the language (keywords, identifiers, operators, literals, brackets, semicolons, etc.); comments and whitespace are removed [1]; a token is a categorised unit of the source code, e.g. the token type KEYWORD with value 'if', or IDENTIFIER with value 'totalCost' [1]; a symbol table is built during lexical analysis recording all identifier names, their type (variable, function), data type, and memory location — referenced throughout later compilation stages [1].
Q6What is Just-In-Time (JIT) compilation? Describe how Java uses JIT as part of its execution model.[3 marks]
✓ Mark scheme
JIT compilation is a hybrid approach where source code is first compiled to an intermediate bytecode; at runtime, frequently executed sections (hot paths) are compiled to native machine code by the JIT compiler, improving execution speed [1]; in Java: source code (.java) is compiled by the Java compiler to bytecode (.class files) — a portable intermediate form [1]; at runtime, the JVM uses a JIT compiler to identify frequently-executed bytecode and compile it to native machine code for the current platform, combining portability (bytecode runs on any JVM) with near-native speed for hot paths [1].
Q7Why is an interpreted program generally slower than an equivalent compiled program?[2 marks]
✓ Mark scheme
An interpreter must translate each line of source code to machine code at the time it is executed [1]; if a line appears inside a loop, it is translated on every iteration — there is no caching of translated code. A compiled program is pre-translated to machine code, so at runtime the CPU executes machine code directly with no translation overhead [1].
Q8Why is an interpreter preferred over a compiler for certain use cases, such as web scripting and educational programming environments?[2 marks]
✓ Mark scheme
Interpreters allow programs to run on any platform that has the interpreter — JavaScript runs in any browser regardless of the OS; there is no compilation step, so code can be written, tested, and fixed quickly in an interactive REPL, ideal for beginners learning to code [1]; errors are reported immediately at the line where they occur, making it easier for learners to identify and fix mistakes step-by-step without having to compile the entire program first [1].
Topic Quiz
1 of 15
You scored
out of 15
🎯
Mini Test — 1.2.4c Translators
10 questions · 10 marks · 10 minutes
5 MCQ + 5 short answer
⏱10:00
10 marks
Section A — Multiple Choice
Q1A compiler differs from an interpreter in that a compiler:
Q2During compilation, which stage breaks source code into tokens and removes whitespace and comments?
Q3Which is an advantage of an interpreter over a compiler during the development phase?
Q4What does semantic analysis check during compilation?
Q5In Java's execution model using JIT compilation, bytecode is first produced by:
Section B — Short Answer
Q6State one advantage and one disadvantage of a compiler compared to an interpreter.
Mark schemeAdvantage: compiled programs run faster — no translation overhead at runtime. Disadvantage: compilation takes time before the program can be run; platform-specific executable (must recompile for different OS/architecture); errors only reported after full compilation. [1 mark]
Q7What is a symbol table and when is it built?
Mark schemeA symbol table is a data structure built during lexical analysis that records all identifiers (variables, functions, labels) with their name, type, and memory location. It is referenced throughout the compilation process for type checking and code generation. [1 mark]
Q8Why is an interpreted program slower to execute than a compiled one, especially inside loops?
Mark schemeAn interpreter translates each line to machine code at the time of execution. Inside a loop, the same line is translated on every iteration — there is no caching of the translated instruction. A compiled program is already machine code, so no translation overhead exists at runtime. [1 mark]
Q9What is the purpose of the syntax analysis stage of compilation?
Mark schemeSyntax analysis checks that the sequence of tokens produced by lexical analysis follows the grammar rules of the programming language. It builds a parse tree (abstract syntax tree) representing the program structure and reports any syntax errors (e.g. missing brackets, incorrect statement structure). [1 mark]
Q10What does the code optimisation stage of compilation achieve?
Mark schemeCode optimisation improves the generated machine code for efficiency — reducing the number of instructions, removing redundant computations, optimising loop execution, and reducing memory usage — without changing the program's observable behaviour. [1 mark]