Sunteți pe pagina 1din 93

CS13S2 r|nc|p|es of Comp||er Des|gn uest|on and answers

-----------------------------------------------------------------------------------------------------
1) What |s a comp||er?


Slmply sLaLed, a compller ls a program LhaL reads a program wrlLLen ln one language-Lhe source
language-and LranslaLes lL lnLo an equlvalenL program ln anoLher language-Lhe LargeL language (see
flg.1) As an lmporLanL parL of Lhls LranslaLlon process, Lhe compller reporLs Lo lLs user Lhe presence of
errors ln Lhe source program.
Compllers are someLlmes classlfled as slngle-pass, mulLl-pass, load-and-go, debugglng, or
opLlmlzlng, dependlng on how Lhey have been consLrucLed or on whaL funcLlon Lhey are supposed Lo
perform. uesplLe Lhls apparenL complexlLy, Lhe baslc Lasks LhaL any compller musL perform are
essenLlally Lhe same.

2) What are the phases of a comp||er?
hases of a Comp||er
1. Lexlcal analysls ( scannlng )
o 8eads ln program, groups characLers lnLo Lokens
2. SynLax analysls ( parslng )
o SLrucLures Loken sequence accordlng Lo grammar rules of Lhe language.
3. SemanLlc analysls
o Checks semanLlc consLralnLs of Lhe language.
4. lnLermedlaLe code generaLlon

o 1ranslaLes Lo lower level represenLaLlon.
S. rogram analysls and code opLlmlzaLlon
o lmproves code quallLy.
6. llnal code generaLlon.

3) Lxp|a|n |n deta|| d|fferent phases of a comp||er.

1nL DIIILkLN1 nASLS CI A CCMILLk
ConcepLually, a compller operaLes ln pboses, each of whlch Lransforms Lhe source program from
one represenLaLlon Lo anoLher.



1he flrsL Lhree phases, forms Lhe bulk of Lhe analysls porLlon of a compller. Symbol Lable
managemenL and error handllng, are shown lnLeracLlng wlLh Lhe slx phases.


Symbo| tab|e management

An essenLlal funcLlon of a compller ls Lo record Lhe ldenLlflers used ln Lhe source program and
collecL lnformaLlon abouL varlous aLLrlbuLes of each ldenLlfler. A symbol toble ls a daLa sLrucLure
conLalnlng a record for each ldenLlfler, wlLh flelds for Lhe aLLrlbuLes of Lhe ldenLlfler. 1he daLa sLrucLure
allows us Lo flnd Lhe record for each ldenLlfler qulckly and Lo sLore or reLrleve daLa from LhaL record
qulckly. When an ldenLlfler ln Lhe source program ls deLecLed by Lhe lex analyzer, Lhe ldenLlfler ls
enLered lnLo Lhe symbol Lable.

Lrror Detect|on and keport|ng

Lach phase can encounLer errors. A compller LhaL sLops when lL flnds Lhe flrsL error ls noL as
helpful as lL could be.
1he synLax and semanLlc analysls phases usually handle a large fracLlon of Lhe errors deLecLable by Lhe
compller. 1he lexlcal phase can deLecL errors where Lhe characLers remalnlng ln Lhe lnpuL do noL form
any Loken of Lhe language. Lrrors when Lhe Loken sLream vlolaLes Lhe synLax of Lhe language are
deLermlned by Lhe synLax analysls phase. uurlng semanLlc analysls Lhe compller Lrles Lo deLecL
consLrucLs LhaL have Lhe rlghL synLacLlc sLrucLure buL no meanlng Lo Lhe operaLlon lnvolved.

1he Ana|ys|s phases

As LranslaLlon progresses, Lhe compller s lnLernal represenLaLlon of Lhe source program changes.
Conslder Lhe sLaLemenL,
poslLlon := lnlLlal + raLe * 10

1he lexlcal analysls phase reads Lhe characLers ln Lhe source pgm and groups Lhem lnLo a sLream
of Lokens ln whlch each Loken represenLs a loglcally coheslve sequence of characLers, such as an
ldenLlfler, a keyword eLc. 1he characLer sequence formlng a Loken ls called Lhe lexeme for Lhe Loken.
CerLaln Lokens wlll be augmenLed by a lexlcal value . lor example, for any ldenLlfler Lhe lex analyzer

generaLes noL only Lhe Loken ld buL also enLer s Lhe lexeme lnLo Lhe symbol Lable, lf lL ls noL already
presenL Lhere. 1he lexlcal value assoclaLed Lhls occurrence of ld polnLs Lo Lhe symbol Lable enLry for Lhls
lexeme. 1he represenLaLlon of Lhe sLaLemenL glven above afLer Lhe lexlcal analysls would be:

ld1: = ld2 + ld3 * 10

SynLax analysls lmposes a hlerarchlcal sLrucLure on Lhe Loken sLream, whlch ls shown by synLax Lrees (flg
3).

Intermed|ate Code Generat|on

AfLer synLax and semanLlc analysls, some compllers generaLe an expllclL lnLermedlaLe represenLaLlon of
Lhe source program. 1hls lnLermedlaLe represenLaLlon can have a varleLy of forms.
ln Lhree-address code, Lhe source pgm mlghL look llke Lhls,

Lemp1: = lnLLoreal (10)
Lemp2: = ld3 * Lemp1
Lemp3: = ld2 + Lemp2
ld1: = Lemp3

Code Cpt|m|sat|on

1he code opLlmlzaLlon phase aLLempLs Lo lmprove Lhe lnLermedlaLe code, so LhaL fasLer runnlng
machlne codes wlll resulL. Some opLlmlzaLlons are Lrlvlal. 1here ls a greaL varlaLlon ln Lhe amounL of

code opLlmlzaLlon dlfferenL compllers perform. ln Lhose LhaL do Lhe mosL, called opLlmlslng compllers ,
a slgnlflcanL fracLlon of Lhe Llme of Lhe compller ls spenL on Lhls phase.

Code Generat|on

1he flnal phase of Lhe compller ls Lhe generaLlon of LargeL code, conslsLlng normally of
relocaLable machlne code or assembly code. Memory locaLlons are selecLed for each of Lhe varlables
used by Lhe program. 1hen, lnLermedlaLe lnsLrucLlons are each LranslaLed lnLo a sequence of machlne
lnsLrucLlons LhaL perform Lhe same Lask. A cruclal aspecL ls Lhe asslgnmenL of varlables Lo reglsLers.

4) What |s group|ng of phases?
Group|ng of hases
o ltoot eoJ : machlne lndependenL phases
o Lexlcal analysls
o SynLax analysls
o SemanLlc analysls
o lnLermedlaLe code generaLlon
o Some code opLlmlzaLlon
o 8ock eoJ : machlne dependenL phases
o llnal code generaLlon
o Machlne-dependenL opLlmlzaLlons

S) Lxp|a|n w|th d|agram how a statement |s comp||ed .




6)What are ro|es and tasks of a |ex|ca| ana|yzer?

Molo 1osk: 1ake a Loken sequence from Lhe scanner and verlfy LhaL lL ls a synLacLlcally correcL program.
5ecooJoty 1osks:
o rocess declaraLlons and seL up symbol Lable lnformaLlon accordlngly, ln preparaLlon for
semanLlc analysls.
o ConsLrucL a synLax Lree ln preparaLlon for lnLermedlaLe code generaLlon.
o Def|ne Context free grammar.
o Context-free Grammars
position := initial + rate * 60
The Phases of a
CompiIer
lexical analyzer
id
1
:= id
2
+ id
3
* 60
syntax analyzer
:=
id
1
+
semantic analyzer
:=
id
1
+
intermediate code generator
temp1 := inttoreal (60)
temp2 := id
3
* temp1
3 id 2
code optimizer
temp1 := id
3
* 60.0
code generator
MOVF id3, R2
MULF #60.0, R2
MOVF id2, R1

o A cootext-ftee qtommot for a language speclfles Lhe synLacLlc sLrucLure of programs ln LhaL
language.
o ComponenLs of a grammar:
o a flnlLe seL of Lokens (obLalned from Lhe scanner),
o a seL of varlables represenLlng relaLed seLs of sLrlngs, e.g., Jeclototloos, stotemeots,
expresslons.
o a seL of rules LhaL show Lhe sLrucLure of Lhese sLrlngs.
o an lndlcaLlon of Lhe Lop-level seL of sLrlngs we care abouL.
o Context-free Grammars: Def|n|t|on
o lormally, a conLexL-free grammar C ls a 4-Luple C = (v, 1, , S), where:
o v ls a flnlLe seL of votlobles (or oootetmlools). 1hese descrlbe seLs of relaLed sLrlngs.
o 1 ls a flnlLe seL of tetmlools (l.e., Lokens).
o ls a flnlLe seL of ptoJoctloos, each of Lhe form
o A
o where A v ls a varlable, and (v 1)* ls a sequence of Lermlnals and nonLermlnals.
o S v ls Lhe stott symbol.
Lxample of ClC :
L ==>LAL | (L) | -L | ld
A==> + | - | * | / |
Where L,A are the non-term|na|s wh||e |d, +, *, -, ],(, ) are the term|na|s.
6) What are parsers?
arser
AccepLs sLrlng of Lokens from lexlcal analyzer (usually one Loken aL a Llme)
verlfles wheLher or noL sLrlng can be generaLed by grammar
8eporLs synLax errors (recovers lf posslble)
1nL kCLL CI A AkSLk

arser obLalns a sLrlng of Lokens from Lhe lexlcal analyzer and verlfles LhaL lL can be generaLed by
Lhe language for Lhe source program. 1he parser should reporL any synLax errors ln an lnLelllglble
fashlon.

1he Lwo Lypes of parsers employed are:
1.1op down parser: whlch bulld parse Lrees from Lop(rooL) Lo boLLom(leaves)
2.8oLLom up parser: whlch bulld parse Lrees from leaves and work up Lhe rooL.
1herefore Lhere are Lwo Lypes of parslng meLhods Lop-down parslng and boLLom-up parslng.

7) What are parse trees?
Parse Trees
nodes are non-Lermlnals.
Leaves are Lermlnals.
8ranchlng corresponds Lo rules of Lhe grammar.
1he leaves glve a senLence of Lhe lnpuL language.
lor every senLence of Lhe language Lhere ls aL leasL one parse Lree.
SomeLlmes we have more Lhen one parse Lree for a senLence.
Crammars whlch allow more Lhan one parse Lree for some senLences
are called amblguous and are usually noL good for compllaLlon.
8) What are d|fferent k|nds of errors encountered dur|ng comp||at|on?
Comp||er Lrrors
Lexlcal errors (e.g. mlsspelled word)
SynLax errors (e.g. unbalanced parenLheses, mlsslng semlcolon)
SemanLlc errors (e.g. Lype errors)
Loglcal errors (e.g. lnflnlLe recurslon)
Lrror Pandllng
8eporL errors clearly and accuraLely
8ecover qulckly lf posslble
oor error recover may lead Lo avalanche of errors
9) What are d|fferent error recovery strateg|es?
Lrror kecovery strateg|es
an|c mode: dlscard Lokens one aL a Llme unLll a synchronlzlng Loken ls found
hrase-|eve| recovery: erform local correcLlon LhaL allows parslng Lo conLlnue
Lrror roduct|ons: AugmenL grammar Lo handle predlcLed, common errors

G|oba| roduct|on: use a complex algorlLhm Lo compuLe leasL-cosL sequence of changes leadlng
Lo parseable code

10) Lxp|a|n kecurs|ve descent pars|ng.
kecurs|ve descent pars|ng: corresponds Lo flndlng a lefLmosL
derlvaLlon for an lnpuL sLrlng
LqulvalenL Lo consLrucLlng parse Lree ln pre-order
Lxample:
Crammar: S ! cAd A ! ab [ a
lnpuL: cad
rob|ems:
1. backLracklng lnvolved ()bufferlng of Lokens requlred)
2. lefL recurslon wlll lead Lo lnflnlLe looplng
3. lefL facLors may cause several backLracklng sLeps
Compller ConsLrucLlon: arslng p. 3/31
11) G|ve an examp|e of amb|guous grammar.
Lxamples
Amblgous grammar:
L ::= L _ L | L + L | 1 | ( L )
unamblgous grammar
L ::= L + 1 | 1
1 ::= 1 _ l | l
l ::= 1 | ( L )
8) WhaL ls lefL recurslon? Pow lL ls ellmlnaLed?


12) What |s |eft factor|ng?
Left Iactor|ng
8ewrlLlng producLlons Lo delay declslons
Pelpful for predlcLlve parslng
noL guaranLeed Lo remove amblgulLy



A 1 | 2


A A
A 1 | 2






13) What |s top down pars|ng?
1op Down ars|ng
Can be vlewed Lwo ways:
ALLempL Lo flnd lefLmosL derlvaLlon for lnpuL sLrlng
ALLempL Lo creaLe parse Lree, sLarLlng from aL rooL, creaLlng nodes ln preorder
Ceneral form ls recurslve descenL parslng
May requlre backLracklng
8ackLracklng parsers noL used frequenLly because noL needed

14) What |s pred|ct|ve pars|ng?
A speclal case of recurslve-descenL parslng LhaL does noL requlre backLracklng
MusL always know whlch producLlon Lo use based on currenL lnpuL symbol
Can ofLen creaLe approprlaLe grammar:
removlng lefL-recurslon
lefL facLorlng Lhe resulLlng grammar

1S) Def|ne LL(1) grammar.
LL(1) Grammars
AlgorlLhm covered ln class can be applled Lo any grammar Lo produce a parslng Lable
lf parslng Lable has no mulLlply-deflned enLrles, grammar ls sald Lo be LL(1)
llrsL L , lefL-Lo-rlghL scannlng of lnpuL
Second L , produces lefLmosL derlvaLlon
1 refers Lo Lhe number of lookahead symbols needed Lo make declslons


16) What |s sh|ft reduce pars|ng?
Sh|ft-keduce ars|ng

Cne slmple form of boLLom-up parslng ls shlfL-reduce parslng
SLarLs aL Lhe boLLom (leaves, Lermlnals) and works lLs way up Lo Lhe Lop (rooL, sLarL symbol)
Lach sLep ls a reducLlon :
SubsLrlng of lnpuL maLchlng Lhe rlghL slde of a producLlon ls reduced
8eplaced wlLh Lhe nonLermlnal on Lhe lefL of Lhe producLlon
lf all subsLrlngs are chosen correcLly, a rlghLmosL derlvaLlon ls Lraced ln reverse

Sh|ft-keduce ars|ng Lxamp|e




17) Def|ne nand|e.
nand|es
lnformally, a hand|e of a sLrlng:
ls a subsLrlng of Lhe sLrlng
MaLches Lhe rlghL slde of a producLlon
8educLlon Lo lefL slde of producLlon ls one sLep along reverse of r|ghtmost
der|vat|on
LefLmosL subsLrlng maLchlng rlghL slde of producLlon ls noL necessarlly a handle
MlghL noL be able Lo reduce resulLlng sLrlng Lo sLarL symbol
ln example from prevlous sllde, lf reduce aAbcde Lo aAAcde, can noL reduce Lhls Lo S
Iorma||y, a hand|e of a r|ght-sentent|a| form :
ls a producLlon A and a poslLlon of where may be found and replaced wlLh A
! a"#e
" "bc | b
# -> d
abbcde
aAbcde
aAde
aABe
S
S
rm
=> aABe
rm
=>aAde
rm
=>aAbcde
rm
=> abbcde

8eplaclng A by leads Lo Lhe prevlous rlghL-senLenLlal form ln a rlghLmosL
derlvaLlon of
So lf S rm*=> Aw rm=> w Lhen A ln Lhe poslLlon followlng ls a handle of w
1he sLrlng w Lo Lhe rlghL of Lhe handle conLalns only Lermlnals
Can be more Lhan one handle lf grammar ls amblguous (more Lhan one rlghLmosL
derlvaLlon)

