Sunday, February 26, 2012

The Phases of Compiler

Read the Basics HERE


There are several Phases in compilers are there. Those are

  1. Lexical Analyser : Performs Lexical Analysis 
  2. Syntax Analyser : Performs Syntax Analysis
  3. Semantic Analyser : Performs Semantic Analysis
  4. Intermediate Code Generator : Performs Intermediate Code Generation
  5. Code Optimizer : Performs Code Optimization
  6. Code Generator : Generates Target Code 



The Symbol Table is a Data Structure which is useful to store the identifiers in the program. 

The Error Handler is useful in Detecting and Reporting the errors. 

These two are useful from the starting to the ending of the conversion. ( I mean from the starting phase to the ending phase).  

NOTE : We can group these phases into two parts.. 
  • Analysis : In which the compiler ANALYZES the given program. For this, the compiler SCANS and DIVIDES the program into parts. The first THREE phases comes under this category (shortcut : Check the names, if you can find the terms Analysis or Analiser, then that phase comes under the ANALYSIS category
  • Synthesis : In which the compiler JOINS the parts of the divided program and makes the target program. (The last two phases comes under this category)


Now lets have a look at the functionalities of all these phases. 

Lexical Analysis : It is nothing but analyzing the LEXEMs (tokens), in this phase, the compiler scans the entire program and pics the tokens and groups them.

Syntax Analysis : Syntax is nothing but the structure. It is nothing but to Analyze the structure of the program (whether it is correct or not)

Semantic Analysis : In this the compiler checks the MEANING of the program.

(Note : The above two process will execute simultaniously, so in some books they combine these two names and call the process as Syntax and Semantic Analysis)

Intermediate Code Generation : After scanning the program, the compiler generates an intermediate code (which is neither High level code nor Low level code, which looks like the Microprocessor Code)

Note : Most of the compilers generates the TAC (Three Address Code) as the Intermediate Code. It contains, maximum 3 variable, so we call it as the Three Address Code. (note : we can convert any problem into this TAC form).

it just look like, Temp1 = Temp2 + Temp3  (temporary  variables) 
       or Temp2 = Id + Temp4     etc,

Code Optimization : It helps to improve the Intermediate Code. It make assure that the target code will run EFFICIENTLY in LESS TIME ( i mean, it should give the desired output in less amount of time)

Code Generation : It is the final phase. It generates the relocatable machine code or assembly code. It takes care about the target machine and where to store the variables in the target machine. Yea ofcourse, it takes care about the ORDER of the instruction execution too.


Thats all for now Friends.. Happy Reading.. 


                                                                                                              

No comments:

Post a Comment