0% found this document useful (0 votes)
53 views11 pages

Three Address Code: Examples & Types

The document provides examples of generating Three Address Code (TAC), Quadruples, Triples, and Indirect Triples for various programming problems. Each problem illustrates the conversion of expressions and control structures into different representations of intermediate code. The examples cover arithmetic operations, conditional statements, and loops.

Uploaded by

talhaibnmusa
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)
53 views11 pages

Three Address Code: Examples & Types

The document provides examples of generating Three Address Code (TAC), Quadruples, Triples, and Indirect Triples for various programming problems. Each problem illustrates the conversion of expressions and control structures into different representations of intermediate code. The examples cover arithmetic operations, conditional statements, and loops.

Uploaded by

talhaibnmusa
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

Three Address Code with Quadruples,

Triples, and Indirect Triples


Problem 1: a = b + c * d

Three Address Code (TAC):


• t1 = c * d
• t2 = b + t1
• a = t2

Quadruples:
Operator Arg1 Arg2 Result

* c d t1

+ b t1 t2

= t2 a

Triples:
Index Operator Arg1 Arg2

0 * c d

1 + b (0)

2 = (1)

Indirect Triples:
Pointer Table:

Index Points to Triple

0 0

1 1

2 2
Triple Table:

Index Operator Arg1 Arg2

0 * c d

1 + b (0)

2 = (1)
Problem 2: a = (b + c) * (d - e)

Three Address Code (TAC):


• t1 = b + c
• t2 = d - e
• t3 = t1 * t2
• a = t3

Quadruples:
Operator Arg1 Arg2 Result

+ b c t1

- d e t2

* t1 t2 t3

= t3 a

Triples:
Index Operator Arg1 Arg2

0 + b c

1 - d e

2 * (0) (1)

3 = (2)

Indirect Triples:
Pointer Table:

Index Points to Triple

0 0

1 1

2 2

3 3
Triple Table:

Index Operator Arg1 Arg2

0 + b c

1 - d e

2 * (0) (1)

3 = (2)
Problem 3: if (a < b) x = y + z; else x = y - z;

Three Address Code (TAC):


• if a < b goto L1
• goto L2
• L1: t1 = y + z
• x = t1
• goto L3
• L2: t2 = y - z
• x = t2
• L3:

Quadruples:
Operator Arg1 Arg2 Result

if< a b L1

goto L2

+ y z t1

= t1 x

goto L3

- y z t2

= t2 x

Triples:
Index Operator Arg1 Arg2

0 if< a b

1 goto L2

2 + y z

3 = (2)

4 goto L3

5 - y z

6 = (5)
Indirect Triples:
Pointer Table:

Index Points to Triple

0 0

1 1

2 2

3 3

4 4

5 5

6 6

Triple Table:

Index Operator Arg1 Arg2

0 if< a b

1 goto L2

2 + y z

3 = (2)

4 goto L3

5 - y z

6 = (5)
Problem 4: while (a < b) { a = a + 1; }

Three Address Code (TAC):


• L1: if a >= b goto L2
• t1 = a + 1
• a = t1
• goto L1
• L2:

Quadruples:
Operator Arg1 Arg2 Result

if>= a b L2

+ a 1 t1

= t1 a

goto L1

Triples:
Index Operator Arg1 Arg2

0 if>= a b

1 + a 1

2 = (1)

3 goto L1

Indirect Triples:
Pointer Table:

Index Points to Triple

0 0

1 1

2 2

3 3
Triple Table:

Index Operator Arg1 Arg2

0 if>= a b

1 + a 1

2 = (1)

3 goto L1
Problem 5: x = a * b + c / d

Three Address Code (TAC):


• t1 = a * b
• t2 = c / d
• t3 = t1 + t2
• x = t3

Quadruples:
Operator Arg1 Arg2 Result

* a b t1

/ c d t2

+ t1 t2 t3

= t3 x

Triples:
Index Operator Arg1 Arg2

0 * a b

1 / c d

2 + (0) (1)

3 = (2)

Indirect Triples:
Pointer Table:

Index Points to Triple

0 0

1 1

2 2

3 3
Triple Table:

Index Operator Arg1 Arg2

0 * a b

1 / c d

2 + (0) (1)

3 = (2)

You might also like