0% found this document useful (0 votes)
61 views23 pages

Introduction to Semantic Analysis

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views23 pages

Introduction to Semantic Analysis

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

UNIT IN

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

aSSoCiate So me inFoma notat o ns and these notation


aYe Cad led as
Semont iC Rules.
Suntax Drected de FiniHOn (S DD) :

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

In Semantic nruie, tihe Qtéribute Can be representea


Os Val Iexe, va moy be CVaIGble ov may be
a
Stning data tqpe ov
onyth1 9
Cons der
Gramman E E+T

E T

T T*F.

T F

F id
Ru le
SemantiC
[Link] + T Val
[Link]

[Link] T. Val

[Link] T. VaJ* [Link]


TVal F. val

id lex val
FVal

Evaluate the expresson +3

[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

with vadues Prom 1ES Children.


panentE & taking the

Inher1ted DEtT1bute

n o d e takes value From ItS pOvents1blinas


then E 1S Atttbute
called InheTtted

E9 A BCD

C. AA. panent

c. sl 8.
[Link]. J Sblngs
Tpes OF SDD

Thene ave TUOO TYpes

I S attrtbuted SDD

. L attribUted 's DD

. S attributed SDD

atErbutes
SDD that Uses Only SqnthesIzed

CoUted S- attYIouted SDD

E9 A BCD

.S .s

A S c.s

S D S

L-attTibuted SDD: A SDD that VSes both Sunthesizeo

i n h e tted
and attibutes then tt 1S called

SDD. But inher ted attTIbutesS we


L attvibuted

should Conslcder on/y leFE SIbling


E9 A 8CD

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

Eront End BocK End

In IC TepTesentaton , we are
UStn9 3-hddTess

Code TalseS om high leve to LoLo leyel

Variants OF Syntax T eer


These aTe TuoO Types
Construct 1on OF is
DG's For Ex pTessIonS

Value umoer Method FOT ConstTUcting DAG's

DAG efeS dnected ncyclic Groph ,1t med ns


with for
1 a 9raph Cles. Tt 1s vevy useFul
tmplementin9 taansFoxmations on basic blocKS

PropeTtCS of
DAGSs :
UsUally Constructed
S'ng

by USing
3-nddieS

Code

. DPG' S ave Used to de ter hmine Common SUb

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

constonts, identE teTS.


1 S C Y e a t e d

DAG, a neuonode the


. while ConstrUCE ing a.
with
node
any
does not exist
only
ohen they

Same value
ave

the
Form X Y
assiqnmenE 1nstrUCtioS OF
3 The
a r e n e c e s s a T (

not perFomed Oole sS they

ConstrOchon oF DAGS

ConstTOCE DAG FOx ehe FollouIng

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

constOct DAG FOr the Follooing 3-nddTess Code

TO a+b

T To+C
d To+ T

ConstTUCE DAG FoY the Fo tlooing 3 Ad dTesS code.

=b+C
b,d
b a-d

C btC

d a-d

Value NumbeT methOd FoT constnUCting Dh4S

In va) ue NUmbeT Method the node OF a DAG

Store d TecoTds. The inteqe


index OFEhe necomd 1S Known as Valve Number oF

that node.

The SiqnatOve >


oF node 1S a et cop ,u,7
uwhere Op IS the and Labe.
Operator

JS ts LeFt chld

S ttS TIght chlc


E9 a +b+a

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

*Tesuit FIeid 1S Usecd to e p r e seng e sult

e xpression.

qiven ExpresS on;:


QUadTUple FoY the
constUct

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

OY 1Ocattons. It 1S Simila toTple.

Constrvct IndIneCt FOT x (a+b) * CC+d)


TIple

Op ag Cng
Ct d OD C b

O d
X 43
100 fo
io3 3 O

Implement Folouong st atements nto Inteymed ate co

GvadTuples, Tples & Indinect T p eS.

