The computer programs earlier were coded primarily in assembly language. Writing programs in assembly was time-consuming and also limited to the machine on which these were developed.
Later high-level programming languages were developed writing code in this is more like writing it in English. This enhances the code portability and productivity of programmers. Before executing code written in a high-level language you need to convert it into computer understandable machine or binary code. For this purpose compilers and interpreters are used.
This article is all about compiler, interpreter, and difference between these two. If you know any programming language then probably you may familiar with these words.
What is a compiler?
A compiler is a computer program that converts code written in a high-level language to computer-readable machine code. It compiles the whole program in one go and displays all the errors if there any in the program. Otherwise, it converts the human-readable code into machine code.
Some of the popular programming languages that use compilers are given below.
For example, GCC is a compiler used for C and C++ programming languages.
Working of compiler
The compilation of code is done in two-phase these are given below-
Analysis phase– In the analysis phase, all the source code is analyzed for errors this includes grammatical, syntax, and semantic errors, etc. After this, an intermediate code is generated. This phase is also known as the front-end phase.
Synthesis phase – In this phase intermediate code generated in the analysis phase is optimized and converted to target machine code. This phase is also known as the backend phase.
Once the code is translated to machine code it is executed to perform the required task.
What is an interpreter?
An interpreter is a computer program that converts each line of code written in a high-level programming language into computer-readable machine code. Unlike a compiler that processes all the code in a single go, it interprets each line of code one by one.
If the interpreter finds something wrong in a statement it will stop and display the error when you correct that statement it will move to execute the next line of code.
In this way, it converts all the code into computer-executable code.
Some programming languages that use interpreter are –
Working of interpreter
The interpreter executes a source code line by line and whenever it encounters an error in the statement it prompts the programmer for correction. It does not proceed until the error in the line is corrected. Once the code is corrected the control of execution move to the next line in the code.
If it does not find any error it converts source code into targeted machine code.
Compiler vs interpreter: The differences
The following table show some difference in compiler and interpreter.
|Scan the whole program in one go||It translates one line of the code at a time|
|Errors and warnings are displayed at the end of compilation||Error or warnings are displayed line by line|
|Generates object codes so requires comparatively more memory||Object code is not generated so it is memory efficient|
|Compiler are fast in overall executing the code||Interpreters are comparatively slower than compilers|
|Once the object code is generate the source code is not needed for execution||It requires source code every time you execute the code|
|Program execution is separate from compilation. A program can be executed one it is compiled successfully||Program execution is the part of interpretation process so it is done line by line|
|C/C++, Java are some languages that uses compilers||PHP, Python, Ruby etc are some languages that uses interpreter|
More about compilers and interpreters
As compilers generate object code you can execute a program without having source code but if you want to make any changes to it you need to go back to the source code of that program.
In an interpreted language, no object file is generated so you are always working with the source code. That means interpreters make it easier to work with source code.
You have successfully learned about compilers and interpreters and the differences between them. Now for any query write us in the comments below.