Wednesday, December 26, 2018

Difference between Compiler and Interpreter

Difference between Compiler and Interpreter

Difference Between Compiler and Interpreter with full explanation
The main difference between the Compiler and Interpreter is that they translate high-level programming language into machine code but in different way. Actually both do the same work but in different way.
This is the main concept. Let's go the know with an explanation.
First of all, you must have a clear knowledge of programming language, machine code etc. If you do not have clear knowledge then you can read our article What is a programming language and machine language?
We already know that a computer processor or microprocessor only understand the machine code or machine language. The machine code is the combination of 0s and 1s and it is very difficult to write a program in machine language. So, therefore, the assembly language discovered. We can easily write a program in assembly language. But assembly language programmers are machine depended. An assembly language program can be used only with a particular machine. We can not use an assembly language program on the different machines. So we need the general purpose languages like BASIC, FORTRAN etc which are machine independent and these are can be used in the different machine.
As a microprocessor or computer processor cannot understand any language except machine language. So we need to convert general purpose or high-level languages into the machine language. To convert programming language into machine language we use a program, it may be Compiler or maybe Interpreter. So Compiler and Interpreter are nothing but a program. They are not any device.

What is the Compiler?

The Compiler is a program that reads a program written in general purpose languages or high-level languages as source program and translate it into an equivalent program in machine code or machine language as a target program with reports to its user if any error present in the source program.

compiler

What is Interpreter

The Interpreter is a program that reads a part of a program at a time written in high-level languages program and translates it into machine code or machine language. This process will continue until the whole program is converted into an equivalent program in machine language.

Compiler VS Interpreter



Comparison   Elements

Compiler

Interpreter

Input
The input of the compiler is the whole program written in high-level language.
The Interpreter takes only a part or a single line of a program written in high-level language as input.

Output
The output of the compiler is the whole program in machine code equivalent to the input program.
The output of the Interpreter is the part or single line of the input program in machine code at a time.
No. of output per program
It gives the only one output for a particular program.
It gives no. of outputs in a sequence for a particular program.
Target program
Compiler can produce a Target Program.
The interpreter cannot produce the target program.
Characteristics
The compiler is a more complicated program
than Interpreter.
The interpreter is a simpler program than Compiler.
Source Code
The compiler does not need source code once the program is compiled.
The interpreter has needed the source code every time until the whole program is translated.
Object Code
The compiler usually generates intermediate object code.
The interpreter does not generate any intermediate object code.
Time required
As the compiler translates the whole program at a time that is why it needs less time than Interpreter to translate a particular program.
The interpreter has needed more time than Compiler to translate the same program.

Memory required
We know that compiler and Interpreter both is nothing but a program. So they need memory. As the compiler generates object code that is why it needs more memory than Interpreter.
The interpreter does not produce any object code that is why it needs less memory than the compiler.
Errors Displaying
Compiler displays all the errors in the input program together only after compilation of the whole program.
Interpreter displays the errors of the source program one by one when it translating the statements of the program.
Error correction
To correct the errors in the source program we need to read the whole program so the error detection is difficult for the compiler.
To correct the errors in the source program we do not need to read the whole program so the error detection is easy for Interpreter.
Uses
The Use of Compiler is best for practical application and production.
The use of Interpreter is best for practice programming, development, research.
Execution
As the compiler takes less time to translate the program and can translate the whole program at a time so the execution process also required less time.
As the interpreter translates the program step by step so the execution time also required more time.
Efficiency
The efficiency of the Compiler is less than Interpreter.
The efficiency of the Interpreter is more than Compiler.
Programming Languages
Compiler mainly used for translate C, C++, C# programming languages.
Interpreter mainly used for translate Java, PHP, Python, Ruby programming languages.
Debugging
Debugging is hard for the compiler.
Debugging is very easy for Interpreter.
Attachment with software
The compiler can be easily attached to any programming writer software.
It is very hard to attach the Interpreter to any programming writer software.
Code Alternation
The compiler does not allow the alternation of codes when it is in running condition.
Code alternation is possible during the running time of the interpreter.
Code Optimization
Code optimization can be expected.
Code optimization is not possible.
These are the main important differences between compiler and Interpreter. If you want to know more details about compiler and Interpreter then read the below parts.

How Compiler converts program?

To translate the program written in high-level languages into the program in machine code or machine language Compiler operates in different phases,
  1. Lexical analysis
  2. Syntax analysis
  3. Semantic analysis
  4. Intermediate Code Generation
  5. Code optimization
  6. Code generation
In the Lexical analysis, Syntax analysis, Semantic analysis phases the compiler breaks up the source or input program written in high-level languages into the into constituent pieces and creates the intermediate representation of the input high-level language programme. These three phases together are called Analysis.
compiler different phases
We already knew that compiler generates a target program as it's output. So in the Intermediate Code Generation, Code optimization, Code generation phases the compiler constructs the target program according to the intermediate representation. These three phases together called Synthesis.
So the basic working principle of the Compiler is that the compiler first breaks up the source program written in high-level languages then it constructs a program(called target program) in machine codes or machine language.

How Interpreter converts program

To convert the high-level language program into machine language the Interpreter has two parts,
  1. Parser
  2. Evaluator
The parser takes the single line or statements of the source program as input and produces the structural parse of the input program.
Then the evaluator takes the structural parse of the program as input and evaluates that program.
interpreter

The difference of Compiler, Interpreter with Assembler

Actually, an assembler is also a translator. The compiler and interpreter can convert the high-level language program into the machine language, but an assembler only can convert the assembly language programme into machine code which can be directly used by the processor. 
The efficiency and speed of Assembler are more than both compiler and interpreter. 
Assembler is a small program which is stored in the machine by the manufacturer.
For example, if you buy a microcontroller, the assembler already installed in the microcontroller.
Conclusion
I have tried to explain easily the difference between Compiler and Interpreter. I also have given the idea of the difference of compiler and interpreter with assembler.
The compiler and Interpreter do same work but the assembler does different work.

If you like this article please do share. Thank you for visiting the website. keep visiting for more updates.

No comments:

Post a Comment