I -(b+c)

(c+b) (c+d) la+bt c) -

b -c + b -c
at (b+c)
uaduple ;
t b+C

OP ang ong0nesUt
-

a
t3

TTple Indirect TTIpleS


oP a91 ar2 Op ag1 ang
b b C

30 a

BosIc 8ock

conse CUtIVe

The Basic 8lock 1S a sequence


at the
enters
netwoYk which the FlOO OF ConE 6

beqnn ing and LeaveS at the end. i1

a b.* Ctd

61= b*C -t1)


t t d -a)
C ta (3

Above 3-AddTess Code


me block FOT

Entry

Cxit
BasIC Bl0Cks .
PaTtitioning Intermediate Code into

nto basic
Any gven code can be paTtiEioned

blocks USIing the Following Tules

Rule- Deter mimn9 L e a d e YS

colle d aS Leade
the CO de
Folloong state ments OF

First Statement OF Ehe Code


the conditronal
OF
Statenment that 1S a toget
statement.
onconditiona goto
immedia tely aFter 9ot
Statement that appeons

statement IS a leade

elocKS
Rule-i: DeteMiNiNg Basic

leader tll the


Follou the
the Statements that
bloCR.
basiC
next leade OPpearS FoTM One

the Code IS Called . aS


Te Frst Statement OF

the Fnst leadeT.

FInst leader S colled


3. THe block contaning the

Initial
bloCk
aS

basic blocks FOT tne 9Iven thmee


the
Compute
addresS Staterments.

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

stotement 1Sa leade


goto

Code POstitoned into


NoO the 9iven Can be

tu0o basic blocks as.

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

PlOuD qTaph FOT Ehe Set g


9oph IS simply a dvected
basic bloCk6 a the FlOo OF
Show6
F (OuD gmaph
dep1CE hoto tha
in Formatton. Flod q1aph 1sS
Osed to

PO9am con 1S among the blocks


POTsed
being
Flbð naph Us ed to llostatethe FloO 0OF

Control betuween basic bloCks

e bQsIC blocRS sey ve aS nodeS OF Floto qna ph.

There S 9dnected edge Fom block e too

bloCk B F Ba aPPeaS Immediately aFtex B) i

Ehe, CO de.

DTOW address Statement


Flo qraph Fov Ehe EnTee

given nbove
the basic blockS.
FTstly, we cormpute
COntrol inFOTmation
Secondly, e asslgn the
F loLD

The Teqied Flod qnoph S

. 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

addreSS code. [Link]


nodes e edges.
t h o e e

Find the

.i 7. att4

8. j j+
. J
3.t 5 *?
9. iF JL:5 9oto (3 )

. t tI+J 1O. i+)

I F I45 qotota)

t 5*i

63

iF L=5 goto L3)

B
i+
?F i 5 goto (9)

TDta no OF Nodes lbCkS =¢|

TOt a no oF EdgeS S

nddre sS COdJe FoT he Followin9


wte Thmee

epmessIoN. IF P<B and C D then t:) ekse t : o


Thee nddness codeFOT the 9Ivenexpres

TF (ALB) 90to (3)

&. 90toc)

3. IF CcLD) goto C6)

S
gotolt

Ron Tinme EnVITOnment

When a POqTam 1S
xecotion, 1t 1S satd t o

in TUNti Me A State OF the


toTgeE machine
that

includes sOFt waTe branies , environment NaTIables

whic PToVl de Senvices to the nUpning PTOcesS eS IS

Knouon aS Run Tie Envinonment

pockag9e thot S 9eneTated OIth the

execotable PTOgam and which p0vIde a pOcesS

Com UNIcot (on the pToceSS ond t h e 1un tim


amon9
environmennt 1S Knoon aS RoN T1m¢ SUPPOTt System

when the PTOgrm 1S e xecuted , the memovy allocab

ond de allocation IS t O ken Cave

hct Votion Recod:


ncttvation recod 1S a data stTUcture

thot S actoted | cieated whcn a PTOcedUre OY

Fonct1on IS involed The pocegS oF


executing a

prO cedume S Called OcttvOtton The ne cessaTy

in d
InFovmaton FOT calting9 prOce done IS aVada ble

OCtivotion necond3 nctivat/on necord Conta t

the Fo llo uoIn9 Onts.


Retor Value
I S Used by colling Proce dure

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

sUppl p a r o mete r s tO the

non-locas cato
nccesS LinK: Tt 1S3 Used to e Fer

held Othe actiVa Elon re cod.

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

executon OF the PTOcedure.

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.

Stack wher the


In a
Flows
control
assome. thaE the PTO9Tam
we called
pTOcedUre
1S
manne nd uohen
S equential
cal led POCeduTe.
to the
Its contnol IS t a n S F e r Ted
TetuNS
execUted ,t
PTOCedUTe
c a l ledN
ohen a
oF
Eupe
the Control
ck to the caller Th
t easler t o n e p r e s e nE Q IeS
Control Floco make S
n the Fo1m o F a t T c e ,knoon
oF activations

the actIvot lon ETee


mainc

int n

neod annayl);

qUIcksOt C,n);

quicksot Ciot m, int n)


int pontitton (m,n);

quicksOrE (m, i-t)


quIckso16 (i+,nl
The tctivation tee Fo th1S PTO9TO m ollbe

ma in

read aTOYC )

Panttti6nCt,n) slt,u)) s (6,4)

pontit ron t,n) Gs (ta) 69S (4)

Stonoge OTganisot ionStora9e allocation stnote9ies i

TnE executtng tonget pTOgnam qUns in its

own logicas addiesS Space n which each p109

Val ue hos a locahon.


Tne
manoge ment
on9an isation OF thislogic

SpOce 1S blt the compiler, Operog


a d d e S S shased
Operating Sys tem
m a c h i ne The
SusteD tOrget
es
addresS into physicd addne ss
maps
the logical
memoTY

which ane USUolly Spnecd thToUghout

ae the
cedures and voIabl
3.
The Code
envionmenE

entittes FOT whic the UntIme

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

PrOcedures : ine [Link]


1S Staic,
P O r e l 0 n

though the ne xt
called andomly
M a t n E a i n
the

thus 1E TequTes stack storage

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

OF TUn-Eime mem bTY:


sub atvISIOn
TypIcal

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.

memOTY CYeated atE


Comple Eime then e
totic ne onl
me moTY will be cTeated onCe
3 statrc allocation 6Upports. the dynamIC da ta

StTUCEtUne that meanS memoTY 1S CTeated only!

comple time and de allo Cated aFter pTOgam

completton.

The dTa ubaCK UOith s Eatt¢


a lloca&ton 1S, that the
storage
SIZe Should be Knoon at
and positton oF dota 6bjects

CompHe timC.

S noothe dTOLaback i s e s t 1 i c t o n .oF the ecUis'oD

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

to detemined Ot TUn time

The Static allo c a t on con be complet ed Ftne 12

of the dota Objects S callcd comple tiro e.

Stack Stovoqe llocaton

storage alloca10n, stoTo9e IS O190nlsed


tIn Stack
as a stoch.

act iVaton TCCOd IS pUshed nto the stack


when activaon beqins and t 1S Popped
when the

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

wOTK S On the baS S OF LIFO


TE
pocess.
6UPpOTts the TecUTSIOn

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

stonoge allocation SuppoTts the ecorSton


He ap

prDCeSs
dvont aqe longe
lange
block
OF storcge Can be
blocks at qUn t ime
Panttt roned into smalle

DIS hdvanta9e tceates thee prOblem OF

Faqment a7 on
E9
FOCt Cint n)

7FCne =1)

else

etuT n * Fact Cn-1))2

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

You might also like