Compilation and Interpretation: Two Execution Models
You'll learn the fundamental distinction between compilers that translate entire programs upfront and interpreters that execute code directly, line by line.
Two Paths to Execution
After you've written code and it passes semantic analysis, how does it actually run? There are two primary approaches: compilation and interpretation.
Compilation: Translate First, Run Later
A compiler reads your entire source program and translates it into machine code (or another lower-level representation) before execution begins. Think of it like translating a novel from French to English — you complete the entire translation, publish the English book, and then readers can enjoy it without needing French knowledge.
The compilation process produces an executable file that your computer can run directly. Each time you run the program, you're executing the pre-translated machine code, not analyzing the original source.
Interpretation: Execute As You Go
An interpreter reads your source code and executes it directly, typically one statement or expression at a time. It's like having a simultaneous translator at a conference — they translate and speak each sentence as they hear it, without waiting for the entire speech to finish.
The interpreter maintains the program state and evaluates your code on-the-fly, translating each operation to machine instructions moment by moment.
The Practical Difference
Compilation requires a separate build step but results in faster execution. Interpretation allows immediate execution without compilation but typically runs slower since translation happens during runtime.
Many modern systems blur these lines (Java compiles to bytecode then interprets it; Python compiles to bytecode internally), but the core distinction remains: do you translate everything first, or execute while translating?
Key Takeaway: Compilers translate entire programs into executable code before running them, while interpreters execute source code directly during runtime — two fundamentally different models for bringing programs to life.