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

Backpatching

The document discusses the implementation of backpatching for boolean expressions using a two-pass approach. It outlines the process of constructing a syntax tree, walking it in depth-first order, and generating labels for jump statements that are filled in later. Additionally, it describes functions for managing label lists and provides a translation scheme for boolean expressions and control statements.

Uploaded by

akhilakrosuri
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)
7 views

Backpatching

The document discusses the implementation of backpatching for boolean expressions using a two-pass approach. It outlines the process of constructing a syntax tree, walking it in depth-first order, and generating labels for jump statements that are filled in later. Additionally, it describes functions for managing label lists and provides a translation scheme for boolean expressions and control statements.

Uploaded by

akhilakrosuri
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/ 4

Backpatching

’The easiest jay to implement the Syntan-dected


deftnttons for boolean epreSstons is to usc two
passes
’FiSt, onshuct a Synto tee tor the input,and
then walk the tee tn depth -rst order,
the ttanslattons.
(omputtng
-The main problerm Loth genesang Code for boolean
expreSstons and ftoo- of -Contol Statementa in a
Single pass u that dng one, single pass. may
not: knoco the labels that contol mut go to at the
ttme the jump statement
are qenerated.
Hence, a seres of bianchtng Statementt twth the
tangeti f the Tumps left unspecitied is generated.
Lach statement o be pt on a lut of gto statement
Lohose labels wl be felled tn ohen the
the proper label
Can be determined.
Je call ths Subse quent flltng tn of label
backpatehng
To manpulate li sts of label, we use three funtons:
|. makel:st (i) cr eates a neco tat
Contanng only i, "an
tndex into the aray ot quadruplea;
makeltat rets a pontey to the hst t hay made
2- merge (P, Pa) Concaten ates the usts potnted to by
Pi
and P, ard tetns ns a pointer to the concatenated
Q
(tst.
3. backpatch (P) tnset i as the taget label -for each
of the stalermenta on the ltst pointed to by P.
Boolean Ezpresstons :
We roa conshutt a tanglatton
() E’ E, oF M E Scherne sutable for prod1scin1
(2) |E, and ME quoduples for boolean enpreaANS
(3) | not E dung botorn-up paSi0g The
4) | (E) qrammer e wse i3the
(5)
(G)
| d, reop td
1true
-foloong
|false
(3) Me

The translatton scherne as tolloos :


(0) E’E, or ME
t backpatch (Ei-falselzt, M.quad);
Etuelt st:=merge (Eruelut'
E-truelist);

(2)
E.fobelut:= E,falselist 4
E’E, and MEZ { back patch (E,.tuettst , Mquad);
Eteuetst ! = E.tueltst i
E.falset : merge
(3) E-’not E (Efalschst,Efalselut)
E.tueltst : =Efalselist ;
E falselut : E truetst ; ?
(4) E (e)
E.truelist =E trueltst;
EfalselSt : = Etfalseltst i?
5) E’id, relop id,
EEhuelst : = makdut (nextquad);
Efalselist=makelist (nertquad +);
emit ("f"id,-place relop.op tda-ploae
qoto-)
emit ('gdto-)
(6)
E’true S Etruetst : = makel st (netquad);
emt ("goto?
) E false
FE.falsetst : = makeltst (net quod)
emt ("goto)}
() ME fM.quad: neztquad ?
Scheme to tmplement the Translatton:
The semantic rules tor the revised gramman
follouas:
() S if Ethen M,S, N else M262
backpatch (E:truel:st, M,.quad):
backpatch (E-fasc ltst , Ma.qod);
S.neittist : =merge (S1-nezt t:st, merge
(N.neatlist ,Sz .nettltst)
We backpatch the lums wohen E is true to the
Quadruple Mi-quad,ohich is the begtnin1 of the Code
for Si- Stm1laaly, oe backpatch iumps uohen E ts fase
to go to the begnin1 of the Code for S2. The (1st
S.necttist incude all iumps out ot S and S as
loell as the ump generated N.
(2) N ’e $ N.nezttst : -makelat (nertquad) ;
emt ('goto-)3
(3) M e M.quad :=nextaod
(2) S’fE then Ms, backpatch (Etrueltst, M. quad);
S-nextlist : =merge (Efalse list,
(5) S ’hele ME do M,S, SiDerttist)y
lbackpatoh (S1-nextst ,;'
Mi-quad);
backpaBeh (Etrueltst , M uad);
S"nexttst : = E.faekst
emt ( goto' Miquad)3
(6) S’beg1n end FS.nettist : = L.nextlist
() s’A
fS.nexttist
The ass1gnmet 6-nexttst = nd inttalizes Snexthst to
an empty hst.
(8) L 4 ; MS backpatch (4.hexttst ,M.quad)/
Linextltst : = S.nexttst ?
The statement follouotng L in ordey of ereatton a the
beginming of S. thus the LI. nexttst ltst ts backpatched
to the
beginng of the code for S, ohch is gven by
Mquad
(4) L’S
fL.netltst : S.ne tltst
Example;
if (<Do || 7 200 && -4) 10;

Ss S.nettst 1D3, ID5}, 1D1}


Backpatch í£1b0, (o4} iD6
S-netlisk io}
B-F=lo3, (05
B-t-$ioo}
Bot {lo4}
BfFiD3,105}
&& mi= lb4 8t= lD43
Bt-{D23
B-ftb5}
accoidig to the produtons
yS’if (8) MS, { Backpatch (B
«trueltstM"inshr) i
S-nettst = merge ( B.falselist,
TAC 1S
goto (o6 Sy-nextl:st):
101 goto l02
Io2: 1f y 7200 goto 104
ro3 : 9oto l04

Io5: goto (D7

You might also like