Community resourceWorksheet
OCR H446 1.2.2 Stages of compilation
Part 4 of 7 · H446 1.2.2 · Applications generation
Lexical analysis, syntax analysis, code generation and optimisation are examined in H446 1.2.2 through the evidence each stage receives and hands on. Working a single assignment statement through the whole sequence, this worksheet ties every stage to something visible, such as a token stream or an abstract syntax tree.
Students will:
- describe what lexical analysis removes, preserves and produces from source code
- explain what syntax analysis checks and the structure it builds from the tokens
- trace one statement through tokens, tree structure and generated code
- identify which stage reports a given error and what then reaches the next stage
- state the purpose of optimisation and the limits of what it may change
Inside: 6 explanation cells, 1 multiple-choice question, 3 fill-in-the-blanks cells and 4 written answers. 40 marks, about 45 to 55 minutes.
Series: H446 1.2.2 · Applications generation, part 4 of 7.
Shared by Coding PathwayVerified teacher
- 14 cells
- About 45 minutes
- CC BY-SA 4.0
- Shared 31 Aug 2026
- Updated 3 Sept 2026
Preview
The whole resource, exactly as a class sees it. Answers and marking are held back.
Stages of compilation
Four named stages and visible outputs
OCR names lexical analysis -> syntax analysis -> code generation -> optimisation. Some compiler designs place optimisation at different points, but use this qualification route and these required functions.
Worked model: 1 Lexical analysis
The lexer removes unnecessary comments/whitespace, recognises lexemes and produces tokens such as identifier, assignment operator and integer literal. It creates/uses symbol-table entries for identifiers. Spaces inside a string literal are data, so they are not simply removed.
subtotal = cost * 4, subtotal is an gap 1, = an gap 2, cost an gap 3, * an gap 4, and 4 an gap 5.- identifier
- assignment operator
- multiplication operator
- integer literal
- comment
2 Syntax analysis
The parser checks whether the token sequence follows the language grammar, reports syntax errors and builds an abstract syntax tree (AST) representing structure. The AST for an assignment separates the target from the expression; it is not the same thing as the token list.
Which stage/output pair is correct?
- ALexical analysis: object code
- BCode generation: tokens
- COptimisation: syntax-error correction
- DSyntax analysis: checked abstract syntax tree
- lexical analysis
- syntax analysis
- code generation
- optimisation
Trace area = width * height through the first three stages. Give a plausible token stream, a compact text description of its AST and the purpose of code generation.
Do not invent machine opcodes. Show structure and state change.
Students type their answer here.
Describe the purpose of code optimisation and explain why ‘the optimiser fixes syntax errors and changes what the program calculates’ is wrong.
Separate purpose from possible techniques.
Students type their answer here.
Apply the model independently
The remaining tasks change the context or reduce the support. Complete them without copying the worked model, then check that each explanation connects a mechanism to its consequence.
A comment contains spaces, a string literal contains North Gate, and a statement misses a closing bracket. Explain what lexical analysis removes or preserves, which stage reports the bracket error, and what state reaches code generation if the source is valid.
Whitespace inside a string is data; unnecessary layout whitespace is not.
Students type their answer here.
Repair this account: ‘The syntax analyser converts words into tokens. The lexical analyser builds the tree. Optimisation creates the executable and the linker puts it in RAM.’ Rewrite the sequence with correct tools, outputs and moments.
Include AG05 boundaries only where needed.
Students type their answer here.
Review your understanding
Before submitting, check that you can explain the main distinction in your own words, apply it in an unfamiliar context and justify each consequence rather than only naming a feature.