18) What |s hand|e prun|ng?

8epeaL Lhe followlng process, sLarLlng from sLrlng of Lokens unLll obLaln sLarL symbol:
LocaLe handle ln currenL rlghL-senLenLlal form
8eplace handle wlLh lefL slde of approprlaLe producLlon
1wo problems LhaL need Lo be solved:
Pow Lo locaLe handle
Pow Lo choose approprlaLe producLlon

19) Lxp|a|n stack |mp|ementat|on of sh|ft reduce pars|ng.

Sh|ft-keduce ars|ng
uaLa sLrucLures lnclude a sLack and an lnpuL buffer
SLack holds grammar symbols and sLarLs off empLy
lnpuL buffer holds Lhe sLrlng w Lo be parsed
arser shlfLs lnpuL symbols onLo sLack unLll a handle ls on Lop of Lhe sLack
Pandle ls reduced Lo Lhe lefL slde of approprlaLe producLlon
lf sLack conLalns only sLarL symbol and lnpuL ls empLy, Lhls lndlcaLes success
Act|ons of a Sh|ft-keduce arser
Sh|ft Lhe nexL lnpuL symbol ls shlfLed onLo Lhe Lop of Lhe sLack
keduce 1he parser reduces Lhe handle aL Lhe Lop of Lhe sLack Lo a nonLermlnal (Lhe lefL
slde of Lhe approprlaLe producLlon)
Accept 1he parser announces success
Lrror 1he parser dlscovers a synLax error and calls a recovery rouLlne

Sh|ft keduce ars|ng Lxamp|e
Stack Input Act|on
5 |d1 + |d2 * |d35 sh|ft
5|d1 + |d2 * |d35 reduce by L |d

5L + |d2 * |d35 sh|ft
5L + |d2 * |d35 sh|ft
5L + |d2 * |d35 reduce by L |d
5L + L * |d35 sh|ft
5L + L * |d35 sh|ft
5L + L * |d3 5 reduce by L |d
5L + L * L 5 reduce by L L * L
5L + L 5 reduce by L L + L
5L 5 accept


20) What are v|ab|e pref|xes?
V|ab|e ref|xes
1wo deflnlLlons of a vlable preflx:
A preflx of a rlghL senLenLlal form LhaL can appear on a sLack durlng shlfL-reduce parslng
A preflx of a rlghL-senLenLlal form LhaL does noL conLlnue pasL Lhe rlghL end of Lhe
rlghLmosL handle
Can always add Lokens Lo Lhe end of a vlable preflx Lo obLaln a rlghL-senLenLlal form

21) Lxp|a|n conf||cts |n sh|ft reduce pars|ng.
Conf||cts |n Sh|ft-keduce ars|ng
1here are grammars for whlch shlfL-reduce parslng can noL be used
Sh|ft]reduce conf||ct: can noL declde wheLher Lo shlfL or reduce
keduce]reduce conf||ct: can noL declde whlch of mulLlple posslble reducLlons Lo make
SomeLlmes can add rule Lo adapL for use wlLh amblguous grammar

22) What |s operator precedence pars|ng?
Cperator-recedence ars|ng
A form of shlfL-reduce parslng LhaL can apply Lo cerLaln slmple grammars
no producLlons can have rlghL slde
no rlghL slde can have Lwo ad[acenL nonLermlnals
CLher essenLlal requlremenLs musL be meL

Cnce Lhe parser ls bullL (ofLen by hand), Lhe grammar can be effecLlvely lgnored

23) What are precedence re|at|ons?
recedence ke|at|ons
ke|at|on Mean|ng
a < b a "y|e|ds precedence to" b
a = b a "has the same precedence as" b
a > b a "takes precedence over" b

24) now precedence re|at|ons are used?
Us|ng recedence ke|at|ons (1)
Can be LhoughL of as dellmlLlng handles:
< Marks lefL end of handle
> Appears ln Lhe lnLerlor of handle
= Marks rlghL end of handle
Conslder rlghL-senLenLlal 0a1 1 1 an8n:
Lach l ls elLher slngle nonLermlnal or
Lach al ls a slngle Loken
Suppose LhaL exacLly one precedence relaLlon wlll hold for each al, al+1 palr
uslng recedence 8elaLlons (2)

Mark beglnnlng and end of sLrlng wlLh $
8emove Lhe nonLermlnals
lnserL correcL precedence relaLlon beLween each palr of Lermlnals







< < < $

< *

<

< +

i
$ * i








Us|ng recedence ke|at|ons (3)
1o flnd Lhe currenL handle:
Scan Lhe sLrlng from Lhe lefL unLll Lhe flrsL > ls encounLered
Scan backwards (lefL) from Lhere unLll a < ls encounLered
LveryLhlng ln beLween, lncludlng lnLervenlng or surroundlng nonLermlnals, ls Lhe handle
1he nonLermlnals do noL lnfluence Lhe parse!

2S) Lxp|a|n operator precedence pars|ng a|gor|thm.

seL lp Lo polnL Lo Lhe flrsL symbol ln w$
lnlLlallze sLack Lo $
repeaL forever
lf $ on Lop of sLack ln lp polnLs Lo $
reLurn success
else
leL a be LopmosL symbol on sLack
leL b be symbol polnLed Lo by lp
id + id * id
$ < id > + < id > * < id > $

lf a < b or a = b
push b onLo sLack
advance lp Lo nexL lnpuL symbol
else lf a > b
repeaL
pop x
unLll Lop symbol on sLack < x
else
error()

26) Lxp|a|n a heur|st|c to produce a proper set of precedence re|at|ons.
recedence and Assoc|at|v|ty (1)
lor grammars descrlblng arlLhmeLlc expresslons:
Can consLrucL Lable of operaLor-precedence relaLlons auLomaLlcally
PeurlsLlc based on precedence and assoclaLlvlLy of operaLors
SelecLs proper handles, even lf grammar ls amblguous
1he followlng rules are deslgned Lo selecL Lhe proper handles Lo reflecL a glven seL of
assoclaLlvlLy and precedence rules for blnary operaLors :

lf operaLor 1 has hlgher precedence Lhan operaLor 2, make 1 > 2 and 2 < 1
lf 1 and 2 are of equal precedence:
lf Lhey are lefL assoclaLlve, make 1 > 2 and 2 > 1
lf Lhey are rlghL assoclaLlve, make 1 < 2 and 2 < 1

27) What |s an operator grammar?
A grammar havlng Lhe properLy (among oLher essenLlal requlremenLs) LhaL no producLlon
rlghL slde ls or has Lwo ad[acenL nonLermlnals ls called an operator grammar.

Cperator Grammar Lxamp|e
L L + L | L L | L * L | L ] L
| L ^ L | (L) | -L | |d

Where
^ ls of hlghesL precedence and ls rlghL-assoclaLlve
* and / are of nexL hlghesL precedence and are lefL-assoclaLlve
+ and are of lowesL precedence and are lefL-assoclaLlve

28) What are precedence funct|ons?
recedence Iunct|ons (1)
uo noL need Lo sLore enLlre Lable of precedence relaLlons
SelecL Lwo precedence funcLlons f and g :
f(a) < g(b) whenever a < b
f(a) = g(b) whenever a = b
f(a) > g(b) whenever a > b










29) now precedence funct|ons are constructed?

recedence Iunct|ons A|gor|thm
CreaLe symbols fa and fg for all Lokens and $
lf a = b Lhen fa and gb musL be ln same group
arLlLlon symbols lnLo as many groups as posslble
lor all cases where a < b, draw edge from group of gb Lo group of fa
lor all cases where a > b, draw edge from group of fa Lo group of gb
lf graph has cycles, no precedence funcLlons exlsL
CLherwlse:
f(a) ls Lhe lengLh of Lhe longesL paLh beglnnlng aL group of fa
0 5 0 5 5 3 1 1 $
0 6 6 0 4 4 4 2 2 &
$ i ) ( ^ / * - +

g(a) ls Lhe lengLh of Lhe longesL paLh beglnnlng aL group of ga

























Precedence Functions Example
< < < $

< *

<

< +

i
$ * + i
0 5 3 1 $
0 4 4 2 &
$ i * +









30) now error recovery |s enforced |n operator precedence parsers?
Detect|ng and nand||ng Lrrors
Lrrors can occur aL Lwo polnLs:
lf no precedence relaLlon holds beLween Lhe Lermlnal on Lop of sLack and currenL
lnpuL
lf a handle has been found, buL no producLlon ls found wlLh Lhls handle as rlghL slde
Lrrors durlng reducLlons can be handled wlLh dlagnosLlc message
Lrrors due Lo lack of precedence relaLlon can be handled by recovery rouLlnes speclfled ln
Lable

31) What are Lk parsers?
Lk arsers
L8 arsers us an efflclenL, boLLom-up parslng Lechnlque useful for a large class of ClCs
1oo dlfflculL Lo consLrucL by hand, buL auLomaLlc generaLors Lo creaLe Lhem exlsL (e.g.
?acc)
L8(k) grammars
L refers Lo lefL-Lo-rlghL scannlng of lnpuL
8 refers Lo rlghLmosL derlvaLlon (produced ln reverse order)
k refers Lo Lhe number of lookahead symbols needed for declslons (lf omlLLed,
assumed Lo be 1)
32) What are the benef|ts of Lk parsers?
8enef|ts of Lk ars|ng
Can be consLrucLed Lo recognlze vlrLually all programmlng language consLrucL for whlch
a ClC can be wrlLLen
MosL general non-backLracklng shlfL-reduce parslng meLhod known
Can be lmplemenLed efflclenLly
Pandles a class of grammars LhaL ls a superseL of Lhose handled by predlcLlve parslng
Can deLecL synLacLlc errors as soon as posslble wlLh a lefL-Lo-rlghL scan of lnpuL


33) Lxp|a|n Lk pars|ng A|gor|thm w|th d|agrams. Ior a g|ven grammar and pars|ng tab|e
1abu|ate the moves of Lk parser for a g|ven |nput str|ng |d * |d + |d.
Mode| of Lk arser



1he L8 parslng program works as follows :
1he schemaLlc of L8 parser conslsLs of an lnpuL,an ouLpuL,a sLack,a drlver program,a parslng Lable LhaL
has Lwo parLs (acLlons and goLo)
urlver program ls Lhe same for all L8 arsers
SLack conslsLs of sLaLes (sl) and grammar symbols (xl)
Lach sLaLe summarlzes lnformaLlon conLalned ln sLack below lL
Crammar symbols do noL acLually need Lo be sLored on sLack ln mosL lmplemenLaLlons
SLaLe symbol on Lop of sLack and nexL lnpuL symbol used Lo deLermlne shlfL/reduce declslon
arslng Lable lncludes acLlon funcLlon and goLo funcLlon
Act|on funct|on
8ased on sLaLe and nexL lnpuL symbol
AcLlons are shlfL, reduce, accepL or error
Goto funct|on
8ased on sLaLe and grammar symbol
roduces nexL sLaLe
ConflguraLlon (s0x1s1 xm sm,al al+1 an$) lndlcaLes rlghL-senLenLlal form x1x2 xmalal+1 an
lf acLlon[sm,al] = shlfL s, enLer conflguraLlon (s0x1s1 xmsmals,al+1 an$)
lf acLlon[sm,al] = reduce A 8, enLer conflguraLlon (s0x1s1 xm-rsm-rAs, al+1 an$), where s =
goLo[sm-r,A] and r ls lengLh of 8
lf acLlon[sm,al] = accepL, slgnal success

lf acLlon[sm,al] = error, Lry error recovery
Lk ars|ng A|gor|thm
seL lp Lo polnL Lo Lhe flrsL symbol ln w$
lnlLlallze sLack Lo s0
repeaL forever
leL s be LopmosL sLaLe on sLack
leL a be symbol polnLed Lo by lp
lf acLlon[s,a] = shlfL s
push a Lhen s onLo sLack
advance lp Lo nexL lnpuL symbol
else lf acLlon[s,a] = reduce A 8
pop 2*|8| symbols of sLack
leL s be sLaLe now on Lop of sLack
push A Lhen goLo[s ,A] onLo sLack
ouLpuL producLlon A 8
else lf acLlon[s,a] == accepL
reLurn success
else
error()
Lk ars|ng 1ab|e Lxamp|e






(1) E E + T
(2) E T
(3) T T * F
(4) T F




















Lk ars|ng Lxamp|e
Moves of Lk parser on |d * |d + |d


Stack Input Act|on
(1) s0 |d * |d + |d 5 sh|ft
r r r r 10
r r r r 11
r r s r 9
s s 8
1 s s 7
3 9 s s 6
r r r r 5
3 2 8 s s 4
r r r r 3
r r s r 2
a s 1
3 2 1 s s 0
F T E $ ) ( * + i
goto action sta
te

(2) s0 |d sS * |d + |d 5 reduce by I |d
(3) s0 I s3 * |d + |d 5 reduce by 1 I
(4) s0 1 s2 * |d + |d 5 sh|ft
(S) s0 1 s2 * s7 |d + |d 5 sh|ft
(6) s0 1 s2 * s7 |d sS + |d 5 reduce by I |d
(7) s0 1 s2 * s7 I s10 + |d 5 reduce by 1 1 * I
(8) S0 1 s2 + |d 5 reduce by L 1
(9) s0 L s1 + |d 5 sh|ft
(10) s0 L s1 + s6 |d 5 sh|ft
(11) s0 L s1 + s6 |d sS 5 reduce by I |d
(12) s0 L s1 + s6 I s3 5 reduce by 1 I
(13) s0 L s1 + s6 1 s9 5 reduce by L L + 1
(14) s0 L s1 5 accept

34) What are three types of Lk parsers?
1hree methods:
a. SLk (s|mp|e Lk)
l. noL all LhaL slmple (buL slmpler Lhan oLher Lwo)!
ll. WeakesL of Lhree meLhods, easlesL Lo lmplemenL
b. Construct|ng canon|ca| Lk pars|ng tab|es
l. MosL general of meLhods
ll. ConsLrucLed Lables can be qulLe large
c. LALk pars|ng tab|e (|ookahead Lk)
l. 1ables smaller Lhan canonlcal L8
ll. MosL programmlng language consLrucLs can be handled
3S) Lxp|a|n the non-recurs|ve |mp|ementat|on of pred|ct|ve parsers.









T
E
$
T
E
$
! #$%&'()'*% #+$,%$
!NPUT SYNBOL NON-
TERN!NAL !" + * ( ) $
E E TE E TE
E E +TE E E
T T FT T FT
T T T *FT T T
F F !" F (E)
STACK:
id id id + NPUT:
Predictive Parsing
Program
$
OUTPUT:
E
F
T
E
$
F T
T E
PARSNG
TABLE:
(Aho,Seth|,
U||man,
pp. 186)

36) What are the advantages of us|ng an |ntermed|ate |anguage?

Advantages of Us|ng an Intermed|ate Language
!"#$%&"#'(& - 8ulld a compller for a new machlne by aLLachlng a new code generaLor Lo an
exlsLlng fronL-end.
2. )*#'+',$#'-( - reuse lnLermedlaLe code opLlmlzers ln compllers for dlfferenL languages and
dlfferenL machlnes.
.-#": Lhe Lerms lnLermedlaLe code , lnLermedlaLe language , and lnLermedlaLe
represenLaLlon are all used lnLerchangeably.



















CS13S2 r|nc|p|es of Comp||er Des|gn Un|t-III uest|on and answers
-----------------------------------------------------------------------------------------------------
37) What |s |ntermed|ate code?

