“If you lie to the compiler, it will get its revenge” Henry Spencer
In this section we will explain what the compiler and interpreter are and what are their purposes?
In programming language section we mentioned that a computer’s language is just the combination of 0s and 1s. Because of the level of difficulty that this language has for anyone that wants to learn and use it, computer designers started to work on higher level languages like C and C++ and others that are closer to English syntax and so developers can learn and use them in their application development.
But there is a problem that needs to be solved here.
As mentioned, the language of a computer is just 0s and 1s, so how does a computer know how to run instructions written via a higher-level language like C or C++ or any other language?
This is where compilers and interpreters come in!
Compilers and interpreters are applications just like any other application you see around and their job is to interpret the syntax of higher-level language into computer’s language so that computers can run them.
But here’s the difference between Compilers and Interpreters:
Just like an interpreter-person that interprets a pre-written book or an article in one language to another, compilers do this interpretation between a source-code written in a higher-level language and the computer’s language.
Basically compilers do the interpretation on the source-code and produce the final machine code.
On the other hand we have Interpreter programs that act like an interpreter person whose job is to interpret between two persons with two different languages when they are actually in a live conversation. In this situation this interpreter gets the speech of one person and interpreted to the language of the other person. Then when the other person says something the interpreter translate this speech back to the first person.
To explain more: when we use interpreters, our source code is interpreted one line at a time and then this line is executed by the machine and when the machine finished the execution of that line of code the next line in the source code is interpreted and again that second line is executed by the processor and so on…
So interpreter application does the live interpretation but compilers interpret the entire source code in the first place and give you the final machine code that you can run on the target machine.
Depending on the type of language, they might use compiler or interpreter or even both!
Some of the languages that use interpreters to run their source code line by line are: PHP and JavaScript.
Also some of the languages that use compilers to compile (interpret the entire source code before executing it) are: C and C++
And those that are hybrid (use both techniques) are: Java and C# to name a few.
The pros of compiler based languages are:
It is faster because the compiler will first compile the entire source code and then gives the final machine code so the machine can run the entire code without waiting for any delay of translating one line of the source code at a time and then running it.
Also your source code will be hidden and no one can see that because what you send to a machine is the compiled version of the source code not the actual source code.
The cons of compiler based languages are:
If there’s a bug in the source code, there’s nothing you can do to fix the bug at runtime unless you fix the source code and compile the code again. This might end up catastrophic because if the machine does not stop running your application because of the bug, it might go further and run more instructions of your application and increase the problem within the program.
The pros of using interpreter languages are:
Your source code is interpreted one line at a time and then that line will be run by the machine and if there’s a bug in the source-code, you can fix that before interpreter interpret you next line of code.
The cons of using interpreter languages are:
You source code will be public and anyone can see that.
Also because there’s an interpreter between the source code and the machine, this can slow down the process of executing the source-code instructions.
Note: Both interpreters and compilers will check the syntax of your source-code and make sure they are following the syntax of the related language and if they find any inconsistency in the source-code, they’ll remind you about the error and stop compiling and interpreting the source-code.
Using either compiler or interpreter based language, the bottom line is the fact that developers can write their code in a language that they can understand and then send their final source code into the related compiler or interpreter of the language in order to get and run the machine code.
But for those compiler-based-languages and to some extends interpreter-based-languages, this machine code alone cannot run on the target computer and there are other pre-requisites and libraries that need to be added to this compiled code.
What are these pre-requisites and libraries and why our source code needs them is the topic of the next section which is “the linkers“.