0% found this document useful (0 votes)
5 views55 pages

unit3part3pdf

The document discusses Syntax Directed Translation Schemes (SDTS) for intermediate code generation, focusing on constructs like IF, IF-Else, and WHILE. It provides grammar productions and corresponding semantic rules for generating intermediate code, along with examples illustrating the application of these rules. The document emphasizes the use of backpatching and merging of next instruction lists in the context of control flow statements.

Uploaded by

nirajdhanore04
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)
5 views55 pages

unit3part3pdf

The document discusses Syntax Directed Translation Schemes (SDTS) for intermediate code generation, focusing on constructs like IF, IF-Else, and WHILE. It provides grammar productions and corresponding semantic rules for generating intermediate code, along with examples illustrating the application of these rules. The document emphasizes the use of backpatching and merging of next instruction lists in the context of control flow statements.

Uploaded by

nirajdhanore04
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/ 55

INTERMEDIATE CODE GENERATION

USING
SYNTAX DIRECTED TRANSLATION SCHEMES
(SDTS)

PROF. V. BONGIRWAR
NOTE

SDTS studied till now will also be used


SDTS for IF, IF-Else
Production Semantic Rule
S→if E then M S1 backpatch(E.true,M.quad);
S.next= merge(E.false,S1.next);
S→if E then M1 S1 N else M2 S2 backpatch(E.true,M1.quad);
backpatch(E.false,M2.quad);
S.next=merge(S1.next, merge(N.next,
S2.next);
S→while M1 E do M2 S1 backpatch(S1.next, M1.quad);
backpatch(E.true,M2.quad);
S.next= E.false;
gen(‘goto’ M1.quad)
S→do M1 S1 while M2 E backpatch(S1.next,M2.Quad);
backpatch(E.true , M1.Quad);
S.next = E.false;
M→Є M.quad = nextquad;
N→Є N.next= makelist(nextquad),
gen(goto __)
Extra Statements
Production Semantic Rule

S→begin L end S.next = L.next;

S→A S.next = nil;

L→L1; M S backpatch(L1.next , M.quad);


L.next = S.next;

L→S L.next = S.next;

L →e L . next = S.next;
SDTS for If-then Construct

Grammar: if E then S1
Production Semantic Rule
S→if E then M S1 backpatch(E.true, M.quad);
S.next= merge(E.false,S1.next);

M→ M.quad = nextquad;


SDTS for If-then Construct Example
Q1. if (a>10) then p=q+r
Production Semantic Rule
if (a>10) then S→if E then M S1 backpatch(E.true,M.quad);
p=q+r S.next= merge(E.false,S1.next);
M→ M.quad = nextquad;

if E then M S1
Production Semantic Rule
if (a>10) then S→if E then M S1 backpatch(E.true,M.quad);
p=q+r S.next= merge(E.false,S1.next);
M→ M.quad = nextquad;

if E then M S1

( E )

E→E1relop E2 E.true = makelist(nextquad);


E.false= makelist(nextquad+1);
gen (if E1.place relop.val E2.place
goto____);
gen(goto ____);
Production Semantic Rule
if (a>10) then S→if E then M S1 backpatch(E.true,M.quad);
p=q+r S.next= merge(E.false,S1.next);
M→ M.quad = nextquad;

if E then M S1

( )
E
E relop E

E→E1relop E2 E.true = makelist(nextquad);


E.false= makelist(nextquad+1);
gen (if E1.place relop.val E2.place
goto____);
gen(goto ____);
if (a>10) then
p=q+r

if E then M S1

( E )

E relop E
>
id id
if (a>10) then
p=q+r

if E then M S1

( E )

E relop E
>
id id
Production Semantic Rule
A →id : = E gen( id.place ‘ =’ E . place)

S→A S.next = nil;


if (a>10) then
p=q+r

if E then M S1

( E ) A

E relop E id = E
>
id id
Production Semantic Rule
A →id : = E A .code = E. code || gen( id.place ‘ =’ E . place)

S→A S.next = nil;


if (a>10) then
p=q+r

if E then M S1

( E ) A

E relop E id = E
>
E + E
id id
if (a>10) then
p=q+r

if E then M S1

( E ) A

E relop E id = E
>
E + E
id id

id id
if (a>10) then p=q+r
S

if E then M S1

( E ) A

E relop E id = E
>
E + E
id id
id.place = a id.place = 10
id id
if (a>10) then p=q+r
S

if E then M S1

( E ) A
E.place = a E.place = 10 
E relop E id = E
>
E + E
id id
id.place = a id.place = 10
id id
E.true = makelist(nextquad);
if (a>10) then p=q+r E→E1 relop E2
E.false= makelist(nextquad+1);
gen (if E1.place relop.val E2.place goto____);
gen(goto ____);
S
TAC:
if E then M S1 100)

