AQA GCSE Computer Science (8525), section 3.4.4 (Classification of programming languages and translators)
Why translators are needed
A processor can only run machine code, so code written in a high-level or assembly language must be translated. A translator is a program that does this. There are three types.
Compiler
A compiler translates the whole of a high-level program into machine code in one go, before it runs. It produces an executable file.
- The program then runs quickly, without the compiler.
- The source code does not need to be shared: only the executable is.
- Errors are reported after the whole program is translated.
- The program must be recompiled after any change.
Interpreter
An interpreter translates and runs a high-level program one statement at a time. It does not produce a separate executable file, and does not generate machine code directly: it calls machine code subroutines in its own code to carry out each statement.
- Easy to test and debug: it stops at the first error.
- The program runs more slowly, because it is translated every time.
- The interpreter must be present to run the program.
Assembler
An assembler translates assembly language into machine code. Each line of assembly is converted into a single machine code instruction.
Comparing
| Compiler | Interpreter | Assembler | |
|---|---|---|---|
| Translates | high-level language | high-level language | assembly language |
| Output | machine code (all at once) | none (runs statements directly) | machine code |
| Speed of the final program | fast | slower | fast |
| Error reporting | after the whole program | at the line with the error | after the whole program |
When to use each
- Compiler: for finished programs to be sold or shared, because they run fast and the code stays private.
- Interpreter: while writing and testing, because you can run the code straight away and find errors easily.
- Assembler: for programs written in assembly language, such as for embedded systems.
Example: Python
Python is normally interpreted: the interpreter translates and runs a script one statement at a time, so an error in a later line only stops the program when that line is reached.
Common mistake: saying an interpreter "turns the code into machine code and saves it". An interpreter does not produce a separate machine code file. Also, remember the assembler is used for assembly language only, not for high-level code.
Exam tip: for a comparison, give a feature of each, linked by a word such as "whereas": "A compiler translates the whole program at once, whereas an interpreter translates and runs it one line at a time".
Check yourself
Which translator translates the whole program before it is run?
A compiler.
Why is an interpreter useful when writing a program?
It runs the code straight away and stops at an error, making testing and debugging easier.
What does an assembler translate?
Assembly language into machine code.