0% found this document useful (0 votes)
24 views25 pages

Acd Asaign2

Uploaded by

mhys2827
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)
24 views25 pages

Acd Asaign2

Uploaded by

mhys2827
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
You are on page 1/ 25

Asignnost-&

la)Describe theproceduve of eliminating left Recuvssion.


A-eft RecuYssion:
A Context frec qrammaY is called left recuYSs0on if a
AOn-texminal has a tefl most symbol appars alternative ly
lat the time of dexivalion either immadiatels Ccalled divect
eft recuSsion) or through somcother morn-terminad definiion
Caalled indrect lefE yecevssion)
Direct left recUYSSion
A’Aa
ex- F’E+Elid
`ndixect left recuYGG*ON.

B> A?|S

F ’E
Elimination of left xecurssion:
qrammar production is in the form A-A|B
lhen the
ew non- termina A' the follooing
y inbroducinga by means of Moore's proposal
produchions re included
A->BA'

yecunssion elimination,he indirect (eft


or indirect left
TeCUYSsion is Conueyted to diyect lefA xecur $Sion_ohd
eliminated bu mneans of Mcore's
eft xecuYSsion ts
proposal
Algorithm usedt
A1 --..An
Arrange all NT in ovdey
forifyom 1ton) do l
for Ci from 1to i-1) do l
Teplace cach production
Ai-’ Aj

Alheve,Aj’xd<k
eliminate left recuYssion omon Ai production

1b) Eliminate leftrecuYssion for the follouing qrammar.


E’ EtI|I

F’(e)lid
A-eft Recuyssion!
A CFG is called lefi
xecuYSsion if a NI has a
left most symbol appears
alternative ls at the tme cf
deiyation eithex iynmediatelý Ccallecd dixectleft
thoough some clher NI definition (indiyect le p4 xecuYSsion)e
YeLurssiÝn)
Dixect left ecunSsion

Sndixect left YcCUNSsion


A>BB
B- AS
ex; E-> F+Eid

Elimination of left recuyssion:


When the ranmax paccucion in tHhc fovm AsAa|ß, hy
inhoducing
included by mans
a new NI A' the follouoing pxnducions ae
of Mooye's prqposal.
A’BA'
A'>&A'e
left
FoY indirect left Yecuxssion elimínation, the indixect
xecnssion is cOnerted to dizect left xeCUr£sihm and eft
Moones pogoasal
ecuISsion is eliminated hy maans of Mcoxes
Algorithm used
in ovde AAn
AYarge all T
i
foy (i fxom 1to n)do
fom to i-) do i
foy
3eplace each poatucBion
AiAj
eliminate left ecrSsion among Ai paduction.
Problenm!
i) E>E+II
A

Aftey elimination of leet xecurssion the paocuuction hecomes.


E-’TE!

A B

After elimination Of left KecuYSsion the pxoduction becomes.


TFT'
T’*ET'I8
ii) F’ (E)id
There is no lef xecurssion in the qiven pxoduc hion.
Aftex xemoval of left ecuYssion the qramnnar becomes
E’TE
E’+TE
T’FT
T'’ *ETle
F’CE)lid
aa.Jllusbate the rules to be followed in finding the
FIRST and FoLLON.
A: FIRST C):
EIRSIC&) is a sct of ternminal Symbols that are
EIRST Symbols appaing at RHS in derivation of
10n- teyminal
Follauing are Yules used to compute the FlRSTO:
1. there is
a Yule X’a then EIRST(X) =a
2. Jf thene is
a rule X> then FIRSTX) -&
3 o theXule A’ XX2 ---XE
Xk then
FIRSTCA)- {EIRST Cx)UFIRSTCA2) FIRST (XE)}
Lhere 1<k X; <n_such that 1sjs k-1
FOLLOJALC)
FOLuOa(A) is defined as the set of teminal symbols
that appar immediatels to the right of A
Rules foY Computing FollGA)L
1. For the stayt symbol place $ in FollQnCs)
9 .Jf theye is a produchian A->x BA thcn eery thing
in EIRSTCB) is to ke placed in FollON(B)
3. Jf theye is a production A->& BB Cor) A-Xß wheye
EIRST CB)= je then FolON CA) = FOLlo (B) (av) FollO(8)
= FOllONCA) means eeIything in FoOkN(A) is in
FOlLOlN CB).
ah) Find EIRST and FolO foy the following raMmay ?
E’E+TII
T’TFF
definition, aulesin ala)
F’(E)lid.
A:EIRSTC!
FLRSTCF
) - iCid?
FIRST CX
1: The qiven qramnar is left xecuxSSi ve.
Eliminate left yecunssion_ Bom qyammar.
E’E+TI
aftex eliminaton of left xecuYSS iOO
E’TE
E’ +TE l8

after elimination of left yeceuYSSiGn


T’FT!
T ’ *FT'l8
i) F> (E) lid
Ihere is no left
YecurSSibn.
Afteret re CuYSSiorn the qrammar
ESTE! becomeS
E'’ +TE'2
T’FT'
T'*FTle
F(E)lid
Q. FIRST C):
FIRST (F) iGid
FIRST CT) i*e3
FIRST Ct) = FIRGT(F) iidi
FIRST CE) > itigi
FIRST E) FRST (1)
- iC id?
3: FOlOW !
FOllOAI CE) = follON(E)= D i f
FOLON (T)
FOILO CE) = +)*, ),$?

3)Considex the qrammar


E’E+TI
T’ TFF
F»E)|id
Design predichve parsing table and check luhether given
qYamma is LLC) GYammar or not ?
A: LLC) Gyamma
a + b I\p tote

top pases

A
Siack
Fredictive pansex that is YeCUISS0ve descent parser nedinn
no back tyacking,an be constyucted for a class of qiamm
Called
1. The fyst ' ' in LL6) stands for scannin ilp Brôm
Left to ight The second Stands for heft most
deivation
for using one ip sumbol of Look abead at
each step to make parsing acion decisions
No left xecsive CoY) ambiqus qxammar an be LLC)
ÇYamnmmr ç'of LLG) if and onls if tuhenevex
A’JA ane this & disinct oduchions of qhe
follauing Conditions hold
1. FoY no terminal `a' do both
&EBdexive sbigs
beginning with A
aAA most one of aEB can dexie Hhe empa
a 8 B’E, then_ ß does not shing
derive any shing baginning
toi th termina in FolLOlwCA), like
uuise if < then Bdees
not dexive ans shing beginning aitha
texminal in FOMO[a)
Construcion of a paecictive parsing table:
Input - (yammar G
Output- pcase table 'M!
MeodFor each pxoluctionA of tk ammaY do
the follouing
FoY each
teminal a' in
|2
MCALa FIRSTC)add A’x to
J t ' is in FRSTC«), then for
in FoIOIN CA), add a each tesminal b'
Sf E is in FIRST Ax to M[A,b].
Ca) and in FOHOW CA), add
A>x to MA,$J aS well.
3f afte peofox M0nq the
at all in above thexe is no pacduchon
MA,a] then set M[ia] to
|depyesented by an empty enty in the talble)eyxoYCwhich noxmally
Procedune to construct LC) panse table:
Step1 Ihe qamar should not e left
LAmaiqus. xecurSsiCn (ox)
Stepa: alculate FIRST and FoLQal foY
Stepa consbuct a paxse table with non-Ne CuTSi ve qiamma
where noo NTOrexe tupleS. No-of
teYminals non-TmmAas
texmiAals_anc attibute
Stepu: EIL Hhe table using fhe procedure for COnshuchion of
pxedichive paxse table.
Problem!
Step 1' Eliminate eft secuassi&n fom Qammax
)E’E+1T
aftex eimination of left YeCuaSSin

