Sunteți pe pagina 1din 5

UNIT 1: COMPILER DESIGN

Introduction to compiler
Compiler and Translator
Translator
A translator is a program that takes input a program written in one programming
language and produce as output a program in another language
Need o translator
Machine language is in the form of bits its very tough to communicate.
Its tough to make program
Its terribly tedious to remove error
Structure o compiler : its dierent p!ases
E
ach
phase transforms the source program from one representation
into another representation.
They communicate with error handlers.
They communicate with the symbol table.
Le"ical anal#$er
Le"ical %nal#$er reads the source program character by character and returns the
tokens of the source program.
A token describes a pattern of characters having same meaning in the source
program. (such as identifiers operators keywords numbers delimeters and so
on!
E"# newval #$ oldval % &' $( tokens# newval identifier
#$ assignment operator
oldval identifier
% add operator
&' a number
)uts information about identifiers into the symbol table.
*egular e"pressions are used to describe tokens (le"ical constructs!.
A (+eterministic! ,inite -tate Automaton can be used in the implementation of a
le"ical analy.er.
S#nta" anal#$er
The synta" of a language is specified by a conte"t ree &rammar (/,0!.
The rules in a /,0 are mostly recursive.
A synta" analy.er checks whether a given program satisfies the rules implied by a
/,0 or not.
1 If it satisfies the synta" analy.er creates a parse tree for the given
program.
E"# 2e use 34, (3ackus 4aur ,orm! to specify a /,0
assgstmt 5( identifier #$ e"pression
e"pression 5( identifier
e"pression 5( number
e"pression 5( e"pression % e"pression
Semantic anal#$er
A semantic analy.er checks the source program for semantic errors and collects
the type information for the code generation.
Type5checking is an important part of semantic analy.er.
4ormally semantic information cannot be represented by a conte"t5free language
used in synta" analy.ers.
/onte"t5free grammars used in the synta" analysis are integrated with attributes
(semantic rules!
1 the result is a synta"5directed translation
1 Attribute grammars
E"#
newval #$ oldval % &'
The type of the identifier newval must match with type of the
e"pression (oldval+12)
Intermediate code &eneration
A compiler may produce an e"plicit intermediate codes representing the source
program.
These intermediate codes are generally machine (architecture independent!. 3ut
the level of intermediate codes is close to the level of machine codes.
E"#
newval #$ oldval 6 fact % &
id& #$ id' 6 id7 % &
M89T id'id7temp& Intermediates Codes (Quadraples)
A++ temp&:&temp'
M;< temp'id&
Code optimi$er
The code optimi.er optimi.es the code produced by the intermediate code
generator in the terms of time and space.
E"#
M89T id'id7temp&
A++ temp&:&id&
Code &enerator
)roduces the target language in a specific architecture.
The target program is normally is a relocatable ob=ect file containing the machine
codes.
E"#
( assume that we have an architecture with instructions whose at least one
of its operands is
a machine register!
M;<Eid'*&
M89T id7*&
A++ :&*&
M;<E*&id&
Compiler construction tools
A number of tools have been developed variously called compiler compiler , compiler
generator or translator writing system
The input for these systems may contain
&. a description of source language.
'. a description of what output to be generated.
7. a description of the target machine.

The principal aids provided by compiler5compiler are
&. -canner 0enerator
'. )arser generator
7. ,acilities for code generation

S-ar putea să vă placă și