Cambridge A-Level Computer Science 9618 – Language Translators: Interpreters
5.2 Language Translators
Objective
Show understanding of the need for an interpreter for translation and execution of a high‑level language program.
Why Translation Is Required
High‑level languages (e.g., Python, JavaScript, Ruby) are designed for human readability. The computer hardware, however, can only execute machine code – a binary representation of instructions. Therefore a translation step is mandatory to bridge the gap between the programmer’s intent and the processor’s capabilities.
Two Main Translation Strategies
Compilation – The entire source program is translated into machine code before execution.
Interpretation – The source program is translated and executed line‑by‑line at run‑time.
When an Interpreter Is Needed
Rapid Development and Testing – Immediate feedback after each statement encourages exploratory programming.
Platform Independence – The same source can run on any system that provides a compatible interpreter, without recompilation.
Dynamic Features – Languages that support dynamic typing, reflection, or runtime code generation rely on an interpreter to resolve types and symbols on the fly.
Educational Environments – Interpreters simplify learning by hiding the compilation step, allowing students to focus on algorithmic thinking.
How an Interpreter Works
The interpreter repeatedly performs the following cycle:
Read the next statement or expression from the source code.
Analyse its syntax (parsing) and semantics.
Translate the construct into an intermediate representation (often byte‑code or an abstract syntax tree).
Execute the intermediate representation directly or via a virtual machine.
Comparison of Interpreter and Compiler
Aspect
Compiler
Interpreter
Translation time
Whole program translated before execution
Translation occurs during execution
Execution speed
Typically faster (native machine code)
Generally slower (run‑time translation overhead)
Portability
Requires recompilation for each target platform
Source runs on any platform with a suitable interpreter
Debugging
Debugging after compilation; errors may be harder to trace to source lines
Errors reported at the point of execution, aiding step‑by‑step debugging
Typical use cases
System software, performance‑critical applications
Scripting, rapid prototyping, educational tools
Mathematical Illustration of Run‑Time Overhead
If a program contains \$n\$ statements, a compiled program executes in \$Tc = O(n)\$ time (ignoring constant factors). An interpreter adds a constant translation cost \$k\$ per statement, giving \$Ti = O(k \cdot n) = O(n)\$ but with a larger constant factor. In practice, \$k\$ can be significant, especially for interpreted languages that perform dynamic type checks.
Suggested Diagram
Suggested diagram: Flowchart showing the interpreter cycle – fetch, parse, translate to intermediate form, execute – compared with the compiler pipeline (source → compile → executable → run).
Key Take‑aways
Interpreters translate and execute high‑level code on the fly, eliminating the need for a separate compilation step.
They are essential for languages that emphasise flexibility, rapid development, and platform independence.
Understanding the interpreter’s role helps students appreciate why some languages are chosen for particular problem domains.