E’+TEle
atex eliminaion of left
let ecuY SSi oY

T'ET'

These is na leftYeCUYSSi on.


Aftex elimimating left xecuxSsian the qrammaT econes

E’ +Els
TET

FCe)lid
Step-a Calculate FIRST and FOM0
FiRSTCE)=CGd?
FIRST(T) ={*,s
FIRST (T)- FIRSTCF) -= iGid
FIRST (E)= {+st
EIRST CE] = FIRSTCT) = iGid?.
FonOk CE) = Follo (E) -{J$i
FOIJOC) EOllOW ):

ParseTable
id $
ETE' T

TTFTTFT'
T'
F >(¬)Fid

Fov every LL) qramnmoT, each pnsing tabl. enby Lmiquely


tdenkfies the paoductHon (or) siqnals un
The qiven qrammaz is LL(0)
Lo) of type as it has only
Cne enbs in the cell.

4) Congider the qammar ETE


E'’+TE-TElE
T ’ FT'
T'’*ErFT'e
F’ G
G’CE) id
Calculate FIRST and FOllO for the alove qramar:
A:delnihon, boLedwn fom ala)
i) FIRST O!
FIRST CG)- iGid?
EIRSTG):/AE
FIRST CEF)= FIRST(G) -> Gid
ELRST C)-i*,be
FIRST CT)= FIRST (E) =) í C,idf
EIRST CE): it-s
FIRST (E) = FIRSTC) ) {C idi

FOllON Ce) is,)