ln lnLermedlaLe code generaLlon we use synLax dlrecLed meLhods Lo LranslaLe Lhe source program lnLo
an lnLermedlaLe form programmlng language consLrucLs such as declaraLlons, asslgnmenLs and flow-of-
conLrol sLaLemenLs.


intermediate code is:
Lhe ouLpuL of Lhe arser and Lhe lnpuL Lo Lhe Code CeneraLor.
relaLlvely machlne-lndependenL: allows Lhe compller Lo be tetotqeteJ.
relaLlvely easy Lo manlpulaLe (opLlmlze).

38) What are the advantages of us|ng an |ntermed|ate |anguage?

Advantages of Us|ng an Intermed|ate Language
!"#$%&"#'(& - 8ulld a compller for a new machlne by aLLachlng a new code generaLor Lo an exlsLlng
fronL-end.
2. )*#'+',$#'-( - reuse lnLermedlaLe code opLlmlzers ln compllers for dlfferenL languages and
dlfferenL machlnes.
.-#": Lhe Lerms lnLermedlaLe code , lnLermedlaLe language , and lnLermedlaLe
represenLaLlon are all used lnLerchangeably.


39) What are the types of |ntermed|ate representat|ons?
1here are Lhree Lypes of lnLermedlaLe represenLaLlon:-
1. SynLax 1rees
2. osLflx noLaLlon
3. 1hree Address Code
SemanLlc rules for generaLlng Lhree-address code from common programmlng language consLrucLs are
slmllar Lo Lhose for consLrucLlng synLax Lrees of for generaLlng posLflx noLaLlon.
Craphlcal 8epresenLaLlons

A synLax Lree deplcLs Lhe naLural hlerarchlcal sLrucLure of a source program. A uAC (ulrecLed Acycllc
Craph) glves Lhe same lnformaLlon buL ln a more compacL way because common sub-expresslons are
ldenLlfled. A synLax Lree for
Lhe asslgnmenL sLaLemenL a:=b*-c+b*-c appear ln Lhe flgure.
. flg8.2

osLflx noLaLlon ls a llnearlzed represenLaLlon of a synLax Lree, lL ls a llsL of Lhe nodes of Lhe ln whlch a
node appears lmmedlaLely afLer lLs chlldren. 1he posLflx noLaLlon for Lhe synLax Lree ln Lhe flg ls
a b c umlnus + b c umlnus * + asslgn
1he edges ln a synLax Lree do noL appear expllclLly ln posLflx noLaLlon. 1hey can be recovered ln Lhe
order ln whlch Lhe nodes appear and Lhe no. of operands LhaL Lhe operaLor aL a node expecLs. 1he
recovery of edges ls slmllar Lo Lhe evaluaLlon, uslng a sLaff, of an expresslon ln posLflx noLaLlon.


40) Construct a syntax d|rected def|n|t|on for construct|ng a syntax tree for ass|gnment
statements.
SynLax Lree for asslgnmenL sLaLemenLs are produced by Lhe synLax dlrecLed deflnlLlon ln flg.
roducLlon SemanLlc 8ule
S |d := L S.npLr := mknode( asslgn , mkleaf(|d, |d.place), L.npLr)
L L1 + L2 L.npLr := mknode( + , L1.npLr ,L2.npLr)
L L1 * L2 L.npLr := mknode( * , L1.npLr ,L2.npLr)
L - L1 L.npLr := mkunode( umlnus , L1.npLr)
L ( L1 ) L.npLr := L1.npLr
L |d L.npLr := mkleaf(|d, |d.place)



41) now a syntax tree |s represented?
1hls same synLax-dlrecLed deflnlLlon wlll produce Lhe dag lf Lhe funcLlons mkunode(op, chlld) and
mknode(op, lefL, rlghL) reLurn a polnLer Lo an exlsLlng node whenever posslble, lnsLead of consLrucLlng
new nodes. 1he Loken ld has an aLLrlbuLe place LhaL polnLs Lo Lhe symbol-Lable enLry for Lhe ldenLlfler
ld.name, represenLlng Lhe lexeme assoclaLed wlLh LhaL occurrence of ld. lf Lhe lexlcal analyzer holds all
lexemes ln a slngle array of characLers, Lhen aLLrlbuLe name mlghL be Lhe lndex of Lhe flrsL characLer of
Lhe lexeme. 1wo represenLaLlons of Lhe synLax Lree ln llg8.2 appear ln llg.8.4. Lach node ls represenLed
as a record wlLh a fleld for lLs operaLor and addlLlonal flelds for polnLers Lo lLs chlldren. ln llg 8.4(b),
nodes are allocaLed from an array of records and Lhe lndex or poslLlon of Lhe node serves as Lhe polnLer
Lo Lhe node. All Lhe nodes ln Lhe synLax Lree can be vlslLed by followlng polnLers, sLarLlng from Lhe rooL
aL poslLlon lC.

flg8.4(a)
Iig8.4(b)

42) What |s three address code?
0 id b
1 id c
2 uminus 1
3 * 0 2
4 id b
5 id c
6 uminus 5
7 * 4 6
8 3 7
9 id a
10 assign 9 8
11

1hree-address code ls a sequence of sLaLemenLs of Lhe general form
x:= ? Cp Z
where x, y, and z are names, consLanLs, or compller-generaLed Lemporarles, op sLands for any operaLor,
such as a flxed- or floaLlng-polnL arlLhmeLlc operaLor, or a loglcal operaLor on 8oolean-valued daLa. noLe
LhaL no bullL-up arlLhmeLlc expresslons are permlLLed, as Lhere ls only one operaLor on Lhe rlghL slde of a
sLaLemenL. 1hus a source language expresslon llke x+y*z mlghL be LranslaLed lnLo a sequence
L1 := y * z
L2 : = x + L1
where L1 and L2 are compller-generaLed Lemporary names. 1hls unravellng of compllcaLed arlLhmeLlc
expresslons and of nesLed flow-of-conLrol sLaLemenLs makes Lhree-address code deslrable for LargeL
code generaLlon and opLlmlzaLlon. 1he use of names for Lhe lnLermedlaLe values compuLed by a
program allow- Lhree-address code Lo be easlly rearranged unllke posLflx noLaLlon. Lhree-address code
ls a llnearlzed represenLaLlon of a synLax Lree or a dag ln whlch expllclL names correspond Lo Lhe lnLerlor
nodes of Lhe graph. 1he synLax Lree and dag ln llg. 8.2 are represenLed by Lhe Lhree-address code
sequences ln llg. 8.S. varlable names can appear dlrecLly ln Lhree-address sLaLemenLs, so llg. 8.S(a) has
no sLaLemenLs correspondlng Lo Lhe leaves ln llg. 8.4.
Code for synLax Lree
L1 := -c
L2 := b * L1
L3 := -c
L4 := b * L3
LS := L2 + L4
a := LS

Code for uAC
L1 := -c
L2 := b * L1

LS := L2 + L2
a := LS

1he reason for Lhe Lerm Lhree-address code ls LhaL each sLaLemenL usually conLalns Lhree addresses,
Lwo for Lhe operands and one for Lhe resulL. ln Lhe lmplemenLaLlons of Lhree-address code glven laLer ln
Lhls secLlon, a programmer-deflned name ls replaced by a polnLer Lc a symbol-Lable enLry for LhaL name.
43) What are the types of three address statements?
!"#$% &' !()$$*+,,)$%% -./.$0$1.%
1hree-address sLaLemenLs are akln Lo assembly code. SLaLemenLs can have symbollc labels and Lhere
are sLaLemenLs for flow of conLrol. A symbollc label represenLs Lhe lndex of a Lhree-address sLaLemenL ln
Lhe array holdlng lnLer- medlaLe code. AcLual lndlces can be subsLlLuLed for Lhe labels elLher by maklng a
separaLe pass, or by uslng back paLchlng, dlscussed ln SecLlon 8.6. Pere are Lhe common Lhree-
address sLaLemenLs used ln Lhe remalnder of Lhls book:
1. Ass|gnment statements of Lhe form x: = y op z, where op ls a blnary arlLhmeLlc or loglcal operaLlon.
2. Ass|gnment |nstruct|ons of Lhe form x:= op y, where op ls a unary operaLlon. LssenLlal unary
operaLlons lnclude unary mlnus, loglcal negaLlon, shlfL operaLors, and converslon operaLors LhaL, for
example, converL a flxed-polnL number Lo a floaLlng-polnL number.
3. Copy statements of Lhe form x: = y where Lhe value of y ls asslgned Lo x.
4. 1he uncond|t|ona| [ump goLo L. 1he Lhree-address sLaLemenL wlLh label L ls Lhe nexL Lo be execuLed.
S. Cond|t|ona| [umps such as lf x relop y goLo L. 1hls lnsLrucLlon applles a relaLlonal operaLor (<, =, >=,
eLc.) Lo x and y, and execuLes Lhe sLaLemenL wlLh label L nexL lf x sLands ln relaLlon relop Lo y. lf noL, Lhe
Lhree-address sLaLemenL followlng lf x relop y goLo L ls execuLed nexL, as ln Lhe usual sequence.
6. param x and ca|| p, n for procedure calls and reLurn y, where y represenLlng a reLurned value ls
opLlonal. 1helr Lyplcal use ls as Lhe sequence of Lhree-address sLaLemenLs
param x1
param x2
param xn

call p, n
generaLed as parL of a call of Lhe procedure p(x,, x~,..., x ). 1he lnLeger n lndlcaLlng Lhe number of acLual
parameLers ln call p, n ls noL redundanL because calls can be nesLed. 1he lmplemenLaLlon of procedure
calls ls ouLllne d ln SecLlon 8.7.
7. Indexed ass|gnments of Lhe form x: = y[ l ] and x [ l ]: = y. 1he flrsL of Lhese seLs x Lo Lhe value ln Lhe
locaLlon l memory unlLs beyond locaLlon y. 1he sLaLemenL x[l]:=y seLs Lhe conLenLs of Lhe locaLlon l unlLs
beyond x Lo Lhe value of y. ln boLh Lhese lnsLrucLlons, x, y, and l refer Lo daLa ob[ecLs.
8. Address and po|nter ass|gnments of Lhe form x:= &y, x:= *y and *x: = y. 1he flrsL of Lhese seLs Lhe
value of x Lo be Lhe locaLlon of y. resumably y ls a name, perhaps a Lemporary, LhaL denoLes an
expresslon wlLh an l-value such as A[l, [], and x ls a polnLer name or Lemporary. 1haL ls, Lhe r-value of x ls
Lhe l-value (locaLlon) of some ob[ecL!. ln Lhe sLaLemenL x: = ~y, presumably y ls a polnLer or a Lemporary
whose r- value ls a locaLlon. 1he r-value of x ls made equal Lo Lhe conLenLs of LhaL locaLlon. llnally, +x: =
y seLs Lhe r-value of Lhe ob[ecL polnLed Lo by x Lo Lhe r-value of y.
1he cholce of allowable operaLors ls an lmporLanL lssue ln Lhe deslgn of an lnLermedlaLe form.
1he operaLor seL musL clearly be rlch enough Lo lmplemenL Lhe operaLlons ln Lhe source language. A
small operaLor seL ls easler Lo lmplemenL on a new LargeL machlne. Powever, a resLrlcLed lnsLrucLlon seL
may force Lhe fronL end Lo generaLe long sequences of sLaLemenLs for some source, language
operaLlons. 1he opLlmlzer and code generaLor may Lhen have Lo work harder lf good code ls Lo be
generaLed.

44) Lxp|a|n the process of syntax d|rected trans|at|on of three address code.
Syntax-Directed Translation into Three-Address Code

When Lhree-address code ls generaLed, Lemporary names are made up for Lhe lnLerlor nodes of a synLax
Lree. 1he value of non-Lermlnal L on Lhe lefL slde of L L1 + L wlll be compuLed lnLo a new Lemporary L.
ln general, Lhe Lhree- address code for ld: = L conslsLs of code Lo evaluaLe L lnLo some Lemporary L,
followed by Lhe asslgnmenL ld.place: = L. lf an expresslon ls a slngle ldenLlfler, say y, Lhen y lLself holds
Lhe value of Lhe expresslon. lor Lhe momenL, we creaLe a new name every Llme a Lemporary ls needed,
Lechnlques for reuslng Lemporarles are glven ln SecLlon S.3. 1he S-aLLrlbuLed deflnlLlon ln llg. 8.6
generaLes Lhree-address code for asslgnmenL sLaLemenLs. Clven lnpuL a: = b+ c + b+ c, lL produces
Lhe code ln llg. 8.S(a). 1he synLheslzed aLLrlbuLe S.code represenLs Lhe Lhree- address code for Lhe
asslgnmenL S. 1he non-Lermlnal L has Lwo aLLrlbuLes:
1. L.place, Lhe name LhaL wlll hold Lhe value of L, and

2. L.code, Lhe sequence of Lhree-address sLaLemenLs evaluaLlng L.
1he funcLlon newLemp reLurns a sequence of dlsLlncL names L1, L2,... ln response Lo successlve calls. lor
convenlence, we use Lhe noLaLlon gen(x : = y + z) ln llg. 8.6 Lo represenL Lhe Lhree-address sLaLemenL
x: = y + z. Lxpresslons appearlng lnsLead of varlables llke x, y, and z are evaluaLed when passed Lo gen,
and quoLed operaLors or operands, llke + , are Laken llLerally. ln pracLlce, Lhree- address sLaLemenLs
mlghL be senL Lo an ouLpuL flle, raLher Lhan bullL up lnLo Lhe code aLLrlbuLes. llow-of-conLrol sLaLemenLs
can be added Lo Lhe language of asslgnmenLs ln llg. 8.6 by producLlons and semanLlc rules )llke Lhe ones
for whlle sLaLemenLs ln llg. 8.7. ln Lhe flgure, Lhe code for S - whlle L do S, ls generaLed uslng new
aLLrlbuLes S.begln and S.afLer Lo mark Lhe flrsL sLaLemenL ln Lhe code for L and Lhe sLaLemenL followlng
Lhe code for S, respecLlvely.








1hese aLLrlbuLes represenL labels creaLed by a funcLlon new label LhaL reLurns a new label every Llme lL
ls called. noLe LhaL S.afLer becomes Lhe label of Lhe sLaLemenL LhaL comes afLer Lhe code for Lhe whlle
sLaLemenL. We assume LhaL a non-zero expresslon represenLs Lrue, LhaL ls, when Lhe value of l becomes
zero, conLrol leaves Lhe whlle sLaLemenL. f:expresslons LhaL govern Lhe flow of conLrol may ln general be
8oolean expresslons conLalnlng relaLlonal and loglcal operaLors. 1he semanLlc rules for whlle sLaLemenLs
ln SecLlon 8.6 dlffer from Lhose ln llg. 8.7 Lo allow for flow of conLro1 wlLhln 8oolean expresslons.
osLflx noLaLlon -an be obLalned by adapLlng Lhe semanLlc rules ln llg. 8.6 (or see llg. 2.S). 1he posLflx
noLaLlon for an ldenLlfler ls Lhe ldenLlfler lLself. 1he rules for Lhe oLher producLlons concaLenaLe only Lhe
operaLor afLer Lhe code for Lhe operands. lor example, assoclaLed wlLh Lhe producLlon L L, ls Lhe
semanLlc rule
L.code:= L1.code || umlnus
1n general, Lhe lnLermedlaLe form produced by Lhe synLax-dlrecLed LranslaLlons ln Lhls chapLer can he
changed by maklng slmllar modlflcaLlons Lo Lhe semanLlc rules.

4S) Lxp|a|n |n deta|| the |mp|ementat|on of three address statements.


20#3$0$1./.451% 5' .()$$*+,,)$%% -./.$0$1.%
A Lhree-address sLaLemenL ls an absLracL form of lnLermedlaLe code. ln a compller, Lhese sLaLemenLs
can be lmplemenLed as records wlLh flelds for Lhe operaLor and Lhe operands. 1hree such
represenLaLlons are quadruples, Lrlples, and lndlrecL Lrlples.
uadrup|es
A quadruple ls a record sLrucLure wlLh four flelds, whlch we call op, arg l, arg 2, and resulL. 1he op fleld
conLalns an lnLernal code for Lhe operaLor. 1he Lhree-address sLaLemenL x:= y op z ls represenLed by
placlng y ln arg 1. z ln arg 2. and x ln resulL. SLaLemenLs wlLh unary operaLors llke x: = y or x: = y do noL
use arg 2. CperaLors llke param use nelLher arg2 nor resulL. CondlLlonal and uncondlLlonal [umps puL Lhe
LargeL label ln resulL. 1he quadruples ln llg. P.S(a) are for Lhe asslgnmenL a: = b+ c + b l c. 1hey are
obLalned from Lhe Lhree-address code ln llg. 8.S(a). 1he conLenLs of flelds arg 1, arg 2, and resulL are
normally polnLers Lo Lhe symbol-Lable enLrles for Lhe names represenLed by Lhese flelds. lf so,
Lemporary names musL be enLered lnLo Lhe symbol Lable as Lhey are creaLed.
!)4#3$%
1o avold enLerlng Lemporary names lnLo Lhe symbol Lable. we mlghL refer Lo a Lemporary value bl Lhe
poslLlon of Lhe sLaLemenL LhaL compuLes lL. lf we do so, Lhree-address sLaLemenLs can be represenLed by
records wlLh only Lhree flelds: op, arg 1 and arg2, as ln llg. 8.8(b). 1he flelds arg l and arg2, for Lhe
argumenLs of op, are elLher polnLers Lo Lhe symbol Lable (for programmer- deflned names or consLanLs)
or polnLers lnLo Lhe Lrlple sLrucLure (for Lemporary values). Slnce Lhree flelds are used, Lhls lnLermedlaLe
code formaL ls known as Lrlples. LxcepL for Lhe LreaLmenL of programmer-deflned names, Lrlples
correspond Lo Lhe represenLaLlon of a synLax Lree or dag by an array of nodes, as ln llg. 8.4.




flg8.8(a) Cudraples flg8.8(b)
1rlples
op Arg1 Arg2 8esulL
(0) umlnus c L1
(1) * b L1 L2
(2) umlnus c L3
(3) * b L3 L4
(4) + L2 L4 LS
(S) := LS a
op Arg1 Arg2
(0) umlnus c
(1) * b (0)
(2) umlnus c
(3) * b (2)
(4) + (1) (3)
(S) := a (4)

arenLheslzed numbers represenL polnLers lnLo Lhe Lrlple sLrucLure, whlle symbol-Lable polnLers
are represenLed by Lhe names Lhemselves. ln pracLlce, Lhe lnformaLlon needed Lo lnLerpreL Lhe dlfferenL
klnds of enLrles ln Lhe arg 1 and arg2 flelds can be encoded lnLo Lhe op fleld or some addlLlonal flelds.
1he Lrlples ln llg. 8.8(b) correspond Lo Lhe quadruples ln llg. 8.8(a). noLe LhaL Lhe copy sLaLemenL a:= LS
ls encoded ln Lhe Lrlple represenLaLlon by placlng a ln Lhe arg 1 fleld and uslng Lhe operaLor asslgn. A
Lernary operaLlon llke x[ l ]: = y requlres Lwo enLrles ln Lhe Lrlple sLrucLure, as shown ln llg. 8.9(a), whlle
x: = y[l] ls naLurally represenLed as Lwo operaLlons ln llg. 8.9(b).

Ind|rect 1r|p|es

AnoLher lmplemenLaLlon of Lhree-address code LhaL has been consldered ls LhaL of llsLlng polnLers Lo
Lrlples, raLher Lhan llsLlng Lhe Lrlples Lhemselves. 1hls lmplemenLaLlon ls naLurally called lndlrecL Lrlples.
lor example, leL us use an array sLaLemenL Lo llsL polnLers Lo Lrlples ln Lhe deslred order. 1hen Lhe Lrlples
ln llg. 8.8(b) mlghL be represenLed as ln llg. 8.10.


46) now dec|arat|ons are trans|ated |nto |ntermed|ate code?

DECLARATIONS
As Lhe sequence of declaraLlons ln a procedure or block ls examlned, we can lay ouL sLorage for
names local Lo Lhe procedure. lor each local name, we creaLe a symbol-Lable enLry wlLh lnformaLlon llke
Lhe Lype and Lhe relaLlve address of Lhe sLorage for Lhe name. 1he relaLlve address conslsLs of an offseL
from Lhe base of Lhe sLaLlc daLa area or Lhe fleld for local daLa ln an acLlvaLlon record. When Lhe fronL
end generaLes addresses, lL may have a LargeL machlne ln mlnd. Suppose LhaL addresses of consecuLlve
lnLegers dlffer by 4 on a byLe- addressable machlne. 1he address calculaLlons generaLed by Lhe fronL end
may Lherefore lnclude mulLlpllcaLlons by 4. 1he lnsLrucLlon seL of Lhe LargeL machlne may also favor
cerLaln layouLs of daLa ob[ecLs, and hence Lhelr addresses. We lgnore allgnmenL of daLa ob[ecLs here,
Lxample 7.3 shows how daLa ob[ecLs are allgned by Lwo compllers.

6$73/)/.451% 41 / 8)57$,9)$
1he synLax of languages such as C, ascal, and lorLran, allows all Lhe declaraLlons ln a slngle procedure
Lo be processed as a group. ln Lhls case, a global varlable, say offseL, can keep Lrack of Lhe nexL aval1able
relaLlve address. ln Lhe LranslaLlon scheme of llg. S.l1 non-Lermlnal generaLes a sequence of
declaraLlons of Lhe form ld: 1. 8efore .he flrsL declaraLlon ls consldered, offseL ls seL Lo 0. As each new
name ls seen, LhaL name ls enLered ln Lhe symbol Lable wlLh offseL equal Lo Lhe currenL value of offseL,
and offseL ls lncremenLed by Lhe wldLh of Lhe daLa ob[ecL denoLed by LhaL name. 1he procedure
enLer(name, Lype, offseL) creaLes a symbol-Lable enLry for name, glves lL Lype and relaLlve address
offseL ln lLs daLa area. We use synLheslzed aLLrlbuLes Lype and wldLh for non-Lermlnal 1 Lo lndlcaLe Lhe
Lype and wldLh, or number of memory unlLs Laken by ob[ecLs of LhaL Lype. ALLrlbuLe Lype represenLs a
Lype expresslon consLrucLed from Lhe baslc Lypes lnLeger and real by applylng Lhe Lype consLrucLors
polnLer and array, as ln SecLlon 6.l. lf Lype expresslons are represenLed by graphs, Lhen aLLrlbuLe Lype
mlghL be a polnLer Lo Lhe node represenLlng a Lype expresslon. ln llg. 8. l , lnLegers have wldLh 4 and
real have wldLh 8. 1he wldLh of an array ls obLalned by mulLlplylng Lhe wldLh of each elemenL by Lhe
number of elemenLs ln Lhe array.- 1he wldLh of each polnLer ls assumed Lo be 4.
u
u u , u
u ld : 1 [enLer (ld.name, 1.Lype, offseL),
CffseL:= offseL + 1.wldLh }
1 lnLeger [1.Lype :=lnLeger,
1.wldLh :=4}

1 real [1.Lype := real,
1.wldLh := 8}
1 array [num ] of 11 [1.Lype :=array(num.val, 11.Lype),
1.wldLh :=num.val x 11.wldLh}
1 ^11 [1.Lype :=polnLer (1.Lype),
1.wldLh:=4}


ln ascal and C, a polnLer may be seen before we learn Lhe Lype of Lhe ob[ecL polnLed Lo
SLorage allocaLlon for such Lypes ls slmpler lf all polnLers have Lhe same wldLh. 1he lnlLlallzaLlon of offseL
ln Lhe LranslaLlon scheme of llg. 8.1 ls more evldenL lf Lhe flrsL producLlon appears on one llne as:
[offseL:= 0 } u
non-Lermlnals generaLlng a. called marker non-Lermlnals ln SecLlon S.6, can be used Lo rewrlLe
producLlons so LhaL all acLlons appear aL Lhe ends of rlghL sldes. uslng a marker non-Lermlnal M, (8.2)
can be resLaLed as:
M u
M (offseL:= 0}

Keeping Track oI Scope InIormation

ln a language wlLh nesLed procedures, names local Lo each procedure can be asslgned
relaLlve addresses uslng Lhe approach of llg. 8.11 . When a nesLed procedure ls seen, processlng of
declaraLlons ln Lhe encloslng procedure ls Lemporarlly suspended. 1hls approach wlll he lllusLraLed by
addlng semanLlc rules Lo Lhe followlng language.
u

u u,u | ld: 1 proc ld, u,S

1he producLlon for non-Lermlnals S for sLaLemenLs and 1 for Lypes are noL shown because we focus on
declaraLlons. 1he non-Lermlnal 1 has synLheslzed aLLrlbuLes Lype and wldLh, as ln Lhe LranslaLlon scheme
of llg. lor slmpllclLy, suppose LhaL Lhere ls a separaLe symbol Lable for each procedure ln Lhe language
(8.3). Cne posslble lmplemenLaLlon of a symbol Lable ls a llnked llsL of enLrles for names. Clever
lmplemenLaLlons can be subsLlLuLed lf deslred. A new symbol Lable ls creaLed when a procedure
declaraLlon u proc ld u~, S ls seen, and enLrles for Lhe declaraLlons ln u~ are creaLed ln Lhe new Lable.
1he new Lable polnLs back Lo Lhe symbol Lable of Lhe encloslng procedure, Lhe name represenLed by ld
lLself ls local Lo Lhe encloslng procedure. 1he only change from Lhe LreaLmenL of varlable declaraLlons ln
llg. 8.11 ls LhaL Lhe procedure enLer ls Lold whlch symbol Lable Lo make an enLry ln. lor example, symbol
Lables for flve procedures are shown ln llg. 8. l2. 1he nesLlng sLrucLure of Lhe procedures can be
deduced from Lhe llnks beLween Lhe symbol Lables, Lhe program ls ln llg. 7.22. 1he symbol Lables for
procedures readarray, exchange, and qulcksorL polnL back Lo LhaL for Lhe conLalnlng procedure sorL,
conslsLlng of Lhe enLlre program. Slnce parLlLlon ls declared wlLhln qulcksorL, lLs Lable polnLs Lo LhaL of
qulck sorL.






The semantic rules are deIined in terms oI the Iollowing operations:

l. mkLable(prevlous) creaLes a new symbol Lable and reLurns a polnLer Lo Lhe new Lable. 1he argumenL
prevlous polnLs Lo a prevlously creaLed symbol Lable, presumably LhaL for Lhe encloslng procedure. 1he
polnLer prevlous ls placed ln a header for Lhe new symbol Lable, along wlLh addlLlonal lnformaLlon such
as Lhe nesLlng depLh of a procedure. We can also number Lhe procedures ln Lhe order Lhey are declared
and keep Lhls number ln Lhe header.
2. enLer(Lable, name, Lype, offseL) creaLes a new enLry for name oome ln Lhe symbol Lable polnLed Lo by
Lable. Agaln, enLer places Lype and relaLlve address offseL ln flelds wlLhln Lhe enLry.
3. addwldLh(Lable, wldLh) records Lhe cumulaLlve wldLh of all Lhe enLrles Lable ln Lhe header assoclaLed
wlLh Lhls symbol Lable.
4. enLerproc (Lable, name, newLable) creaLes a new enLry for procedure name ln Lhe symbol Lable
polnLed Lo by Lable. 1he argumenL newLable polnLs Lo Lhe symbol Lable for Lhls procedure name.
1he LranslaLlon scheme ln llg. S. l3 shows how daLa can be lald ouL ln one pass, uslng a sLack LblpLr Lo
hold polnLers Lo symbol Lables of Lhe encloslng procedures. WlLh Lhe symbol Lables oL llg. 8.12, LblpLr
wlll conLaln polnLers Lo Lhe Lables for -orL, qulcksorL, and parLlLlon when Lhe declaraLlons ln parLlLlon are
consldered. 1he polnLer Lo Lhe currenL symbol Lable ls on Lop. 1he oLher sLack offseL ls Lhe naLural
generallzaLlon Lo nesLed procedures of aLLrlbuLe offseL ln llg. 8. l l. 1he Lop elemenL of offseL ls Lhe nexL
avallable relaLlve address for a local of Lhe currenL procedure. All semanLlc acLlons ln Lhe sub-Lrees for 8
and C ln
A 8 C [ acLlonA}
are done before acLlonA Lhe end of Lhe producLlon occurs. Pence, Lhe acLlon assoclaLed wlLh Lhe marker
M ln llg. 8.l3 ls Lhe flrsL Lo be done. 1he acLlon for non-Lermlnal M lnlLlallzes sLack LblpLr wlLh a symbol
Lable for Lhe ouLermosL scope, creaLed by operaLlon mkLable(nll). 1he acLlon also pushes relaLlve
address 0 onLo sLack offseL. 1he non-Lermlnal v plays a slmllar role when a procedure declaraLlon
appears. lLs acLlon uses Lhe operaLlon mkLable(Lop(LblpLr)) Lo creaLe a new symbol Lable. Pere Lhe
argumenL Lop(LblpLr) glves Lhe encloslng scope of Lhe new Lable. A polnLer Lo Lhe new Lable ls pushed
above LhaL for Lhe encloslng scope. Agaln, 0 ls pushed onLo offseL.

lor each varlable declaraLlon ld: 1. an enLry ls creaLed for ld ln Lhe currenL symbol Lable. 1hls
declaraLlon leaves Lhe sLack polnLer unchanged, Lhe Lop of sLack offseL ls lncremenLed by 1.wldLh. when
Lhe acLlon on Lhe rlghL slde of u proc ld: n u,, S occurs. Lhe wldLh of all
declaraLlons generaLed by u1 ls on Lop of sLack offseL. , lL ls recorded uslng addwldLh. and offseL are
Lhen popped, and we reverL Lo examlnlng Lhe declaraLlons ln Lhe closlng procedure. AL Lhls polnL, Lhe
name of Lhe enclosed procedure ls enLered lnLo Lhe symbol Lable of lLs encloslng procedure.

M u [addwldLh(Lop(LblpLr), Lop(offseL)),
op(LblpLr), pop(offseL)}
M [ L := mkLable(nll),
ush(L,LblpLr), push(0,offseL)}
u u1 ,u2
u proc ld , n u1 ,S [ L := Lop(LblpLr),
addwldLh(L.Lop(offseL)),
pop(LblpLr), pop(offseL),
enLerproc(Lop(LblpLr), ld.name, L)}
u ld : 1 [enLer(Lop(LblpLr),ld.name,1.Lype,Lop(offseL)),
Lop(offseL) := Lop(offseL) +1.wldLh }
n [ L := mkLable(Lop(LblpLr)),
ush(L, LblpLr), push(0, offseL)}


Field Names in Records


1he followlng producLlon allows non-Lermlnal 1 Lo generaLe records ln addlLlon Lo baslc Lypes, polnLers,
and arrays:
1 record u end
1he acLlons ln Lhe LranslaLlon scheme of llg. S.l4 emphaslze Lhe slmllarlLy beLween Lhe layouL of records
as a language consLrucL and acLlvaLlon records. Slnce procedure deflnlLlons do noL affecL Lhe wldLh
compuLaLlons ln llg. 8.13, we overlook Lhe facL LhaL Lhe above producLlon also allows procedure
deflnlLlons Lo appear wlLhln records.





1 record L u end [1.Lype := record(Lop(LblpLr)),
1.wldLh := Lop(offseL),
op(LblpLr), pop(offseL) }
L [ L:= mkLable(nll),
ush(L, LblpLr), push (0, offseL) }


AfLer Lhe keyword record ls seen, Lhe acLlng assoclaLed wlLh Lhe marker
creaLes a new symbol Lable for Lhe fleld names. A polnLer Lo Lhls symbol Lable ls pushed onLo sLack
LblpLr and relaLlve address 0 ls pushed onLo sLack . 1he acLlon for u ld: 1 ln llg. 8.13 Lherefore enLers
lnformaLlon abouL Lhe fleld name ld lnLo Lhe symbol Lable for Lhe record. lurLhermore, Lhe Lop of sLack
wlll hold Lhe wldLh of all Lhe daLa ob[ecLs wlLhln Lhe record afLer Lhe flelds have been examlned. 1he
acLlon followlng end ln llg. 8. 14 reLurns Lhe wldLh as synLheslzed aLLrlbuLe 1.wldLh. 1he Lype 1.Lype ls
obLalned by applylng Lhe consLrucLor tecotJ Lo Lhe polnLer Lo Lhe symbol Lable for Lhls record.

CS13S2 r|nc|p|es of Comp||er Des|gn Un|t-IV uest|on and answers

UNIT IV CODE GENERATION 9
Issues in the design of code generator The target machine Runtime Storage management
Basic BIocks and FIow Graphs Next-use Information A simpIe Code generator DAG
representation of Basic BIocks PeephoIe Optimization.


1) What |s the ro|e of code generator |n a comp||er?
!"#$ %$&$'()*"&
The Iinal phase in our compiler model is the 75,$ :$1$)/.5). It takes as
input an intermediate representation oI the source program and produces as output an
equivalent target program.
1he requlremenLs LradlLlonally lmposed on a code generaLor are severe. 1he ouLpuL
code musL be correcL and of hlgh quallLy, meanlng LhaL lL should make effecLlve use of Lhe
resources of Lhe LargeL machlne. Moreover, Lhe code generaLor lLself should run efflclenLly.




f|g. 1

2) Wr|te |n deta|| the |ssues |n the des|gn of code generator.
ISSUES IN THE DESIGN OF A CODE GENERATOR
Whlle Lhe deLalls are dependenL on Lhe LargeL language and Lhe operaLlng sysLem, lssues
such as memory managemenL, lnsLrucLlon selecLlon, reglsLer allocaLlon, and evaluaLlon order are
lnherenL ln almosL all code generaLlon problems.
2;8<! !& !=> ?&6> @>;>A+!&A
1he lnpuL Lo Lhe code generaLor conslsLs of Lhe lnLermedlaLe represenLaLlon of Lhe source
program produced by Lhe fronL end, LogeLher wlLh lnformaLlon ln Lhe symbol Lable LhaL ls used Lo
deLermlne Lhe run Llme addresses of Lhe daLa ob[ecLs denoLed by Lhe names ln Lhe lnLermedlaLe
represenLaLlon.
1here are several cholces for Lhe lnLermedlaLe language, lncludlng: llnear represenLaLlons
such as posLflx noLaLlon, Lhree address represenLaLlons such as quadruples, vlrLual machlne
represenLaLlons such as synLax Lrees and dags.
We assume that prior to code generation the Iront end has scanned, parsed, and translated the
source program into a reasonably detailed intermediate representation, so the values oI names
appearing in the intermediate language can be represented by quantities that the target machine
can directly manipulate (bits, integers, reals, pointers, etc.). We also assume that the necessary
type checking has take place, so type conversion operators have been inserted wherever
necessary and obvious semantic errors (e.g., attempting to index an array by a Iloating point
number) have already been detected. The code generation phase can thereIore proceed on the
assumption that its input is Iree oI errors. In some compilers, this kind oI semantic checking is
done together with code generation.
1AkGL1 kCGkAMS

1he ouLpuL of Lhe code generaLor ls Lhe LargeL program. 1he ouLpuL may Lake on a varleLy of
forms: absoluLe machlne language, relocaLable machlne language, or assembly language.
roduclng an absoluLe machlne language program as ouLpuL has Lhe advanLage LhaL lL can be
placed ln a locaLlon ln memory and lmmedlaLely execuLed. A small program can be complled and
execuLed qulckly. A number of sLudenL-[ob compllers, such as WA1llv and L/C, produce absoluLe
code.
roduclng a relocaLable machlne language program as ouLpuL allows subprograms Lo be
complled separaLely. A seL of relocaLable ob[ecL modules can be llnked LogeLher and loaded for
execuLlon by a llnklng loader. AlLhough we musL pay Lhe added expense of llnklng and loadlng lf we
produce relocaLable ob[ecL modules, we galn a greaL deal of flexlblllLy ln belng able Lo complle
subrouLlnes separaLely and Lo call oLher prevlously complled programs from an ob[ecL module. lf Lhe
LargeL machlne does noL handle relocaLlon auLomaLlcally, Lhe compller musL provlde expllclL relocaLlon
lnformaLlon Lo Lhe loader Lo llnk Lhe separaLely complled program segmenLs.
roduclng an assembly language program as ouLpuL makes Lhe process of code generaLlon
somewhaL easler .We can generaLe symbollc lnsLrucLlons and use Lhe macro faclllLles of Lhe assembler
Lo help generaLe code .1he prlce pald ls Lhe assembly sLep afLer code generaLlon.
8ecause produclng assembly code does noL dupllcaLe Lhe enLlre Lask of Lhe assembler, Lhls cholce ls
anoLher reasonable alLernaLlve, especlally for a machlne wlLh a small memory, where a compller musL
uses several passes.
MLMCk MANAGLMLN1
Mapplng names ln Lhe source program Lo addresses of daLa ob[ecLs ln run Llme memory ls
done cooperaLlvely by Lhe fronL end and Lhe code generaLor. We assume LhaL a name ln a Lhree-address
sLaLemenL refers Lo a symbol Lable enLry for Lhe name.
lf machlne code ls belng generaLed, labels ln Lhree address sLaLemenLs have Lo be converLed
Lo addresses of lnsLrucLlons. 1hls process ls analogous Lo Lhe back paLchlng . Suppose LhaL labels refer
Lo quadruple numbers ln a quadruple array. As we scan each quadruple ln Lurn we can deduce Lhe
locaLlon of Lhe flrsL machlne lnsLrucLlon generaLed for LhaL quadruple, slmply by malnLalnlng a counL of
Lhe number of words used for Lhe lnsLrucLlons generaLed so far. 1hls counL can be kepL ln Lhe quadruple
array (ln an exLra fleld), so lf a reference such as [: qoto l ls encounLered, and l ls less Lhan [, Lhe currenL
quadruple number, we may slmply generaLe a [ump lnsLrucLlon wlLh Lhe LargeL address equal Lo Lhe
machlne locaLlon of Lhe flrsL lnsLrucLlon ln Lhe code for quadruple l. lf, however, Lhe [ump ls forward, so l
exceeds [, we musL sLore on a llsL for quadruple l Lhe locaLlon of Lhe flrsL machlne lnsLrucLlon generaLed
for quadruple [. 1hen we process quadruple l, we flll ln Lhe proper machlne locaLlon for all lnsLrucLlons
LhaL are forward [umps Lo l.

INS1kUC1ICN SLLLC1ICN
1he naLure of Lhe lnsLrucLlon seL of Lhe LargeL machlne deLermlnes Lhe dlfflculLy of
lnsLrucLlon selecLlon. 1he unlformlLy and compleLeness of Lhe lnsLrucLlon seL are lmporLanL facLors. lf
Lhe LargeL machlne does noL supporL each daLa Lype ln a unlform manner, Lhen each excepLlon Lo Lhe
general rule requlres speclal handllng.
lnsLrucLlon speeds and machlne ldloms are oLher lmporLanL facLors. lf we do noL care abouL
Lhe efflclency of Lhe LargeL program, lnsLrucLlon selecLlon ls sLralghLforward. lor each Lype of Lhree-
address sLaLemenL we can deslgn a code skeleLon LhaL ouLllnes Lhe LargeL code Lo be generaLed for LhaL
consLrucL.
lor example, every Lhree address sLaLemenL of Lhe form x := y + z, where x, y, and z are sLaLlcally
allocaLed, can be LranslaLed lnLo Lhe code sequence
MCv y, 80 /* load y lnLo reglsLer 80 */
Auu z, 80 /* add z Lo 80 */
MCv 80, x /* sLore 80 lnLo x */
unforLunaLely, Lhls klnd of sLaLemenL by - sLaLemenL code generaLlon ofLen produces poor code. lor
example, Lhe sequence of sLaLemenLs
a := b + c
d := a + e
would be LranslaLed lnLo
MCv b, 80
Auu c, 80
MCv 80, a
MCv a, 80
Auu e, 80
MCv 80, d
Pere Lhe fourLh sLaLemenL ls redundanL, and so ls Lhe Lhlrd lf a ls noL subsequenLly used.

1he quallLy of Lhe generaLed code ls deLermlned by lLs speed and slze.
A LargeL machlne wlLh a rlch lnsLrucLlon seL may provlde several ways of lmplemenLlng a glven
operaLlon. Slnce Lhe cosL dlfferences beLween dlfferenL lmplemenLaLlons may be slgnlflcanL, a nalve
LranslaLlon of Lhe lnLermedlaLe code may lead Lo correcL, buL unaccepLably lnefflclenL LargeL code. lor
example lf Lhe LargeL machlne has an lncremenL lnsLrucLlon (lnC), Lhen Lhe Lhree address sLaLemenL a
:= a+1 may be lmplemenLed more efflclenLly by Lhe slngle lnsLrucLlon lnC a, raLher Lhan by a more
obvlous sequence LhaL loads a lnLo a reglsLer, add one Lo Lhe reglsLer, and Lhen sLores Lhe resulL back
lnLo a.
MCv a, 80
Auu #1,80
MOV R0, a
lnsLrucLlon speeds are needed Lo deslgn good code sequence buL unforLunaLely, accuraLe
Llmlng lnformaLlon ls ofLen dlfflculL Lo obLaln. uecldlng whlch machlne code sequence ls besL for a glven
Lhree address consLrucL may also requlre knowledge abouL Lhe conLexL ln whlch LhaL consLrucL appears.
kLGIS1Lk ALLCCA1ICN
lnsLrucLlons lnvolvlng reglsLer operands are usually shorLer and fasLer Lhan Lhose lnvolvlng
operands ln memory. 1herefore, efflclenL uLlllzaLlon of reglsLer ls parLlcularly lmporLanL ln generaLlng
good code. 1he use of reglsLers ls ofLen subdlvlded lnLo Lwo subproblems:
1. uurlng reg|ster a||ocat|on, we selecL Lhe seL of varlables LhaL wlll reslde ln reglsLers aL a polnL ln
Lhe program.
2. uurlng a subsequenL reg|ster ass|gnment phase, we plck Lhe speclflc reglsLer LhaL a varlable wlll
reslde ln.
llndlng an opLlmal asslgnmenL of reglsLers Lo varlables ls dlfflculL, even wlLh slngle reglsLer
values. MaLhemaLlcally, Lhe problem ls n-compleLe. 1he problem ls furLher compllcaLed because Lhe
hardware and/or Lhe operaLlng sysLem of Lhe LargeL machlne may requlre LhaL cerLaln reglsLer usage
convenLlons be observed.
CerLaln machlnes requlre reg|ster pa|rs (an even and nexL odd numbered reglsLer) for some
operands and resulLs. lor example, ln Lhe l8M SysLem/370 machlnes lnLeger mulLlpllcaLlon and lnLeger
dlvlslon lnvolve reglsLer palrs. 1he mulLlpllcaLlon lnsLrucLlon ls of Lhe form
M x, y

where x, ls Lhe mulLlpllcand, ls Lhe even reglsLer of an even/odd reglsLer palr.
1he mulLlpllcand value ls Laken from Lhe odd reglsLer palr. 1he mulLlpller y ls a slngle reglsLer. 1he
producL occuples Lhe enLlre even/odd reglsLer palr.
1he dlvlslon lnsLrucLlon ls of Lhe form
u x, y
where Lhe 64-blL dlvldend occuples an even/odd reglsLer palr whose even reglsLer ls x, y represenLs Lhe
dlvlsor. AfLer dlvlslon, Lhe even reglsLer holds Lhe remalnder and Lhe odd reglsLer Lhe quoLlenL.
now conslder Lhe Lwo Lhree address code sequences (a) and (b) ln whlch Lhe only dlfference ls Lhe
operaLor ln Lhe second sLaLemenL. 1he shorLesL assembly sequence for (a) and (b) are glven ln(c).
8l sLands for reglsLer l. L, S1 and A sLand for load, sLore and add respecLlvely. 1he opLlmal cholce for Lhe
reglsLer lnLo whlch a ls Lo be loaded depends on whaL wlll ulLlmaLely happen Lo e.
L := a + b L := a + b
L := L * c L := L + c
L := L / d L := L / d
(a) (b)
Iig. 2 Two three address code sequences
L 81, a L 80, a
A 81, b A 80, b
M 80, c A 80, c
u 80, d S8uA 80, 32
S1 81, L u 80, d
S1 81, L
(a) (b)


f|g.3 CpLlmal machlne code sequence

?=&2?> &B >C+D<+!2&; &A6>A

The order in which computations are perIormed can aIIect the eIIiciency oI the
target code. Some computation orders require Iewer registers to hold intermediate results than
others. Picking a best order is another diIIicult, NP-complete problem. Initially, we shall avoid
the problem by generating code Ior the three -address statements in the order in which they have
been produced by the intermediate code generator.

AkCCnLS 1C CCDL GLNLkA1ICN
1he mosL lmporLanL crlLerlon for a code generaLor ls LhaL lL produce correcL code.
CorrecLness Lakes on speclal slgnlflcance because of Lhe number of speclal cases LhaL code generaLor
musL face. Clven Lhe premlum on correcLness, deslgnlng a code generaLor so lL can be easlly
lmplemenLed, LesLed, and malnLalned ls an lmporLanL deslgn goal.
3) What are bas|c b|ocks and f|owgraphs?
BASIC BLOCKS AND FLOW GRAPHS
A graph represenLaLlon of Lhree-address sLaLemenLs, called a f|ow graph, ls useful for
undersLandlng code-generaLlon algorlLhms, even lf Lhe graph ls noL expllclLly consLrucLed by a code-
generaLlon algorlLhm. nodes ln Lhe flow graph represenL compuLaLlons, and Lhe edges represenL Lhe
flow of conLrol. llow graph of a program can be used as a vehlcle Lo collecL lnformaLlon abouL Lhe
lnLermedlaLe program. Some reglsLer-asslgnmenL algorlLhms use flow graphs Lo flnd Lhe lnner loops
where a program ls expecLed Lo spend mosL of lLs Llme.

BASIC BLOCKS
A bas|c b|ock ls a sequence of consecuLlve sLaLemenLs ln whlch flow of conLrol enLers aL Lhe
beglnnlng and leaves aL Lhe end wlLhouL halL or posslblllLy of branchlng excepL aL Lhe end. 1he followlng
sequence of Lhree-address sLaLemenLs forms a baslc block:
L1 := a*a
L2 := a*b
L3 := 2*L2
L4 := L1+L3
LS := b*b
L6 := L4+LS
A Lhree-address sLaLemenL x := y+z ls sald Lo Jefloe x and Lo ose y or z. A name ln a baslc block ls sald Lo
llve aL a glven polnL lf lLs value ls used afLer LhaL polnL ln Lhe program, perhaps ln anoLher baslc block.
1he followlng algorlLhm can be used Lo parLlLlon a sequence of Lhree-address sLaLemenLs lnLo baslc
blocks.
AlgorlLhm 1: arLlLlon lnLo baslc blocks.
lnpuL: A sequence of Lhree-address sLaLemenLs.
CuLpuL: A llsL of baslc blocks wlLh each Lhree-address sLaLemenL ln exacLly one block.
MeLhod:
1. We flrsL deLermlne Lhe seL of |eaders, Lhe flrsL sLaLemenLs of baslc blocks.
1he rules we use are Lhe followlng:
l) 1he flrsL sLaLemenL ls a leader.
II) Any statement that is the target oI a conditional or unconditional goto is a leader.
III) Any statement that immediately Iollows a goto or conditional goto statement is a
leader.

2. For each leader, its basic block consists oI the leader and all statements up to but not
including the next leader or the end oI the program.
Lxample 3: Conslder Lhe fragmenL of source code shown ln flg. 7, lL compuLes Lhe doL producL of Lwo
vecLors a and b of lengLh 20. A llsL of Lhree-address sLaLemenLs performlng Lhls compuLaLlon on our
LargeL machlne ls shown ln flg. 8.
begln
prod := 0,
l := 1,
do begln
prod := prod + a[l] * b[l],
l := l+1,
end
whlle l<= 20
end
f|g 7: program Lo compuLe doL producL
Let us apply Algorithm 1 to the three-address code in Iig 8 to determine its basic
blocks. statement (1) is a leader by rule (I) and statement (3) is a leader by rule (II), since the
last statement can jump to it. By rule (III) the statement Iollowing (12) is a leader. ThereIore,
statements (1) and (2) Iorm a basic block. The remainder oI the program beginning with
statement (3) Iorms a second basic block.
(1) prod : 0
(2) i : 1
(3) t1 : 4*i
(4) t2 : a | t1 |
(5) t3 : 4*i
(6) t4 :b | t3 |

(7) t5 : t2*t4
(8) t6 : prod t5
(9) prod : t6
(10) t7 : i1
(11) i : t7
(12) iI i20 goto (3)
'4: EF Three-address code computing dot product
prod := 0
i := 1

4) What are the structure preserv|ng transformat|ons on bas|c b|ocks?

TRANSFORMATIONS ON BASIC BLOCKS
A basic block computes a set oI expressions. These expressions are the values oI the
names live on exit Irom block. Two basic blocks are said to be !"#$%&'!() iI they compute the
same set oI expressions.
A number oI transIormations can be applied to a basic block without changing the
set oI expressions computed by the block. Many oI these transIormations are useIul Ior
improving the quality oI code that will be ultimately generated Irom a basic block. There are two
important classes oI local transIormations that can be applied to basic blocks; these are the
structure-preserving transIormations and the algebraic transIormations.
STRUCTURE-PRESERVING TRANSFORMATIONS
The primary structure-preserving transIormations on basic blocks are:
1. common sub-expression elimination
2. dead-code elimination
3. renaming oI temporary variables
4. interchange oI two independent adjacent statements
We assume basic blocks have no arrays, pointers, or procedure calls.
1F ?50051 %9G*$H#)$%%451 $34041/.451
Consider the basic block
a: bc
b: a-d
c: bc
d: a-d
The second and Iourth statements compute the same expression,
namely bc-d, and hence this basic block may be transIormed into the equivalent
block
a: bc
b: a-d

c: bc
d: b
Although the 1
st
and 3
rd
statements in both cases appear to have the same expression
on the right, the second statement redeIines b. ThereIore, the value oI b in the 3
rd
statement is diIIerent Irom the value oI b in the 1
st
, and the 1
st
and 3
rd
statements do
not compute the same expression.
2. 6$/,*75,$ $34041/.451
Suppose x is dead, that is, never subsequently used, at the point where the statement
x: yz appears in a basic block. Then this statement may be saIely removed without
changing the value oI the basic block.
3F A$1/041: .$0#5)/)" I/)4/G3$%
Suppose we have a statement t: bc, where t is a temporary. II we change this
statement to u: bc, where u is a new temporary variable, and change all uses oI this
instance oI t to u, then the value oI the basic block is not changed. In Iact, we can
always transIorm a basic block into an equivalent block in which each statement that
deIines a temporary deIines a new temporary. We call such a basic block a (*+,&'-
.*+, block.
4. 21.$)7(/1:$ 5' %./.$0$1.%
Suppose we have a block with the two adjacent statements
t1: bc
t2: xy
Then we can interchange the two statements without aIIecting the value oI the block
iI and only iI neither x nor y is t1 and neither b nor c is t2. A normal-Iorm basic block
permits all statement interchanges that are possible.

S) What are the |nstruct|ons and address modes of the target mach|ne?
1he LargeL machlne characLerlsLlcs are
Byte-addressable, 4 bytes/word, registers
Two operand instructions of the form
op source, destination

Example opcodes: MOV, ADD, SUB, MULT
Several addressing modes
An instruction has an associated
Cost corresponds to length of instruction

Addressi
ng
Modes &
Extra
Costs














6) Generate target code for the source |anguage statement

(a-b) + (a-c) + (a-c);
The 3AC for this can be written as
t := a b
u := a c
v := t + u
d := v + u ffd live at the end
Show the code sequence generated by the simple code generation
algorithm
What is its cost? Can it be improved?

TotaI cost=12

7) What |s an act|vat|on record?
lnformaLlon needed by a slngle execuLlon of procedure ls managed uslng a
conLlguous block of sLorage called an acLlvaLlon record or frame. lL ls cusLomary
Lo push Lhe acLlvaLlon record of a procedure on Lhe run Llme sLack when Lhe
procedure ls called and Lo pop Lhe acLlvaLlon record off Lhe sLack when conLrol
reLurns Lo Lhe caller.

8) What are the contents of act|vat|on record?





CS13S2 r|nc|p|es of Comp||er Des|gn Un|t-V uest|on and answers
UNIT V CODE OPTIMIZATION AND RUN TIME ENVIRONMENTS 9
ntroduction Principal Sources of Optimization Optimization of basic Blocks ntroduction to Global
Data Flow Analysis Runtime Environments Source Language issues Storage Organization
Storage Allocation strategies Access to non-local names Parameter Passing.
What |s code opt|m|zat|on?
1he code produced by Lhe sLralghL forward complllng algorlLhms can ofLen be
made Lo run fasLer or Lake less space,or boLh. 1hls lmprovemenL ls achleved by
program LransformaLlons LhaL are LradlLlonally called opLlmlzaLlons. Machlne
lndependenL opLlmlzaLlons are program LransformaLlons LhaL lmprove Lhe
LargeL code wlLhouL Laklng lnLo conslderaLlon any properLles of Lhe LargeL
machlne. Machlne dependanL opLlmlzaLlons are based on reglsLer allocaLlon
and uLlllzaLlon of speclal machlne-lnsLrucLlon sequences.

1) L|st the cr|ter|a for code |mprovement transformat|ons.
Slmply sLaLed ,Lhe besL program LransformaLlons are Lhose LhaL yleld Lhe mosL
beneflL for Lhe leasL efforL.
llrsL,Lhe LransformaLlon musL preserve Lhe meanlng of programs. 1haL ls,Lhe
opLlmlzaLlon musL noL change Lhe ouLpuL produced by a program for a glven
lnpuL,or cause an error.
Second,a LransformaLlon musL,on Lhe average,speed up programs by a
measurable amounL.
1hlrd,Lhe LransformaLlon musL be worLh Lhe efforL.
Some LransformaLlons can only be applled afLer deLalled,ofLen Llme-consumlng
analysls of Lhe source program,so Lhere ls llLLle polnL ln applylng Lhem Lo
programs LhaL wlll be run only a few Llmes.
Code mprovement Criteria

A transformation must preserve meaning of a program (correctness
o A transformation must improve (e.g., speed-up) programs by a
measurable amount on average
o A transformation must worth the effort
2) Ind|cate the p|aces for potent|a| |mprovements can be made by the user
and the comp||er.
Programmer
Profiles, change algorithm, transform loops
Compiler: (on intermediate code)
mprove loops, procedure calls, various transformations
Compiler: (on target code)
Use of registers, instruction selection, peephole optimization

3) What are the phases of code |mprovement?
1he code lmprovemenL phase conslsLs of conLrol-flow and daLa-flow analysls
followed
by Lhe appllcaLlon of LransformaLlons


.




















4) D|scuss |n deta|| the pr|nc|pa| sources of opt|m|zat|on.
PrincipaI Sources of Optimization
Function-preserving transformations
o Common sub-expression elimination
o Copy propagation





a := x + y b := x + y
c := x + y
w := x + y
a := w
w :
b :=
c := w









Dead-code elimination
o Dead (or useless) code: statements that evaluate values that never
get used
o Dead-code may appear after transformations

o Constant folding
o Deducing at compile-time value of an expression is a constant and
using the constant instead
o Loop optimizations (especiaIIy inner Ioops)
o Programs tend to spend most of their time in inner loops
o We may improve running time by
even while
Code motion
Places before the loop
E.g.,
while ( i <= limit-2 ) f* limit is loop invariant *f

t = limit 2;

while ( i <= t ) f* limit, t are loop-invariant *f
Strength reduction
E.g., The replacement of multiplication by a subtraction or
addition might be possible when induction vars are modified
in a loop










S) D|scuss opt|m|zat|on of bas|c b|ocks.
Optimizing Basic BIocks
LocaI transformations that preserve $%&'(%'&)
Common sub-expression elimination
Dead-code elimination
LocaI transformations that are aIgebraic
Arithmetic identities; e.g., x+0 = x, x/1 = x
Strength reduction; e.g., x**2 = x*x, x/2=x*0.5
Constant folding

6) What |s dataf|ow ana|ys|s?
An optimizing compiler needs to
j := j - 1
t2 := 4*j
t3 := a[t2]
j := j - 1
t2 := t2 - 4
t3 := a[t2]

Collect info about the program as a whole
Distribute info to each block in the flow graph
E.g., knowing which vars are live on exit from each block
and using it for register allocation
This info is and a compiler collects
this by
nfo can be collected by setting up and solving systems of equations
They relate info at various parts in a program
A is of the form:
out[S| = [S| U ( [S| [S|)
Meaning: S

To set up and solve equations, may have to
proceed forward or backwards
i.e., backwards: define [S| in terms of [S|
Equations are normally set up at basic block level
Need to handle complexities involved in function calls and use of
pointers
Dataflow Analysis: Examples
Reaching Definitions (ud-chains)
Determines which of (assignments to) a variable
may reach each of it
Available Expressions
Determines which expressions are at each point in
a program (on every path from the entry to the point, there
is an evaluation of the expression and none of the vars in it
are assigned values between the last evaluation and the
point)
Liveness (Live-Variable) Analysis
Determines for a given var and a given point whether there

is a of the var along some path from the point to the
exit
Upwards Exposed Uses (du-chains)
Determines what of vars at certain points are reached
by particular (this is the dual of reaching
definitions)
Example
Reaching definitions and its :
- of x in BS is reached by the in B2, B3
- of x in B2 reaches the in B+, BS




Dataflow Analysis: Examples
Copy-Propagation Analysis
Determines that on every path from a copy assignment such as x:=
y, to a use of var x there is no assignments to y
Constant-Propagation Analysis
Determines that on every path from an assignment of a constant to a
variable, x := k, to a use of x the only assignment to x assign the
value k
7) What |s an act|vat|on tree? Lxp|a|n |ts funct|ons.
Every execution of a procedure is called an ACT!vAT!ON.
We can use a tree,called an activation tree to depict the way control
enters and leaves activations.
The LFETME of an activation of procedure P is the sequence of steps
between the first and last steps of P s body, including any procedures
z > 1
x := 1
z > y
x := 2
z := x - 3
y := x + 1
B2 B3
B4
B5
Y
N
Y
N

called while P is running.
Normally, when control flows from one activation to another, it must
(eventually) return to the same activation.
When activations are thusly nested, we can represent control flow with
ACT!vAT!ON TREES.
lo oo octlvotloo ttee ,
1. ocb ooJe tepteseots oo octlvotloo of o ptoceJote.
2. 1be toot tepteseots tbe octlvotloo of tbe molo ptoqtom.
J. 1be ooJe fot o ls tbe poteot of tbe ooJe b lf ooJ ooly lf coottol
flows ftom octlvotloo o to b.
1. 1be ooJe fot 'o' ls to tbe left of tbe ooJe fot 'b' lf ooJ ooly lf tbe llfe
tlme of 'o' occots befote tbe llfe tlme of 'b'.
/0 12$# $%" 4-(#%-5 6#$4768
1be flow of coottol lo o ptoqtom cottespooJs to o Jeptb fltst ttovetsol of tbe
octlvotloo tteetbot stotts ot tbe toot,vlslts oooJe befote lts cbllJteo,ooJ
tecotslvely vlslts cbllJteo ot eocb ooJe lo o left-to-tlqbt otJet.
we coo ose o stock,colleJ o coottol stock to keep ttock of llve ptoceJote
octlvotloos. 1be lJeo ls to posb tbe ooJe fot oo octlvotloo ooto tbe coottol
stock ooJ to pop tbe ooJe wbeo tbe octlvotloo eoJs.
wbeo ooJe 'o' ls ot tbe top of tbe coottol stock,tbe stock cootolos tbe ooJes
olooq tbe potb ftom 'o' to tbe toot.


9) wbot ote tbe stotoqe ollocotloo sttoteqles oseJ ot koo tlme?
Storage AIIocation
Static aIIocation
Storage allocation was fixed during the entire execution of a
program
Stack aIIocation
Space is pushed and popped on a run-time stack during
program execution such as procedure calls and returns.
Heap aIIocation
Allow space to be allocated and freed at any time.
10) Lxplaln wlLh a dlagram Lhe run Llme sLorage organlzaLlon.
kun 1|me Sorage Crgan|zat|on











Code
Static data
Stack
Heap
Code
SLaLlc uaLa
Peap
SLack
Virtual

address




11) WhaL ls sLaLlc allocaLlon?

Static Allocation
Bindings between names and storage are fixed .
The values of local names are retained across activations of a procedure.
Addressing is efficient
Limitations:
No recursive procedures
No dynamic data structures

12) WhaL ls sLack allocaLlon?
Stack Allocation
Recursive procedure require stack allocation .
Activation records (AR) are pushed and popped as activations
begin and end.
The offset of each local data relative to the beginning of AR is
stored in the symbol table.





floaL f(lnL k)
[
floaL c[10],b,
b = c[k]*J.11,
reLurn b,
}
offset = 8 Local c[10]
offset = 4 Parameter k
offset = 0 Return value













13) WhaL are calllng sequences?
rocedure calls are lmplemenLed by generaLlng whaL are known as
calllng sequences ln Lhe LargeL code. A call sequence allocaLes an
acLlvaLlon record and enLers lnformaLlon lnLo lLs flelds. A reLurn
sequence resLores Lhe sLaLe of Lhe machlne so Lhe calllng procedure
can conLlnue execuLlon.
Calling Sequences
A call sequence allocates an activation record and enters information into
its fields .
parameters, return address, old stack top, saving registers, local
data initialization

A return sequence restores the state of the machine
return value, restore registers, restore old stack top, branch to return
address
The code in calling sequence is often divided between the caller and the
callee.

14) WhaL are reLurn sequences?
Possible return sequence
Callee places a return value next to the AR of the caller.
Callee restores top-sp and other registers
Callee branches to the return address
Caller can copy return value into its own AR

1S) WhaL are non-local names?
n a language with nested procedures (or blocks) and static scope (lexical
scope), some names are neither local nor global, they are non-local names.
procedure A
real a;
procedure B
real b;
reference a; non-local
end B
end A;

Example: Non-local names in C

16) Lxp|a|n |n deta|| parameter pass|ng.
Parameter Passing
Parameters
Names that appear in the declaration of a procedure are formal
parameters.
Variables and expressions that are passed to a procedure are actual
parameters (or arguments)
Parameter passing modes
Call by value
Call by reference
Copy-restore
Call by name
Call-by-Value

The actual parameters are evaluated and their are passed to the
called procedure .

A procedure called by value can affect its caller either through nonlocal
names or through pointers.
Parameters in C are always passed by value. Array is unusual, what is
passed by value is a pointer.
Pascal uses pass by value by default, but parameters are passed by
reference.
CaII-by-Reference
Also known as call-by-address or call-by-location. The caller passes to the
called procedure the of the parameter.
f the parameter is an expression, then the expression is evaluated in a
new location, and the address of the new location is passed.
Parameters in Fortran are passed by reference
an old implementation bug in Fortran
func(a,b) { a = b};
call func(3,4); print(3);
Copy-Restore
A hybrid between call-by-value and call-by reference.
The actual parameters are evaluated and their r-values are passed as in
call-by-value. !n addition, l-values are determined before the call.
When control returns, the current r-values of the formal parameters are
copied back into the l-values of the actual parameters.
Call-by-Name
The actual parameters literally substituted for the formals. This is like a
macro-expansion or in-line expansion .
Call-by-name is not used in practice. However, the conceptually related
technique of in-line expansion is commonly used.
n-lining may be one of the most effective optimization transformations if
they are guided by execution profiles.




