Compiler is a translator that is use to translate high level language in low level language .
The compilation process is a sequence of various phases. Each phase takes input from its previous stage, has its own representation of source program, and feeds its output to the next phase of the compiler. Let us understand the phases of a compiler.
Intermediate Code Generation.
It is the ist phase of compilation process in this phase the whole source code is decompose into parts called lexemes. This phase scans the source code as a stream of characters and converts it into meaningful lexemes. Lexical analyzer represents these lexemes in the form of tokens as:
Price = amount + rate * 10 ;
+ -> op2
50 -> constant
This isthe 2nd phase of compilation process in this phase of compilation hierarchical / tree structure is generated from available tokens called parse tree . Tree structure is generated to ensure the grammar of code
This is the 3rd phase of compilation process. Semantic analysis checks whether the parse tree constructed follows the rules of language like
1Type checking is performed (Data type).
2 Array index must be integer
3 operator capabilities is checked
4 whether identifiers are declared before use or not etc.
In fourth phase of compilation process our error free source code which is in the form of parse tree is converted into intermediate code . It represents a program for some abstract machine. It is in between the high-level language and the machine language. This intermediate code should be generated in such a way that it makes it easier to be translated into the target machine code.
Temp2 = id3 * Temp1;
Temp3= id2+ Temp2;
The next phase does code optimization of the intermediate code. 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).
Temp1=id3 * int-to-real(10) ;
Id1= Id2 + Temp1;
In this phase, the code generator takes the optimized representation of the intermediate code and maps it to the target machine language. By using the assembly language
MOVF R1 , id3,
MULF R1 , #10.0,
MOVF R2 , id2,
ADDF R1 , R2,
MOVF id3 , R1,