FolouCt) FonCICEUE) =+)- $,)

FoluCA CT'): FoHOALCTuT)t)


FOlloJalCG)= FIRST CG')

5) Prepane Shift Reduce Rusing for the input string


usinq the
qramma SC1)la
Lsls
(a) (a, Cas)
(b) Ca,a)
A:Shift Reduce Parsing:
->344 isaform of bottom up parseT
nthis a stack holds qramm sumernls md ingt alen
halds the xest of tx
the sthing to be parsed
ie; stack nput Paxser halts and
SACCE Sstul porsing
4 possible acticng_a shifi xeduce pavser
paiser Can may
Shift: shift the next input sumbol on toto the top

R Keduce he night nd of the sting to be redute


nust b at fc top of Hc stack locat the le?t end
cf thc shim tuith in the stack and deeided ita naich
non-tenninal to xeplace the shing
Accepte Announte sucCessfd eompletion c pansing
ouine
Solutiory a) (a, (aa))
STACK INPUT ACTION
Caslaa) Shift C
alaa))$ shifAa
Reduce S>a
Casa))$ Reduce
ls
shiÇt.
shifC
aa)) shift a
Reduce S>a
Reduce LS
a)
sa)# shift
a))$ shift a
Reduce S->a
ReduceL’LS
shift )
Reduce S>(2)
Reduce l>s
shift)
Reduce S ’ )
Acept
(b) Caua)
STAC K INPUL ACTLON
Cana) shift C
$C ShiÆt a
$la Reduce S a
Reduce L>S
$CL Shit
a)s shifta
ReduceS’a
RedeuceLs
$CL. shift)
Reduces 2 )
$S Accept
a Define auqmented qramar.
A: Augmented GramanaY CG):
LAdding an Augment producion that is inhroducivg.
La new non- texminal to dexive a Statt umbof
added to the exising qyammar is called Auamantad
LqrammaY.
lexample Ç: SAa.
A’a

A’Aa
A>a
6b) Conchruct the LRlo) ikens fox the folloucing Gyammar
S>R

L’id
R>
A: PYoeduye to Construct LRO
Step 4: fonm the Auqmented quammar by dding
auqmented production.
Sepa: Inseit C)dot symbol to the Auqune ntal paadac on
Step 3: Ccnsbruct item6 by taveasing the do operator
in the poduction . Whadot is available Gefoxe NI,
appy clasure oparalion: cf maing the do
means
bypoduction.
Stepl Perfom goto opation
the
opeatox to next position to foy the Consbuchien
Step5: exform thc dataflow diacpam
items.
the tablestate, Action and columms and
Step.6: onstuct based on
divided
GoTo fuYther acion andGoTO aYe
tanninals_ non-temiale xespecively
nunbeig to the paoduction as RiRe to Rn.
Step: Give
Fox all the paduchon giuen in the qammar- Rstands fo
xeduce
Step8! Aciane amd GoD ae filled with state ame and ilp where
when teminals are tavezsed Shift opaxatos ae peafoymed when
NT taeed they aYe Rlled oibh item humbey The xeduce function
is fled foY Hhe item belouw,T where the poduchon s competels -
TYavetsed
Dataflos Dioqom:
Soution:

l *R
Cata flow Diaqpam:
s’s.
L
To
RL. Sl-R.
RL
S ’l-R L*R
S’R. |Lid

R
R L-*R"
R
L’id
T6 id id
Lid

State ACTION GOTO


* id S L R
Su G

Accept

Su S5
5
6 Ss S5 8

Y
Construct pasig table fox the qiven grammar
CLR pmcing

CaCld
A: Proceduve !
amay
Step-11 Iaiting Augmernted collection
of LRO) of canonical itene
Step-2: Finding
Step-3 Defining the tuo fumctions G0T0 ACTION in Hhe
CLR panse talole, He yeduchion is placed inM Look Ahaod
Symbal field'
Procedue to find Canonical Stems :
The camonical item is smilar to item tuheYe loak ahead sum
J

added topoduchicn,
Case 1

C>d
then B Did wtth FIRSTC) as lookahed.
Cace S:AdBcia
if B’D

then B e oith texminal followed as look ahead


Case 3 A->ad -Bya
if after B, & lo) ancthing isthexe
Hhen look ahed othe pxevious pdt is added
ie BDa
Case le: for poduct A> alb.
add
A bi
ie, lock ahead of pevioUS pdt is added
Case 5: for stavt Symbol $ is look ahead sybol.
Soution:
Aep1:
S> CC
C’ aC

