Esku Individual Aas Compiler

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 11

SAMARA UNIVERSITY

COLLEGE OF ENGINEERING AND TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE

COUSE TITLE:COMPILER DESIGN

COUSE CODE:

NAME ESKU LULU

ID NO 1100974

SECTION “A”

INDIVIDUAL ASSIGNMENT
General Form-
Three Address Statement Types, Assignment statement, Arithmetic operation, Logical
operation, Unary minus, Copy statement, Unconditional jump are basic concepts discussed of
course.Mar 11, 2012
What are the types of three address statements?
Common Three Address Instruction Forms-

 Assignment Statement- x = y op z and x = op y. Here, ...


 Copy Statement- x = y. Here, ...
 Conditional Jump- If x relop y goto X. Here, ...
 Unconditional Jump- goto X. Here, X is the tag or label of the target statement. ...
 Procedure Call- param x call p return y.

In general, Three Address instructions are represented as-

a = b op c

Here,

 a, b and c are the operands.


 Operands may be constants, names, or compiler generated temporaries.
 op represents the operator.

Examples-

Examples of Three Address instructions are-


 a=b+c
 c=axb

Common Three Address Instruction Forms-


 

The common forms of Three Address instructions are-

1. Assignment Statement-

x = y op z and x = op y

Here,

 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.

2. 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.

3. 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.


 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,Boolean Expressions and Control Flow

BOOLEAN EXPRESSIONS are constructed using boolean operators. We will consider


here the following rules.

E     E   E

E     E   E

E       E

E     E
E         

E     

E     

 Boolean expressions are used as conditions for statements changing the flow
of control.
 Evaluation of boolean expressions can be optimized if it is sufficient to
evaluate a part of the expression that determines its value.
 When translating Boolean expressions into three-address code, we can use
two different methods.

Numerical method.
Assign numerical values to true and false and evaluate the expression
analogously to an arithmetic expression. This is convenient for boolean
expressions which are not involved in flow of control constructs.

Jump method.

Evaluate a boolean expression E as a sequence of conditional and


unconditional jumps to location E.true (if E is true) or to E.false. To be detailed
shortly.

WHILE STATEMENTS WITH THE NUMERICAL METHOD.

The following syntax-directed translation generates code for a while statement.

Production Semantic Rule


S.begin := newlabel

S     E   S1

S.after := newlabel

code1 := generate(S.begin ':') | | E.code | |

code2 := generate('if ' E.place '= 0 '
'goto' S.after) | | S1.code

code3 := generate('goto' S.begin) |
| generate(S.after)

S.code := code1 | | code2 | | code3

With respect to the previous syntax-directed translation

 we have added two new synthesized attributes S.begin and S.after.


 When the value of E becomes zero, control leaves the while statement

FLOW OF CONTROL STATEMENTS WITH THE JUMP METHOD. We will consider here
the following rules.

S
 E   S1

S
 E   S1  S2

 E   S1

 In each of these productions, E is the boolean expression to be translated.


 The boolean expression E is associated with two labels (that
are inherited attributes in the following semantic rules)
o E.true the label to which control flows if E is true,
o E.false the label to which control flows if E is false.
 In each of these productions, S is a flow of control statement associated with
two attributes
o S.next which is a label that is attached to the first 3-address statement
to be executed after the code for S , S.next is an inherited attribute,
o S.code is the translation code for S, as usual it is a synthesized attribute.

Production Semantic Rule

E.true := newlabel
S       E   S1

E.false := S.next

S1.next := S.next

S.code := E.code | | generate(E.true ':') |
| S1.code

E.true := newlabel
S       E   S1   S2

E.false := newlabel

S1.next := S.next

S2.next := S.next

code1 := E.code | | generate(E.true ':') |
| S1.code

code2 := generate('goto' S.next) | |

code3 := generate(E.false ':') | | S2.code

S.code := code1 | | code2 | | code3


S.begin := newlabel

S       E   S1

E.true := newlabel

E.false := S.next

S1.next := S.begin

code1 := generate(S.begin ':') | | E.code

code2 := generate(E.true ':') | | S1.code

code3 := generate('goto' S.begin)

S.code := code1 | | code2 | | code3

Warning! The above is a syntax-directed definition: It provides formulas for the


computation of the attributes S.code (via the computations of the other attributes).

 Since several attributes are inherited and since each action above appears
after its associated production, this is not a translation scheme.
 However it is an L-attributed definition.
 Then its conversion into a translation scheme is obvious.
 From now on, we may present a translation scheme as a syntax-directed
definition if the latter is an L-attributed definition.
 The reason is to make large translation schemes easier to read.

3,Methods of Translating Boolean Expressions:

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 :

Translation scheme using a numerical representation for booleans


 

You might also like