Friday 10 October 2014

Compiler and interpreter: steps, difference?

What is Compiler and interpreter:steps, difference?

It is one of the most important basic questions that can be asked in IT interviews that what is compiler and interpreter:steps, difference?  (difference is at end)

Compiler: A compiler is a program which converts the high level language into its equivalent machine code.

It takes the entire text file of high level language program at once and converts it into a machine readable binary code. It makes the .exe file. We compile the program only once and run .exe file always.

Simple compilation architecture

Interpreter: An interpreter is also the same as compiler but the difference is that each time we need to compile the program. It does the compilation in real time (when needed). Also it takes the program line by line while the compiler takes the entire text file of programming at once.

Steps involved in compiling a program:

Steps that a compiler perform to convert the High level program into machine readable code:

Steps involved in compilation


        1. Lexical analyzer- First, it takes the entire program and converts it into sequence of tokens i.e. meaningful character string.
Example: c= a*(b + c);
Here the compiler does the lexical analysis as below:
Variable c equals a times left bracket b add c right bracket end.

        2. Syntax analyzer- This step of compilation process deals with syntax of program. A program should follow all rules of programming language.

      For example, it will see that if a variable type is declared or not in the program. If not, then it will display an error message.

Note-It also depend whether it is single pass or multipass. Single pass is needed in c & c++ where all the variables are initialized at once.
  -Whereas, for programs written in java, we can only declare a class. Its definition is given later. So to solve these forward references, we need multipasses.

        3. Semantic analyzer: In this step of compilation process, it will check whether the program written is meaningful or not. Although syntax may be alright, but there may be many semantic errors in a program.

Let’s explain it with examples of semantic error in java program-
Example 1:
                    int i;
                   i++;   
*/here it will display a semantic error because ’i‘  variable is not initialized, so there is no meaning to increment i /*
Example 2:
                   int a,b;
                   c=a+b;
*/Here also it will show error because c is not declared /*
So this is the way the semantic analyzer works.

          4. Code generator: After performing above steps, now it will generate the equivalent machine code of the error free program which is understandable by processor (.obj) file.

       5. Linker: The compiler may generate more than one object codes. They are to be combined with external libraries. The task of linker is to combine all these. So linker is a program that combines various object modules into one executable file (.exe).

      Difference between compiler and interpreter:

Following are some main difference:
Compiler
Interpreter
1 It takes the entire program at once
   And scan it for errors.
1 It takes only one instruction at a time
   And scan it for errors.
2 Once it generates the executable
   .exe and we can use it again and
    Again without compiling it again.
2 We every time need to interpret the
   Instructions.
3 It generates the intermediate
   object code after scanning all
   Program.
3 It doesn't generate equivalent object
   code and executes one instruction at
   a time.
4 All the errors are displayed only after
   Scanning entire program.
4 Error is displayed at once when occur
   By terminating the remaining execution.
5 Program doesn't terminate at the
   occurrence of error but after the end
   of the program.
5 Whenever the error is encountered, it
  Get displayed after terminating the execution. I.e. error is the end marker.
6 Examples of languages that uses
   Compiler are c, c++.
6 Examples of languages that use
  Interpreter are Python, Ruby.


1 comment: