Translation processes of interpreters and compilers
Evaluate the translation processes of interpreters and compilers
In this lesson
- The mechanics and use cases of compilers, interpreters, bytecode interpreters and just-in-time (JIT) compilation
- Their differences in error detection, translation time, portability and applicability
- Choosing a translation method for rapid development and testing, performance-critical applications and cross-platform development
LearnWhy translation is needed
A processor can only execute machine code: binary instructions from its own instruction set. Programmers write source code in high-level languages. Something has to translate one into the other.
LearnInside a translator
Compilers and interpreters start with the same analysis stages. They differ in what they do afterwards, and when.
CheckLexical analysis
The statement if total > 100: print("Free delivery") is split into tokens. Classify these tokens.
iftotal>100"Free delivery"print
LearnCompilers
LearnInterpreters
LearnBytecode interpreters and JIT compilation
LabWhen is the error found?
The same short program contains one error. Run it with a compiler (imagine it written in a compiled, statically typed language such as C), a line-by-line interpreter and a bytecode interpreter such as CPython. Watch what runs before the error is reported. In the third case the user types 0 when asked for the number of boxes.
- 1price = 12
- 2qty = 3
- 3boxes = int(input("Boxes: "))
- 4print("Price:", price)
- 5total = price * * qty
- 6print("Total:", total)
- 7per_box = total / boxes
- 8print("Per box:", per_box)
Run every kind of error with all three translators to complete this activity.
LabTranslation time versus run time
The same program, with a loop, is run with each approach. Compare a developer, who changes the code and tests it again and again, with a user, who runs the finished program. Then move from a few loop runs to a million.
The developer edits the code and tests it again and again: every change must be translated before it runs.
Fastest here: Interpreter (from source), 10 ms
- Compiler (ahead of time) 7.50 swhole program → machine code before it runs · translation 7.50 s, execution 100 µs
- Interpreter (from source) fastest10 mseach statement analysed and run, every time · translation 7.0 ms, execution 3.0 ms
- Bytecode interpreter 11 mssource → bytecode once, then the bytecode is interpreted · translation 7.5 ms, execution 3.0 ms
- JIT compiler 11 msbytecode, with frequently run code compiled to machine code at run time · translation 7.5 ms, execution 3.0 ms
translation execution. Bars are drawn to the same scale for the current settings. Try 1–10 and at least 100,000 loop runs in both views to complete this activity.
LearnComparing the approaches
| Compiler (ahead of time) | Interpreter (source) | Bytecode interpreter | JIT compiler | |
|---|---|---|---|---|
| Error detection | Syntax, type and declaration errors at compile time, all reported before the program runs; data-dependent errors at run time | Each error only when its line is reached; code that never runs is never checked | Depends on the language: statically typed (Java, C#) – syntax, type and declaration errors when compiled to bytecode; dynamically typed (CPython) – mainly syntax errors, the rest at run time | As for bytecode (decided by the language, not by the JIT); JIT compilation adds no new checks |
| Translation time | All of it before execution: a long build, repeated after every change; none at run time | None in advance; repeated during execution, every time a statement runs | Compiled to bytecode before the program runs (often before it is shipped), then interpreted at run time | Bytecode produced in advance, plus run-time compilation of hot code (warm-up), which is then cached |
| Execution speed | Fastest | Slowest | Faster than source interpretation | Close to compiled once warmed up |
| Portability | Executable is platform-specific; recompile for each target | Source runs anywhere with the interpreter | Same bytecode runs on any platform with the virtual machine | Bytecode is portable; the machine code it generates is not |
| Applicability | Performance-critical software: operating systems, game engines, trading systems, embedded devices | Rapid development and testing, scripts, teaching | Cross-platform applications and tools | Long-running cross-platform applications: servers, large desktop and web apps |
CheckWhich approach fits?
Choose the most suitable translation approach for each scenario.
- A trading platform must react to market data within microseconds
- A scientist tries out ten versions of a data-cleaning script in one afternoon
- A company's customer system must run on Windows, macOS and Linux from one build
- The firmware of a pacemaker runs on a tiny processor with very little memory
- Students in a first programming course experiment with short programs
- A web server runs the same code for months and handles millions of requests
CheckQuick check
Why can the same Java program run on Windows, macOS and Linux without being recompiled for each?
ExamWorked exam answer
Outline how just-in-time compilation improves the performance of a program distributed as bytecode.
[3]ReviewMistakes examiners see
“A language is compiled or interpreted” is too simple: it is the implementation that compiles or interprets. Python can be run by CPython (bytecode interpreter) or PyPy (JIT).
Bytecode is not machine code. It is an intermediate form that still needs a virtual machine.
A compiler does not find every error. Data-dependent errors such as division by zero still happen at run time.
JIT is not the same as ahead-of-time compilation: it compiles during execution, on the user's computer.
In an evaluate question, use the guide's criteria by name (error detection, translation time, portability, applicability), link each to the scenario, and finish with a justified recommendation.
Exam practiceExam-style questions
5 questions · 14 marks. Answer each one first, then mark it yourself against the markscheme. Your answers are saved in this browser only.
A start-up is building a fitness app. It needs a quick prototype to show investors, which will change after every investor meeting, a video-analysis component that measures a user's movements in real time, and a desktop version that runs on Windows, macOS and Linux.
Outline one difference between the way a compiler and an interpreter translate source code.
Describe what happens during syntax analysis.
Explain why using an interpreter is suitable for developing and testing the prototype.
The desktop version will be distributed as bytecode. Explain one disadvantage of this compared with distributing native executables.
Evaluate the use of ahead-of-time compilation for the real-time video-analysis component.
0 of 5 questions marked. Mark every question to see your total.
Exam-style questions written for Praxis IB in the format of the 2027 specimen papers. They are not IB past-paper questions.