ToolSphere
Back to all articles
student

Three Address Code: Examples and Quadruples

2026-10-027 min read

Try it: free Three Address Code Generator

Turn expressions and simple control flow into three-address code

Open →

What three-address code is

Three-address code (TAC) is the intermediate representation a compiler produces between parsing and machine code. Each instruction has at most three addresses: two operands and a result. A long expression is broken into a sequence of these, one operator at a time, with temporaries t1, t2, … holding the partial results.

The instruction forms you will meet in a compiler-design course are:

Form Example Meaning
Binary operation t1 = b * c apply an operator to two operands
Unary operation t1 = minus c negate, or another one-operand operator
Copy x = t3 assign one address to another
Unconditional jump goto L2 continue at label L2
Conditional jump if a < b goto L1 jump only when the comparison holds

Example 1: an expression

Translate x = a + b * c - d. Multiplication binds tighter than + and -, and + and - associate left to right, so b * c is evaluated first:

t1 = b * c
t2 = a + t1
t3 = t2 - d
x = t3

Each temporary holds exactly one subexpression: t1 is b * c, t2 is a + b * c, t3 is the whole right-hand side. The final copy assigns it to x. Parentheses change only the order: x = a - (b - c) gives t1 = b - c, t2 = a - t1, x = t2.

Example 2: unary minus (Dragon Book 6.10)

The classic textbook example is a = b * -c + b * -c:

t1 = minus c
t2 = b * t1
t3 = minus c
t4 = b * t3
t5 = t2 + t4
a = t5

Notice that b * -c is computed twice. Straightforward translation doesn't notice repetition; the DAG (below) fixes that.

Quadruples

A quadruple stores an instruction as four fields: op, arg1, arg2 and result. Example 2 becomes:

# op arg1 arg2 result
(0) minus c t1
(1) * b t1 t2
(2) minus c t3
(3) * b t3 t4
(4) + t2 t4 t5
(5) = t5 a

Because each result is named explicitly, an optimiser can move quadruples around freely.

Triples

A triple drops the result field. A later instruction refers to an earlier value by its position, written (n):

# op arg1 arg2
(0) minus c
(1) * b (0)
(2) minus c
(3) * b (2)
(4) + (1) (3)
(5) = a (4)

Temporaries disappear, which saves space, but moving a triple breaks every reference to its number.

Indirect triples

Indirect triples keep the triples fixed and add a separate list of pointers to them, conventionally numbered from 35 as in the Dragon Book: 35 → (0), 36 → (1), and so on. An optimiser reorders the pointer list; the triples never move, so their (n) references stay valid.

Example 3: if-else and while

Conditions become jumps. if (a < b) x = 1 else x = 2 translates to:

    if a < b goto L1
    goto L2
L1: x = 1
    goto L3
L2: x = 2
L3:

A while loop puts a label at the test and jumps back to it at the end of the body:

L1: if i < n goto L2
    goto L3
L2: t1 = i + 1
    i = t1
    goto L1
L3:

Many university exams number instructions instead of using labels, starting at 100. The same loop then reads 100: if i < n goto 102, 101: goto 105, and so on, with each jump pointing at an instruction number.

With && and ||, only as much of the condition is tested as needed: in x < 100 || x > 200, the second test is skipped whenever the first is true. That short-circuit behaviour is why the jumping-code translation has a true exit and a false exit for every condition.

The DAG: computing each value once

A directed acyclic graph (DAG) for a basic block gives each distinct value a single node. In Example 2, b * -c becomes one node used twice, so code regenerated from the DAG is three instructions instead of six:

t1 = minus c
t2 = b * t1
a = t2 + t2

DAGs also notice when two variables end up holding the same value. In the block a = b + c, b = a - d, c = b + c, d = a - d, the last line recomputes a - d, already held by b, so the regenerated code ends with the copy d = b.

Try it

The Three Address Code Generator produces all of the above for your own input: three-address code with a note on what each temporary holds, quadruples, triples, indirect triples, the syntax tree and the DAG, with labels or instruction numbers starting at 100. The examples in this guide are the ones it reproduces from the Dragon Book (Aho, Lam, Sethi and Ullman).

Use the Basic Blocks and Flow Graph to explore the control flow of three-address code. For a related walkthrough, read FIRST and FOLLOW Sets Example, Step by Step.

Looking for other tools?

Explore ToolSphere Homepage →