Show understanding of how an interpreter can execute programs without producing a translated version
16.2 Translation Software – Interpreters and Compilers
Learning objective
Explain how an interpreter can execute a program without producing a separate translated version, and compare this with the compilation process used by a compiler.
1. Interpreters – Executing Directly from Source
1.1 What an interpreter does
Reads the source program, translates each statement (or a small group of statements) on the fly, and executes it immediately.
The original source text is never turned into an object file or a standalone executable.
Typical environments: scripting languages, REPLs (Read‑Eval‑Print Loops), educational tools, and many web‑based languages.
1.2 Execution model (statement‑by‑statement)
Read next line (or token stream) from the source file.
For a compiled program the lexical, parsing and code‑generation costs are incurred once, after which the execution time \(T_{\text{exec}}\) is usually much smaller.
2.4 Diagram – compiler pipeline
Standard compilation flow.
3. Describing Language Syntax – BNF and Railroad Diagrams
3.1 Backus‑Naur Form (BNF)
Formal notation for a context‑free grammar.
Each rule: <non‑terminal> ::= <definition>.
Terminals are written in quotes, non‑terminals in angle brackets.
BNF / Railroad diagram for a simple arithmetic grammar (as shown above).
RPN evaluation stack diagram – illustrate tokens being pushed and popped during evaluation.
7. Key take‑aways
An interpreter translates and executes each statement on the fly; no separate translated version is ever produced.
A compiler performs the same translation steps once for the whole program, producing object code that can be run repeatedly without re‑analysis.
Both rely on the same underlying grammar (BNF or railroad diagrams) to recognise valid statements.
Reverse Polish Notation demonstrates a simple, stack‑based way of evaluating expressions – a technique used by many interpreters and virtual machines.
Trade‑offs:
Interpreters – rapid feedback and high portability, but slower execution.
Compilers – faster run‑time performance and optimisation, but require a separate compile step and are less portable without recompilation.
Support e-Consult Kenya
Your generous donation helps us continue providing free Cambridge IGCSE & A-Level resources,
past papers, syllabus notes, revision questions, and high-quality online tutoring to students across Kenya.