Translators (SQA National 5 Computing Science): Revision Note
Exam code: X816 75
Translators
What is a translator?
A translator is a program that converts source code written in a programming language into machine code so that it can be executed by a computer’s processor
There are two main types of translator:
Compiler – translates all the source code into machine code at once, producing an executable file
Interpreter – translates and executes the program one line at a time
Translator | How it works | When it’s used | Notes |
|---|---|---|---|
Compiler | Translates the whole program into machine code before running | Used when a program is complete and ready to be distributed | Creates an executable file |
Interpreter | Translates and runs one line of code at a time | Used during program development and testing | Easier to find and fix errors |
Key points:
A compiler must recompile the program each time it changes
An interpreter stops when it finds an error
Both translate high-level languages (e.g. Python, Java, C++)
Worked Example
A programmer has written a fixed loop to calculate the total duration of 10 songs for a playlist:
REPEAT 10 TIMES
RECEIVE songDuration FROM KEYBOARD
SET totalDuration TO totalDuration + songDuration
END REPEATExplain the difference in how a Compiler and an Interpreter would translate the lines of code inside the loop (RECEIVE and SET) during the execution process.
[4]
Answer
Compiler:
Translates the full code (including the loop) once [1 mark]
Before execution begins [1 mark]
Interpreter:
Translates the code line by line [1 mark]
Repeatedly (for every iteration of the loop) during execution [1 mark]
Unlock more, it's free!
Did this page help you?