Translators: Compilers & Interpreters
Computers only understand machine code (binary). Translators convert human-readable source code into machine code. The two main types — compilers and interpreters — work very differently.
Learning Objectives
- 11.5.1.7 Differentiate between compilers and interpreters
- 11.5.1.8 Describe advantages and disadvantages of each
Conceptual Anchor
The Translator Analogy
A compiler is like translating an entire book before publishing — slow upfront, but the reader gets a fast, finished product. An interpreter is like a live translator at a conference — translates sentence by sentence, slower overall but you hear each part immediately.
Rules & Theory
Three Types of Translators
| Translator | Source | Output | Example |
|---|---|---|---|
| Assembler | Assembly (2GL) | Machine code | NASM, MASM |
| Compiler | High-level (3GL) | Machine code (executable) | GCC (C/C++), javac |
| Interpreter | High-level (3GL) | Executes directly (no file created) | Python, JavaScript |
Compiler vs Interpreter
| Feature | Compiler | Interpreter |
|---|---|---|
| Translation | Entire program at once | Line by line |
| Output | Creates executable (.exe) | No executable created |
| Execution speed | Fast (pre-compiled) | Slow (translates each run) |
| Error reporting | All errors after compilation | Stops at first error |
| Debugging | Harder (all errors at once) | Easier (one error at a time) |
| Distribution | Share executable (source hidden) | Must share source code |
| Memory | Needs more (stores executable) | Less (no extra file) |
| Re-translation | Only when source changes | Every time program runs |
The Translation Process
COMPILER:
Source Code (.cpp) → [Compiler] → Machine Code (.exe)
↓
Run anytime without compiler
INTERPRETER:
Source Code (.py) → [Interpreter] → Execute line by line
↓
Needs interpreter every time
ASSEMBLER:
Assembly (.asm) → [Assembler] → Machine Code
(1-to-1 translation)Hybrid Approach: Java
Java uses both: the compiler (javac) produces bytecode (.class), which is then run by the JVM (Java Virtual Machine) — an interpreter. This combines compilation speed with platform independence.
Common Pitfalls
Interpreters Are Always Slow
Modern interpreters use JIT (Just-In-Time) compilation to compile hot code paths at runtime. JavaScript V8 engine (in Chrome) is extremely fast despite being "interpreted".
Tasks
Name the 3 types of translators and state what each translates.
Explain why a compiled program runs faster than an interpreted one.
A developer is choosing between Python (interpreted) and C++ (compiled) for a real-time game engine. Which should they choose and why?
Self-Check Quiz
Q1: What does a compiler produce?
Q2: Why is an interpreter better for debugging?
Q3: What does an assembler do?