Translators & the Stages of Compilation (OCR A-Level CS 1.2.2)

OCR A-Level CS 1.2.2: assemblers, compilers and interpreters, the four stages of compilation, and linkers, loaders and libraries. Diagrams and exam tips.


Free Translators Compilation revision resources (OCR A-Level Computer Science, 1.2.2)

We’ve made exam-style practice for this exact topic, free to download: Translators Compilation question sheet, mark scheme and cheat sheet. Grab them, have a go, then read the full guide below.

The code you write is not what the processor runs. A translator turns your source code into machine code first. Spec point 1.2.2 covers the three types of translator, the four stages of compilation, and the roles of linkers, loaders and libraries — reliably examined on Paper 1, including "explain two differences between a compiler and an interpreter" and a 9-mark "explain the process of compilation".

This guide works through each translator, the compilation stages, and how separate code is linked and loaded into a running program.

What is a translator and what are the three types?

A translator is a program that converts code from one language into another, usually into the machine code the CPU can execute. There are three types:

  • Assembler — translates assembly language into machine code, roughly one line of assembly to one machine-code instruction.

  • Compiler — translates a whole high-level language program into machine code (object code) in one go, before it is run.

  • Interpreter — translates and executes a high-level program one line at a time, as it runs.

What is the difference between a compiler and an interpreter?

A compiler translates the entire program once and produces object code that can be saved and run again later without the compiler present. Compilation is slower to start, but the final program runs fast, and the source code does not need to be shared. However, the object code is hardware-specific, so you need a different compiler for each platform.

An interpreter translates and runs the program line by line each time. There is no separate object file, so it is excellent for development and testing because errors are reported as soon as the offending line runs. The trade-off is that it runs more slowly (translation happens every time) and the interpreter must be present to run the program.

What are the four stages of compilation?

OCR expects four stages, in order:

  1. Lexical analysis — the source code is broken into tokens (keywords, identifiers, operators), whitespace and comments are removed, and a symbol table is built to record identifiers.

  2. Syntax analysis — the tokens are checked against the grammar rules of the language; any syntax errors are reported here, and a structure (parse tree) is built.

  3. Code generation — the parsed program is turned into object code (machine code).

  4. Optimisation — the code is improved to run faster or use less memory, without changing what it does.

What are linkers, loaders and libraries?

A library is a collection of pre-written, pre-compiled and tested code (subroutines) that programmers can reuse rather than writing from scratch — saving time and improving reliability.

A linker combines the compiled program with any library code and other object files it uses into a single executable, resolving the references between them.

A loader then copies that executable into memory (RAM) and prepares it to run when the program is launched.

Worked example

A graphics program calls a library routine to draw a circle. The programmer's code is compiled to object code, but the "draw circle" routine lives in a separate, pre-compiled library. The linker joins the two into one executable so the call works; when the user opens the program, the loader copies it into RAM and execution begins. Without the linker, the program would not know where the library routine is.

Common exam mistakes

  • Saying an interpreter "is faster". Interpreted programs run slower because translation happens every run; the advantage is easier debugging during development.

  • Getting the compilation stages out of order. It is lexical → syntax → code generation → optimisation. Syntax analysis directly follows lexical analysis.

  • Placing error reporting wrongly. Syntax errors are caught in syntax analysis, not lexical analysis.

  • Confusing linker and loader. The linker combines code into one executable; the loader copies it into memory to run.

  • Vague "library" answers. A library is pre-written, reusable, tested code; mention the time/reliability benefit.

Quick recap

  • Three translators: assembler (assembly → machine code), compiler (whole high-level program → object code), interpreter (line by line as it runs).

  • Compiler: fast final program, hardware-specific, source kept private. Interpreter: slower, great for debugging, needs the interpreter present.

  • Stages of compilation: lexical analysis → syntax analysis → code generation → optimisation.

  • Library = reusable pre-compiled code; linker combines it into one executable; loader copies the executable into RAM to run.

Frequently asked questions

What are the three types of translator? An assembler (translates assembly language into machine code), a compiler (translates a whole high-level program into object code before running), and an interpreter (translates and runs a high-level program one line at a time).

What is the difference between a compiler and an interpreter? A compiler translates the entire program once into object code that can be saved and run later without the compiler. An interpreter translates and executes the program line by line each time it runs, which is slower but reports errors as they occur, making debugging easier.

What are the four stages of compilation? Lexical analysis (break code into tokens, build the symbol table), syntax analysis (check grammar and report syntax errors), code generation (produce object code) and optimisation (improve speed or memory use without changing behaviour).

What happens during lexical analysis? The source code is broken into tokens such as keywords, identifiers and operators. Whitespace and comments are removed and a symbol table is created to store information about identifiers. It is the first stage of compilation.

What is the difference between a linker and a loader? A linker combines the compiled program with library code and other object files into a single executable, resolving references between them. A loader then copies that executable into memory (RAM) and prepares it to run.

What is a library and why use one? A library is a collection of pre-written, pre-compiled and tested code that programmers can reuse. Using libraries saves development time and improves reliability because the code is already tested, though the programmer must trust code they did not write.

 

 

 

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com

Logo

All trademarks, logos and brand names are the property of their respective owners. All company, product and service names used in this website are for identification purposes only. Use of these names, trademarks and brands does not imply endorsement.


Follow us on:

Icon
Icon
Icon
Icon
Icon

Support@shuttlelearning.com