Programming Languages and Translators
Software · 4 question types
Exam Frequency Analysis
Past paper frequency (2018 to 2024)
This topic accounts for approximately 4% of your exam marks.
Compiler vs interpreter differences are a recurring 3-mark question.
Every programming language sitting above raw machine code must first be converted into machine code before the CPU can execute it. A translator is the piece of software that performs this conversion. Three kinds appear in the syllabus:
| Translator | Translates | Used for |
|---|---|---|
| Assembler | Assembly language → machine code | Low-level (2nd generation) source code |
| Compiler | High-level language → machine code, all in one go | Programs that are finished and ready to distribute |
| Interpreter | High-level language → machine code, line by line, running each line straight after translation | Programs that are being developed or run interactively |
Assembler
The simplest of the three. An assembler takes an assembly-language source file and produces an equivalent machine-code file. Because the languages are one-to-one, the job comes down to a lookup table: each mnemonic is replaced with its binary equivalent.
For example, the assembler sees LDA 5, looks LDA up in its table, finds the binary opcode 10000001, and produces the machine-code bytes 10000001 00000101.
Compiler
A reads the whole high-level program once, translates the entire thing into machine code, and produces a standalone executable file that can be run later without the compiler being present.
How a compiler is used:
- The programmer writes a source file (e.g.
program.cpp). - The compiler reads the file and translates it into machine code.
- The compiler produces a separate executable file (e.g.
program.exe). - The executable can then be distributed and run on any compatible machine.
- If the source is changed, the compiler must be run again before the new version can be tested.
Interpreter
An works through the high-level source line by line. After translating a line, it executes that line straight away before moving on to the next.
How an interpreter is used:
- The programmer writes a source file (e.g.
program.py). - The interpreter starts at the first line, translates it and runs it.
- It moves on to the second line and does the same.
- If a line contains an error, execution stops at that line, before the rest of the program is processed.
- The interpreter has to be installed on any machine that runs the program, since the source is never bundled into a standalone executable.
Side-by-side
| Feature | Compiler | Interpreter |
|---|---|---|
| Translates... | The whole program in one go | One line at a time |
| Produces an executable? | Yes, a separate file | No |
| Runs the program? | No; the executable is run later | Yes; translates and runs in one step |
| Speed of execution | Fast (already in machine code) | Slower (translates as it runs, every time) |
| Reports errors... | At the end of compilation (or at the first one found that stops compilation) | Stops at the line where the error is found |
| Easier to debug? | Harder (must recompile after every change) | Easier (run, see error, fix, run again immediately) |
| Distribution | Easy; share the executable | Harder; the recipient also needs the interpreter |
| Source code visible to user? | No (only the executable is shared) | Yes (the source must be shared with the interpreter) |
| Typical use | Released, polished software | Active development and scripting |
Which to use when
- During development, an interpreter is convenient because each change can be tried immediately.
- For distribution, a compiler is usually preferred because the program runs faster and the source stays private.
- Some languages (Java, C#) use both: a compiler turns the source into a low-level intermediate code (bytecode), and an interpreter runs the bytecode on each user's machine.
How a compiler works
Describing how a compiler translates (and reports errors) comes up often, so you need to know it translates the whole program at once into machine code, produces a standalone executable, and reports all errors together at the end — unlike an interpreter, it does not work line by line.
Identifying which translator is which
What comes up: a description ("converts and executes one line at a time", "creates an executable file") asks you to name the translator.
Watch out: match by the one defining property — interpreter = translates/executes one line at a time (stops at the first error); compiler = translates the whole program and produces an executable (all errors reported together); assembler = assembly language → machine code.