Complier vs interpreter in a programming language.
We generally write a computer program using a high-level language. A high-level language is one that is understandable by humans. But a computer does not understand high-level language. It only understands program written in 0's and 1's in binary, called the machine code. A program written in a high-level language is called source code. We need to convert the source code into machine code and this is accomplished by compilers and interpreters. Hence, a compiler or an interpreter is a program that converts a program written in a high-level language into machine code understood by the computer.
There are a lot of tradeoffs, but they are oriented towards details of the implementation of the language, not so much the language itself. So try not to worry about it so much.
Let me quote a simple, non-programming example. Consider the following sequence of operations:
- Think of a number (say n)
- Add 30
- Subtract 20
- Multiply by 4
- Add 10
- Divide by 2
Let us assume the above sequence of operations to be a computer program with “n” being the input and analyze what happens with this program when given to a Compiler and an Interpreter.
Before even asking you what “n” is, the compiler will do a pre-processing step. It will generate something easier to process. For example, something like “((n+30-20)*4+10)/2” (In binary). Once this is generated, the execution starts and it asks you what “n” is and merely substitutes the value you supply into this formula. Even if you repeatedly run this program many times, the compilation (pre-processing) step happens only once. So you have a compact understandable notation that is easy for processing. The formula we generated is equivalent to the machine code that the compiler generates.
Compilers are programs that take source code (as text files), and translate that code into another language.
The Interpreter will literally follow the instructions written there, step by step without any preprocessing step. It will first ask you what “n” is, then add 30 to it, then subtract 20 from it, and so on. No matter how many times you run the program, it does the same set of steps over and over again.
The interpreter is a program that translates statements of a program into machine code. It translates only one statement of the program at a time. It reads only one statement of the program, translates it, and executes it. Then it reads the next statement of the program again translates it and executes it. In this way, it proceeds further until all the statements are translated and executed.
Hope this was useful to you! Let me know in the comment section.