You are on page 1of 43

Compiler

Construction
Sohail Aslam
Lecture 40
2
Boolean Expressions
E E
1
and M E
2
{ backpatch(E
1
.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
3
Boolean Expressions
E E
1
and M E
2
{ backpatch(E
1
.truelist, M.quad);
E.truelist = E
2
.truelist;
E.falselist =
merge(E
1
.falselist, E
2
.falselist);}
This target is obtained using the marker
nonterminal M.

4
Boolean Expressions
E E
1
and M E
2
{ backpatch(E
1
.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(E
1
.falselist, M.quad);
E.truelist =
merge(E
1
.truelist, E
2
.truelist);
E.falselist = E
2
.falselist;
}
If E
1
is false, need to test E
2
6
Boolean Experssions
E not E
1
{
E.truelist = E
1
.falselist;
E.falselist = E
1
.truelist;
}

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 _ );
}
9
Boolean Experssions
E true
{
E.truelist = makelist(nextquad());
emit(goto _ );
}


10
Boolean Experssions
E false
{
E.falselist = makelist(nextquad());
emit(goto _ );
}

11
Boolean Expressions
M e
{
M.quad = nextquad();
}


12
Backpatching
consider again, the boolean
expression
a < b or c < d and e < f
We carry out a bottom-up
parse

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
15
E.t = {100}
E.f = {101}
c < d a < b e < f or and
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
e
18
Backpatching
The reduction of c < d to E,
the two quadruples
102: if c < d goto _
103: goto _
are generated
19
E.t = {100}
E.f = {101}
E.t = {102}
E.f = {103}
c < d
a < b
e < f
M.q = 102
or and
e
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.
21
E.t = {100}
E.f = {101}
E.t = {102}
E.f = {103}
c < d
a < b
e < f
M.q = 104
M.q = 102
or and
e
e
22
Backpatching
Reducing e < f to E,
generates
104: if e < f goto _
105: goto _
23
E.t = {100}
E.f = {101}
E.t = {102}
E.f = {103}
E.t = {104}
E.f = {105}
c < d
a < b
e < f
M.q = 104
M.q = 102
or and
e
e
24
Backpatching
We now reduce by the
production
E E
1
and M E
2


25
E.t = {100}
E.f = {101}
E.t = {104}
E.f = {103, 105}
E.t = {102}
E.f = {103}
E.t = {104}
E.f = {105}
c < d
a < b
e < f
M.q = 104
M.q = 102
or and
e
e
26
Semantic Actions
E E
1
and M E
2
{ backpatch(E
1
.truelist, M.quad);
E.truelist = E
2
.truelist;
E.falselist =
merge(E
1
.falselist, E
2
.falselist);}
27
Backpatching
The semantic action calls
backpatch({102},104)
where {102} denotes E
1
.truelist
containing only 102


28
Backpatching
The six statements generated so
far are thus
100: if a < b goto _
101: goto _
102: if c < d goto _
103: goto _
104: if e < f goto _
105: goto _
29
Backpatching
The semantic action calls
backpatch({102},104)
which fills in 104 in statement
102.
30
Backpatching
The call fills in 104 in statement
102
100: if a < b goto _
101: goto _
102: if c < d goto
103: goto _
104: if e < f goto _
105: goto _
104
31
Semantic Actions
E E
1
and M E
2
{ backpatch(E
1
.truelist, M.quad);
E.truelist = E
2
.truelist;
E.falselist =
merge(E
1
.falselist, E
2
.falselist);}
32
E.t = {100}
E.f = {101}
E.t = {104}
E.f = {103, 105}
E.t = {102}
E.f = {103}
E.t = {104}
E.f = {105}
c < d
a < b
e < f
M.q = 104
M.q = 102
or
and
e
e
104
103 105
33
Backpatching
We now reduce by the
production
E E
1
or M E
2


34
E.t = {100, 104}
E.f = {103, 105}
E.t = {100}
E.f = {101}
E.t = {104}
E.f = {103, 105}
E.t = {102}
E.f = {103}
E.t = {104}
E.f = {105}
c < d
a < b
e < f
M.q = 104
M.q = 102
or
and
e
e
35
Semantic Actions
E E
1
or M E
2
{
backpatch(E
1
.falselist, M.quad);
E.truelist =
merge(E
1
.truelist, E
2
.truelist);
E.falselist = E
2
.falselist;
}
36
Backpatching
The semantic action calls
backpatch({101},102)
which fills in 102 in statement
101.
37
Backpatching
100: if a < b goto _
101: goto
102: if c < d goto 104
103: goto _
104: if e < f goto _
105: goto _
102
38
Backpatching
100: if a < b goto _
101: goto 102
102: if c < d goto 104
103: goto _
104: if e < f goto _
105: goto _
These instructions will have their
targets filled later in the compilation
39
Semantic Actions
E E
1
or M E
2
{
backpatch(E
1
.falselist, M.quad);
E.truelist =
merge(E
1
.truelist, E
2
.truelist);
E.falselist = E
2
.falselist;
}
40
E.t = {100, 104}
E.f = {103, 105}
E.t = {100}
E.f = {101}
E.t = {104}
E.f = {103, 105}
E.t = {102}
E.f = {103}
E.t = {104}
E.f = {105}
c < d
a < b
e < f
M.q = 104
M.q = 102 or
and
e
e
{100} {104}
{103, 105}
41
Flow-of-Control Statements
We now use backpatching
to translate flow-of-control
statements in one pass
We will use the same list-
handling procedures as
before
42
Flow-of-Control Statements
We now use backpatching
to translate flow-of-control
statements in one pass
We will use the same list-
handling procedures as
before
43
Flow-of-Control Statements
S if E then S
| if E then S else S
| while E do S
| begin L end
| A
L L ; S
| S

You might also like