CS13S2-r|nc|p|es of Comp||er Des|gn uest|on 8ank
Ak1-8

Un|t-I

1) WlLh a neaL block dlagram, explaln Lhe varlous phases of a compller ln deLall. Assumlng an
expresslon glve Lhe ouLpuL of each phase.
2) Lxplaln lnpuL bufferlng ln deLall.
3) ConsLrucL a ulA for Lhe glven regular expresslon uslng subseL consLrucLlon meLhod. Also
mlnlmlze
Lhe number of sLaLes ( a / b ) * a bb
4) Lxplaln ln deLall abouL Lhe role of lexlcal analyzer wlLh Lhe posslble error recovery acLlons.
S) Lxplaln ln deLall Lhe couslns of Lhe compller.
6) WhaL are Lhe compller consLrucLlon Lools. Lxplaln.
7) ueflne regular expresslons? WhaL are Lhe rules LhaL deflne regular expresslons over alphabeL L
?
8) WrlLe shorL noLes on
a) Lokens, paLLerns and lexemes
b) regular deflnlLlons
c) LranslLlon dlagrams.
9) WhaL are Lex and Lex speclflcaLlon ? Pow lexlcal analyzer ls consLrucLed uslng lex? WrlLe a
Lex program LhaL recognlzer Lhe Lokens.
10) ConsLrucL nlA for (a/b)* and converL lnLo ulA.

Un|t-II

1) ConsLrucL a predlcLlve parslng.Lable for Lhe grammar
L -> L + 1 / l
1 -> 1 * l / l
l -> (L) / ld
2) Clve Lhe LAL8 parslng Lable for Lhe grammar.
S -> L = 8 / 8

L -> * 8 / ld
8 -> L
3) Conslder Lhe grammar
L -> 1L
L -> + 1L / L
1 -> l1
1 -> *l1 / L
l -> (L) / ld
ConsLrucL a predlcLlve parslng Lable for Lhe grammar shown above. verlfy wheLher Lhe lnpuL
sLrlng
ld + ld * ld ls accepLed by Lhe grammar or noL.
4) Conslder Lhe grammar.
L -> L + 1
L -> 1
1 -> 1 * l
1 -> l
l -> (L) / ld
ConsLrucL an L8 parslng Lable for Lhe above grammar. Clve Lhe moves of Lhe L8 parser on
ld * ld + ld
S) lor Lhe grammar glven below, calculaLe Lhe operaLor precedence relaLlon and
Lhe precedence funcLlons
L -> L + L | L L |
| L * L | L | L |
| L ^ L | (L) |
| -L | ld
6) Compare Lop down parslng and boLLom up parslng meLhods.

7) WhaL are L8 parsers? Lxplaln wlLh a dlagram Lhe L8 parslng algorlLhm.
8) WhaL are parser generaLors?
9) Lxplaln recurslve descenL parser wlLh approprlaLe examples.
10) Compare SL8, LAL8 and L8 parses.
Un|t-III
1) Pow would you generaLe lnLermedlaLe coder for Lhe flow of conLrol sLaLemenLs? Lxplaln wlLh
examples.
2) Lxplaln varlous daLa sLrucLures used for symbol Lable consLrucLlon. Compare Lhem.
3) Lxplaln abouL Lhe dlfferenL Lypes of Lhree address sLaLemenLs.
4) WhaL are Lhe meLhods of LranslaLlng 8oolean expresslons.
S) WhaL ls back baLchlng? Lxplaln wlLh an example.
6) Lxplaln procedure call wlLh an example.
7) Pow asslgnmenL sLaLemenLs are LranslaLed lnLo Lhree address code?
8) Lxplaln how declaraLlons are done ln a procedure uslng synLax dlrecLed LranslaLlons.
9) WhaL ls a Lhree address code? MenLlon lLs Lypes. Pow would you lmplemenL Lhese address
sLaLemenLs? Lxplaln wlLh sulLable examples.
10) WrlLe synLax dlrecLed LranslaLlon for arrays.

Un|t-IV
1) ulscuss ln deLall abouL Lhe run Llme sLorage arrangemenL.
2) WhaL are dlfferenL sLorage allocaLlon sLraLegles. Lxplaln.
3) WrlLe ln deLall abouL Lhe lssues ln Lhe deslgn of code generaLor.
4) ulscuss brlefly abouL uAC
S) Lxplaln ln deLall abouL prlmary sLrucLure processlng LransformaLlons of baslc

Un|t-V

1) Lxplaln ln deLall abouL prlnclpal sources of opLlmlzaLlon.
2) uescrlbe ln deLall Lhe opLlmlzaLlon of baslc blocks wlLh an example.
3) Lxplaln ln deLall varlous meLhods of passlng parameLers
4) Lxplaln peep hole opLlmlzaLlon wlLh sulLable examples.
S) WhaL are acLlvaLlon Lrees? ConsLrucL an acLlvaLlon Lree for qulck sorL. Lxample.
6) WhaL are Lhe quesLlons Lo be asked Lo undersLand how a compller musL organlze lLs sLorage
and blnd names.
7) WhaL ls an acLlvaLlon record? WhaL are lLs flelds represenL?
8) WrlLe abouL daLa flow analysls of sLrucLured program.
9) WhaL do you me an by machlne dependenL and machlne lndependenL opLlmlzaLlons?
10) Clve an algorlLhm Lo consLrucL uAC from glven baslc block.



?50#43$) 6$%4:1 * -$. J
1. What |s a comp||er?
A compller ls a program LhaL reads a program wrlLLen ln one language Lhe source language and
LranslaLes lL lnLo an equlvalenL program ln anoLher language-Lhe LargeL language. 1he compller reporLs
Lo lLs user Lhe presence of errors ln Lhe source program.
2. What are the two parts of a comp||at|on? Lxp|a|n br|ef|y.
Analysls and SynLhesls are Lhe Lwo parLs of compllaLlon.
1he analysls parL breaks up Lhe source program lnLo consLlLuenL pleces and creaLes an lnLermedlaLe
represenLaLlon of Lhe source program.
1he synLhesls parL consLrucLs Lhe deslred LargeL program from Lhe lnLermedlaLe represenLaLlon.
3. L|st the subparts or phases of ana|ys|s part.
Analysls conslsLs of Lhree phases:
Llnear Analysls.
Plerarchlcal Analysls.
SemanLlc Analysls.
4. Dep|ct d|agrammat|ca||y how a |anguage |s processed.
SkeleLal source program

reprocessor

Source program

Comp||er


1argeL assembly program

Assemb|er

8elocaLable machlne code

