OCR H446, section 1.2.2 (e)
A compiler doesn't translate a program in a single step. It works in stages, each of which takes the output of the one before.
Stage 1: Lexical analysis
The lexical analyser (lexer) reads the source code as plain characters and breaks it into tokens, the smallest meaningful units of the language.
- It removes what the machine doesn't need: white space and comments.
- It recognises keywords (
if,while), identifiers (variable names), operators, literals (numbers and strings) and punctuation. - It builds a symbol table that records the names used (variables, functions) and details such as their type.
- It reports lexical errors: for example an illegal character or a badly formed name.
Example. The line total = price * 3 // cost becomes the tokens:
| Token | Type |
|---|---|
total |
identifier |
= |
assignment operator |
price |
identifier |
* |
arithmetic operator |
3 |
number literal |
The comment // cost has been removed.
Stage 2: Syntax analysis
The syntax analyser (parser) checks that the tokens are arranged according to the rules (grammar) of the language, and builds a parse tree (syntax tree) showing the structure of the program.
- It finds syntax errors: for example a missing bracket, a keyword in the wrong place, or
x = = 3. - The grammar is often described with Backus-Naur Form or syntax diagrams.
- It also usually checks the meaning (for example that a variable has been declared before use, and the types match): this is called semantic analysis.
For total = price * 3, the tree shows = at the top, with total on one side and the multiplication of price and 3 on the other:
The order of operations is built into the tree: the multiplication is worked out before the result is assigned.
Stage 3: Code generation
The code generator turns the syntax tree into machine code (or intermediate code such as bytecode), choosing instructions, registers and memory addresses.
For total = price * 3, simple assembly-style output might be:
LDA price ; load price
MUL three ; multiply by 3
STA total ; store in total
(The exact instructions depend on the target processor.)
Stage 4: Optimisation
The optimiser improves the generated code so that it runs faster or uses less memory, without changing what it does.
| Optimisation | Example |
|---|---|
| Constant folding | x = 2 * 3.14 is worked out at compile time as x = 6.28 |
| Removing redundant code | Dead code that can never run, or a variable that is never used, is deleted |
| Common sub-expression elimination | a*b that appears twice is calculated once and reused |
| Loop optimisation | A calculation that doesn't change is moved outside the loop |
| Reducing memory use | Reusing registers and memory |
Optimisation may happen at several points, and more than once.
Errors found at each stage
| Stage | Example error |
|---|---|
| Lexical analysis | An illegal character, such as price # 3 |
| Syntax analysis | A missing closing bracket: print("hi" |
| Semantic analysis | Using a variable that has not been declared |
| Code generation and later | Rare, such as running out of memory |
Errors that happen while the program runs (such as dividing by zero) can't be found by the compiler.
Worked example
Task: show what lexical analysis and optimisation do to area = 3.14 * 2 * 2 // circle.
- Lexical analysis: the tokens are
area,=,3.14,*,2,*,2. The comment is discarded, andareais added to the symbol table. - Syntax analysis: checks it's a valid assignment, and builds a tree of the two multiplications.
- Code generation: produces instructions to multiply and store.
- Optimisation: constant folding works out once, at compile time, so the final code simply stores 12.56 in
area, and no multiplication is done when the program runs.
Exam tip: learn the order of the four stages and one job for each: lexical (breaks source into tokens, removes white space and comments), syntax (checks the grammar, builds a tree), code generation (produces the machine code) and optimisation (makes it faster or smaller).
Check yourself
What does lexical analysis produce?
A stream of tokens, with white space and comments removed. It also builds a symbol table.
What kind of error does syntax analysis find?
Errors in the structure of the code that break the rules of the language, such as a missing bracket or a keyword in the wrong place.
What is the purpose of optimisation?
To make the generated code run faster or use less memory, without changing what the program does.
Give an example of an optimisation
Constant folding: an expression of constants such as 2 * 3.14 is worked out once at compile time. (Other examples: removing dead code, moving unchanging calculations out of a loop.)