Compilation and interpretation
| English | Chinese | Pinyin |
|---|---|---|
| interpreter | 解释器 | jiě shì qì |
| compiler | 编译器 | biān yì qì |
| lexical analysis | 词法分析 | cí fǎ fēn xī |
| tokens | 词法单元 | cí fǎ dān yuán |
| syntax analysis | 语法分析 | yǔ fǎ fēn xī |
| abstract syntax tree | 抽象语法树 | chōu xiàng yǔ fǎ shù |
| semantic analysis | 语义分析 | yǔ yì fēn xī |
| code generation | 代码生成 | dài mǎ shēng chéng |
| optimisation | 优化 | yōu huà |
How code gets translated
- Source code must become machine code to run.
- An interpreter 解释器 does it line by line; a compiler 编译器 does it all at once, in phases.
- Knowing the compiler's stages explains where each kind of error is caught.
How an interpreter runs a program
- An interpreter translates and runs the source at the same time, statement by statement.
- For each line it analyses, type-checks, then executes the action.
- Errors are reported immediately (and it usually stops); no executable is produced.
- It re-translates every run (slower) but gives fast feedback and is portable.

A compiler translates all at once into an executable; an interpreter reads and runs each line
An interpreter:
It works line by line, reporting errors as it reaches them; nothing is saved as an executable, and it is generally slower.
The stages of compilation
- Lexical analysis 词法分析 — group characters into tokens 词法单元 (keywords, identifiers, operators), discarding whitespace/comments.
- Syntax analysis 语法分析 (parsing) — check the tokens fit the grammar and build an abstract syntax tree 抽象语法树; a missing bracket is a syntax error.
- Semantic analysis 语义分析 — check it makes sense (variables declared, types match).
- Code generation 代码生成 — emit target machine code from the tree.
- Code optimisation 优化 — remove redundant work, fold constants, reorder for the pipeline.

Compilation runs in stages: lexical analysis, syntax analysis, semantic analysis, code generation and optimisation

A syntax (railroad) diagram for an assignment statement
The phases of compilation
Step through what a compiler does to your source. Each phase hands its output to the next — characters become tokens, tokens become a tree, the tree becomes optimised machine code.
Lexical analysis (the lexer) turns:
The lexer groups characters into tokens and discards whitespace/comments; parsing then builds the tree.
Match each compiler phase to what it does.
Each phase transforms the program a step further: tokens, then a tree, then checked, then optimised code.
Put the compiler stages in order.
Lexical → syntax → semantic → code generation → optimisation.
Code optimisation aims to:
Optimisation improves the generated code (constant folding, removing redundancy, reordering for the pipeline).
The best of both worlds
- Some languages compile to bytecode, which a virtual machine then interprets.
- This gives portability (run anywhere) with reasonable speed.
- Reverse Polish Notation (RPN) writes the operator after its operands, needing no brackets.
You've got it
- an interpreter translates + runs line by line (immediate errors, no executable, slower)
- compiler stages: lexical (tokens) → syntax (parse → AST) → semantic → code generation → optimisation
- a syntax error is caught during syntax analysis
- the compiler's output is a stand-alone executable