Search

21 February, 2022

What is a Compiler and Interpreter? Difference Between Compiler and Interpreter

 compiler is a program that translates a source program written in some high-level programming language (such as Java) into machine code for some computer architecture (such as the Intel Pentium architecture). The generated machine code can be later executed many times against different data each time.

An interpreter reads an executable source program written in a high-level programming language as well as data for this program, and it runs the program against the data to produce some results. One example is the Unix shell interpreter, which runs operating system commands interactively.

Note that both interpreters and compilers (like any other program) are written in some high-level programming language (which may be different from the language they accept) and they are translated into machine code. For a example, a Java interpreter can be completely written in C, or even Java. The interpreter source program is machine independent since it does not generate machine code. (Note the difference between generate and translated into machine code.) An interpreter is generally slower than a compiler because it processes and interprets each statement in a program as many times as the number of the evaluations of this statement. For example, when a for-loop is interpreted, the statements inside the for-loop body will be analyzed and evaluated on every loop step. Some languages, such as Java and Lisp, come with both an interpreter and a compiler. Java source programs (Java classes with .java extension) are translated by the javac compiler into byte-code files (with .class extension). The Java interpreter, called the Java Virtual Machine (JVM), may actually interpret byte codes directly or may internally compile them to machine code and then execute that code (JIT: just-in-time compilation).



What is Interpreter?

An interpreter is a computer program, which converts each high-level program statement into the machine code. This includes source code, pre-compiled code, and scripts. Both compiler and interpreters do the same job which is converting higher level programming language to machine code. However, a compiler will convert the code into machine code (create an exe) before program run. Interpreters convert code into machine code when the program is run.

Difference Between Compiler and Interpreter

Basis of difference Compiler Interpreter
Programming Steps
  • Create the program.
  • Compile will parse or analyses all of the language statements for its correctness. If incorrect, throws an error
  • If no error, the compiler will convert source code to machine code.
  • It links different code files into a runnable program(know as exe)
  • Run the Program
  • Create the Program
  • No linking of files or machine code generation
  • Source statements executed line by line DURING Execution
Advantage The program code is already translated into machine code. Thus, it code execution time is less. Interpreters are easier to use, especially for beginners.
Disadvantage You can’t change the program without going back to the source code. Interpreted programs can run on computers that have the corresponding interpreter.
Machine code Store machine language as machine code on the disk Not saving machine code at all.
Running time Compiled code run faster Interpreted code run slower
Model It is based on language translation linking-loading model. It is based on Interpretation Method.
Program generation Generates output program (in the form of exe) which can be run independently from the original program. Do not generate output program. So they evaluate the source program at every time during execution.
Execution Program execution is separate from the compilation. It performed only after the entire output program is compiled. Program Execution is a part of Interpretation process, so it is performed line by line.
Memory requirement Target program execute independently and do not require the compiler in the memory. The interpreter exists in the memory during interpretation.
Best suited for Bounded to the specific target machine and cannot be ported. C and C++ are a most popular programming language which uses compilation model. For web environments, where load times are important. Due to all the exhaustive analysis is done, compiles take relatively larger time to compile even small code that may not be run multiple times. In such cases, interpreters are better.
Code Optimization The compiler sees the entire code upfront. Hence, they perform lots of optimizations that make code run faster Interpreters see code line by line, and thus optimizations are not as robust as compilers
Dynamic Typing Difficult to implement as compilers cannot predict what happens at turn time. Interpreted languages support Dynamic Typing
Usage It is best suited for the Production Environment It is best suited for the program and development environment.
Error execution Compiler displays all errors and warning at the compilation time. Therefore, you can’t run the program without fixing errors The interpreter reads a single statement and shows the error if any. You must correct the error to interpret next line.
Input It takes an entire program It takes a single line of code.
Output Compliers generates intermediate machine code. Interpreter never generate any intermediate machine code.
Errors Display all errors after, compilation, all at the same time. Displays all errors of each line one by one.
Pertaining Programming
languages
C, C++, C#, Scala, Java all use complier. PHP, Perl, Ruby uses an interpreter.