Thursday, July 23, 2015

Difference Between Compiler and Interpreter

Both compiler and interpreter are translators.In their simple definition, they are the programs that are used to convert one language into another language.
Your one language can be programming language like C, C++, Java etc. and another language can be machine language (binary code).
You can see from below diagram what they both does:







There is one more important work of these two programs other than translating the code. They are also used to check errors in the code. If your code is erroneous then they will not translate your code into another code.

So based on these two workings i.e. Checking Errors and Translation, compiler and interpreter differs from each other.

The major difference is that the compiler analyzes the whole code at a time and interpreter analyzes one line at a time.

So compiler reads the code fully at once, gives all the errors at once, and if code is error free then converts the full code whereas interpreter works line by line, it reads one single line at once, give error only at that particular line, and if that line is error free then it converts only that particular line.

Then question arises, which technique is the best to use?

If compiler is the best then why anybody will use interpreter at all and vice versa. Both the techniques have their advantages and disadvantages.

> Compiler is Slow but gives all the errors at once

             Now think you have developed a programs but it contains many errors at different lines. May be all these errors are coming due to some single problem in your code. So it will be better for you if you receive all the errors at the same time and you can find out the root cause of  all your errors. Compiler will take a a bit time to analyze your code but its better for you to get all the errors at once.
         Getting all the errors at once will make your debugging and development time very short. So what will you prefer to convert your source code into binary code? Obviously Compiler because source code is developed by human and humans learn by making errors.

>Interpreter is Fast but gives only one error at once

          Who doesn't like fast speed? But if you convert your source code using interpreter then the fast speed of interpreter will be only fruitful when your source code does not have errors (which is not true every time). You will get your error one by one, and it will get very difficult to find out the root cause of your errors when your code becomes complex. So interpreter's speed is very useful in case your code does not have any errors at all.

* Both compiler and interpreter will only check Syntax and Semantic (programming rules) errors.


In my previous post of how java is platform independent language? you have seen that Java uses both compiler and interpreter.

For converting source code, it uses compiler, since source code can be erroneous, so we want all the errors in one go. And for converting byte code into native code JVM uses an interpreter since byte code is already error free due to prior use of compiler, so at run time we will get fast speed of conversion by interpreter.

Hope that helped!!

2 comments: