Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE

COUSE TITLE: COMPILER DESIGN

COUSE CODE:3112

NAME ESKU LULU

ID NO 1100974

SECTION “A”

INDIVIDUAL ASSIGNMENT
INSTRUCTOR NAME: MOHAMED UMER

Page 1 of 7
Explain in detail with an example
1,Three Address Statement Types

Common Three Address Instruction Forms-

A. Assignment Statement-
Here,

x = y op z and x = op y

 x, y and z are the operands.


 op represents the operator.

It assigns the result obtained after solving the right side expression of the assignment operator to the left
side operand.

For example ,

B. Copy Statement-

x=y

Here,

 x and y are the operands.


 = is an assignment operator.

It copies and assigns the value of operand y to operand x.

C. Conditional Jump-

If x relop y goto X

Here,

 x & y are the operands.


 X is the tag or label of the target statement.
 relop is a relational operator.

If the condition “x relop y” gets satisfied, then-

 The control is sent directly to the location specified by label X.


 All the statements in between are skipped.

If the condition “x relop y” fails, then-

 The control is not sent to the location specified by label X.

Page 2 of 7
 The next statement appearing in the usual sequence is executed.

4. Unconditional Jump-

goto X

Here, X is the tag or label of the target statement.

On executing the statement,

 The control is sent directly to the location specified by label X.


 All the statements in between are skipped.

5. Procedure Call-

param x call p return y

Here, p is a function which takes x as a parameter and returns y.

2,Flow control/positional representation of Boolean expression

Control Flow

Here we discuss the uses of Boolean expressions to alter the flow of control. We will learn about a new
non-terminal B for the same.

Now we consider the conversion of Boolean expression into three-address code in the context of
statement such as those generated by the following grammar:

S -> if ( B ) S1

S -> if ( B ) S1 else S2

S -> while ( B ) S1

In the above expressions, non-terminal B represents a Boolean expression and S represents a statement.
Now let us try to translate the above code.

The translation of if ( B ) S1 consists of B.code followed by S1.code, as illustrated in Fig. 6.35(a).


Within B.code are jumps based on the value of B. If B is true, control follows to the first instruction of
S1.code, and if B is false, control flows to the instruction immediately following S1.code

For if statement

Page 3 of 7
B.code -> to B.true

-> to B.false

B.true: S1.code

B.false: …..

For if-else statement

B.code -> to B.true

-> to B.false

B.true: S1.code

Goto S.next

B.false: S2.code

S.next: ……

For while statement

begin : B.code -> to B.true

-> to B.false

B.true: S1.code

Goto begin

B.false: S2.code

The labels for the jumps in B.code and S.code are managed using inherited attributes

3,Methods of Translating Boolean Expressions:

Boolean expressions 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.

Here we consider boolean expressions generated by the following grammar :

Page 4 of 7
E->EorE | EandE |notE | ( E ) |id relop id | true | false

There are two principal methods of representing the value of a boolean expression.

They are :

To encode true and false numerically and to evaluate a boolean expression analogously to an
arithmetic expression. Often, 1 is used to denote true and 0 to denote false.

To implement boolean expressions by flow of control, that is, representing the value of a boolean
expression by a position reached in a program. This method is particularly convenient in
implementing the boolean expressions in flow-of-control statements, such as the if-then and
while-do statements.

Numerical Representation

Here, 1 denotes true and 0 denotes false. Expressions will be evaluated completely from left to right, in a
manner similar to arithmetic expressions.
For example :

* The translation for a or b and not c is the three-address sequence


t1 : = not c
t2 : = b and t1
t3 : = a or t2

* A relational expression such as a < b is equivalent to the conditional statement


if a < b then 1 else 0

which can be translated into the three-address code sequence (aga statement numbers at 100) :
100 : if a < b goto 103 101 : t : = 0
102 : goto 104
103 : t : = 1
104 :
Short-Circuit Code

Page 5 of 7
We can also translate a boolean expression into three-address code without generating code for any of the
boolean operators and without having the code necessarily evaluate the entire expression. This style of
evaluation is sometimes called “short-circuit” or “jumping” code. It is possible to evaluate boolean
expressions without generating code for the boolean operators and, or, and not if we represent the value of
an expression by a position in the code sequence.

Translation of 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

Flow-of-Control Statements

We now consider the translation of boolean expressions into three address code in the context of if-then,
if-then-else, and while-do statements such as those generated by the following grammar:

S->ifEthenS1
| if E then S1 else S2
| while E do S1

In each of these productions, E is the Boolean expression to be translated. In the translation, we assume
that a three-address statement can be symbolically labeled, and that the function newlabel returns a new
symbolic label each time it is called.

Page 6 of 7
E.true is the label to which control flows if E is true, and E.false is the label to which control
flows if E is false.

The semantic rules for translating a flow-of-control statement S allow control to flow from the
translation S.code to the three-address instruction immediately following S.code.

S.next is a label that is attached to the first three-address instruction to be executed after the code
for S.

Page 7 of 7

You might also like