SLIDE 1 / 11
CSZone.co.uk
OCR H446 · Component 1 · 1.2.4

Translators: Compilers,
Interpreters & Assemblers

OCR A Level Computer Science · cszone.co.uk
H446 SpecA Level
Learning Objectives

By the end of this topic you will be able to:

Explain the role and process of a compiler, interpreter and assembler
Compare compilers and interpreters in terms of error reporting, speed and portability
Describe the stages of compilation (lexical analysis, syntax analysis, code generation, optimisation)
Explain the advantages of bytecode / intermediate code and virtual machines
Compiler

Compiler

A compiler translates the entire source program into machine code in one go, producing a standalone executable. The source code is not needed at run time.
Fast execution: the compiled machine code runs directly without any translator overhead. Once compiled, the program runs at full hardware speed.
All errors reported together: the compiler scans the whole program and reports all errors before producing output. This can make debugging harder as there may be many errors listed at once.
Platform-specific: compiled code targets a specific CPU architecture and OS. Code compiled for Windows x86-64 won't run on ARM without recompilation.
Examples: C, C++, Go, Rust — all use compilers. GCC, Clang, javac (to bytecode).
Interpreter

Interpreter

An interpreter translates and executes the source program line by line at run time. No standalone executable is produced; the interpreter must be present to run the program.
Easier debugging: errors are reported immediately when the offending line is reached during execution. Execution halts at the first error, making it easy to locate the problem.
Slower execution: each line is re-translated every time it executes (e.g. a loop body is re-interpreted on every iteration), creating significant overhead.
Portable: the source code runs on any platform that has an interpreter for the language. Useful during development and scripting.
Examples: Python (CPython), JavaScript in the browser, early BASIC.
Stages of Compilation

Stages of Compilation

1. Lexical analysis: source code is tokenised — keywords, identifiers, operators and literals are identified and converted into tokens. Whitespace and comments are removed.
2. Syntax analysis (parsing): tokens are checked against the grammar of the language to build a parse tree (abstract syntax tree). Syntax errors are reported here.
3. Semantic analysis: checks meaning — type checking, scope resolution, declaration before use. Produces an annotated syntax tree.
4. Code generation: the annotated tree is translated into intermediate or target machine code.
5. Code optimisation: redundant instructions are removed, loops optimised, registers used efficiently to produce faster, smaller output code.
Bytecode & Assembler

Bytecode, JVM and Assembler

Bytecode / intermediate code: some compilers (e.g. Java) produce platform-independent bytecode rather than native machine code. Bytecode is then run by a Virtual Machine (e.g. JVM). Combines portability of interpretation with some speed benefits.
JIT compilation: Just-in-Time compilers (used in Java, .NET, V8 for JS) compile bytecode to native machine code at run time for frequently executed code paths, combining portability and performance.
Assembler: translates assembly language (mnemonics) to machine code. Near one-to-one translation. Uses a symbol table to resolve labels. Can be single-pass or two-pass (needed for forward references).
Exam Practice
OCR H446 Style · 5 marks
Compare the use of a compiler and an interpreter for translating a high-level language program. Include differences in how errors are handled.
[5 marks]
1
A compiler translates the whole program before execution; an interpreter translates and executes line by line at run time.
1
Compiled programs run faster once compiled as no translation overhead exists at run time; interpreted programs are slower as each line is retranslated on every execution.
1
A compiler reports all syntax errors after analysing the entire program; an interpreter stops and reports the error immediately when the offending line is reached.
1
Compiled code is platform-specific (must be recompiled for different architectures); interpreted code is portable as long as an interpreter is available on the target platform.
1
A compiler produces a standalone executable; an interpreter requires the source code and the interpreter itself to be present each time the program is run.
Common Mistakes

Don’t Lose Marks

!
Saying interpreters cannot find errors — they do find errors, but only when the erroneous line is executed. Branches that are never taken will not trigger their errors, unlike a compiler which checks the whole program.
!
Saying compilers are always better — interpreters are preferred during development for rapid testing; they are also more portable. Always contextualise your comparison.
!
Confusing bytecode with machine code — bytecode is not machine code. It is an intermediate format run by a virtual machine. It is more portable than native machine code but needs a JVM or equivalent to execute.
1.2.4c Complete
Well done! ✓
Translators: Compilers, Interpreters and Assemblers
Return to lesson to continue