Three address code in Compiler (original) (raw)

Last Updated : 11 Apr, 2026

An intermediate representation used in compiler design to simplify complex expressions by breaking them into a sequence of simple instructions. Each instruction contains at most three addresses: two operands and one result.

Applications of Three-Address Code in Compilers

**General Representation

a = b
a = op b
a = b op c

Where a, b, or c represents operands like names, constants or compiler-generated temporaries and op represents the operator

**Example-1: Convert the expression a * - (b + c) into three address codes.

The given expression is:
a * − (b + c)

To convert it into three-address code, we break it into smaller operations using temporary variables:

  1. First, compute the expression inside the parentheses:
    t1 = b + c

  2. Apply unary minus to the result:
    t2 = -t1

  3. Multiply the result with a:
    t3 = a * t2

Final Three-Address Code:
t1 = b + c
t2 = -t1
t3 = a * t2

Example 2,: Write three address codes for the following code

for(i = 1; i<=10; i++)
{
a[i] = x * 5;
}

C++ `

i = 1 # Initialize i L1: # Start of the loop if i > 10 goto L2 # Check the loop condition t1 = x * 5 # Compute x * 5 and store in t1 a[i] = t1 # Assign t1 to a[i] i = i + 1 # Increment i goto L1 # Go back to the start of the loop L2: # End of the loop

`

**Implementation of Three Address Code

There are 3 representations of three address code namely

**1. Quadruple:

**Example - Consider expression a = b * - c + b * - c. The three address code is:

t1 = uminus c (Unary minus operation on c)
t2 = b * t1
t3 = uminus c (Another unary minus operation on c)
t4 = b * t3
t5 = t2 + t4
a = t5 (Assignment of t5 to a)

Quadruple Representation

**2. Triples:

**Example - Consider expression a = b * - c + b * - c

Triples Representation

**3. Indirect Triples

**Example - Consider expression a = b * - c + b * - c

Indirect Triples**Question - Write quadruple, triples and indirect triples for following expression : (x + y) * (y + z) + (x + y + z)

**Explanation - The three address code is:

(1) t1 = x + y
(2) t2 = y + z
(3) t3 = t1 * t2
(4) t4 = t1 + z
(5) t5 = t3 + t4

Quadruple Representation

Triples Representation

Indirect Triples

Process of Generating Three-Address Code

1. Source Code Parsing and Abstract Syntax Tree Generation.

2. TAC Instructions Generation

3. Expression Evaluation

4. Control Flow Constructs

5. Procedure Calls