Steps: Eind Cancnical itens

S'’S$ Is

I
s'->-S,4 S->C-C
C->ac..
S’"CC,$ C->"aCf
C>-al,ald C>"do
I3
C’ *dald
aC,alda IB

aC,ald C->aC. yald


C->-d,ald

c-dald
CLR- Ponce Table
ACTION GOTO
States
d C
I
2
S Su
accept
2 Se S4 5
S3 Sy

8) Cesiqn the LALR pmsx fox the follauing Grammar


S>AA
A’aA

A: Paocedure:
l: zite augmented qrammar
9. To_ find canonical items for the qammAY
3: Defining 8 functions GoTOE_ACTION In cLR Parser
SAme
h. From_ CLR Conshruct LALR puser lby Combin
ttems with diffenent lokaheod
Solation: s'>s
S>AA
AaA
A’b
sS$
S>A-A. ASAA:,$
97 A aA$
A-bi |A’.aA,s A->aA$
S-’ AA,
foAaA,atb |A’b$
AaA,albl A>aAalb Ab$
A’-bi alb |A-b,alb aA
AaAa]b|
Abatb
Su
CLR-Pase Table
ACTIDN GOTO
STATES
A
I
2
S3 Sy
hccept
S
S6
Sy

5
6 S4
In the above CLR table the items of I3 and Is
ne similax Loith diffesent lcok ahead- Simillaxly
Ty and T4 , Tg and T9 .
as:
|Based on this the cLR table is modtfied
LALR Parse Table!

STATES AcTON GOTO


S A
2
Sac
Accept
S36 Sut 5
36 S36 Sut

Analyse diffevent types of gntermediate Code nith an ezample.


A: Compilex qenerate an intexmediate cocde qerexation an easy
to zepeent form of source language 2ohich is caled
in texmediate languagitond backend
Static Code
psex Type
Checkex
TCQ Genextor
Adantags of TCGi
’A ompilex for diffeent machine cm be created by
|attaching diffesent backend to the existing fxonterds of
each machine
>A Compiler for diffexent Souie language Con the camemachine)
an becxeaBed by providing differentfsantends fin conesçon
ding sowe language to existing backend
>A maching indepandent code opimizex can be appied to
TC in oxdex to ophimize the code qenexation.
Type of TCG:
Thexe ahypes
Abstact syntay tree
2) polish notation
(3) Thiee address cade
o Abstyact Syntax trge:
A natuxal hierardhical structuxe is represented by syntax
Directed Acycic qxaph lor) DAG is very much similax
tree.
but they are in More conpactorM
|to suntax tree
Syotax bree: DAG'
assign assiqd

Uminvs 6 uminuS uminus


2) Polish Notation: Also called as " Prefix Notation
Revense polish notation Posix"
St is used 4sng postfix(or) posiz
ip, = Ca+b) + C-d)
prefix Nctation = *t ab-cd
posix Notation = abtcd-*
G) Three - address code: Sn thxee -addrecs cade form the
most thmee addesc ane used toxepresent any statement
An addess can be amame, a contant and a compiler
qenaated temporay genexal vaaiable.
General fon: a: bc
eachinstzuchion Shouldcontainat most and at most one
opexator n RuS.
xample Q=b+ctd
ta

ti:= b+c
ta: t+d
ai= t
Implanentacion of thmee addve ss code :
’Thee addaess Code is an abstact fom of TC that
lcan be implernanted as an 3ecoyd with thc
addegs fecls
It has 3 epesentations:
)Quadruples:
z addzess code the insuchion doesn't speciy
Hhe zepsesentation of them in the data structure Jn
Compiles these insbucion can be implemented as object
(ox) as xeCox ds uith feels fox the opesator and opezands
Aquadzuples has u fkds, which 0e call opezand, arqument
1, nqument &TeSult
) Tiples
Sn this xepresertation it has onls 2fáds fre lds
opexatox arqument1, axqument a
) Sndinect triples
This consists of listing of poíntess to tipks, 3ather
Hhan aa listing of by themselves.
10)Produce quaduple,triples and indizect tipls for follouwing
exprecsion: aty)* Cy*z)+ (xy+z)
A:Three- addyesG code:
In Three -addyess_code foYM the most thyee -addess ane
used to Yepresent any statement
An addyoss can be a nam, a Constant and a compilez
genexated tempoxavy general vaviable.
Geneyal foYm a:bc
each instruction should contain at most and at most one
opesatoy in RHS

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy