1. What Are Compilers and Interpreters?
A computer's CPU ultimately executes machine instructions, while source code written by developers is usually expressed in high-level programming languages.
For example:
print("Hello World")
The CPU cannot directly understand this Python source code. It requires compilation, interpretation, or other execution mechanisms provided by the language implementation to convert the program into executable operations.
Compilers and interpreters are two important kinds of programs that perform this work.
1.1 Compiler
A compiler is a program that converts a source program into another form.
Common conversion targets include:
- Machine code.
- Assembly code.
- Bytecode.
- Intermediate representation (IR).
- Code in other high-level languages.
A compiler usually first analyzes the source code and then converts it into target code.
For example, C programs usually need to be compiled and linked to produce an executable file.
flowchart TB
A["C source code"] --> B["Lexical analysis"]
B --> C["Syntax analysis"]
C --> D["Semantic analysis"]
D --> E["Intermediate representation IR"]
E --> F["Code optimization"]
F --> G["Generate target code"]
G --> H["Linking"]
H --> I["Executable file"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style D fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style E fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style F fill:#FFF5DE,stroke:#E8D29A,color:#334155
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
The compiler's core responsibility is to analyze the program and generate a target representation, but it is not necessarily responsible for ultimately executing the program.
1.2 Interpreter
An interpreter is a runtime program responsible for executing a program.
It reads program code or some intermediate representation and performs the corresponding operations according to the semantics defined by the language.
For example, an interpreter can:
- Read source code or bytecode.
- Execute instructions.
- Manage variables and objects.
- Handle function calls.
- Execute conditional judgments and loops.
- Manage exceptions.
- Call runtime libraries.
A conceptual process of interpreted execution:
flowchart TB
A["Source code or bytecode"] --> B["Read instruction"]
B --> C["Parse or identify instruction"]
C --> D["Execute corresponding operation"]
D --> E{"Are there more instructions?"}
E -->|"Yes"| B
E -->|"No"| F["Program execution ends"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#FFF5DE,stroke:#E8D29A,color:#334155
style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Note that this is a simplified interpreted execution model.
Modern interpreters do not necessarily read source code line by line directly. Many interpreters first convert source code into an AST, bytecode, or another intermediate representation, and then execute that representation.
1.3 The Essential Difference Between Compilation and Interpretation
The main difference between the two lies in how processing is handled before and after program execution.
| Comparison Dimension | Compiler | Interpreter |
|---|---|---|
| Core responsibility | Transform program representations | Execute programs |
| Primary input | Source code or intermediate representation | Source code or intermediate representation |
| Primary output | Target code or another representation | Program execution results |
| Execution method | Usually generates target code first | Driven by a runtime |
| Typical representatives | GCC, Clang, Rustc | CPython, Ruby MRI |
| Runtime dependencies | Depends on the target code and runtime environment | Usually requires a corresponding runtime |
Compilers and interpreters are not completely opposing concepts.
A compiler can generate bytecode, and an interpreter can execute bytecode; an interpreter can also invoke a JIT compiler during execution to compile hot code into machine code.
Therefore, modern language runtimes often include both compilation and interpretation mechanisms.
II. How Compilers Work
A compiler usually consists of multiple phases, each responsible for a specific code analysis or transformation task.
2.1 Lexical Analysis
Lexical analysis is the first important phase in a compiler's processing of source code.
The lexer reads the source code character stream and groups characters into tokens with semantic categories.
For example:
int sum = a + 10;
can be recognized as:
| Token | Type |
|---|---|
int |
Keyword |
sum |
Identifier |
= |
Assignment operator |
a |
Identifier |
+ |
Addition operator |
10 |
Integer literal |
; |
Separator |
The main tasks of lexical analysis are:
- Identify keywords.
- Identify identifiers.
- Identify operators.
- Identify literals.
- Ignore whitespace and comments (as determined by the language rules).
- Report lexical errors.
The result of lexical analysis is usually a token sequence.
2.2 Syntax Analysis
The parser checks, according to the language's grammar rules, whether a token sequence forms a valid program structure.
For example:
int result = a + b;
The parser identifies:
- Variable declarations.
- Type declarations.
- Assignment expressions.
- Addition expressions.
- Statement terminators.
Syntax analysis usually builds an abstract syntax tree (AST).
Illustrative structure:
VariableDeclaration
├── Type: int
├── Name: result
└── Initializer
└── BinaryExpression
├── Left: a
├── Operator: +
└── Right: b
Syntax analysis is mainly responsible for checking whether the program structure conforms to the language grammar; it is not responsible for determining whether all variables and expressions have the correct types.
2.3 Semantic Analysis
Semantic analysis checks whether a program complies with the language's semantic rules.
For example:
int a = 10;
int b = a + 20;
Semantic analysis may check:
- Whether variables have been declared.
- Whether identifiers can be resolved correctly.
- Whether expression types are compatible.
- Whether function call arguments match.
- Whether scoping rules are correct.
- Whether type conversions are valid.
For example:
int a = "hello";
In C, this code has an initialization problem that violates type constraints, and the compiler will usually diagnose the related error.
2.4 Intermediate Representation (IR)
Intermediate representation is a form of program representation used internally by a compiler.
A compiler can translate source code from different languages into a unified or relatively unified IR, and then analyze, optimize, and generate code from the IR.
For example:
int result = a + b * 2;
A simplified three-address code representation:
t1 = b * 2
t2 = a + t1
result = t2
The main value of IR:
- Reduce coupling between source languages and target machines.
- Support cross-platform code generation.
- Facilitate compiler optimizations.
- Facilitate static analysis.
- Support sharing compiler infrastructure across multiple languages.
Common IR technologies include LLVM IR, GIMPLE, and Java bytecode. Note that their levels of abstraction and design goals are not exactly the same.
2.5 Code Optimization
Code optimization is the process of improving a program's execution efficiency, code size, or other target attributes without changing the program's specified semantics.
Common optimizations include:
| Optimization technique | Purpose |
|---|---|
| Constant folding | Evaluate constant expressions at compile time |
| Constant propagation | Substitute known constants into subsequent expressions |
| Dead code elimination | Remove code that does not affect the program's result |
| Common subexpression elimination | Avoid recomputing the same expression |
| Loop optimization | Improve loop execution efficiency |
| Inlining | Replace a function call with the function body |
| Register allocation | Allocate CPU registers sensibly |
For example:
int a = 10;
int b = 20;
int c = a + b;
The optimizer may transform it into:
int c = 30;
This is a simple example of constant propagation and constant folding.
Real optimizations must also consider overflow, floating-point precision, observable side effects, and language semantics; they cannot arbitrarily change program behavior.
2.6 Code Generation
The code generation stage is responsible for converting the compiler's internal representation into the code required by the target platform.
Common targets include:
- x86-64 machine code.
- ARM64 machine code.
- WebAssembly.
- Assembly language.
- Bytecode.
For example:
Source code
|
v
Intermediate representation
|
v
Target platform code generation
|
v
Machine instructions
Different target platforms have different instruction set architectures (ISAs), and the code generator must generate the corresponding instructions for the target architecture.
2.7 Linking
For programs that require linking, after the compiler generates object files, a linker is usually still needed to perform symbol resolution and combine the object files.
The linker is mainly responsible for:
- Symbol resolution.
- Relocation.
- Static library linking.
- Handling dynamic library dependencies.
- Generating an executable file or shared library.
The complete compilation and linking process:
flowchart TB
A["Source code"] --> B["Compiler"]
B --> C["Object file"]
C --> D["Linker"]
E["Static or dynamic library"] --> D
D --> F["Executable file"]
F --> G["Operating system loading"]
G --> H["Program execution"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Note that compilation, assembly, linking, and loading are distinct stages. Some language toolchains bundle several of these stages into a single command, but their responsibilities are not the same.
3. How Interpreters Work
The core task of an interpreter is to execute a program representation.
The internal architecture of interpreters can vary greatly, but it typically involves source code processing, runtime data management, and instruction execution.
3.1 Reading the Source Code
The simplest interpreters can read the source code directly and parse it.
For example:
print(1 + 2)
The interpreter needs to recognize:
- A function call.
- The function name.
- The argument list.
- The addition expression.
- Numeric literals.
In practice, however, interpreters usually do not simply read line by line and execute characters directly.
3.2 Building an Abstract Syntax Tree
Many interpreters first convert the source code into an AST.
For example:
result = 10 + 20
This can be represented as:
Assign
├── Target
│ └── Name: result
└── Value
└── BinOp
├── Left: 10
├── Operator: Add
└── Right: 20
The AST preserves the syntactic structure of the program, which facilitates subsequent interpretation or bytecode generation.
3.3 Interpretive Execution
The interpreter can traverse the AST and perform the corresponding operation according to the node type.
For example:
Execute Assign node
|
v
Execute BinOp node
|
v
Evaluate left operand
|
v
Evaluate right operand
|
v
Perform addition
|
v
Bind the result to variable result
In an actual implementation, the interpreter may execute the program using recursive traversal, the visitor pattern, virtual machine instruction dispatch, or other approaches.
3.4 Bytecode Interpretation
Many modern interpreters first translate source code into bytecode, which is then executed by a virtual machine.
Take CPython as an example: Python source code typically goes through parsing and compilation to produce a code object, which contains information such as bytecode.
You can inspect part of the bytecode through the dis module:
import dis
def add(a, b):
return a + b
dis.dis(add)
The specific bytecode instructions change with the Python version, so the output of one version should not be treated as a fixed implementation across all versions.
A simplified execution process:
flowchart TB
A["Python source code"] --> B["Lexical and syntax processing"]
B --> C["AST"]
C --> D["Compile to bytecode"]
D --> E["Code object"]
E --> F["CPython virtual machine"]
F --> G["Execute bytecode"]
G --> H["Produce runtime result"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style H fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
CPython's traditional execution model relies mainly on bytecode interpretation, but modern CPython also includes optimization mechanisms such as the adaptive interpreter. Other Python implementations may also adopt different execution strategies.
IV. Comparison of How Compilers and Interpreters Execute
4.1 Traditional Compiled Execution
Traditional compiled execution usually generates object code first, which is then loaded and executed by the operating system.
For example, with the C language:
gcc main.c -o main
./main
The process can be summarized as:
flowchart TB
A["Write source code"] --> B["Compile"]
B --> C["Generate object file"]
C --> D["Link"]
D --> E["Generate executable file"]
E --> F["Load program"]
F --> G["CPU executes machine instructions"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style G fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Typical characteristics of this approach:
- The compilation phase may take considerable time.
- A large amount of static analysis and optimization can be performed in advance.
- The generated native machine code can usually be executed directly by the CPU.
- The original source code may not be needed at runtime.
- The executable file is usually tied to the target platform and ABI.
4.2 Traditional Interpreted Execution
In traditional interpreted execution, the interpreter is responsible for reading the program representation and executing it.
For example, to run this in CPython:
python main.py
The process can be summarized as:
flowchart TB
A["Write Python source code"] --> B["Start the Python interpreter"]
B --> C["Parse the source code"]
C --> D["Generate code objects"]
D --> E["Virtual machine executes bytecode"]
E --> F["Call runtime features"]
F --> G["Output program results"]
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style F fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style G fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Characteristics of traditional interpreted execution:
- Usually requires a corresponding interpreter or runtime.
- Code execution depends on the runtime's instruction scheduling.
- Convenient for dynamic loading and runtime checks.
- Some implementations may incur relatively high execution overhead.
- The runtime can dynamically manage objects and program state.
4.3 Core Differences
| Comparison Dimension | Compiled Execution | Interpreted Execution |
|---|---|---|
| Main processing timing | Target code is usually generated before or during execution | The program representation is executed at runtime |
| Main execution object | Target machine code or other target representation | Source code, AST, bytecode, etc. |
| Runtime dependency | Depends on the target code and its execution environment | Usually requires an interpreter or virtual machine |
| Optimization approach | Static optimization, link-time optimization, JIT, etc. | Interpreter optimization, runtime optimization, JIT, etc. |
| Startup overhead | May involve compilation and loading overhead | May involve parsing and initialization overhead |
| Runtime performance | Depends on code generation and optimization quality | Depends on interpreter and runtime optimization |
| Cross-platform capability | Machine code usually depends on the target platform | Bytecode or source code can be executed by runtimes on different platforms |
One cannot simply assume that compiled programs are always faster than interpreted programs.
Actual performance depends on:
- Target code quality.
- Runtime optimization capabilities.
- Program execution mode.
- I/O overhead.
- Memory management.
- Effectiveness of JIT compilation.
- Runtime environment.
V. Compiled Languages and Interpreted Languages
Programming languages are often classified into compiled languages and interpreted languages.
This classification helps with a preliminary understanding of language implementation, but it is not a strict classification of language specifications.
The same language can have multiple implementation approaches.
5.1 Common Compiled Languages
| Language | Common Implementation Approach |
|---|---|
| C | Compiled to native machine code |
| C++ | Compiled to native machine code |
| Rust | Compiled to native machine code or other targets |
| Go | Usually compiled to native machine code |
| Swift | Usually generates target code through a compilation toolchain |
These languages usually employ a compilation toolchain to convert source code into code executable on the target platform.
5.2 Common Interpreted or Virtual Machine Languages
| Language | Common Implementation Approach |
|---|---|
| Python | CPython usually compiles to bytecode, which is interpreted and executed by a virtual machine |
| Ruby | MRI usually uses a bytecode virtual machine |
| PHP | Common implementations execute compiled opcodes through the Zend Engine |
| JavaScript | Modern engines usually combine bytecode interpretation with JIT compilation |
These languages do not necessarily adopt a purely line-by-line interpretation approach.
5.3 Why is Java both compiled and interpreted?
Java is a typical case for understanding hybrid execution models.
Java source code is usually first compiled into JVM bytecode by javac.
javac Main.java
java Main
The generated .class files contain JVM bytecode.
The JVM can execute bytecode through an interpreter, or use a JIT compiler to compile hot code into native machine code.
flowchart TB
A["Java Source Code"] --> B["javac Compilation"]
B --> C["JVM Bytecode"]
C --> D["JVM Loading and Verification"]
D --> E["Interpreter Execution"]
D --> F["Hotspot Code Analysis"]
F --> G["JIT Compilation"]
G --> H["Native Machine Code"]
E --> I["Program Execution"]
H --> I
style A fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style B fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style C fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style D fill:#FFF5DE,stroke:#E8D29A,color:#334155
style E fill:#E8EDF2,stroke:#B8C3CE,color:#334155
style F fill:#E5F4EC,stroke:#A3CBB5,color:#334155
style G fill:#F3EDFC,stroke:#C4B2E3,color:#334155
style H fill:#E0F0FE,stroke:#93BCE8,color:#1E4976
style I fill:#E5F4EC,stroke:#A3CBB5,color:#334155
linkStyle default stroke:#A3B8AC,stroke-width:1.5px
Java's execution process includes multiple stages:
javaccompiles source code into bytecode.- The JVM loads and verifies the bytecode.
- The interpreter can first execute the bytecode.
- The JVM collects runtime performance information.
- The JIT compiler identifies hotspot code.
- Hotspot code is compiled into native machine code.
- Subsequent execution can use the optimized machine code.
Therefore, Java has both a compilation stage and stages of interpreted execution and just-in-time compilation.
5.4 Why Does JavaScript Also Need JIT?
Modern JavaScript engines usually adopt a multi-tier execution architecture.
Taking V8 as an example, its execution process may involve:
- Source code parsing.
- AST construction.
- Bytecode generation.
- Interpreted execution.
- Hotspot code analysis.
- Optimizing compilation.
- Deoptimization.
Simplified process: