Introduction to Semantic Analysis
Introduction to Semantic Analysis
Cemanttc fnalyöis
Int
oduction to
Semontic fnalysIS
In
Semantic Analusis the compilei naJysis the
meaning OF the pionam The Senmonic -ncuysis is don.
n order
t o btain the precise meaning OF the
Pmo9amming9 ConstUcts.
Suntax Dine cted Translation
(sbT)
SDT adds Semant ic Toies to the C FG.
Semantic Rules
. In stDT along oith tne Cnommar we Can
SDD
SpeciFies the values OFottTtbutes by associaing
Semontic TOles ith the Grammo P1oductions. Simplya
CFCG Ithottibutes & Semontic TOles 1S Ccu led as SDD.
one
In SDP every
non- ten minal
1 the p o ductio O qets moie
tnon one attribUtes
E T
T T*F.
T F
F id
Ru le
SemantiC
[Link] + T Val
[Link]
[Link] T. Val
id lex val
FVal
[Link]=23
[Link] = 21
[Link] =
EVa 7
[Link] 3*
[Link]-2
[Link] Val 7
F. Val : 3
[Link] 3
[Link]
Types OF Att1butes
Synthesized -nttributess
A t t r b U t e s
. Inheited
a vaJue Frorm
: IF a node ta kes
Dttrbutes
Synthes lzed
Synthe si2ed atEibute
childen then 1t IS called
E9
A.S =B.S
A S C.S
AS D S
Heve, A1S B,C,D ave tne children and
the poent
IS Synthesized AEEtbute assoCiated
nothing but
Inher1ted DEtT1bute
E9 A BCD
C. AA. panent
c. sl 8.
[Link]. J Sblngs
Tpes OF SDD
I S attrtbuted SDD
. L attribUted 's DD
. S attributed SDD
atErbutes
SDD that Uses Only SqnthesIzed
E9 A BCD
.S .s
A S c.s
S D S
i n h e tted
and attibutes then tt 1S called
A.S :B.s
A S =C S
S -D S
C.i Ai
Order OF
EVolution
depende n
should
conside
we
FOT evaluating SDD S
9Taph
Dependency eraph
that
directed qnaph
1SS a
Dependency Graphb attributes
among the
epre Sents Flow oF i n F o m m ao
t n
he
t o d e t e r m i r n e
in a parse Eree.It1SUsed
atEributes
in a paTSe trC
e valuaton order O
showSthe values
annoeated porse,ETee
hile an d e t e t m i n e s
9raph
a dependency
each node But
at
CompUted.
hese vaJues are
houw
[Link]
E-val t [Link] f
E- E+ T
E9
T ¬.vad T Val }
[Link]
T T*E
Tval T. val *
T
T. Val
F. Vol
F id
2FVal id lex val f
+3* 7 O [Link] 25 G
T . Val':a
ENal =4 +
R
Val
[Link] + [Link]
i d . l e xV a l T
T val 3
FVol : 4
T d lexval 3
O [Link] 4
Intnoduction to Intermediate Co de Geneiation;
I DnolysIs - Synthesis
mode OF a
compile, the
FYont -end analySIS the souTCe pronOm CTeates
intermediate TepresentatIon F c o d e , and FTOm which
the
backend qenerates target Code
I n t e x m e d t a t e
c od e
PaTser Semantic Code
ICG Code
nodyeer
Optimi2atrorn Generaior
In IC TepTesentaton , we are
UStn9 3-hddTess
PropeTtCS of
DAGSs :
UsUally Constructed
S'ng
by USing
3-nddieS
Code
exprcSIons.
RU l e s FoT ConstavcEing DAa's
's:
alwcy6 operatovs,
nodess are
In a DAG ntev!OT
alwaysnom
nodes (LeaF, n o des) ane
extexiOT
Same value
ave
the
Form X Y
assiqnmenE 1nstrUCtioS OF
3 The
a r e n e c e s s a T (
ConstrOchon oF DAGS
K K-+ 5
a+bta
K K+5
DAG
SyntCa* Tree
5 d
a+b+Ca
DAG
Syntax Thee
t a-+b
a+b+ a+b DAG
Sunta* Tnee
O b
O b
DAG
Ca+b)*(a+b+C)
Syntax TTee
b a
5
ata*(b-c)+(b-c)* d 6. b*-C+b * -c
C
d
(a*b+C),-la* b+ c) BClata)+ (ota)) +(ta 40) + a40)
a b
c*d)+(C*d)
a+b+ a*b
O. d= (a« b)+ (a* b)) -(
C b
3 Address Code
ave atmost
Code Form, theve
In 3-hoddTesS
stateme ntThe
Used to nepresentany
3- hddTessesS aTe
Code is a b op c.
oF the 3 - d d ess
general FOTm
can be names,
[Link]
here, a,b.c ae
comple 9enerated TemperaIesS.
constonts
E9: a b+Ctd
State ment
The for the hbove
3 ddiess Code
tib+C
hese ti ta ame the TermpoO7IeS
La t1t d
9enevohe d by thee Compiler.
conStxUct DAG FOT atb + atb by Usin9
3 ddnese Code
a kb
ta a+b
13
3 t+t1
TO a+b
T To+C
d To+ T
=b+C
b,d
b a-d
C btC
d a-d
that node.
JS ts LeFt chld
oa
5
O
3
lementation OF
3-nddTesS Code
Quadnupies
Tiples
IndmecE tiples
Ovadruples: FoUr
necOYC StTUctoe c
UadrUple is a
E
Fields. They ane OPangl, aga,ne sult
ne
OP Fie ld is Used t o e p r e s e n t Ope^a t0
nt OperCnds.
angi
0sed to nepmese
QF an
e xpression.
X = (a+b) * Cc +d)
E- a +b angaesult
OP ng
a
3t
*
t3
ih thiec
Tiples: tpe 15 YCCO
sTUCtOe
Fields 11ey ae
meth0d Ue
op avq, ang
to Ovoid cnteing Into the
Tempoa1
nomes
qoboi
Table. Heve, Tempo1OTY 15 1eplaced by posit 10n tt
Uaiue
statements that Comptes iE
construct Tples Fo thhe gven ExpTesSIon
X (a+b) CC+d)
ti a + b
Op ang ang
C +d
a b
3 ii+t
d
t3
O
3
IndiTect Tples
e feve nces
Here , we ane USing Pointens instead OF
Op ag Cng
Ct d OD C b
O d
X 43
100 fo
io3 3 O
I -(b+c)
b -c + b -c
at (b+c)
uaduple ;
t b+C
OP ang ong0nesUt
-
a
t3
30 a
BosIc 8ock
conse CUtIVe
a b.* Ctd
Entry
Cxit
BasIC Bl0Cks .
PaTtitioning Intermediate Code into
nto basic
Any gven code can be paTtiEioned
colle d aS Leade
the CO de
Folloong state ments OF
statement IS a leade
elocKS
Rule-i: DeteMiNiNg Basic
Initial
bloCk
aS
PROD = o
9.
addT (A) -
4
3 T
T4 addr (8) -4
TI 4+X
6. T3 TTTIJ
TS T4 CTJ
T3XTSS
8 T6
PROD PROD + T6
I+1
F I 20 GOTO (s)
we have
Statement OF the
PROD O tS a leade2 SInce Fnst
code aleader.
condittonas
OF the
TI 4 X1 S a leader Snce target
PROD:O
3 Taaddr (A)
T4 a d d r (B)-4
.
TI 4x1
S
T3 Ta TT
TS T4 TTIJ,
1 3 x TS
T6
PROb + TG
PROD
I I+I
I L8 0 a O T o l s )
IF
FloO GTaphs
FOuD erOph IS a dIrected q o p wIth F low
basic blocks.
controol inFoTmation added to the
Ehe, CO de.
given nbove
the basic blockS.
FTstly, we cormpute
COntrol inFOTmation
Secondly, e asslgn the
F loLD
. PROD =O
3. T addr CA)-4
Q Tu QddY (B)-4t
S. TI C+XI
T3:TaTTJ
TS Ts CTIJ
8. T6 T3 TSs
PROD
PROD + T
T I +)
CO TO tE2)
IF I 2D
constUct osic blocKs and FID gaph roT the qvcn
Find the
.i 7. att4
8. j j+
. J
3.t 5 *?
9. iF JL:5 9oto (3 )
I F I45 qotota)
t 5*i
63
B
i+
?F i 5 goto (9)
TOt a no oF EdgeS S
&. 90toc)
S
gotolt
When a POqTam 1S
xecotion, 1t 1S satd t o
in d
InFovmaton FOT calting9 prOce done IS aVada ble
n e t o r n
value Cadting9 PTocedure
PaTaMeter : proceduiees
P c t v a U
TE 1S used by callng
c o l led P T O c e d u r e s
non-locas cato
nccesS LinK: Tt 1S3 Used to e Fer
inFoTmo tlon
S Ov e d Moch1ne
stotuS TIt holds the
pTOce dure 1S
about
stotus OF machine beFoC the
called
loca to the
LOCa bata It ho lds the data that IS
a T i S e S
Volues that
TempoTaTes It StoeS the
expresstonD.
evaluaton OF an
the
nctivatton Tree ;
E n e e s t n v C t U n e
that
IS a
Acttvat ion
tree dUIN9
pTOgnam
a
made by
calls
repre sents Func tion new oCtivatton
Functton 16
called a
execuElon. w h e n the
FroM the
PoPPe d
stack and
EO the
necowd S PUshed
Fonctlon retuins.
int n
neod annayl);
qUIcksOt C,n);
ma in
read aTOYC )
ae the
cedures and voIabl
3.
The Code
envionmenE
T e q u i r e men&S.
manages
the memOTY
OF the pi097O
text poTtton
code Tt 1S ehe
onetme, and ItS
doesnE change at
thot time
Compile
KnoOn at
T e q U r eM e n é s ane
memoTY
ane
any pTogram
though the ne xt
called andomly
M a t n E a i n
the
actnattonsS.
call and
pTOcedUre
SUSed
m e c h a n i s n
Heap allocaton
vOrIables THe
the
neallocaion as
allocaion or
to maintatn the
Knouon aE Tontme ony
VarLales are
Stack qs0wS
C o dA
e rea heP
Globa Stattc memoTy
towads higher
Area
9TO c u S
* Tne Heap
Stock
Lower memoTy.
touwandS
Heap
StaticStoraqe allocaton:
allbcaton, nameS ae bound
t I Static
storage locattons.
completton.
CompHe timC.
PoceduTe.
dvotages
T t 1S easu to implement
[Link] allocDS tu pe Checkin9 dumin9. Comprlatton.
DISAdUontaqes
. Fixed si2e
. It (S not
posSible to USe VaTIables whOse sl2e has
actiuaorn
end
they oe
sO thot
. n c t t U a i o n
necod contans the locals
e cor
to Tesh Storoqe in each activotton
bound
Vation
CCH
when the
me value OF locals detected
ends.
Ethis allocaton
ndvantoges
ecUSIoN
I t SUpports
da ta tem
strOCtUTe For the
Yt ceates a data
dynamically.
DIS Advantages;
PotnEeyg
Can be done UStng
d d r e ssing
ndex ReqisterS.
e
llocatton
Heap StoTa99e
the most
lexible alloCatto
allocaHon
.tHeoP
Scheme.
be done
can
OF mermoy
2. lloca tion de alloca ion
place depcnding UPoN
ond at ann
ot ony time
Mequyement.
Ene UseTs
Callocote memovy to
1S
used to
3. Heop allocaror Vaytab les
when the
dunamically &
VOTIables
tne
then claimsS t bac
more
Use d
ane no
prDCeSs
dvont aqe longe
lange
block
OF storcge Can be
blocks at qUn t ime
Panttt roned into smalle
Faqment a7 on
E9
FOCt Cint n)
7FCne =1)
else
Fact (6)
TetunO1
n-2
nEin-s)-
D:3
n F (n-1)
niECn-)
n etn-1)nS
nkE n-1) :G n
Tctumo 180