Translators, Compilers & Interpreters (OCR GCSE Computer Science): Revision Notes
📚 Revision Notes
Translators, Compilers & Interpreters
Purpose of Translators
Computers can only understand machine code (binary). Since human programmers use assembly or high-level programming languages, these must be translated into machine code for the computer to execute them. Translators serve this purpose, and there are different types for different levels of languages:
- Assembler: Translates assembly code into machine code.
- Compiler: Translates high-level language into machine code by converting the entire programme at once.
- Interpreter: Translates high-level language into machine code line by line, executing each instruction after translation.
Types of Translators
Assembler
- The assembler is responsible for translating assembly language into machine code.
- This is a direct and efficient translation process but is limited to the specific hardware for which the assembly language is designed.
Compiler
- A compiler translates the entire programme written in a high-level language into machine code (also known as object code) before running the programme.
- Once the programme is compiled, the resulting object code can be executed directly without needing the source code or the compiler itself.
- This method is generally used when distributing software to ensure that users cannot access the source code. It also allows the programme to run quickly, as it doesn't need to be translated repeatedly. However, compiling can be slow, and errors are shown only after the entire code is compiled, making it more challenging to debug.
Interpreter
- An interpreter translates the programme line by line, converting each instruction into machine code and executing it before moving to the next one.
- Unlike a compiler, an interpreter does not create object code, and the source code must be translated every time the programme is run.
- This makes interpreters ideal for the development process, as they allow programmers to test and debug their code in stages, getting real-time feedback on errors. However, it is slower than a compiled programme since the translation occurs each time the programme is executed.
infoNote
Object Code: The machine code produced by a compiler from the source code. It can be executed independently from the original source code and the compiler.
Characteristics of Compilers and Interpreters
Compiler
- Converts the entire code into machine code in one go, creating object code that can be executed later.
- Better suited for distributing software as it hides the source code and makes the programme run faster.
Interpreter
- Converts code line by line, executing it immediately after each instruction.
- Best for the development process since it provides real-time feedback and helps find errors more easily.
Differences, Benefits, and Drawbacks
| Feature | Compiler | Interpreter |
|---|---|---|
| Translation Method | The entire programme is converted to object code at once. | Line-by-line translation and execution. |
| Run Time | Faster, as it produces standalone object code. | Slower, as it translates every time it is run. |
| Error Handling | Errors are shown after the entire programme is compiled. | Easier to identify errors, as it stops at each error. |
| Distribution | Ideal for software distribution, as the source code is hidden. | Source code must be available for execution. |
| Development Usage | More useful for finished products. | Preferred during development and testing. |
infoNote
Key Points to Remember
- Translators are essential for converting human-readable code into machine code.
- Compilers are more efficient for distributing software and executing final programmes, but slower during the development phase due to the entire programme being compiled at once.
- Interpreters offer a flexible environment for development but are less efficient for execution, as they translate code in real time.