0% found this document useful (0 votes)
22 views32 pages

4.CD Unit 4.3.pptx

Compiler Design 4.3

Uploaded by

Rucha Gavas
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)
22 views32 pages

4.CD Unit 4.3.pptx

Compiler Design 4.3

Uploaded by

Rucha Gavas
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/ 32

Assignments Statements

In
Compiler design

Copyright ©
Prof. Vikas Chavan
Assistant Professor
CSE, JNEC.
● The p returns the entry for id.name in the symbol table.
● The Emit function is used for appending the three
address code to the output file. Otherwise it will report
an error.
● The newtemp() is a function used to generate new
temporary variables.
● E.place holds the value of E.
● 𝐄. 𝐏𝐋𝐀𝐂𝐄 − It tells about the name that will hold the
value of the expression.

● 𝐄mit− It represents a sequence of three address


statements evaluating the expression E in grammar
represents an Assignment statement. Emit represents
the three address codes of the statement. CODE for
non-terminals on the left is the concatenation of CODE
for each non-terminal on the right of Production.
● Example 1:
x=a+b*c
● Example 2:
a=(b+-c)*d
Boolean expressions
In
Compiler Design
Boolean expressions
have two primary purposes.

1. They are used for computing the logical


values.
2. They are also used as conditional
expression that alter the flow of control, such
as if-then-else or while-do
Boolean expression are composed of the
boolean operators(and ,or, and not)
applied to elements that are
Boolean variables or relational
expressions.

Relational expressions are of the form

E1 relop E2 , where E1 and E2 are


arithmetic expressions.
Consider the grammar

E → E or E
E → E and E
E → not E
E → (E)
E → id relop id
E → id
E → TRUE
E → FALSE

The relop is any of <,≤,=, ≠, >, or ≥.. We


assume that or and and are left associative. or
has lowest precedence ,then and ,then not
Methods of Translating Boolean Expressions: There are
two methods of representing the value of a Boolean.

1) To encode true or false Numerically


a) 1 is used to denote true.
b) 0 is used to denote false.
2) To evaluate a boolean expression analogously to an
Arithmetic expression.
a) Flow of control –representing the value of a
boolean expression by a position reached in a
program.
b) Used in flow of control statements such as
if-then-else or while-do statements.
Numerical Representation of Boolean Expressions:
First consider the implementation of boolean
expressions using
● 1 to denote TRUE and
● 0 to denote FALSE.
● Expressions will be evaluated from left to right.

Example 1:
A or B and C
Three address sequence:
T1 = B and C
T2 = A or T1
Example 2:

A relational expression A< B


is equivalent to the
conditional statement. if A < B then 1 else 0

Three address sequence:


(1) if A < B goto (4)
(2) T=0
(3) goto (5)
(4) T=1
(5) ---
Semantic Rules for Boolean Expression:

Production rule Semantic actions

E → E1 OR E2 {E.place = newtemp();

Emit (E.place ':=' E1.place 'OR' E2.place)}

E → E1 AND E2 {E.place = newtemp();

Emit (E.place ':=' E1.place 'AND' E2.place)}

E → NOT E1 {E.place = newtemp();

Emit (E.place ':=' 'NOT' E1.place)}

E → (E1) {E.place = E1.place}


Production rule Semantic actions

E → id relop id2 {E.place = newtemp();

Emit ('if' id1.place relop.op id2.place 'goto'

nextstat + 3);

EMIT (E.place ':=' '0')

EMIT ('goto' nextstat + 2)

EMIT (E.place ':=' '1')}

E → TRUE {E.place := newtemp();

Emit (E.place ':=' '1')}

E → FALSE {E.place := newtemp();

Emit (E.place ':=' '0')}


● The EMIT function is used to generate the three
address code and the newtemp( ) function is used to
generate the temporary variables.
● The E → id relop id2 contains the next_state and it
gives the index of next three address statements in the
output sequence.
Example 1:
Code for a < b or c < d and e <f

100: if a < b goto 103


101: t1 = 0
102: goto 104
103: t1 = 1
104: if c < d goto 107
105: t2 =0
106: goto 108
107: t2 = 1
108: if e < f goto 111
109: t3 =0
110: goto 112
111: t3 = 1
112: t4 = t2 and t3
113: t5 = t1 or t4
Production rule Semantic actions
Example 1:
E → E1 OR E2 {E.place = newtemp();
Code for a < b or c < d and e <f
Emit (E.place ':=' E1.place 'OR' E2.place)}

100: if a < b goto 103 E → E1 AND E2 {E.place = newtemp();

101: t1 = 0 Emit (E.place ':=' E1.place 'AND' E2.place)}