Loader] ||nk ed|tor llbrary, relocaLable ob[ecL les

AbsoluLe machlne code
S. What |s ||near ana|ys|s?
Llnear analysls ls one ln whlch Lhe sLream of characLers maklng up Lhe source program ls read from lefL
Lo rlghL and grouped lnLo Lokens LhaL are sequences of characLers havlng a collecLlve meanlng.
Also called lexlcal analysls or scannlng.
6. L|st the var|ous phases of a comp||er.
1he followlng are Lhe varlous phases of a compller:
Lexlcal Analyzer
SynLax Analyzer
SemanLlc Analyzer
lnLermedlaLe code generaLor
Code opLlmlzer
Code generaLor
7. What are the c|ass|f|cat|ons of a comp||er?
Compllers are classlfled as:
Slngle- pass

MulLl-pass
Load-and-go
uebugglng or opLlmlzlng
8. What |s a symbo| tab|e?
A symbol Lable ls a daLa sLrucLure conLalnlng a record for each ldenLlfler, wlLh flelds for Lhe aLLrlbuLes of
Lhe ldenLlfler. 1he daLa sLrucLure allows us Lo flnd Lhe record for each ldenLlfler qulckly and Lo sLore or
reLrleve daLa from LhaL record qulckly.
Whenever an ldenLlfler ls deLecLed by a lexlcal analyzer, lL ls enLered lnLo Lhe symbol Lable. 1he
aLLrlbuLes of an ldenLlfler cannoL be deLermlned by Lhe lexlcal analyzer.
9. Ment|on some of the cous|ns of a comp||er.
Couslns of Lhe compller are:
reprocessors
Assemblers
Loaders and Llnk-LdlLors
10. L|st the phases that const|tute the front end of a comp||er.
1he fronL end conslsLs of Lhose phases or parLs of phases LhaL depend prlmarlly on Lhe source language
and are largely lndependenL of Lhe LargeL machlne. 1hese lnclude
Lexlcal and SynLacLlc analysls
1he creaLlon of symbol Lable
SemanLlc analysls
CeneraLlon of lnLermedlaLe code
A cerLaln amounL of code opLlmlzaLlon can be done by Lhe fronL end as well. Also lncludes error handllng
LhaL goes along wlLh each of Lhese phases.
11. Ment|on the back-end phases of a comp||er.

1he back end of compller lncludes Lhose porLlons LhaL depend on Lhe LargeL machlne and generally
Lhose porLlons do noL depend on Lhe source language, [usL Lhe lnLermedlaLe language. 1hese lnclude
Code opLlmlzaLlon
Code generaLlon, along wlLh error handllng and symbol- Lable operaLlons.
12. Def|ne comp||er-comp||er.
SysLems Lo help wlLh Lhe compller-wrlLlng process are ofLen been referred Lo as compller-compllers,
compller-generaLors or LranslaLor-wrlLlng sysLems.
Largely Lhey are orlenLed around a parLlcular model of languages , and Lhey are sulLable for generaLlng
compllers of languages slmllar model.
13. L|st the var|ous comp||er construct|on too|s.
1he followlng ls a llsL of some compller consLrucLlon Lools:
arser generaLors
Scanner generaLors
SynLax-dlrecLed LranslaLlon englnes
AuLomaLlc code generaLors
uaLa-flow englnes
14. D|fferent|ate tokens, patterns, |exeme.
1okens- Sequence of characLers LhaL have a collecLlve meanlng.
aLLerns- 1here ls a seL of sLrlngs ln Lhe lnpuL for whlch Lhe same Loken ls produced as
ouLpuL. 1hls seL of sLrlngs ls descrlbed by a rule called a paLLern assoclaLed wlLh Lhe
Loken
Lexeme- A sequence of characLers ln Lhe source program LhaL ls maLched by Lhe
paLLern for a Loken.
1S. L|st the operat|ons on |anguages.

Un|on - L u M =[s | s ls ln L or s ls ln M}
Concatenat|on LM =[sL | s ls ln L and L ls ln M}
k|eene C|osure L* (zero or more concaLenaLlons of L)
os|t|ve C|osure L+ ( one or more concaLenaLlons of L)
16. Wr|te a regu|ar express|on for an |dent|f|er.
An ldenLlfler ls deflned as a leLLer followed by zero or more leLLers or dlglLs.
1he regular expresslon for an ldenLlfler ls glven as
|etter (|etter | d|g|t)*
17. Ment|on the var|ous notat|ona| shorthands for represent|ng regu|ar express|ons.
Cne or more lnsLances (+)
Zero or one lnsLance (?)
CharacLer classes ([abc] where a,b,c are alphabeL symbols denoLes Lhe regular
expresslons a | b | c.)
non regular seLs
18. What |s the funct|on of a h|erarch|ca| ana|ys|s?
Plerarchlcal analysls ls one ln whlch Lhe Lokens are grouped hlerarchlcally lnLo nesLed collecLlons wlLh
collecLlve meanlng.
Also Lermed as arslng.
19. What does a semant|c ana|ys|s do?
SemanLlc analysls ls one ln whlch cerLaln checks are performed Lo ensure LhaL componenLs of a program
flL LogeLher meanlngfully.
Malnly performs Lype checklng.
20. L|st the var|ous error recovery strateg|es for a |ex|ca| ana|ys|s.

osslble error recovery acLlons are:
anlc mode recovery
ueleLlng an exLraneous characLer
lnserLlng a mlsslng characLer
8eplaclng an lncorrecL characLer by a correcL characLer
1ransposlng Lwo ad[acenL characLers
?50#43$) 6$%4:1 * -$. K
1. Def|ne parser.
Plerarchlcal analysls ls one ln whlch Lhe Lokens are grouped hlerarchlcally lnLo nesLed collecLlons wlLh
collecLlve meanlng.
Also Lermed as arslng.
2. Ment|on the bas|c |ssues |n pars|ng.
1here are Lwo lmporLanL lssues ln parslng.
SpeclflcaLlon of synLax
8epresenLaLlon of lnpuL afLer parslng.
3. Why |ex|ca| and syntax ana|yzers are separated out?
8easons for separaLlng Lhe analysls phase lnLo lexlcal and synLax analyzers:
Slmpler deslgn.
Compller efflclency ls lmproved.
Compller porLablllLy ls enhanced.
4. Def|ne a context free grammar.
A conLexL free grammar C ls a collecLlon of Lhe followlng
v ls a seL of non Lermlnals
1 ls a seL of Lermlnals

S ls a sLarL symbol
ls a seL of producLlon rules
C can be represenLed as C = (v,1,S,)
roducLlon rules are glven ln Lhe followlng form
non Lermlnal (v u 1)*
S. 8r|ef|y exp|a|n the concept of der|vat|on.
uerlvaLlon from S means generaLlon of sLrlng w from S. lor consLrucLlng derlvaLlon Lwo Lhlngs are
lmporLanL.
l) Cholce of non Lermlnal from several oLhers.
ll) Cholce of rule from producLlon rules for correspondlng non Lermlnal.
lnsLead of chooslng Lhe arblLrary non Lermlnal one can choose
l) elLher lefLmosL derlvaLlon lefLmosL non Lermlnal ln a senLlnel form
ll) or rlghLmosL derlvaLlon rlghLmosL non Lermlnal ln a senLlnel form
6. Def|ne amb|guous grammar.
A grammar C ls sald Lo be amblguous lf lL generaLes more Lhan one parse Lree for some
senLence of language L(C).
l.e. boLh lefLmosL and rlghLmosL derlvaLlons are same for Lhe glven senLence.
7. What |s a operator precedence parser?
A grammar ls sald Lo be operaLor precedence lf lL possess Lhe followlng properLles:
1. no producLlon on Lhe rlghL slde ls .
2. 1here should noL be any producLlon rule possesslng Lwo ad[acenL non Lermlnals aL Lhe rlghL
hand slde.
8. L|st the propert|es of Lk parser.

1. L8 parsers can be consLrucLed Lo recognlze mosL of Lhe programmlng
languages for whlch Lhe conLexL free grammar can be wrlLLen.
2. 1he class of grammar LhaL can be parsed by L8 parser ls a superseL of
class of grammars LhaL can be parsed uslng predlcLlve parsers.
3. L8 parsers work uslng non backLracklng shlfL reduce Lechnlque yeL lL ls
efflclenL one.
9. Ment|on the types of Lk parser.
SL8 parser- slmple L8 parser
LAL8 parser- lookahead L8 parser
Canonlcal L8 parser
10. What are the prob|ems w|th top down pars|ng?
1he followlng are Lhe problems assoclaLed wlLh Lop down parslng:
8ackLracklng
LefL recurslon
LefL facLorlng
AmblgulLy
11. Wr|te the a|gor|thm for IIkS1 and ICLLCW.
IIkS1
1. lf x ls Lermlnal, Lhen ll8S1(x) lS [x}.
2. lf x ls a producLlon, Lhen add Lo ll8S1(x).
3. lf x ls non Lermlnal and x ?1,?2..?k ls a produc on, Lhen place a ln ll8S1(x) lf for some l , a
ls ln ll8S1(?l) , and ls ln all of ll8S1(?1), ll8S1(?l-1),
ICLLCW
1. lace $ ln lCLLCW(S),where S ls Lhe sLarL symbol and $ ls Lhe lnpuL rlghL endmarker.

2. lf Lhere ls a producLlon A 8 , Lhen everyLhlng ln ll8S1( ) excepL for ls placed ln
lCLLCW(8).
3. lf Lhere ls a producLlon A 8, or a produc on A 8 where ll8S1( ) conLalns , Lhen
everyLhlng ln lCLLCW(A) ls ln lCLLCW(8).
12. L|st the advantages and d|sadvantages of operator precedence pars|ng.
Advantages
1hls Lypeof parslng ls slmple Lo lmplemenL.
D|sadvantages
1. 1he operaLor llke mlnus has Lwo dlfferenL precedence(unary and blnary).Pence lL ls hard Lo handle
Lokens llke mlnus slgn.
2. 1hls klnd of parslng ls appllcable Lo only small class of grammars.
13. What |s dang||ng e|se prob|em?
AmblgulLy can be ellmlnaLed by means of dangllng-else grammar whlch ls show below:
sLmL lf expr Lhen sLmL
| lf expr Lhen sLmL else sLmL
| oLher
14. Wr|te short notes on ACC.
?ACC ls an auLomaLlc Lool for generaLlng Lhe parser program.
?ACC sLands for ?eL AnoLher Compller Compller whlch ls baslcally Lhe uLlllLy avallable from unlx.
8aslcally ?ACC ls LAL8 parser generaLor.
lL can reporL confllcL or amblgulLles ln Lhe form of error messages.
1S. What |s meant by hand|e prun|ng?
A rlghLmosL derlvaLlon ln reverse can be obLalned by handle prunlng.

lf w ls a senLence of Lhe grammar aL hand, Lhen w = n, where n ls Lhe nLh rlghL-senLenLlal form
of some as yeL unknown rlghLmosL derlvaLlon
S = 0 => 1 => n-1 => n = w
16. Def|ne Lk(0) |tems.
An L8(0) lLem of a grammar C ls a producLlon of C wlLh a doL aL some poslLlon of Lhe rlghL slde.
1hus, producLlon A x?Z ylelds Lhe four lLems
A .x?Z
A x.?Z
A x?.Z
A x?Z.
17. What |s meant by v|ab|e pref|xes?
1he seL of preflxes of rlghL senLenLlal forms LhaL can appear on Lhe sLack of a shlfL-reduce parser are
called vlable preflxes. An equlvalenL deflnlLlon of a vlable preflx ls LhaL lL ls a preflx of a rlghL
senLenLlal form LhaL does noL conLlnue pasL Lhe rlghL end of Lhe rlghLmosL handle of LhaL senLenLlal
form.
18. Def|ne hand|e.
A handle of a sLrlng ls a subsLrlng LhaL maLches Lhe rlghL slde of a producLlon, and whose reducLlon
Lo Lhe nonLermlnal on Lhe lefL slde of Lhe producLlon represenLs one sLep along Lhe reverse of a
rlghLmosL derlvaLlon.
A handle of a rlghL senLen al form ls a produc on A and a posl on of where Lhe sLrlng
may be found and replaced by A Lo produce Lhe prevlous rlghL-senLenLlal form ln a rlghLmosL
derlva on of . 1haL ls , lf S => Aw => w,Lhen A ln Lhe posl on followlng ls a handle of w.
19. What are kerne| & non-kerne| |tems?
kerne| |tems, whlsh lnclude Lhe lnlLlal lLem, S' .S, and all lLems whose doLs are noL aL Lhe lefL
end.
Non-kerne| |tems, whlch have Lhelr doLs aL Lhe lefL end.
20. What |s phrase |eve| error recovery?

hrase level error recovery ls lmplemenLed by fllllng ln Lhe blank enLrles ln Lhe predlcLlve parslng
Lable wlLh polnLers Lo error rouLlnes. 1hese rouLlnes may change, lnserL, or deleLe symbols on
Lhe lnpuL and lssue approprlaLe error messages. 1hey may also pop from Lhe sLack.

?50#43$) 6$%4:1 * -$. L
1. What are the benef|ts of |ntermed|ate code generat|on?
A Compller for dlfferenL machlnes can be creaLed by aLLachlng dlfferenL back end Lo Lhe
exlsLlng fronL ends of each machlne.
A Compller for dlfferenL source languages can be creaLed by provlng dlfferenL fronL ends
for correspondlng source languages L exlsLlng back end.
A machlne lndependenL code opLlmlzer can be applled Lo lnLermedlaLe code ln order Lo
opLlmlze Lhe code generaLlon.
2. What are the var|ous types of |ntermed|ate code representat|on?
1here are malnly Lhree Lypes of lnLermedlaLe code represenLaLlons.
SynLax Lree
oslx
1hree address code
3. Def|ne backpatch|ng.
8ackpaLchlng ls Lhe acLlvlLy of fllllng up unspeclfled lnformaLlon of labels uslng approprlaLe
semanLlc acLlons ln durlng Lhe code generaLlon process.ln Lhe semanLlc acLlons Lhe funcLlons
used are mkllsL(l),merge_llsL(p1,p2) and backpaLch(p,l)
4. Ment|on the funct|ons that are used |n backpatch|ng.
mkllsL(l) creaLes Lhe new llsL. 1he lndex l ls passed as an argumenL Lo Lhls funcLlon
where l ls an lndex Lo Lhe array of quadruple.
merge_llsL(p1,p2) Lhls funcLlon concaLenaLes Lwo llsLs polnLed by p1 and p2. lL reLurns
Lhe polnLer Lo Lhe concaLenaLed llsL.
backpaLch(p,l) lnserLs l as LargeL label for Lhe sLaLemenL polnLed by polnLer p.
S. What |s the |ntermed|ate code representat|on for the express|on a or b and not c?

1he lnLermedlaLe code represenLaLlon for Lhe expresslon a or b and noL c ls Lhe Lhree address
sequence
L1 := noL c
L2 := b and L1
L3 := a or L2
6. What are the var|ous methods of |mp|ement|ng three address statements?
1he Lhree address sLaLemenLs can be lmplemenLed uslng Lhe followlng meLhods.
Cuadruple : a sLrucLure wlLh aLmosL four flelds such as operaLor(C),arg1,arg2,resulL.
1rlples : Lhe use of Lemporary varlables ls avolded by referrlng Lhe polnLers ln Lhe
symbol Lable.
lndlrecL Lrlples : Lhe llsLlng of Lrlples has been done and llsLlng polnLers are used
lnsLead of uslng sLaLemenLs.
7. G|ve the syntax-d|rected def|n|t|on for |f-e|se statement.
1. S lf L Lhen S1
L.Lrue := new_label()
L.false :=S.nexL
S1.nexL :=S.nexL
S.code :=L.code | | gen_code(L.Lrue : ) | | S1.code
2. S lf L Lhen S1 else S2
L.Lrue := new_label()
L.false := new_label()
S1.nexL :=S.nexL
S2.nexL :=S.nexL

S.code :=L.code | | gen_code(L.Lrue : ) | | S1.code| | gen_code( go Lo ,S.nexL) |
|gen_code(L.false : ) | | S2.code
?50#43$) 6$%4:1 * -$. M
1. Ment|on the propert|es that a code generator shou|d possess.
1he code generaLor should produce Lhe correcL and hlgh quallLy code. ln oLher words,
Lhe code generaLed should be such LhaL lL should make effecLlve use of Lhe resources
of Lhe LargeL machlne.
Code generaLor should run efflclenLly.
2. L|st the term|no|og|es used |n bas|c b|ocks.
Def|ne and use Lhe Lhree address sLaLemenL a:=b+c ls sald Lo deflne a and Lo use b
and c.
L|ve and dead Lhe name ln Lhe baslc block ls sald Lo be llve aL a glven polnL lf lLs
value ls used afLer LhaL polnL ln Lhe program. And Lhe name ln Lhe baslc block ls
sald Lo be dead aL a glven polnL lf lLs value ls never used afLer LhaL polnL ln Lhe
program.
3. What |s a f|ow graph?
A flow graph ls a dlrecLed graph ln whlch Lhe flow conLrol lnformaLlon ls added Lo Lhe baslc
blocks.
1he nodes Lo Lhe flow graph are represenLed by baslc blocks
1he block whose leader ls Lhe flrsL sLaLemenL ls called lnlLlal block.
1here ls a dlrecLed edge from block 81 Lo block 82 lf 82 lmmedlaLely follows 81 ln Lhe
glven sequence. We can say LhaL 81 ls a predecessor of 82.
4. What |s a DAG? Ment|on |ts app||cat|ons.
ulrecLed acycllc graph(uAC) ls a useful daLa sLrucLure for lmplemenLlng LransformaLlons on baslc
blocks.
uAC ls used ln
ueLermlnlng Lhe common sub-expresslons.

ueLermlnlng whlch names are used lnslde Lhe block and compuLed ouLslde Lhe
block.
ueLermlnlng whlch sLaLemenLs of Lhe block could have Lhelr compuLed value
ouLslde Lhe block.
Slmpllfylng Lhe llsL of quadruples by ellmlnaLlng Lhe common su-expresslons and noL
performlng Lhe asslgnmenL of Lhe form x := y unless and unLll lL ls a musL.
S. Def|ne peepho|e opt|m|zat|on.
eephole opLlmlzaLlon ls a slmple and effecLlve Lechnlque for locally lmprovlng LargeL code. 1hls
Lechnlque ls applled Lo lmprove Lhe performance of Lhe LargeL program by examlnlng Lhe shorL
sequence of LargeL lnsLrucLlons and replaclng Lhese lnsLrucLlons by shorLer or fasLer sequence.
6. L|st the character|st|cs of peepho|e opt|m|zat|on.
8edundanL lnsLrucLlon ellmlnaLlon
llow of conLrol opLlmlzaLlon
Algebralc slmpllflcaLlon
use of machlne ldloms
7. now do you ca|cu|ate the cost of an |nstruct|on?
1he cosL of an lnsLrucLlon can be compuLed as one plus cosL assoclaLed wlLh Lhe source and
desLlnaLlon addresslng modes glven by added cosL.
MCv 80,81 1
MCv 81,M 2
Su8 S(80),*10(81) 3
8. What |s a bas|c b|ock?
A baslc block ls a sequence of consecuLlve sLaLemenLs ln whlch flow of conLrol enLers aL Lhe
beglnnlng and leaves aL Lhe end wlLhouL halL or posslblllLy of branchlng.
Lg. L1:=a*S

L2:=L1+7
L3:=L2-S
L4:=L1+L3
LS:=L2+b
9. now wou|d you represent the fo||ow|ng equat|on us|ng DAG?
a:=b*-c+b*-c

?50#43$) 6$%4:1 * -$. N
1. Ment|on the |ssues to be cons|dered wh||e app|y|ng the techn|ques for code opt|m|zat|on.
1he semanLlc equlvalence of Lhe source program musL noL be changed.
1he lmprovemenL over Lhe program efflclency musL be achleved wlLhouL changlng Lhe
algorlLhm of Lhe program.
2. What are the bas|c goa|s of code movement?
1o reduce Lhe slze of Lhe code l.e. Lo obLaln Lhe space complexlLy.
1o reduce Lhe frequency of execuLlon of code l.e. Lo obLaln Lhe Llme complexlLy.
3. What do you mean by mach|ne dependent and mach|ne |ndependent opt|m|zat|on?
1he machlne dependenL opLlmlzaLlon ls based on Lhe characLerlsLlcs of Lhe LargeL
machlne for Lhe lnsLrucLlon seL used and addresslng modes used for Lhe lnsLrucLlons
Lo produce Lhe efflclenL LargeL code.
1he machlne lndependenL opLlmlzaLlon ls based on Lhe characLerlsLlcs of Lhe
programmlng languages for approprlaLe programmlng sLrucLure and usage of
efflclenL arlLhmeLlc properLles ln order Lo reduce Lhe execuLlon Llme.
4. What are the d|fferent data f|ow propert|es?
Avallable expresslons
8eachlng deflnlLlons
Llve varlables

8usy varlables
S. L|st the d|fferent storage a||ocat|on strateg|es.
1he sLraLegles are:
SLaLlc allocaLlon
SLack allocaLlon
Peap allocaLlon
6. What are the contents of act|vat|on record?
1he acLlvaLlon record ls a block of memory used for managlng Lhe lnformaLlon needed by a
slngle execuLlon of a procedure. varlous flelds f acLlvaLlon record are:
1emporary varlables
Local varlables
Saved machlne reglsLers
ConLrol llnk
Access llnk
AcLual parameLers
8eLurn values
7. What |s dynam|c scop|ng?
ln dynamlc scoplng a use of non-local varlable refers Lo Lhe non-local daLa declared ln mosL recenLly
called and sLlll acLlve procedure. 1herefore each Llme new flndlngs are seL up for local names called
procedure. ln dynamlc scoplng symbol Lables can be requlred aL run Llme.
8. Def|ne symbo| tab|e.
Symbol Lable ls a daLa sLrucLure used by Lhe compller Lo keep Lrack of semanLlcs of Lhe varlables. lL
sLores lnformaLlon abouL scope and blndlng lnformaLlon abouL names.
9. What |s code mot|on?

Code moLlon ls an opLlmlzaLlon Lechnlque ln whlch amounL of code ln a loop ls decreased. 1hls
LransformaLlon ls appllcable Lo Lhe expresslon LhaL ylelds Lhe same resulL lndependenL of Lhe
number of Llmes Lhe loop ls execuLed. Such an expresslon ls placed before Lhe loop.
10. What are the propert|es of opt|m|z|ng comp||er?
1he source code should be such LhaL lL should produce mlnlmum amounL of LargeL code.
1here should noL be any unreachable code.
uead code should be compleLely removed from source language.
1he opLlmlzlng compllers should apply followlng code lmprovlng LransformaLlons on source
language.
l) common subexpresslon ellmlnaLlon
ll) dead code ellmlnaLlon
lll) code movemenL
lv) sLrengLh reducLlon
11. What are the var|ous ways to pass a parameter |n a funct|on?
Call by value
Call by reference
Copy-resLore
Call by name
12. Suggest a su|tab|e approach for comput|ng hash funct|on.
uslng hash funcLlon we should obLaln exacL locaLlons of name ln symbol Lable.
1he hash funcLlon should resulL ln unlform dlsLrlbuLlon of names ln symbol Lable.
1he hash funcLlon should be such LhaL Lhere wlll be mlnlmum number of colllslons. Colllslon ls such a
slLuaLlon where hash funcLlon resulLs ln same locaLlon for sLorlng Lhe names.

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