Programming Languages and Translators
Software · 4 question types
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.
Naming a translator or describing how a compiler works
Question: Identify or name the translator from what it does or the language it translates, or describe how a compiler translates a program and reports errors (1–3 marks).
Asked in 3 of the 17 papers. To name the translator, match the clue: converting and executing line by line, stopping at an error, is the interpreter; producing an executable file, or one error report of every error before execution, is the compiler; an assembly-language program needs the assembler. If asked what the high-level code is translated into, write machine code (low-level language or object code also earns the mark).
Describing how a compiler translates earns three marks: it converts the high-level code into machine code, translates all of it before execution, and creates an executable file. Describing how it reports errors earns two: it produces an error report after the attempt to compile, listing every error, which must be corrected before the program can run. Line-by-line checking belongs to the interpreter.