Compiler Design Notes

What is complier ?

    Compiler is  program which takes one language (source program) as input and translate it into an equivalent another language(target program). 


phases of compiler

       As we have discuss earlier the process of compilation is carried out in two parts: analysis and synthesis.
Again the analysis is carried out in three phases : lexical analysis, syntax analysis and semantic analysis. And  the synthesis is carried out with the help of intermediate code generation, code generation and code optimization. Let us discuss these phases one by one.

Phases of Compiler
1. Lexical Analysis 

          The lexical analysis is also called scanning. In this step the source program is read and then it is broken into stream of string. Such string are called tokens. hence token are nothing but the collection of characters having some meaning.

Then in lexical analysis phase this statement is broken up into series of token as follows.

1. Th identifier total

2. The assignment symbol

3. The plus sign

4.The multiplication sign

5.The constant number 10


2. Syntax Analysis

    1. In this step tokens are arranged in hierarchical structure that ultimately helps in finding the syntax of the source string. 
   2. The syntax analysis determines the structure of the source string by grouping the tokens together. 
   3. The hierarchical structure generated in this phase is called parse tree or syntax tree. 
   4. The syntax analysis is also called Parsing. 


3. Semantic Analysis

     1. In this step the meaning of the source string is determined.
     2. meaning of source string means matching parenthesis in the expression, or matching of if ..else              statement or performing arithmetic operations of the expressions or checking the scope of operation.

4. Intermediate Code Generation

     1.The intermediate code is a kind of code which is easy to generate and this code can be easily to             converted to target code.
  2. this code is a variety of forms such as three address code,  quadruple, triple, posix.
   

5. Code Optimization

1. The next phase does code optimization of the intermediate code. 

2. Optimization can be assumed as something that removes unnecessary code lines, and  arranges the sequence of statements in order to speed up the program execution without    wasting resources (CPU, memory).

   

6. Code Generation

1. Code generation is the last and final phase of a compiler.

2. It gets inputs from code optimization phases and produces the page code or object code    as a result. 

3. The objective of this phase is to allocate storage and generate relocatable machine code. It also allocates memory locations for the variable.  

4. The instructions in the intermediate code are converted into machine instructions. This     phase coverts the optimize or intermediate code into the target language.
5. The target language is the machine code. Therefore, all the memory locations and registers are also selected and allotted during this phase. 

6. The code generated by this phase is executed to take inputs and generate expected.



Difference between interpreter and compiler:


Interpreter

1. In source program gets interpreted every time, it is to be executed and every time the source program is analyzed.


2. Interpretation is less efficient than compiler.


3. The interpreters do not produce object code.


 4. The interpreters can be made portal because they do not produce object code.


5. Interpreters are simpler and give us improved debugging environment.


  Compiler


1. In the process of compilation  the program is analyzed only once and then the code generated.


2. complier is efficient than interpreter.


3. The compiler produce object code.


4. The compilers has to be present on the host machine when particular program needs to be compiled.


5. The compiler is a complex program and it requires large amount of memory.


 



















Post a Comment

2 Comments