0% found this document useful (0 votes)
5 views

ATCD Assignment 3

Uploaded by

mmohamad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

ATCD Assignment 3

Uploaded by

mmohamad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Name: MO Mubsher Azam alassMAte

Date
Req no: BU2lcSENOS00301 Page

Assignment -
What is the structure of Compiler?
Discuss the decomposition of a
compiler into phase s and explain each
phase s
Ans Tne Structure of a Compier
Single box thak maps a
Source program Rntb a Semantically
eauivalent target program
here are ho parts to ths
analysis and Synthesis. Maeping
Ihe anaysts part break up the Sourca
program întO ConsHtuent piece and
Ymposes a arammaica Structure on
them
t then wse HhrS Structure to create
an Ratermeciae representaion of the
SOurce progam.
If the anasis part detectS that the
Sorce progcam is eiher Syntacically
semanttcally unsound
then it must orovice iotomaive messoge,
So the wer can take corrective action.
Ihe analysis part alao Collets înformation,
about the Source program and Stores
Rt fao data Shucture Caled a Symbo)
table, cohich is passech along oitth
the îotermediate reprenentathon to
the sunthests part
Ihe syothonis art Consttucts the
desired torget orogcam fom the
Rakermeoiate representotion and
tho ioformaioo fa the mbol
table
lhe anausis part ?o often caled
the froot end of the Compurer the
Sinthesis part Îs ttho back end
fr BacK -end
front end

tAoalysis. Sunthenty
Source Interme diote Mochin
Code Y

Representat'on

A compilor can have many phases and


passes.
Pass.A pass efers tn the trauersa
of acompilr through tthe eoire
program
Phone:A phane of a Gomptur i6 a
distinguishable stage ,cobich takes
input from the previou stageprocasse
and ytelch oUkput that can be wed
a fopuut for the next Stage A pas
Con bave more than one
phase
Page.

A tupical de composiHon o a compiler


Rato phses is shooo below

Cooracter sream.
Lexica Analyzer
token stream

Syntax Analyzer
Syntar tree

Serantic Analyzer
Syntax tree
ftror
Talble Internediae code Generator Handiin
întermediate reprerenteion
Machine - Independent Code
optimizer
întetmeciate representation
Code Generator
target mahine code
Machine - Dependent
Code Optimizer
target< macnine code

The Symbol bable hîeh stores infomation


about the entire SOLree proaram than
t a l d hae otheruise produced trom
an un optimtz2ec intermediate reprereta
Hon. 8nce pimizaton is opkonal, One
or the other of the oo optimization phae
Some Compilrs have a machine-independent
optmization phase bekuoeen the font
eod nd t e back end.
Date
Page

Lexical Analyzer
H lexicol anodyzer,or lexeris a
fundarOental component of a compile
or interpreter H proceSses the soure
code nf aa praqram and breaks It
inko disCre te units called tkans, îhese
tokens represent meaniagful language
eloments Such as Keyords,Rclentifers,
Titeralsoperaton, and Synbols, The laxers
Qutpuut serves as Pnpuk for subse quan
Stagenof ComRLaHonor tatetpreta kion

Syntax analyzer
A Siyatax analyzeoften caleda parser
isa Key conpanent ofacompilur or
Îoterpre ter. lt analyzes the seaune oE
Lokensgenerated by he lexical ancyzer
to entorca the Longuage's qrammacal
rules ensurinq correc SyntaX he parser
Contrucks a hierarchicad eSructure, Ke
an absttact Sntax tree (AST) ko represot
the program's Sntackic Struerure
faciutatíog fucHher analysts and Code
qeneraionAny Syntax erors are
reporhd diaring phane
Semantic analyzer.
A Semantte analyzer is a critica
componant in the compilaton process.
it gos beyond Siyntax analysis, exantoing
the ne aniaq and Correcness of the
prograns constructst enforces laguage
Specific SemanHc rules,type checking
and ensures proper wSage of Vatiables
and functions lhe Semantic analyzer
helpsdetect and reportLogtcal

ntermediae Cocde generator


