You are on page 1of 5

Chapter-1 : Introduction

1.1 Introduction to Compiler.


1.2 Phases of Compiler.

1.1 Introduction to Compiler:

Compiler is a translator program that translates a program written in High Level Language (HLL)
the source program and translates it into an equivalent program in assembly Language (LLL) the target
program. Compiler used to convert human readable program into assembly language program and also
finds error from the program.

Two parts of compilation


A compiler can broadly be divided into two phases based on the way they compile.
Analysis Phase
Known as the front-end of the compiler, the analysis phase of the compiler reads the source program,
divides it into core parts, and then checks for lexical, grammar, and syntax errors. The analysis phase
generates an intermediate representation of the source program and symbol table, which should be fed to
the Synthesis phase as input.
Source program--------------->Intermediate representation
And symbol table
Synthesis Phase
Known as the back-end of the compiler, the synthesis phase generates the target program with the help
of intermediate source code representation and symbol table.

Intermediate representation-------------------> Target program


And symbol table

A compiler can have many phases and passes.

• Pass : A pass refers to the traversal of a compiler through the entire program.
• Phase : A phase of a compiler is a distinguishable stage, which takes input from the previous stage,
processes and yields output that can be used as input for the next stage. A pass can have more than one
phase.
1.2 Phases of Compiler:
Phase Definition:

1. A compiler operates in phases.

2. A phase is a logically interrelated operation that takes source program in one representation and
produces output in another representation.

Phases of a Compiler:
Phase 1:Lexical Analyzer:-
1. The first phase of compiler is Lexical analyzer

2. Lexical Analyzer reads the source program one character at a time, the source program into a
sequence of atomic units called lexemes. Then lexeme will be represented using tokens. It makes
the entry of the corresponding tickets into the symbol table and passes that token to next phase.
For Example :
Input: stream of characters
Output: Token
Token Template: <token-name, attribute-value>
c=a+b*5;

The above source program can be read by lexical analyzer, and that source program can be partitioned
into units, each unit can be considered as token.

Hence, <id, 1 >=< id, 2> + <id, 3 > * < 5>

Phase 2:Syntax Analyzer :

1. The second phase of a compiler is Syntax Analyzer otherwise called as parser.Syntax Analyzing
is also called as Parsing.
2. The parser uses the tokens produced by the lexical analyzer to create a tree-like intermediate
representation – called syntax tree.
3. In this phase all the syntactical error will be identified using syntax tree.
4. Syntax tree is a compressed representation of the parse tree in which the operators appear as
interior nodes and the operands of the operator are the children of the node for that operator.

Input: Tokens
Output: Syntax tree
The output of Syntax Analyzer is:

Phase 3:Semantic Analyzer :


1. The third phase of a compiler is Semantic Analyzer.
2. 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.
3. Semantic Analyzer will check for Type mismatches, incompatible operands, a function called
with improper arguments, an undeclared variable, etc.
4. Output of this phase is again syntax tree only.
The output of Semantic Analyzer is:

Phase 4:Intermediate Code Generation:

1. The fourth phase of compiler is Intermediate Code Generation.

2. The intermediate code generation representation called three-address code . it has consists of an
assembly like instructions with a maximum of three operands per instruction.

3. This phase acts as a bridge between analysis and synthesis phase.


The output of the intermediate code generator can consist of the three-address code sequence that is
maximum it should have three operands only.
t1 = int to float (5)
t2 = id3 * t1
t3 = id2 + t2
id1 = t3 ( note: t1,t2 and t3 are considered as temporary variable).
Phase 5:Code Optimization:
1. The Fifth phase of compiler is Code Optimization.
2. The code-optimization phase attempts to improve the intermediate code so that better target code
will result.
3. This phase will result in minimizing the code in order to increase the efficiency of the system
and to minimize the usage of resources like CPU, memory etc.
The output of the code optimization is
t1 = id3 * 60
id1 = id2 + t1.
Phase 6:Code Generation:
1. The Last phase of compiler is Code Generation.
2. The code generator takes as an input code optimization of the source program and maps it into
the target language.
3. If the target language is machine language, registers or memory locations are selected for each of
the variables used by the program.
The output of the code generation is
MOV R1, id3
MUL R1, 60
MOV R2, id2
ADD R2, R1
MOV id1, R2 (note: R1 and R2 can be considered as registers).
Symbol Table Manager:
1. Symbol table is used to store all the information about identifiers used in the program.
2. It is a data structure containing a record for each identifier, with fields for the attributes of the
identifier.
3. It allows finding the record for each identifier quickly and to store or retrieve data from that
record.
4. Whenever an identifier is detected in any of the phases, it is stored in the symbol table.
Example
int a, b; float c; char z;

Error Handling:
1. In the compiler design process error may occur in all the below-given phases:
• Lexical analyzer: Wrongly spelled tokens
• Syntax analyzer: Missing parenthesis
• Code Optimizer: When the statement is not reachable
• Code Generator: Unreachable statements
• Symbol tables: Error of multiple declared identifiers
2. All of these errors will be reported to error handler which handles the error to perform the
compilation process.

You might also like