CIE AS & A Level Computer Science, section 5.2 (Language translators)
Why translators are needed
A processor can only execute machine code. Programs written in assembly or high-level languages must be translated first.
- An assembler translates assembly language into machine code.
- A compiler translates a whole high-level program into machine code (the object code) before it is run.
- An interpreter translates and executes a high-level program one statement at a time, without producing a separate translated file.
Comparing compilers and interpreters
| Compiler | Interpreter | |
|---|---|---|
| Translation | whole program at once | line by line, while running |
| Output | an executable file | none |
| Speed of running | faster (already translated) | slower (translates every time) |
| Errors | all reported after compilation | stops at the first error |
| Distribution | the source code does not need to be given | the source code and interpreter are needed |
| Best for | finished programs | development and testing, easy debugging |
Benefits and drawbacks
Compiler
- Benefits: fast execution, the source code is kept private, no translator needed to run the program.
- Drawbacks: compiling takes time, and it is harder to find and fix errors during development.
Interpreter
- Benefits: errors are found as the program runs, which is good for testing, and programs can run on any computer with the interpreter.
- Drawbacks: slower, and the source code is always needed.
Partly compiled and partly interpreted
Some languages use both. Java is compiled into bytecode, which is then interpreted (or compiled on the fly) by a virtual machine, so the same bytecode can run on different computers.
Common mistake: saying an interpreter "compiles the program line by line". An interpreter executes each statement as it translates it and does not produce an executable file. Also, a compiler reports all errors after translation, not the first one only.
Exam tip: when asked to justify the use of a compiler or interpreter, name the situation (development and testing versus a finished program for sale) and give the matching benefit.
Check yourself
What does an assembler translate?
Assembly language into machine code.
Give one advantage of a compiler over an interpreter.
The compiled program runs faster, or the source code does not have to be supplied.
Why is an interpreter useful during program development?
It runs the program line by line and stops at an error, which makes testing and debugging easier.