1-Phases of compiler
1-Phases of compiler
LECTURE 1
2 The linker takes input of object code generated by compiler/assembler. And the loader takes input of
executable files generated by linker.
3 Linking can be defined as process of combining various pieces of codes and source code to obtain
executable code. Loading can be defined as process of loading executable codes to main memory for
further execution.
4 Linkers are of 2 types: Linkage Editor and Dynamic Linker.Loaders are of 4 types: Absolute, Relocating,
Direct Linking, Bootstrap.
5 Another use of linker is to combine all object modules. It helps in allocating the address to executable
codes/files.
6 Linker is also responsible for arranging objects in program’s address space. Loader is also responsible for
adjusting references which are used within the program
Source-to-source
translator/Compiler
A compiler that translates a high-level language into
another high-level language is called a source-to-
source translator.
input ? output
Previous revision
What is this?
• Analysis part
• Synthesis part.
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.
The first phase of scanner works as a text
scanner. This phase scans the source
code as a stream of characters and
converts it into meaningful sequence
called lexemes. For each lexeme,Lexical
analyzer represents these lexemes in the
form of tokens as:
<token-name, attribute-value>
This tree shows the order in which the operations must execute
The tree has an interior node labeled *
with (id, 3) as its left child and the
integer 60 as its right child. The node
(id,3) represents the identifier r a t e .
The node labeled * makes it explicit
that we must first multiply the value of
rate by 60
The node labeled + indicates that we must add the result of this multiplication to the value of i n i t i a l .
The subsequent phases of the compiler use the grammatical structure to help analyze the source program
and generate the target program.
Tools
Parser generators that automatically produce syntax
analyzers from a grammatical description of a
programming language
Semantic analysis checks whether the parse tree
constructed follows the rules of language. For
example, type checking,assignment of values is
between compatible data types, and adding string to
an integer. Also, the semantic analyzer keeps track of
identifiers, their types and expressions; whether
identifiers are declared before use or not etc. The
semantic analyzer produces an annotated syntax tree
as an output.
Coercion
The language specification may permit some
type conversions
Suppose that position, i n i t i a l , and r a t e
have been declared to be floating-point
numbers, and that the lexeme 60 by itself
forms an integer. The type checker in the
semantic analyzer discovers that the operator *
is applied to a floating-point number r a t e and
an integer 60. In this case, the integer may be
converted into a floating-point number.The
output of the semantic analyzer has an
extra node for the operator inttofloat, which
explicitly converts its integer argument into a
floating-point number.
After semantic analysis the compiler generates an
intermediate code of the source code for the target
machine. It represents a program for some abstract
machine. It is in between the high-level language and
the machine language. This intermediate code should
be generated in such a way that it makes it easier to
be translated into the target machine code
The machine-independent code-optimization phase
attempts to improve the intermediate code so that better
target code will result. Optimization removes unnecessary
code lines, and arranges the sequence of statements in
order to speed up the program execution without wasting
resources (CPU, memory).
code generator takes the optimized representation of
the intermediate code and maps it to the target
machine language. The code generator translates the
intermediate code into a sequence of (generally) re-
locatable machine code. Sequence of instructions of
machine code performs the task as the intermediate
code would do.
KeyNote:
The organization of storage at run-time depends on the
language being compiled.
Storage-allocation decisions are made either during
intermediate code generation or during code generation.
Functional Division
1. Analysis Phase
2. Synthesis Phase
1-Analysis Phase
Reads the source program, divides it into core parts
and then checks for lexical, grammar and syntax
errors.
The analysis phase generates
1. intermediate representation of the source program
2. Symbol table
which should be fed to the Synthesis phase as input.
Symbol Table
It is a data-structure maintained throughout all the
phases of a compiler.
All the identifier's names along with their types are
stored here.
The symbol table makes it easier for the compiler to
quickly search the identifier record and retrieve it.
The symbol table is also used for scope management.
Key Note :
If the analysis part detects that the source program is
either syntactically ill formed or semantically
unsound, then it must provide informative messages,
so the user can take corrective action.
2-Synthesis Phase