Compilers and Interpreters
Compilers and Interpreters
These are programs which translate computer programs from high-level languages such as Pascal,
C++, Java or JavaScript into the raw 1s and 0s which the computer can understand, but the human
programmers cannot:
You write this The computer translates it ... into this, which it can run
Compilers
Compilers were the first sort of translator program to be written. The idea is simple: You write the
program, then hand it to the compiler which translates it. Then you run the result.
The compiler takes the file that you have written and produces another file from it. In the case of
Pascal programs, for instance, you might write a program called myProg.pas and the Pascal compiler
would translate it into the file myProg.exe which you could then run. If you tried to examine the
contents of myProg.exe using, say, a text editor, then it would just appear as gobbled-gook.
The compiler has another task apart from translating your program. It also checks it to make sure that it
is grammatically correct. Only when it is sure that there are no grammatical errors does it do the
translation. Any errors that the compiler detects are called compile-time errors or syntax errors. If it
finds so much as one syntax error, it stops compiling and reports the error to you. Here is an example
of the C++ compiler reporting a whole list of errors:
Most "serious" languages are compiled, including Pascal, C++ and Ada.
Interpreters
An interpreter is also a program that translates a high-level language into a low-level one, but it does it
at the moment the program is run. You write the program using a text editor or something similar, and
then instruct the interpreter to run the program. It
takes the program, one line at a time, and translates each line before running it: It translates the first
line and runs it, then translates the second line and runs it etc. The interpreter has no "memory" for the
translated lines, so if it comes across lines of the program within a loop, it must translate them afresh
every time that particular line runs. Consider this simple Basic program:
10 FOR COUNT = 1 TO 1000
20 PRINT COUNT * COUNT
30 NEXT COUNT
Line 20 of the program displays the square of the value stored in COUNT and this line has to be carried
out 1000 times. The interpreter must also translate that line 1000 times, which is clearly an inefficient
process. However, interpreted languages do have their uses, as we will see in a later section.