A translator is used to translate a source code written in high level language to machine code, so, that computer can process and execute the converted code and provide the desired output. A compiler and interpreter are examples of such translators.
Compiler and interpreter are itself programs that are responsible for turning a source code to machine code.
Back in the days, people used to code in low level languages which could be directly processed in machine code. More or less, it was a set of instructions that was easy to process and understand by system.
A low level language is difficult to write, understand and debug. This is the reason, people start moving towards high level languages. Such as C++, Java and Python. As these languages were closer to natural language that a human can understand and easy to debug.
But the drawback was, these high level languages were not readable and understandable to computer system. Therefore, compilers and interpreter are used as code translators.
Difference between Interpreter and Compiler in Tabular form
Compiler | Interpreter |
It reads entire source code and translate it to machine code | It reads, translates and executes each and every statement of a source code line by line |
It produces intermediate code which can be executed afterwards | Translation and execution of a source code take place together |
It doesn't provide platform independent code. In other words, a executable produced on Window might not be run on Mac OS | It provides platform independent code |
It generates an object code or executable | As it scans, translates and executes code on the fly, therefore, no intermediate code is generated |
Java,Scala and C++ are some examples | JavaScript, Python and PHP are some examples |
The object code can't be debugged | Since, the source code is visible. One can use and debug the source code |
An object code runs faster and performs better as compared to Interpreter | Due to line by line execution of a code, it serves slower code execution |
Since, it is not platform independent. Therefore, it is not portable | It is portable |
The source code doesn't need to be shared with others, instead an object code can be used directly on other systems | The source code required to be shared, so that, interpreter can do its task |
What is Compiler ?
A compiler reads entire source code first and translate it to machine code.
It produces an object code that can be executed later in single run.
However, it takes some time to scan, analyse and translate the provided code before producing executable.
Brief working of Compiler
Lexer : it splits the code in tokens
Parser: It identifies grammar of the language and create a parse tree and then to syntax tree
Generator: converts the syntax tree to the desired language
What is Interpreter?
An interpreter reads a source code line by line and immediately executes it.
It doesn’t produce any executable, instead, it scans,translates and executes source code line-wise simultaneously.
In other words, it runs a code on fly. It does its work much faster, but it slows down the code execution speed and performance.
But behind the scenes, it scans a line from the source code, translate it and then execute it one by one until the interpreter scans last line of the code.