Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
26 views

CompilerLab1 1637

1. The document discusses the various phases of a compiler: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. 2. Lexical analysis scans the source code and converts it to tokens. Syntax analysis checks the syntax and constructs a parse tree. Semantic analysis checks for semantic consistency. 3. Intermediate code is generated and then optimized before the final code generation phase converts it to machine code. Each phase transforms the representation of the source code.

Uploaded by

Chiku Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

CompilerLab1 1637

1. The document discusses the various phases of a compiler: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. 2. Lexical analysis scans the source code and converts it to tokens. Syntax analysis checks the syntax and constructs a parse tree. Semantic analysis checks for semantic consistency. 3. Intermediate code is generated and then optimized before the final code generation phase converts it to machine code. Each phase transforms the representation of the source code.

Uploaded by

Chiku Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Name:Krithika Shivkumar

RegNo : 21BCE1637

LAB 1: PHASES OF COMPILER

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.

There are the various phases of compiler:


Lexical Analysis:
Lexical analyzer phase is the first phase of compilation process. It takes source
code as input. It reads the source program one character at a time and
converts it into meaningful lexemes. Lexical analyzer represents these lexemes
in the form of tokens.
The primary functions of this phase are:
1. Identify the lexical units in a source code
2. Classify lexical units into classes like constants, reserved words, and
enter them in different tables. It will Ignore comments in the source
program
3. Identify token which is not a part of the language

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

Intermediate Code Generation


In the intermediate code generation, compiler generates the source code into
the intermediate code. Intermediate code is generated between the high-level
language and the machine language. The intermediate code should be
generated in such a way that you can easily translate it into the target machine
code.
Functions on Intermediate Code generation:
1. It should be generated from the semantic representation of the source
program
2. Holds the values computed during the process of translation
3. Helps you to translate the intermediate code into target language
4. Allows you to maintain precedence ordering of the source language
5. It holds the correct number of operands of the instruction

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

You might also like