102: goto 104 E → NOT E1 {E.place = newtemp();

103: t1 = 1 Emit (E.place ':=' 'NOT' E1.place)}

104: if c < d goto 107 E → (E1) {E.place = E1.place}


105: t2 =0
E → id relop id2 {E.place = newtemp();
106: goto 108 Emit ('if' id1.place relop.op id2.place 'goto'
107: t2 = 1 nextstat + 3);
108: if e < f goto 111 EMIT (E.place ':=' '0')

109: t3 =0 EMIT ('goto' nextstat + 2)

EMIT (E.place ':=' '1')}


110: goto 112
111: t3 = 1 E → TRUE {E.place := newtemp();

112: t4 = t2 and t3 Emit (E.place ':=' '1')}

113: t5 = t1 or t4 E → FALSE {E.place := newtemp();

Emit (E.place ':=' '0')}


Example 2: p>q AND r<s OR u>r
100: if p>q goto 103
101: t1:=0
102: goto 104
103: t1:=1
104: if r>s goto 107
105: t2:=0
106: goto 108
107: t2:=1
108: if u>v goto 111
109: t3:=0
110: goto 112
111: t3:= 1
112: t4:= t1 AND t2
113: t5:= t4 OR t3
Production rule Semantic actions
Example 2: p>q AND r<s OR u>r E → E1 OR E2 {E.place = newtemp();

100: if p>q goto 103 Emit (E.place ':=' E1.place 'OR' E2.place)}

101: t1:=0 E → E1 + E2 {E.place = newtemp();

Emit (E.place ':=' E1.place 'AND' E2.place)}


102: goto 104
E → NOT E1 {E.place = newtemp();
103: t1:=1
Emit (E.place ':=' 'NOT' E1.place)}
104: if r>s goto 107
E → (E1) {E.place = E1.place}
105: t2:=0
E → id relop id2 {E.place = newtemp();
106: goto 108
Emit ('if' id1.place relop.op id2.place 'goto'
107: t2:=1 nextstat + 3);

108: if u>v goto 111 EMIT (E.place ':=' '0')

EMIT ('goto' nextstat + 2)


109: t3:=0
EMIT (E.place ':=' '1')}
110: goto 112
E → TRUE {E.place := newtemp();
111: t3:= 1 Emit (E.place ':=' '1')}

112: t4:= t1 AND t2


E → FALSE {E.place := newtemp();
113: t5:= t4 OR t3 Emit (E.place ':=' '0')}
Case Statements
In
Compiler Design
Switch and case statement is available in a variety of
languages. The syntax of case statement is as follows:

switch E
begin
case V1: S1
case V2: S2
.
.
.
case Vn-1: Sn-1
default: Sn
end
Code to evaluate E into T
goto TEST
L1: code for S1
goto NEXT
L2: code for S2
goto NEXT
.
.
Ln-1: code for Sn-1
goto NEXT
Ln: code for Sn
goto NEXT
TEST: if T = V1 goto L1
if T = V2 goto L2
.
.
if T = Vn-1 goto Ln-1
goto
NEXT:
● When switch keyword is seen then a new temporary T

and two new labels test and next are generated.

● When the case keyword occurs then for each case

keyword, a new label Li is created and entered into the

symbol table. The value of Vi of each case constant

and a pointer to this symbol-table entry are placed on

a stack.
Procedures call
In
Compiler Design
Procedures call

Procedure is an important and frequently used


programming construct for a compiler. It is used to
generate good code for procedure calls and
returns.

Calling sequence:
The translation for a call includes a sequence of
actions taken on entry and exit from each
procedure. Following actions take place in a
calling sequence:
● When a procedure call occurs then space is
allocated for activation record.
● Evaluate the argument of the called procedure.
● Establish the environment pointers to enable
the called procedure to access data in
enclosing blocks.
● Save the state of the calling procedure so that it
can resume execution after the call.
● Also save the return address. It is the address
of the location to which the called routine must
transfer after it is finished.
● Finally generate a jump to the beginning of the
code for the called procedure.
Grammar for a simple procedure call statement
1. S → call id(Elist)
2. Elist → Elist, E
3. Elist → E
A suitable transition scheme for procedure call would be:

Production Rule Semantic Action

S → call id(Elist) for each item p on QUEUE do

GEN (param p)

GEN (call id.PLACE)

Elist → Elist, E append E.PLACE to the end of QUEUE

Elist → E initialize QUEUE to contain only

E.PLACE
Thank You

Copyright ©
Prof. Vikas Chavan
Assistant Professor
CSE, JNEC.

You might also like