E.T = { }
E.F= { }( E ) A
E.place = 10 
E relop E id = E
E.place = a
>
E + E
id id
id.place = a id.place = 10
id id
E.true = makelist(nextquad);
if (a>10) then p=q+r E→E1 relop E2
E.false= makelist(nextquad+1);
gen (if E1.place relop.val E2.place goto____);
gen(goto ____);
S
TAC:
if E then M S1 100) If a>10 goto ______
101) goto ________
E.T = {100} 102)
E.F= {101} ( E ) A
E.place = 10 
E relop E id = E
E.place = a
>
E + E
id id
id.place = a id.place = 10
id id
if (a>10) then p=q+r
S
E.T = {100}
E.F= {101} M.quad = 102 TAC:
if E then M S1 100) If a>10 goto ______
101) goto ________
E.T = {100} 102)
E.F= {101} ( E ) A
E.place = 10 
E relop E id = E
E.place = a
>
E + E
id id
id.place = a id.place = 10
id id
TAC:
if (a>10) then p=q+r 100) If a>10 goto ______
101) goto ________
S 102)
E.T = {100}
E.F= {101} M.quad = 102

if E then M S1
E.T = {100}
E.F= {101} ( E ) A
E.place = 10 
E relop E id = E
E.place = a
id.place = p
>
E + E
id id
id.place = a id.place = 10
id id
id.place = q id.place = r
TAC:
if (a>10) then p=q+r 100) If a>10 goto ______
101) goto ________
S 102)
E.T = {100}
E.F= {101} M.quad = 102

if E then M S1
E.T = {100}
E.F= {101} ( E ) A
E.place = 10 
E relop E id = E
E.place = a
id.place = p
>
E + E E.place = r
id id E.place = q
id.place = a id.place = 10
id id
id.place = q id.place = r
E→ E1+E2 T= newTemp( );
E.place : = T;
E.code : = E1.code || E2.code || gen(E.place ‘ =’ E1.place ‘+’ E2.place)
TAC:
if (a>10) then p=q+r 100) If a>10 goto ______
101) goto ________
S 102) T1=q+r
E.T = {100}
M.quad = 102 103)
E.F= {101}

if E then M S1
E.T = {100}
E.F= {101} ( E ) A T=T1
E.place = 10  E.place=T1
gen
E relop E id = E
E.place = a
id.place = p
>
E + E E.place = r
id id E.place = q
id.place = a id.place = 10
id id
id.place = q id.place = r
E→ E1+E2 T= newTemp( );
E.place : = T;
E.code : gen(E.place ‘ =’ E1.place ‘+’ E2.place)
TAC:
if (a>10) then p=q+r 100) If a>10 goto ______
101) goto ________
S 102) T1=q+r
E.T = {100}
E.F= {101} M.quad = 102 103) p=T1

if E then M S1
E.T = {100}
E.F= {101} ( E ) A
E.place = 10  T=T1
E relop E id = E E.place=T1
E.place = a
id.place = p
>
E + E E.place = r
id id E.place = q
id.place = a id.place = 10
id id
id.place = q id.place = r

Production Semantic Rule


A →id : = E gen( id.place ‘ =’ E . place)
(THIS IS GENERATE)
TAC:
if (a>10) then p=q+r 100) If a>10 goto ______
101) goto ________
S 102) T1=q+r
E.T = {100}
E.F= {101} M.quad = 102 103) p=T1

if E then M S1 S1.next=null
E.T = {100}
E.F= {101} ( E ) A
E.place = 10  T=T1
E relop E id = E E.place=T1
E.place = a
id.place = p
>
E + E E.place = r
id id E.place = q
id.place = a id.place = 10
id id
id.place = q id.place = r

S→A S.next = nil;


if (a>10) then p=q+r TAC:
Production Semantic Rule 100) If a>10 goto _102_
S→if E then M S1 backpatch(E.true,M.quad); 101) goto ________
S.next= merge(E.false,S1.next); 102) T1=q+r
103) p=T1
Backpatch (100,102)
S.next= {101}
S
E.T = {100}
E.F= {101} M.quad = 102

if E then M S1 S1.next=null
E.T = {100}
E.F= {101} ( E ) A
E.place = 10  T=T1
E relop E id = E E.place=T1
E.place = a
id.place = p
>
E + E E.place = r
id id E.place = q
id.place = a id.place = 10
id id
id.place = q id.place = r
Example 2

if(c>2 and
d<5) then Production Semantic Rule
begin S→begin L S.next = L.next;
a=b+c; end
q=p+r; S→A S.next = nil;
end L→L1; M S backpatch(L1.next , M.quad);
L.next = S.next;

L→S L.next = S.next;

L →Є L . next = S.next;
S

if E then M S1

E relop E begin L end



>
L ; M S
id id

if(c>2) then S  A
begin
a=b+c; A id = E
q=p+r;
end E
id = E + E

E + E id id

id id
Answer for Q2.
SDTS for IF-THEN-ELSE
Grammar: S→ if E then M1 S1 N else M2 S2
Production Semantic Rule

S→if E then M1 backpatch(E.true,M1.qua


S1 N else M2 S2 d);
backpatch(E.false,M2.qu
ad);
Code of E S.next=merge(S1.next,
False
merge(N.next, S2.next);
True
M→ M.quad = nextquad;
Code of S1
Next of S1 N→ N.next=
Goto__ Next of if-else makelist(nextquad),
gen(goto __)
Code of S2 Next of S2
Example : If a<b then x=y +z else p= q +r
Example : If a<b then x=y +z else p= q +r

if E N S2
then M1 S1 M2
Example : If a<b then x=y +z else p= q +r

if E N S2
then M1 S1 M2

E relop E

id id
Example :If a<b then x=y +z else p= q +r

if E N S2
then M1 S1 M2

E relop Є A
E

id id id = E

E + E

id id
Example : If a<b then x=y +z else p= q +r

if E N S2
then M1 S1 M2

E Є Є
relop E Є A

id id id = E

E + E

id id
Example : If a<b then x=y +z else p= q +r

if E N S2
then M1 S1 M2

E Є Є A
relop E Є A

id id id = E id = E

E + E E + E

id id id id
Example : If a<b then x=y +z else p= q +r

if E N S2
then M1 S1 M2

E Є Є A
relop E Є A

Id. Place=p
id id id = E id = E
Id. Place=a Id. Place=b Id. Place=x
E + E E + E
Id. Place=r
Id. Place id id
Id. Place=y id id Id. Place=q
=z
E→E1 relop E.true = makelist(nextquad);
E2 E.false= makelist(nextquad+1);
Example : gen (if E1.place relop.val
If a<b then x=y +z else p= q +r E2.place goto____);
gen(goto ____);

S
if

E.T={100} E then N M2 S2
M1 S1
E.f={101}
gen

E Є Є A
relop E Є A
E. Place=a
E. Place=b
Id. Place=p
id id id = E id = E
Id. Place=b Id. Place=x
Id. Place=a
E + E E + E
100) If a < b goto__ Id. Place=r
Id. Place id id
101) goto____ Id. Place=y id id Id. Place=q
=z
E→ T= newTemp( );
E1+E2 E.place : = T;
Example : E.code : gen(E.place ‘ =’ E1.place ‘+’
E2.place)
If a<b then x=y +z else p= q +r

S
if

E.T={100} E then N M2 S2
M1 S1
E.f={101}
M. Q=102
gen Є

E Є Є A
relop E A
E. Place=a
E. Place=b
Id. Place=p
id id id = E id = E
Id. Place=b Id. Place=x
Id. Place=a
T= T1 E + E E + E
100) If a < b goto__
101) goto____ E. place=T1
Id. Place=r
gen Id. Place id id
102) T1= y + z Id. Place=y id id
=z Id. Place=q
Producti Semantic Rule
on
A →id : gen( id.place ‘ =’ E . place)
=E

S
if

E.T={100} E then N M2 S2
M1 S1
E.f={101} S1.next=null
M. Q=102
gen Є

E Є Є A
relop E gen A
E. Place=a
E. Place=b
Id. Place=p
id id id = E id = E
Id. Place=b Id. Place=x
Id. Place=a
100) If a < b got0__ T= T1 E + E E + E
101) goto____ E. place=T1
Id. Place=r
102) T1= y + z gen Id. Place id id
Id. Place=y id id Id. Place=q
103) x=T1 =z
100) If a < b got0__ 104) goto____
N→ N.next=
101) goto____ 105) makelist(nextquad),
102) T1= y + z
gen(goto __)
103) x=T1

S N.next=104
if

E.T={100} E then N M2 S2
M1 S1
E.f={101} S1.next=null
M. Q=102 M.Q=105
gen Є

E Є Є A
relop E gen A
E. Place=a
E. Place=b
Id. Place=p
id id id = E id = E
Id. Place=b Id. Place=x
Id. Place=a
T= T1 E + E E + E
E. place=T1
Id. Place=r
gen Id. Place id id
Id. Place=y id id Id. Place=q
=z
E→ E1+E2 T= newTemp( );
100) If a < b got0__ 104) goto____ E.place : = T;
101) goto____ 105) T2= q+r E.code : gen(E.place ‘ =’ E1.place ‘+’
102) T1= y + z 106) p=T2 E2.place)
103) x=T1 A →id : gen( id.place ‘ =’ E . place)
=E
S
if N,next=104

