What is a Language Translator?
To run a program on a computer, the program needs to be translated into the machine language of the computer on which it will run. A language translator is a computer program that translates program instructions from one language into another without loss of original meaning. There are three types: assembler, compiler, and interpreter.
Assembler
An assembler translates instructions from assembly language into machine language. This process is called assembling.
Compiler
A compiler is a computer program that translates an entire block of instructions written in a high-level language into machine language instructions before executing it. The high-level language program is called the source code, and the generated machine language program is called the object code. Examples of compiled languages include Pascal, C, C++, FORTRAN, Java, and Python.
Advantages: fast in execution; the object/executable code can be distributed or executed without the compiler present; the object program can be reused without recompilation.
Disadvantages: debugging is harder, since it is not as good at finding errors; when an error is found, the whole program has to be re-compiled.
Interpreter
An interpreter is a computer program that translates and executes instructions written in a high-level language into machine language, one line at a time. If a program performs a section of code 1000 times, that section is translated into machine code 1000 times, since each line is interpreted and then executed. Examples include BASIC and Lisp.
Advantages: good at locating errors since interpretation is done line by line; debugging is easier since the interpreter stops when it encounters an error; no need to retranslate the whole program if an error is corrected.
Disadvantages: slow, since interpretation is done line by line; translation must be redone every time the program is executed, since no object code is produced; the interpreter must be present for the program to run.
Compilers vs Interpreters
- Compilers translate source code into machine code before execution, while interpreters translate and execute source code line by line.
- Compilers produce an executable file, while interpreters do not.
- Compilers are faster in execution, while interpreters are slower.
- Compilers detect errors at compile-time, while interpreters detect errors at runtime.