An intermediate code generator is a phone
Conpilr that transforme the hiah-buel.
SOurce Code iotonan înteme diote represent
atioo (iR) that is stmpler and cOser to
machioe code This lR is de signed to
focilRtate further opimizaion and code
qenecahion t absracts awag the spectt
dorokls of the ttarget architecture
making it easier to genorate effient
Code for d& ferent pLotforms

Machine independent cocle Optmi2er


A machine iodependeot Coce optimizer ?s
a compiler Component hat impoves the
ebbiie0g oF a program s fatemecltate
Code or abstract syatax tree CA5T)
Qn the details of a
oithou relying
epecific compwker architecture, It cpple
Date
Page

OpHmzaton kechaiqus that


applicable acOSS diEferent platforms
to proouce more efGient Cnd aster
Tunninq code his phase enhances
pragran efocmed by eltntnating redundar
operaions redc®ng memory uage,
YmproviOg Overall exeution Gpee

Code generaor
Acode genrator is a cucial companant
in a compiler that ranslates hi¡h
\ewel Sourca Coce or an întermoci ote
represenkakion IR) toto machine Cado
or assembly Lanauage RpecFic to o
target Chnputer architecture t maps
tho programs logica construts to loww
lewel nzttucions registers andd memory
addreSs es,

Machine dependent code optimízex


A rmachine-dependont code optimizer is
a compilar phae thot focses
fmprouing the efHcieny and performane
of qeneated Code baNed on Specrfic
chracthateistiS ot te target Compuer
arcaitecture. lt wtilizes architectSe -SpeeK
knasledge to appiy optimizaion that
eploit harduare features, such as
TRestucion Set extensions or memOry
hierarchiea, enhane eKecuton Speecd
and resoure etiization
Page.

2Dscuss th Sciornce of Builinga


Compilex to Bootstrappiog and
and CrOSs
Compila
na Compiler design deals coith complcakecl
real Oorld problems.
Picst tho problem is taken
A mathematCo abstrackion is formulodd
Solve. wiog mathemaHcal techoqu

Modeltng in compiler denign and


implementatioo
Right 'mathemtical model and right
algorithó
Fundacnenta models Aait state
machine reguwlar expression and
Context free qrammac

KThe Science of cocle optimZaion


opkmizaton: lt is an attempt mad
by compilec to prodce Code that
is mOre eikeak than theoviow
One

Compilu optimizahion -Derignobjective


Mst fmprove perfocmane of
programs
Optmizakion must be Correct
Compilaion Kme Mut be Kept reronable
Engineering effort reauired mst be
manaaeable
Page.

App(fcaions
Ine
of
Compler Techooogy
programmer expresses an algorithm
wsiog Ehe Lnguageand the compiler
musk anslate thcr program to thak
targe anguage
Progcammers wsing Loo awel Programmi
Language hawe mo
More ContsolOuer
Compkaion and cOO prod Con podce moe
ebbicient code
opimizing comilers în clce techotques
to improve the perfornaneof
Qeneral code, thws of fsettfog
the Rnebicieny îatTodcec b
High leve abstracion>

Bootsrapping
Bootetapping s the techni for
prodcing a self- compiing compi ler
Ehal is Com:lr or assembler orttten
Lin the Soure programmig lagage
that ?t Rotens o compile
An iot ttal Core versio ot the Computer
is aeecated io a differeot languaae
(conicn coulcl be a6emblylenguage
SucceNiVe e xpandec versions of thhe
LCompiler are deweloped wsing this
miotmal subeSet obthe Language
Mang compilecs for many prograMming
Lonauages are hootstpped incducina
Compiers or BASICALGOL C D Pancal
PL/L Fackordava Ruet, Puthon a
more,
Page

*CroSS-Compîlr
Comole is a Compler Copable
dcreatiag exeutable cocle for a
plakfom other thanone on wbich
the compiler is Cunning
H crosS- Comiler is a compiler that
tuns on on machineanc prodcces
sbieck cod oc anothQr mochine
The Cross - Comopi er is uned to Rmplument
the compilec, ohtch is choteled by
three languagesi
he Source lanquage
) The object onguage
) he language in cohich f is ortttes