S2.next=null
E.T={100} E then N M2 S2
M1 S1
E.f={101} S1.next=null
M. Q=102 M.Q=105
gen Є
gen
E Є Є A
relop E gen A
E. Place=a
E. Place=b
Id. Place=p
id id id = E id = E
Id. Place=b Id. Place=x
Id. Place=a
T= T1 E + E E + E
T= T2 E. place=T1
E. place=T2 Id. Place=r
gen Id. Place id id
gen Id. Place=y id id Id. Place=q
=z
S→if E then M1 S1 backpatch(E.true,M1.quad);
100) If a < b goto 102 104) goto____ N else M2 S2 backpatch(E.false,M2.quad);
101) Goto 105 105) T2= q+r S.next=merge(S1.next,
102) T1= y + z 106) p=T2 merge(N.next, S2.next);
103) x=T1
Backpatch(100, 102)
Backpatch(101, 105)
S
S.next= (104)
if N,next=104

S2.next=null
E.T={100} E then N M2 S2
M1 S1
E.f={101} S1.next=null
M. Q=102 M.Q=105
gen Є
gen
E Є Є A
relop E gen A
E. Place=a
E. Place=b
Id. Place=p
id id id = E id = E
Id. Place=b Id. Place=x
Id. Place=a
T= T1 E + E E + E
T= T2 E. place=T1
E. place=T2 Id. Place=r
gen Id. Place id id
gen Id. Place=y id id Id. Place=q
=z
SDTS for While

Code of E
False Grammar: S→ while E do S1
Production Semantic Rule
True

S.next
S→ while backpatch(S1.next,
Code of S1
M1 E do M1.quad);
Goto__ M2 S1 backpatch(E.true,M2.quad);
S.next= E.false;
gen(‘goto’ M1.quad)
M1→ M1.quad = nextquad;
M1→ M2.quad = nextquad;
Example while a<b do x=y+z
Example while a<b do x=y+z

M1. Q=100 S1
while M1 E do M2

ε ε A
E relop
E. Place=a E
E. Place=b id = E
Id. Place=x
id id E. Place=z
Id. Place=a Id. Place=b E. Place=y E + E

id id
Id. Place=y Id. Place=z
Example E→E1
E2
relop E.true = makelist(nextquad);
E.false= makelist(nextquad+1);

while a<b do x=y+z gen (if E1.place relop.val


E2.place goto____);
gen(goto ____);

E.T={100} S
E.F={101}
gen
M1. Q=100 S1
while M1 E do M2 M2. Q=102

ε ε A
E relop
E. Place=a E
E. Place=b id = E
Id. Place=x
id id E. Place=z
Id. Place=b E. Place=y E + E
Id. Place=a
100 ) if a<b goto____
101) goto_____ id
id
Id. Place=y Id. Place=z
Example E→
E1+E2
T= newTemp( );
E.place : = T;
E.code : gen(E.place ‘ =’ E1.place ‘+’
while a<b do x=y+z E2.place)
A →id : gen( id.place ‘ =’ E . place)
=E
E.T={100} S
E.F={101}
gen
M1. Q=100 S1 S1.next=nil
while M1 E do M2 M2. Q=102

ε ε A gen
E relop T=T1
E. Place=a E
E. Place=b id = E E.place=T1
Id. Place=x gen
id id E. Place=z
Id. Place=a Id. Place=b E. Place=y E + E

100 ) if a<b goto____


101) goto_____ id
id
102) T1= y + z
Id. Place=y Id. Place=z
103)x=T1
Example S→while M1 E backpatch(S1.next, M1.quad);
do M2 S1 backpatch(E.true,M2.quad);

while a<b do x=y+z S.next= E.false;


gen(‘goto’ M1.quad)

Backpatch(100,102)
E.T={100} S s.next= 101
E.F={101} gen
gen
M1. Q=100 S1 S1.next=null
while M1 E do M2
M2. Q=102

ε ε A gen
E relop T=T1
E. Place=a E
E. Place=b id = E E.place=T1
Id. Place=x gen
id id E. Place=z
Id. Place=a Id. Place=b E. Place=y E + E
100 ) if a<b goto ___
101) goto_____
102) T1= y + z id id
103)x=T1 Id. Place=y Id. Place=z
104) goto 100
Example 2
while (a < b and b>d) do
If a==1
then
c=c+1
else
d= d+1
while (a < b and b>d) do
If a==1
then
c=c+1
else
d= d+1
SDTS for do While

Code of S1
False Grammar: S→ do S1 while E
S1.next
Productio Semantic Rule
True
n
S.next S→do M1 backpatch(S1.next,M2.Quad);
Code of E S1 while backpatch(E.true , M1.Quad);
M2 E S.next = E.false;

M1→ M1.quad = nextquad;


M1→ M2.quad = nextquad;
Example
do
a=1
b=x * y
While(b >=a)
Thank You

Happy Learning !

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