Translators and the Stages of Compilation

A-Level Computer Science · Compilation

Translators

Computers only run machine code, so high-level and assembly programs must be translated. There are three types of translator:

TranslatorWhat it does
CompilerTranslates the whole high-level program into machine code in one go, producing an executable that runs on its own. Slow to translate, but fast to run; errors reported all at the end.
InterpreterTranslates and executes the program line by line at run time. Easier to debug (stops at the first error) but slower to run; needs the interpreter present each time.
AssemblerTranslates assembly language into machine code (roughly one-to-one).

Compiled code is not portable (tied to the target processor); interpreted/bytecode approaches can be more portable.

The stages of compilation

A compiler works in four main stages:

1. Lexical analysis

The source code is broken into tokens (keywords, identifiers, operators, numbers). Whitespace and comments are removed. A symbol table is created to record identifiers. (FSMs/regular expressions are used to recognise tokens.)

2. Syntax analysis (parsing)

The tokens are checked against the language's grammar rules to ensure the structure is valid, building an abstract syntax tree. Syntax errors (e.g. a missing bracket) are reported here.

3. Semantic analysis

Checks the meaning — e.g. type checking, that variables are declared before use, and that operations are valid. Catches errors that are grammatically correct but meaningless.

4. Code generation (and optimisation)

The final machine code (object code) is produced. Optimisation improves the code to run faster or use less memory (e.g. removing redundant instructions).

Worked example

At which stage would a missing closing bracket be detected, and at which would using an undeclared variable be caught?

  • A missing bracket breaks the grammarsyntax analysis. An undeclared variable is grammatically fine but meaningless → semantic analysis. ✓

Common mistakes

  • Confusing a compiler (whole program at once, standalone executable) with an interpreter (line by line at run time).
  • Mixing up the stages — lexical (tokens), syntax (grammar/parse tree), semantic (meaning/types), code generation.
  • Saying compiled code is portable — it's processor-specific.

Exam tips

  • Learn the three translators with a pro and con of compilers vs interpreters.
  • Learn the four compilation stages in order and what each detects/produces.
  • Know that lexical analysis produces tokens and a symbol table.

Key facts to remember

  • Translators: compiler (all at once, fast to run, not portable), interpreter (line by line, easy to debug, slower), assembler (assembly → machine code).
  • Compilation stages: 1 lexical (tokens), 2 syntax (grammar/parse tree), 3 semantic (meaning/types), 4 code generation (+ optimisation).
  • Syntax errors caught at syntax analysis; meaning errors at semantic analysis.
Don't understand a part?

Sign in and ask our AI tutor to explain any passage in plain English.

Try AI explanations →

← All A-Level Computer Science notes