CompilerLab1 1637
CompilerLab1 1637
RegNo : 21BCE1637
Aim:
The study of phases of compiler
Complier
The compilation process contains the sequence of various phases. Each phase takes
source program in one representation and produces output in another representation.
Each phase takes input from its previous stage.
Syntax Analysis
Syntax analysis is the second phase of compilation process. It takes tokens as
input and generates a parse tree as output. In syntax analysis phase, the
parser checks that the expression made by the tokens is syntactically correct
or not.
list of tasks performed in this phase:
1. Obtain tokens from the lexical analyzer
2. Checks if the expression is syntactically correct or not
3. Report all syntax errors
4. Construct a hierarchical structure which is known as a parse tree
Semantic Analysis
Semantic analysis is the third phase of compilation process. It checks whether
the parse tree follows the rules of language. Semantic analyzer keeps track of
identifiers, their types and expressions. The output of semantic analysis phase
is the annotated tree syntax.
Functions of Semantic analyses phase are:
1. Helps you to store type information gathered and save it in symbol table
or syntax tree
2. Allows you to perform type checking
3. In the case of type mismatch, where there are no exact type correction
rules which satisfy the desired operation a semantic error is shown
4. Collects type information and checks for type compatibility
5. Checks if the source language permits the operands or not
Code Optimization
Code optimization is an optional phase. It is used to improve the intermediate
code so that the output of the program could run faster and take less space. It
removes the unnecessary lines of the code and arranges the sequence of
statements in order to speed up the program execution.
The primary functions of this phase are:
1. It helps you to establish a trade-off between execution and compilation
speed
2. Improves the running time of the target program
3. Generates streamlined code still in intermediate representation
4. Removing unreachable code and getting rid of unused variables
5. Removing statements which are not altered from the loop
Code Generation
Code generation is the final stage of the compilation process. It takes the
optimized intermediate code as input and maps it to the target machine
language. Code generator translates the intermediate code into the machine
code of the specified computer.
It also allocates memory locations for the variable. The instructions in the
intermediate code are converted into machine instructions. This phase coverts
the optimize or intermediate code into the target language.
The target language is the machine code. Therefore, all the memory locations
and registers are also selected and allotted during this phase. The code
generated by this phase is executed to take inputs and generate expected
outputs.
Summary:
1. Compiler operates in various phases each phase transforms the source
program from one representation to another
2. Six phases of compiler design are 1) Lexical analysis 2) Syntax analysis
3) Semantic analysis 4) Intermediate code generator 5) Code optimizer 6)
Code Generator
3. Lexical Analysis is the first phase when compiler scans the source code
4. Syntax analysis is all about discovering structure in text
5. Semantic analysis checks the semantic consistency of the code
6. Once the semantic analysis phase is over the compiler, generate
intermediate code for the target machine
7. Code optimization phase removes unnecessary code line and arranges
the sequence of statements
8. Code generation phase gets inputs from code optimization phase and
produces the page code or object code as a result
9. A symbol table contains a record for each identifier with fields for the
attributes of the identifier
10. Error handling routine handles error and reports during many
phases