Only machine code can be understood by the computer. Everything else has to be translated (converted) into machine code.
Translators are a type of software which translate (convert) code into machine code that the computer can run.
There are three types of translator: compilers, interpreters, and assemblers.
Compilers and interpreters work on high-level languages. Assemblers work on assembly language.
Table 1 shows the differences between compilers and interpreters.
Table 1
| Compilers | Interpreters |
|---|---|
| Translates the whole source code at the same time and creates an executable file. | Translates and runs the source code one instruction at a time, without creating an executable file. |
| Only needed once to create the executable file. Then, the file can be run again and again without the compiler. | Needed every time the program is run because no executable file created. |
| Compiled program executes faster because it is in machine code. | Runs slower because program needs to be translated each time. |
| Other people cannot see the original source code once program has been compiled. | Other people can see and copy the source code if distributing an interpreted program. |
Note: interpreters don't translate directly into machine code — instead, they take each instruction in the code and call machine code subroutines within their own code to carry out that instruction.
Which translator is better for code which will be distributed: compiler or interpreter?
Compiler:
- Customers will not need to have the translator installed on their computer
- Other people will not be able to see and copy the source code
- The program will execute faster as it is in machine code