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.
- C/C++
- COBOL
- Java
- ALGOL
- Ada
- Fortran
- Swift
- F#
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 –
- PHP
- Ruby
- Python
- JavaScript
- Bash
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.
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.
Conclusion
You have successfully learned about compilers and interpreters and the differences between them. Now for any query write us in the comments below.