Docsity

Boolean Expressions, Backpatching - Compiler Construction - Lecture Slides, Slides of Compiler Construction

Partial preview of the text

Download Boolean Expressions, Backpatching - Compiler Construction - Lecture Slides and more Slides Compiler Construction in PDF only on Docsity!

Compiler

Construction

Lecture 40

2

Boolean Expressions

E → E 1 and M E 2

{ backpatch(E1.truelist, M.quad); E.truelist = E 2 .truelist; E.falselist = merge(E 1 .falselist, E 2 .falselist);}

So the target for E 1 .truelist must be the beginning of code generated for E (^2)

4

Boolean Expressions

E → E 1 and M E 2

{ backpatch(E1.truelist, M.quad); E.truelist = E 2 .truelist; E.falselist = merge(E 1 .falselist, E 2 .falselist);}

M.quad records the number of the first statement of E 2 .code.

5

Boolean Expressions: OR

E → E 1 or M E 2

backpatch(E1.falselist, M.quad); E.truelist = merge(E 1 .truelist, E 2 .truelist); E.falselist = E 2 .falselist; } If E1 is false, need to test E 2

7

Boolean Experssions

E → ( E 1 ) { E.truelist = E 1 .truelist; E.falselist = E 1 .falselist; }

8

Boolean Experssions

E → id 1 relop id 2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id 1 relop id 2 ‘goto _’) ; emit(‘goto _’ ); }

10

Boolean Experssions

E → false { E.falselist = makelist(nextquad()); emit(‘goto _’ ); }

11

Boolean Expressions

M → ε { M.quad = nextquad(); }

13

Backpatching

 In response to reduction of a < b to E, the two quadruples

100: if a < b goto _

101: goto _

are generated

14

Recall

E → id 1 relop id 2 { E.truelist = makelist(nextquad()); E.falselist = makelist(nextquad()+1); emit(‘if’ id 1 relop id 2 ‘goto _’) ; emit(‘goto _’ ); } View this in a parse tree

16

Backpatching

 The marker non-terminal M in the production

E → E 1 or M E 2

records the value of nextquad which at this time is 102.

17

E.t = {100} E.f = {101}

c < d

a < b

e < f

M.q = 102

or and

ε

19

E.t = {100} E.f = {101}

E.t = {102} E.f = {103} c < d

a < b

e < f

M.q = 102

or and

ε

20

Backpatching

 The marker non-terminal M in the production

E → E 1 and M E 2

records the value of nextquad which at this time is 104.