Monday 5 June 2017

Explain all phases of compilation with one sample example



           Here, we will see six phases of compiler with an example.
           Let us take one statement which is common in many High Level Languages.
x=a+b*c
           Here, x,a,b,c are identifiers and =,+,* are operators.


1. Lexical Analyzer

           High level language program has several parts. e.g. keywords, macros, identifiers, operators etc. These are called as tokens. Lexical analyzer accepts the high level language program and tokenize it. These tokens are saved into the symbol table. To find out the tokens, lexical analyzer uses regular expressions(patterns). It matches the words from programs with regular expressions and identify whether which token it is.
            For above example,
id=id1+id2*id3 
            (For simplicity, I have mentioned like this. We can also write it as id op id1 op1 id2 op2 id3 where id,id1,id2,id3 represents identifiers and op,op1,op2 represents operators).

2. Syntax Analyzer(Parser)

             Syntax analyzer prepares a parse tree from input it got from above step.
Parse tree is useful to decide the sequence of operations or priorities of operations.
             For above example,




3. Semantic Analyzer

             Semantic analyzer checks the semantics of a expression(statement). This means, it checks whether all the entities in the statement are as per the rules or not. For example, we can not assign the new value to the constant or constant value should not be on the left hand side of a = operator. Such kinds of rules are checked by the semantic analyzer.
             Above example, is semantically correct.

4. Intermediate Code generator

            Intermediate code generator, generates code in terms of temporary variables.
            For above example, intermediate code will be as follows,
temp1=id2*id3
temp2=id1+temp1
id=temp2

5. Code Optimizer 

          Code optimizer optimizes(in this case, reduces) the size of code if it is possible.
          For above example,
temp1=id2*id3
id=id1+temp1

6. Code Generator

          Code generator generates the assembly code in terms of registers from the input it got from above steps.
        For above example (suppose id1,id2,id3 are saved to registers AX,BX,CX respectively), then assembly code will be
MUL BX,CX
ADD AX, CX

No comments:

Post a Comment