Interpretation and compilation in more detail
Introduction
Programmers write programs in source code. This might be using BASIC or Pascal or C++ or Visual Basic, for example. However, this code cannot be executed directly on a computer. First, the source code must be translated into a form that the computer can understand. This can be done using interpreters and compilers.
Compilers and interpreters
This type of translator takes the whole source code and compiles it into object code. The object code (sometimes called machine code) can then be run. Pascal is an example of a programming language that uses compilation. An Interpreter on the other hand takes the source code and translates the first line of the program and then executes it. It then does the second line, and the third line, until it gets to the end of the code. BASIC, LISP, PROLOG and APL are programming languages that use interpretation.
Compilation and interpretation compared
-
- Compilation is much faster than interpretation. Once the compilation process has been completed, the object code will run faster than the same interpreted code. And you only have to compile the code once.
- Object code does not need the compiler to actually run it, only to convert it from source code to object code. Therefore, the object code produced by a compiler can be distributed to other computers without the compiler. You could write a program and then sell the object code on CD to anyone who wants to run the application.
- Object code is difficult to 'read' so that it can be distributed without actually revealing the code. This helps protect your intellectual rights.
- Source code can be compiled in sections to produce object code in sections. You can't do this with interpreters. If you have a lack of free space in RAM, compilation may be more useful.
- If there is an error in interpretation, the program will run successfully up to the point where the error occurs. Then it will stop. All of the variables are available for inspection at that point. Once the error has been corrected, the program doesn’t need to be recompiled - it’s just run again using the interpreter. Because of this, debugging and program development are easier and quicker using interpretation compared to compilation.
- Interpreters are far simpler to write than compilers. This is why JAVA works - because it is a relatively easy job to write an interpreter for a particular computer or application compared to writing a compiler for the machine.
How is source code actually translated?
A translator's job is to turn source code into object code. It must be able to translate good code that follows all the rules of the programming language as well as signal problems with bad code. There are three distinct steps to producing object code: lexical analysis, syntactic analysis and code generation.
During the lexical and syntactical analysis stages, any code that cannot be analysed will be passed to the error analyser software routine. If errors are found, the report generator will display error messages and object code will not be produced. If the code produced by the lexical and syntactical analysis stages produce no errors, then the object code is generated and the report generator will say as much. It will report on other details, such as how much space the object code takes up in memory.