Programming Languages and Translators · Cambridge syllabus 4.2 · Lesson 2 of 3
Translators
An assembler translates assembly language into machine code. A compiler translates the whole program at once, before it runs, and produces an executable file; it reports all the errors it finds in one error report. An interpreter translates and executes the program line by line, and stops as soon as it reaches an error.
By the end, you can
- Explain how a compiler translates source code.
- Identify advantages of compilation.
- Identify disadvantages of compilation.
- Explain how an interpreter translates and executes code.
- Identify advantages of interpretation.
- Identify disadvantages of interpretation.
- Compare compilers and interpreters.
- Explain why interpreters are useful during development.
- Explain why compiled programs can be suitable for final distribution.
Compiler
A processor executes machine code and nothing else, so any high-level program must be translated first. A compiler translates the whole source program into machine code in one go, before it is run, producing an executable file.
Compilation happens once. The result is a complete program that runs on its own, with no translator present and no translation happening while it runs, so it executes quickly — the translation cost has already been paid.
That is why software is distributed compiled: the user needs no translator installed, and the source code is not given away, which protects it.
The drawbacks appear while developing. The whole program must compile before any of it can be tested, and a compiler reports errors as a list at the end rather than stopping at the first, so a small change means compiling again and waiting. The executable also only runs on the type of machine it was compiled for, so a separate version must be produced for each.
Worth remembering
- Translates the entire program once, producing an executable that runs on its own.
- Fast execution and no translator needed by the user; source code stays private.
- The whole program must compile before any testing, and it is machine specific.
Interpreter
An interpreter translates and runs a program one statement at a time. It takes a line, converts it, executes it immediately, and moves to the next — and it does this every time the program runs, so no executable file is ever produced.
This is excellent while writing a program. Execution stops at the first error and reports exactly where it is, which makes mistakes easy to find; a single line can be changed and the program run again immediately with no waiting; and part of a program can be tested before the rest exists.
It is also portable in a useful way: the same source code runs on any machine that has an interpreter for that language, since it is the interpreter, not the program, that is machine specific.
The cost is speed. Translation happens every run, and a line inside a loop is translated again on every pass, so an interpreted program runs noticeably slower than the equivalent compiled one. The interpreter must also be installed on every machine that runs the program, and the source code has to be supplied, so it cannot be kept private.
Worth remembering
- Translates and executes one statement at a time, every time the program runs.
- Stops at the first error and reports where it is, which suits development.
- Slower, needs the interpreter installed, and the source code must be supplied.
Comparison
Set side by side, the two translators differ in four ways worth memorising: when translation happens, what it produces, how errors are reported, and how fast the result runs.
A compiler translates everything before execution, produces an executable file, reports all the errors together at the end of compilation, and gives a fast-running program. An interpreter translates line by line during execution, produces no file, stops at the first error and reports it immediately, and runs more slowly.
Interpreters suit development because the cycle is short. Change one line, run, see the result. There is no wait for the whole program to compile, errors point at a line, and unfinished code can be tested as it is written. Nearly all learning and experimenting is done this way.
Compilers suit distribution. The finished program is fast, needs no translator on the user’s machine, and can be shipped without its source. So an interpreter is mostly used while a program is being developed, and a compiler is used to translate the final program.
Worth remembering
- Compiler: all at once, before running, produces a file, fast to run.
- Interpreter: line by line, while running, no file, stops at the first error.
- Interpreters suit development; compilers suit distributing a finished program.
Worked example
A program with errors on lines 5 and 20: the compiler lists both errors and produces no executable; the interpreter runs lines 1 to 4 and stops at line 5, so the error on line 20 is not reported until line 5 is fixed.
Practice
Warm-up · not marked
Check your understanding
Answer in your own words, then compare with the worked answer. Nothing here is saved or scored.
Worked answer
An assembler.
Check your method, units and explanation against it.
Marked practice
Set 1 of 5 · easiest first · Checked as you go, with the worked answer. Sign in or create a free account to save your progress.