Compilers vs Interpreters






Compilers and Interpreters are programs which translate computer programs from high-level languages such as Pascal, C++, Java or JavaScript into the raw 1's and 0's which the computer can understand, but the human programmers cannot

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 C programs, for instance, you might write a program called myProg.c and the C language 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 gobbledy-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.

Examples of interpreted languages are Basic, JavaScript and LISP.



So Which is Better??
Well, that depends on how you want to write and run your program. The main advantages of compilers are as follows:

* They produce programs which run quickly.
* They can spot syntax errors while the program is being compiled (i.e. you are informed of any grammatical errors before you try to run the program). However, this does not mean that a program that compiles correctly is error-free!

The main advantages of interpreters are as follows:

* There is no lengthy "compile time", i.e. you do not have to wait between writing a program and running it, for it to compile. As soon as you have written a program, you can run it.
* They tend to be more "portable", which means that they will run on a greater variety of machines. This is because each machine can have its own interpreter for that language. For instance, the version of the BASIC interpreter for the PDP series computers is different from the QBasic program for personal computers, as they run on different pieces of hardware, but programs written in BASIC are identical from the user's point of view.

Some computer systems try to get the best of both worlds. for instance, when I was at Durham, we programmed in Pascal on the old PDP/11 machines. Running a Pascal program on those machines was a two-stage process. Firstly, we ran a compiler program (called pc) which compiled the program to a low-level version, and spotted any grammatical errors in the process. We then ran an interpreter program which took the output of pc and ran it. The fact that pc produced something that didn't have to run directly as machine code made the program more portable. Different versions of the low-level interpreter could be written for different machines in the PDP range, each taking as its input the same output from pc

Leave a Reply