High-level and low-level languages
A processor executes machine code — binary instructions specific to that make of processor. People do not write machine code, so programming languages exist at different distances from it. The specification asks for the characteristics and purpose of each level, and the differences between them.
Understanding of assemblers is not required by this specification, so the assembly language layer can be left alone — high-level versus low-level, and the two translators below, is the whole of what is examined here.
- High-level language
- a language close to human language, which is easier to write and understand and is portable between different machines, but must be translated before a processor can run it
- Low-level language
- a language close to machine code, such as assembly language, which is specific to one make of processor but allows direct control of the hardware
- Machine code
- binary instructions specific to a particular make of processor, which it can execute directly
The purpose of translators
A translator converts program code into machine code that the processor can execute. The need for one follows directly from the previous section: a processor cannot execute a high-level language, so something must convert it.
The program as written is called the source code. The machine code produced from it is the object code.
Worked example
A programmer is developing a new game and repeatedly testing small changes. Later the finished game will be sold to customers. Recommend a translator for each stage.
- During development, changes are frequent and each needs testing straight away.
- An interpreter runs code immediately without a compilation step, and reports the first error as it reaches it — which suits finding faults one at a time.
- For the finished product, the priorities change: it must run fast, and the source code should not be given away.
- A compiler produces an executable that runs faster because translation has already happened.
- Add the commercial point, since it is a genuine benefit: the customer receives only the executable.
Answer: An interpreter during development, because code can be run and tested immediately after each change without recompiling, and errors are reported one at a time as they are met. A compiler for the finished game, because the compiled executable runs faster and the customer receives only that file, so the source code is not revealed.
Common mistake
An interpreter does not find all the errors before running. It stops at the first error it reaches, which means later errors are only discovered once the earlier ones are fixed — the opposite of a compiler, which reports them together.
- Translator
- a program that converts program code into machine code the processor can execute
- Compiler
- a translator that converts the whole source code into object code in one go, before the program is run
- Interpreter
- a translator that converts and runs source code one line at a time, stopping at the first error
- Source code
- the program as written by the programmer, before translation
Before you practise
- High-level languages resemble English, are portable, and one statement becomes many machine code instructions.
- Low-level languages are close to machine code, are processor-specific, and give direct control over hardware.
- A translator is needed because a processor can only execute machine code.
- Source code is what is written; object code is the machine code produced.
- A compiler translates the whole program at once and produces an executable.
- An interpreter translates and executes line by line, every time the program runs.
- Compiled code runs faster; interpreted code is easier to test during development.
- A compiler reports all errors together; an interpreter stops at the first one.
- Assemblers are not required by this specification.