0% found this document useful (0 votes)
18 views7 pages

Compiler Construction Design Phases

The document outlines the six phases of compiler design: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It describes the main tasks and functions of each phase, and provides examples. It also discusses symbol table management and the error handling routine.

Uploaded by

abdulwasiuwaris4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views7 pages

Compiler Construction Design Phases

The document outlines the six phases of compiler design: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. It describes the main tasks and functions of each phase, and provides examples. It also discusses symbol table management and the error handling routine.

Uploaded by

abdulwasiuwaris4
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

LECTURE 2

PHASES OF COMPILER DESIGN

Outline
a) What are the Phases of Compiler Design?
b) Phase 1: Lexical Analysis
c) Phase 2: Syntax Analysis
d) Phase 3: Semantic Analysis
e) Phase 4: Intermediate Code Generation
f) Phase 5: Code Optimization
g) Phase 6: Code Generation
h) Symbol Table Management
i) Error Handling Routine:

a) What are the Phases of Compiler Design?


Compiler operates in various phases each phase transforms the source program from
one representation to another. Every phase takes inputs from its previous stage and
feeds its output to the next phase of the compiler. There are 6 phases in a compiler.
Each of this phase help in converting the high level language the machine code. The
phases of a compiler are:
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Intermediate code generator
5. Code optimizer
6. Code generator

Figure 1: Phases of Compiler

All these phases convert the source code by dividing into tokens, creating parse trees,
and optimizing the source code by different phases.

Page 1 of 7
Phase 1: Lexical Analysis
Lexical Analysis is the first phase when compiler scans the source code. This process
can be left to right, character by character, and group these characters into tokens.
Here, the character stream from the source program is grouped in meaningful
sequences by identifying the tokens. It makes the entry of the corresponding tickets
into the symbol table and passes that token to next phase.

The primary functions of this phase are:


a. Identify the lexical units in a source code
b. Classify lexical units into classes like constants, reserved words, and enter
them in different tables. It will Ignore comments in the source program
c. Identify token which is not a part of the language.

Example:
x = y + 10

Tokens
X identifier
= Assignment operator
Y identifier
+ Addition operator
10 Number

Phase 2: Syntax Analysis


Syntax analysis is all about discovering structure in code. It determines whether or
not a text follows the expected format. The main aim of this phase is to make sure
that the source code was written by the programmer is correct or not.
Syntax analysis is based on the rules based on the specific programing language by
constructing the parse tree with the help of tokens. It also determines the structure
of source language and grammar or syntax of the language.
Here, is a list of tasks performed in this phase:
a. Obtain tokens from the lexical analyzer
b. Checks if the expression is syntactically correct or not
c. Report all syntax errors
d. Construct a hierarchical structure which is known as a parse tree

Page 2 of 7
Example
Any identifier/number is an expression
If x is an identifier and y+10 is an expression, then x= y+10 is a statement.
Consider parse tree for the following example
(a+b)*c

In Parse Tree
a. Interior node: record with an operator filed and two files for children
b. Leaf: records with 2/more fields; one for token and other information about
the token
c. Ensure that the components of the program fit together meaningfully
d. Gathers type information and checks for type compatibility
e. Checks operands are permitted by the source language

Phase 3: Semantic Analysis


Semantic analysis checks the semantic consistency of the code. It uses the syntax
tree of the previous phase along with the symbol table to verify that the given source
code is semantically consistent. It also checks whether the code is conveying an
appropriate meaning.

Semantic Analyzer will check for Type mismatches, incompatible operands, a

Page 3 of 7
function called with improper arguments, an undeclared variable, etc.
Functions of Semantic analyses phase are:
a. Helps you to store type information gathered and save it in symbol table or
syntax tree
b. Allows you to perform type checking
c. In the case of type mismatch, where there are no exact type correction rules
which satisfy the desired operation a semantic error is shown
d. Collects type information and checks for type compatibility
e. Checks if the source language permits the operands or not

Example
float x = 20.2;
float y = x*30;

In the above code, the semantic analyzer will typecast the integer 30 to float 30.0
before multiplication

Phase 4: Intermediate Code Generation


Once the semantic analysis phase is over the compiler, generates intermediate code
for the target machine. It represents a program for some abstract machine.
Intermediate code is between the high-level and machine level language. This
intermediate code needs to be generated in such a manner that makes it easy to
translate it into the target machine code.

Functions on Intermediate Code generation:


a. It should be generated from the semantic representation of the source program
b. Holds the values computed during the process of translation
c. Helps you to translate the intermediate code into target language
d. Allows you to maintain precedence ordering of the source language
e. It holds the correct number of operands of the instruction

Example
For example,
total = count + rate * 5
Intermediate code with the help of address code method is:

t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3
Page 4 of 7
Phase 5: Code Optimization
The next phase of is code optimization or Intermediate code. This phase removes
unnecessary code line and arranges the sequence of statements to speed up the
execution of the program without wasting resources. The main goal of this phase is
to improve on the intermediate code to generate a code that runs faster and occupies
less space.

The primary functions of this phase are:


a. It helps you to establish a trade-off between execution and compilation speed
b. Improves the running time of the target program
c. Generates streamlined code still in intermediate representation
d. Removing unreachable code and getting rid of unused variables
e. Removing statements which are not altered from the loop

Example:
Consider the following code
a = intofloat(10)
b=c*a
d=e+b
f=d
Can become
b =c * 10.0
f = e+b

Phase 6: Code Generation


Code generation is the last and final phase of a compiler. It gets inputs from code
optimization phases and produces the page code or object code as a result. The
objective of this phase is to allocate storage and generate relocatable machine code.
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.

Example:
a = b + 60.0
Would be possibly translated to registers.
MOV a, R1

Page 5 of 7
MOV 60.0, R2
ADD R1, R2

Symbol Table Management


A symbol table contains a record for each identifier with fields for the attributes of
the identifier. This component makes it easier for the compiler to search the identifier
record and retrieve it quickly. The symbol table also helps you for the scope
management. The symbol table and error handler interact with all the phases and
symbol table update correspondingly.

Error Handling Routine:


In the compiler design process error may occur in all the below-given phases:
a. Lexical analyzer: Wrongly spelled tokens
b. Syntax analyzer: Missing parenthesis
c. Intermediate code generator: Mismatched operands for an operator
d. Code Optimizer: When the statement is not reachable
e. Code Generator: When the memory is full or proper registers are not allocated
f. Symbol tables: Error of multiple declared identifiers

Most common errors are invalid character sequence in scanning, invalid token
sequences in type, scope error, and parsing in semantic analysis.
The error may be encountered in any of the above phases. After finding errors, the
phase needs to deal with the errors to continue with the compilation process. These
errors need to be reported to the error handler which handles the error to perform the
compilation process. Generally, the errors are reported in the form of message.

Summary
a. Compiler operates in various phases each phase transforms the source
program from one representation to another
b. 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
c. Lexical Analysis is the first phase when compiler scans the source code
d. Syntax analysis is all about discovering structure in text
e. Semantic analysis checks the semantic consistency of the code
f. Once the semantic analysis phase is over the compiler, generate intermediate
code for the target machine
g. Code optimization phase removes unnecessary code line and arranges the
sequence of statements

Page 6 of 7
h. Code generation phase gets inputs from code optimization phase and produces
the page code or object code as a result
i. A symbol table contains a record for each identifier with fields for the
attributes of the identifier
j. Error handling routine handles error and reports during many phases

Page 7 of 7

You might also like