CD Introduction
CD Introduction
Compiler Construction 1
The text book is:
Compiler Construction 2
Language Processing System
Compiler Construction 3
Language Processors.
Assembler:
Assembler
source program object program
(in assembly language) (in machine
language)
Compiler Construction 4
Language Processors.
a compiler is a program that can read a program in one language the source
language - and translate it into an equivalent program in another language - the
target language;
An important role of the compiler is to report any errors in the source program that
it detects during the translation process
Compiler Construction 5
Interpreter
Compiler Construction 6
Definition of Compiler
Compiler Construction 7
Overview of Compilers
Compilation Process:
Data
Compiler Construction 8
Tasks of A Compiler
Compiler Construction 9
Phases of Compiler
Each phase takes input from its previous stage, has its own
representation of source program, and feeds its output to the
next phase of the compiler.
Compiler Construction 10
Phases of Compiler
o • Lexical analysis.
o • Syntax analysis.
o • Semantic analysis.
o • Code optimization.
o • Code generation.
o Code optimization.
Compiler Construction 11
Phases of Compilation Process and Its Output
Error
handler
Compiler phases
Compiler Construction 12
Lexical Analysis (scanner): The first phase of a compiler
Compiler Construction 13
Example: position =initial + rate * 60
1.”position” is a lexeme mapped into a token (id, 1), where id is an abstract symbol
standing for identifier and 1 points to the symbol table entry for position. The
symbol-table entry for an identifier holds information about the identifier, such as its
name and type.
2. = is a lexeme that is mapped into the token (=). Since this token needs no attribute-
value, we have omitted the second component. For notational convenience, the
lexeme itself is used as the name of the abstract symbol.
3. “initial” is a lexeme that is mapped into the token (id, 2), where 2 points to the
symbol-table entry for initial.
4. + is a lexeme that is mapped into the token (+).
5. “rate” is a lexeme mapped into the token (id, 3), where 3 points to the symbol-table
entry for rate.
6. * is a lexeme that is mapped into the token (*) .
7. 60 is a lexeme that is mapped into the token (60)
Blanks separating the lexemes would be discarded by the lexical analyzer.
14
Syntax Analysis (parser) : The second phase of the compiler
The next phase is called the syntax analysis or parsing. It takes the token
produced by lexical analysis as input and generates a parse tree (or syntax
tree).
In this phase, token arrangements are checked against the source code
grammar, i.e. the parser checks if the expression made by the tokens is
syntactically correct.
15
Semantic Analysis: Third phase of the compiler
Also, the semantic analyzer keeps track of identifiers, their types and
expressions; whether identifiers are declared before use or not etc.
Compiler Construction 16
Intermediate Code Generation: three-address code
17
Code Optimization: to generate better target code
18
Code Generation: takes as input an intermediate representation of
the source program and maps it into the target language
19
Symbol-Table Management:
All the identifier's names along with their types are stored
here.
Compiler Construction 21
Error Handling
22
(ii) During type conversion.
Compiler Construction 23
Translation of an assignment
statement
Compiler Construction 24
Parse Tree and Symbol Table
Parse tree defines the program structure; how to combine parts of the
program to produce larger part and so on.
declaration.
Compiler Construction 25
Grouping of Phases
Compiler Construction 26
Front End
Compiler Construction 27
Back End
Compiler Construction 28
Common Back-end Compiling System
Optimizer
Compiler Construction 29
Compiling Passes
Compiler Construction 31
Reducing the Number of Passes
Compiler Construction 32