8whak is the rol of the lexiCal anayzar


explain sith qure 2 what is inpt
what are the lexicalrrors?
Bufferiog
Ansl Lexicod Analyzer
he First phase ota coMpler is calleol
Lexica aalzer
he e ical aaye reacS the Sreom o
Chorocters masing up he soure progom
and araups bhe chaacters Rnto
meaningtud sequcences Called loxemes
îhe sream o tokens is Sent to th
parser For syatax anaysis
Ons Suuch taskK is Shippiag
Strippioq Qut
out Comments
and cohitespaca (hlanK, nosline, tab, and
per baps other characters thot are
wsed to Seperate tokens Ro the iogut
Another task fs Correloing error
Page

messaees gener ated by he compiler


(ththe Gourco pro aram

Lexica Tokns
Parser
begramAnayzer Get noxt token

Symbol
Tabel

Inpuk Bufferiog
To reccogoize tokens reading data
Source program from bard diak is
done Accesaiog hard disk eoch time.
Conbuming so `pecial baffer techoiques
bave been douelopec to reduce the
amouot of ower heac requiecd
One Sch techoiue îs tuoo bufFer schee
each of cohteh îs alternarey badad
Size of each buffer fs N CSize of disk
blok) Ex: 4096 bytes
One read CoMmaod is wsed to read N
Characers
Senttaed Rs a Gpecia characker thoat
Cangot be a part of sOurce program.
f feoer than N charckers rermain io the
aput Gle then Gpec¦al charater
by eoE, marks the end of
epresenked
Source la
Page

Tuoo poiaters to the Ropuk are mitained


PoRnker lexeme Regin morks the beginning
of the cucreot loxemeobose extent
we re atempting
are. o detecmine
Poiater focward scans ahead unkl Q
pattern match f found
=M* eof

toruard
Laxeme beain.

*Lexeal Errors
ComiLe tme errors are ot three bupe
Errors

CompiG tfme Runne

Lexica Syntactc SemantHc


phase phare errorS
errorS errorS

During the loxtcal analets phane th


bype oF error can be dote ctec
that does not match the patern of
any oken hexieal phaseerror te found
durina the eAetion of the program
Date
Page

Lexica phase error can be i


Gpeling error.
Excacing ungth of identiier or numeric
Constants.
Appeaance
To
of illegal
chacdcters
remove Hhe charaetey that Should be
present
To replace oa character 0oith
with an Rn correct
character.
Tconspositfon of too characrers
4Defige Aesembler, loterpreter Loader,
Link edior andIranslotor whak are
khe defferences beueen the compi Ler
and iokerpreter?
Ans AsSembleri
Progammers found it dREficult ho acia
Or reod programs io machïne Language
Theg egio to wse a mneraonie Csymbo)
hor each machine înstructionobich
they cOuld subsequntiy translate.
ialo machine language Such a mnemonic
machine languge ie QO0 callecdan
assermbl LanguageIhe input to an
asembler program R6 calec Source
progroam, the Output is a mochin
languae traslation Cobjetprogram)
\nterpreter:
An ioterpreter is aprogram tnat
appears to execute a Source proaram
Qs if ?t cere machne language

dInput Process output

Sour ca INTERPRETER Program


Progr am output

Data

Languaen Such a BASLc SNO BoLLISP


can be raDsLated wiag înterpreterS
JAVA aso wses takerpreker h proues.
ot interpretaton can be carried o t io
follouiog phases.
) Lextcal analysis
) Syntax analgsts
R Semanic analysis
v DrectExecHOO

Loader and Liok faitori


ODce the aSSembler procedure cn obiec
progrom, thak program Dust koe placec
memory and execited. he assembler
fato
could place the coject program directy
Ra mnemory aod transfer conkrol toit,
hereby causiog- the machineanguge
cOOuLd cate
program to be e0eLte, bis
memory
Core by aioq the assembler io
cwhile the ubers program
Page

execuwted.
îoouercome thás problens of canted
translaion Hme aod mnemorg Syerem
prograMmers loweloped another component
callec Loader, The task of adjusting
progcams hay may be placed in arbitarg
Core bcrions fs caed reocation
Relocoion oaders perFo ns fOur

Translators
Atranslator is aaprogram that akes as
iopta program writen io onelabguage
ond prodce as o t p t a program în
in
anotherLnauageBeside program
braneaioL, the traslakor per foms
Cnother very împor taa rolethe error
deeHon ny violation of d HLL
Bpecication wold be detecked anol
reported to the programmersa \mportant
o of tanslor are
) Translatina the LL program inpur tot
an equivalent ml program. cherewer
Providiog cliagooshc messages
progrommer víolets Specificaioro
the
of th HLL
classmate
Date
Page

Interpeker Compiler
Transloken program Scans the entire
proga
One statemnent at a an and tranates it an
tine a wbole fotO machin
Code.

lnterprekers uhually take Compitrs UEually take


Los aount of tme to a large amount of
analyz.e the &our ce ime o analyze he
Code Houwever, the Sourc code Houwevex,
Overa exeuwtion Eime kho overall execution
ts Comparativel slouer Hme iscomparahuelay
hancompilur faster than interprears
No ooiect Code is Generakes Object Code
generated hence are whichpurther requires
memory efFiçentoooinking, hence reguires
more memorg
Programming languages - rogramming language
ike dovaScript, Python, ikeC CttdavauNe
Rubyit uee înterprerers. complu.
5 Hou to pecityTokans and recognize.
tokons ? Explain hhe LexicalAnalyer
Generator (LEx/ELeX)
AnsTenioology ob anguages
Alphabex: A Goite Ger of Symbols
CASCL charockers)
Shriag
eiatteSequene of
oF Sumbols
alphabet
alasSMAte
Date
Page.

terms of stîng
is the empty string-
s is the length of string 6
Langua9e
Sers of &triogs over sone Rxed olphabet
_the empy Ser fs a languge
ethe get containing empty Shrin
language

n
\angge
Set of al possible Rdeottters ts
Language
Operaóro on trfngs
Concatenation: y repreents the concatenation
SeSeS= S
srings and
gn= s9S..s(n imes) $°=e
PERATON DEEINATION
union of Land M
written LuM M

concatenaton ofL LM&E |S is tn L and t is


Ond M oitten LM

Kleen lsre
denoteszero or more Concakenation
of"L

positive cloure
Orten L denotes One orore concaterahin
classmate
Date
Page

he lexical analyzer gerator


lex Rs_a tool in Lexicad andlysis
phane to recogoize tKns wiog
egular expression.
lex tDol itsef is a lex compiler

Use of lex

Lex Conpiler lexyy

lexyy Compiler

Input Sequene
Strea oF toKens

lexl is an a îapur Rle oriten ina


Languaae cohich de scribes the qenerahe
ol lexic al andyzer The lex Compiler
ransorms lexl tD a C program Kho
lex.ygc is Compied by the C compier to
fle called arout
îhe owkput ob C Compier is the u0orkog
Lexiea analzer uohich takes sre am
oh npt characters and produuca a
Sheam d okons.
elassMAte
Date
Page

Yval is a global Variable hich 1s


parGer
sharec by lexical analyzer and
attrioute
to return the name and an
Value of token.
numertc Code
he attribute Vale Can be
o Symbo) table or nothing
pointer genarahon
Another tool for lexica analyer
fs Pex

FLEX (fast lexical analyzer genaratDr)


generang
s a tool/Compurer programorfor
lexers)
laxtcad analyzers CscaDners
oriten by ern Paxson îin c aound 1437.
oith Ber kely Yacc
t is sed together GNOBisonparser
parser generator or
generaor,
Flex and Bison both are more flexible
produces fCster
thanLex ancl yacc and
Bisonproduce parser From the
Coole
RnpukHle pro vicdcl by the uner.
he fuoction u(ex() is atomakicallg s
generaed by the lex chen it yylox)
providecd oith al&lu and fhis
function Ra expe cted by parser to call
to retrive oken fom current Hais
token Stream.

|lexyy: Sequenes
Lex Rle
Compiler Gu Compiler
Lex.l okens
Lex Souru
Program \nput Stream

You might also like