compilers and interpreters
compilers and interpreters You write this The computer translates it... into this, which it can run
compilers and interpreters 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. 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.
compilers and interpreters 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 PRINT COUNT * COUNT 30 NEXT COUNT
Diff compilers and interpreters We usually prefer to write computer programs in languages we understand rather than in machine language, but the processor can only understand machine language. So we need a way of converting our instructions (source code) into machine language. This is done by an interpreter or a compiler. An interpreter reads the source code one instruction or line at a time, converts this line into machine code and executes it. The machine code is then discarded and the next line is read. The advantage of this is it's simple and you can interrupt it while it is running, change the program and either continue or start again. The disadvantage is that every line has to be translated every time it is executed, even if it is executed many times as the program runs. Because of this interpreters tend to be slow.
Diff compilers and interpreters A compiler reads the whole source code and translates it into a complete machine code program to perform the required tasks which is output as a new file. This completely separates the source code from the executable file. The biggest advantage of this is that the translation is done once only. The program that is run is already translated into machine code so is much faster in execution. The disadvantage is that you cannot change the program without going back to the original source code, editing that and recompiling.