🔒 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

FeatureCompilerInterpreter
TranslationEntire program translated before executionLine-by-line during execution
OutputStandalone executable file (machine code)No separate file produced
Execution speedFast (no translation at runtime)Slower (translate as you run)
Error reportingAll errors listed after full compilationStops at first error — reports immediately
Source code needed to run?No — executable is independentYes — source must be present
PortabilityExecutable is platform-specificSource is portable (interpreter needed)
Development speedSlower — must compile to testFaster iteration — run immediately
Typical languagesC, C++, Java, Rust, GoPython, 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.

Java: source (.java) → compiler → bytecode (.class) → JVM interprets/JIT compiles → native machine 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
Card 1 of 15
Click to reveal
🎉
Complete!
TermDefinition
← 1.2.4b Addressing Modes 1.2.4 Types of Programming Language Next: 1.2.4d Programming Paradigms →
🔒
Pro Content
Subscribe to access all 69 OCR H446 A Level lessons.
£7.99/month
or £59/year
Subscribe now →