Phases of Compiler
The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input.
Here we will see all the phases of a compiler:
- The first phase of the compiler is the Linguistic/Lexical Analysis.
- It takes the source code as an input and changes them into meaningful lexemes.
- This analyzer is also known as a scanner.
- Lexical analyzer converts all the lexemes into a meaningful token. In the next tutorial, we will get to know more about tokens.
- The compiler's second phase is the syntax analyzer.
- It takes the token produced by the first phase as an input, followed by producing a parse tree as an output. Sometimes it also is known as the parser. So, don't get confused.
- In this phase, all the tokens are checked against source code grammar to check if the tokens are syntactically correct or not.
- This is the third phase of the compiler.
- The role of the semantic analyzer is to check if the generated parse tree is meaningful or not.
- It keeps track of identifier, types and their expression. The Annotated syntax tree is the output of this compiler phase.
- Also, type checking is an important part of this phase of the compiler.
Intermediate Code Generation:
- In this phase, it generates intermediate code by taking source code as an input.
- It is neither a high-level language nor a machine level language. It is somewhere in between.
- It can easily produce the intermediate code so that it can be translated into the target machine code.
- The next phase of the compiler is code optimization.
- In this phase, intermediate source code is optimized in such a way that it doesn't alter the meaning of the code.
- Optimization can be done by removing unnecessary code lines so that it takes low memory and less execution time.
- This is the last stage of the compiler.
- It takes optimized code as an input and generates the target code for the machine.
- When the target language is machine code, then for each of the variables used by the program, a register or memory location will be allocated.
The main function of the Compiler is to store the variable names used in the source program and collect information about various attributes of each name. These attributes provide various information about the variable used in the program. In order to store the information about variables, the compiler needs a data structure, which is why it needs a symbol table.
It is the compiler's data structure that is being used to store all the identifiers with their name and types. With the help of the symbol table, it makes it easier to search and retrieve.
All the phases of the compiler are associated with the symbol table and error handler, as shown in the above image.