Docsity

Three Adress Statemnet Types - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
Sohail Aslam Compiler Construction Notes Set:6-108
108
L
Le
ec
ct
tu
ur
re
e
3
38
8
Three-Address Statement Types
Prior to proceeding with flow-of-control construct, here are the types of three-Address
statements that we will use
• Assignment statement
x = y op z
op is a binary arithmetic or logical operation
• Copy statement
x = y
value of y is assigned to x
• Unconditional jump
goto L
The three-address statement with label L is executed next
• Conditional jump
if x relop y goto L
relop is <, =, >=, etc. If x stands in relation relop to y, execute statement with
label L, next otherwise
• Indexed assignment
a) x = y[i]
b) x[i] = y
In a), set x to value in location i memory units beyond location y.
In b), set contents of location i memory units beyond x to y.
We associate with a boolean expression E two labels (attributes); E.true and E.false. The
control flows to E.true if the expression evaluates to true, to E.false otherwise.
Following is syntax-directed translation for
S → if E then S1
E.true = newlabel()
E.false = S.next
S
1.next = S.next
S.code = E.code || gen(E.true ‘:’) || S1.code
The attribute “next” records the label of the next statement to execute. “code” is
string-valued attribute that holds the actual code generated in the form of a character
string. The code can be eventually written out to a file. The || is the string concatenation
operator, that is “hello”|| “ world” will yield the combined string “hello world”.
pf2

Partial preview of the text

Download Three Adress Statemnet Types - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Sohail Aslam Compiler Construction Notes Set:6-

Le Leccttuurree 3 388

Three-Address Statement Types

Prior to proceeding with flow-of-control construct, here are the types of three-Address statements that we will use

  • Assignment statement x = y op z op is a binary arithmetic or logical operation
  • Copy statement x = y value of y is assigned to x
  • Unconditional jump goto L The three-address statement with label L is executed next
  • Conditional jump if x relop y goto L relop is <, =, >=, etc. If x stands in relation relop to y , execute statement with label L , next otherwise
  • Indexed assignment a) x = y[i] b) x[i] = y In a), set x to value in location i memory units beyond location y. In b), set contents of location i memory units beyond x to y.

We associate with a boolean expression E two labels (attributes); E.true and E.false. The control flows to E.true if the expression evaluates to true, to E.false otherwise. Following is syntax-directed translation for

S → if E then S 1

E.true = newlabel() E.false = S.next S 1 .next = S.next S.code = E.code || gen(E.true ‘:’) || S 1 .code

The attribute “ next ” records the label of the next statement to execute. “ code ” is string-valued attribute that holds the actual code generated in the form of a character string. The code can be eventually written out to a file. The || is the string concatenation operator, that is “hello”|| “ world” will yield the combined string “hello world”.

Sohail Aslam Compiler Construction Notes Set:6-

Suppose E is “a < b”. E.code would be

if a < b goto E.true goto E.false

We will discuss semantic rules for boolean expressions shortly

The syntax-directed translation for

S → if E then S 1 else S 2 is

E.true = newlabel() E.false = newlabel() S 1 .next = S.next S 2 .next = S.next S.code = E.code || gen(E.true ‘:’) || S 1 .code || gen(‘goto’ S.next) || gen(E.false ‘:’) || S 2 .code

Similarly, the syntax-directed translation for the while loop is

S → while E do S 1

S.begin = newlabel() E.true = newlabel() E.false = S.next S 1 .next = S.begin S.code = gen(S.begin ‘:’) || E.code || gen(E.true ‘:’) || S 1 .code || gen(‘goto’ S.begin)