Unit 4 - Session 6
Unit 4 - Session 6
Unit 4 - Session 6
COMPILER DESIGN
UNIT 4
SESSION 6
Topics that will be covered
• Assignment Statements
• Boolean Expressions
• Case Statements
ASSIGNMENT
STATEMENTS
Assignment Statements
• As part of the translation of assignments into three-address code, we will learn
• How names can be looked up in the symbol table
• How elements of arrays can be accessed
• How records can be accessed
Names in Symbol Table
Note: The function c(Elist.array) returns the second term of expression (5)
Translation Scheme for addressing array elements – cont..
Note:
The function limit(array,m) returns the number of
elements along the jth dimension of the array nj
Example:
Let A be a 10x20 array with low1=low2=1
Therefore, n1=10 and n2=20
Take w=4
Use the formula
( ( i1 * n2 ) + i2 ) * w + ( base – ( low1 * n2 ) + low2 ) * w
The second term is the constant c
Consider the statement x := A[y,z]
Here i1=y and i2=z. The expression now becomes
( ( y * 20 ) + z ) * 4 + c
Three address code:
t1 := y * 20
t1 := t1 + z
t2 := c
t3 := 4 * t1
t4 := t2 [ t3 ]
x := t4
Semantic action for E → E1 + E2
Type conversions within Assignments
• In a program there will be many different types of
variables and constants
• So the compiler must either reject certain mixed-
type operations or generate appropriate type
conversion instructions
• Suppose there are two types – real and integer
with integers converted to reals when necessary
Example
• Consider the input
x := y + i * j
• Assuming x and y have type real and i and j have
type integer
t1 := i int* j (integer multiplication)
t2 := inttoreal t1 (convert int to real)
t3 := y real+ t2 (floating-point addition)
x := t3
BOOLEAN
EXPRESSIONS
Boolean Expressions
• In programming languages, Boolean expressions have 2 primary purposes
1. They are used to compute logical values
2. They are used as conditional expressions in statements that alter the flow of control such
as if-then, if-then-else and while-do statements
• Boolean expressions are composed of Boolean operators (and, or, and, not) applied to elements
that are Boolean variables or relational expressions
• Grammar for Boolean expressions
E → E or E | E and E | not E | (E) | id relop id | true | false
Methods of Translating Boolean Expressions
• Encode true and false numerically
Examples:
1. True is denoted by 1 and false by 0
2. Any non-zero quantity denotes true and 0 denotes false
3. Any non-negative quantity denotes true and a negative quantity denotes false
• Represent the value of an expression by a position reached in a program
Numerical Representation
Consider the implementation of Boolean expressions using 1 to denote true and 0 to denote false
Example:
Translation for a or b and not c is
t1 := not c
t2 := b and t1
t3 := a or t2
Example:
Consider the relational expression a<b
It is equivalent to the conditional statement if a<b then 1 else 0
Three address code:
100: if a<b goto 103
101: t := 0
102: goto 104
103: t :=1
104:
Translation Scheme
Assume
1. nextstat gives the index of the next three address statement
2. emit increments nextstat after producing each 3-address statement
Annotated parse tree for the expression
a<b or c<d and e<f
E.place=t5
E.place=t4
E.place=t1
E.place=t3
E.place=t2
Short-Circuit Code
• We can translate a Boolean expression into three-address code without generating code for any
of the Boolean operators and without having the code to evaluate the entire expression
• This style of evaluation is called “short-circuit” or “jumping” code
Flow of Control Statements Syntax directed definition
• The grammar for if-then, if-then-else and while-do
statements is
S → if E then S1 |
if E then S1 else S2 |
while E do S1
1. Three address statements are symbolically
labelled
2. A function newlabel returns a new symbolic
label each time it is called
3. Two labels are associated with a Boolean
expression E
• E.true – the label to which control flows if E
is true
• E.false – the label to which control flows if
E is false
4. S.next is an inherited attribute. It is a label that
is attached to the first three-address instruction
to be executed after the code for S
5. The code for E generates a jump to E.true if E is
true and a jump to E.false if E is false
Flow of Control Statements – cont..
Syntax directed definition
Control Flow Translation of Boolean Expressions
• We can discuss E.code, the code produce for the Boolean expressions E in the previous syntax
directed definition
• Suppose E is of the form a < b, the generated code is of the form
if a<b goto E.true
goto E.false
• Suppose E is of the form E1 or E2,
• If E1 is true, then we need not evaluate E2. We immediately know that E itself is true. So
E1.true is the same as E.true
• If E1 is false, then E2 must be evaluated. So E1.false is the label of the first statement in
the code for E2
• No code is needed for an expression E of the form not E1
Control Flow Translation of Boolean Expressions – cont..
Example: