(Compiler Construction — exam-ready, simple explanation with examples, rules, and key points)
Three Address Code (TAC) is an intermediate representation (IR) used in compilers where each instruction has at most three addresses (operands).
👉 It breaks complex expressions into simple step-by-step instructions.
x = y op z
Where:
x → resulty, z → operandsop → operator (+, -, *, /, etc.)✔ Simplifies complex expressions ✔ Helps in optimization ✔ Easy to convert into machine code ✔ Machine-independent
TAC supports different types of statements:
x = y
x = y + z
x = -y
if x < y goto L1
goto L1
a = b + c * d
t1 = c * d
t2 = b + t1
a = t2
👉 Multiplication first, then addition
x = (a + b) * (c - d)
t1 = a + b
t2 = c - d
t3 = t1 * t2
x = t3
if (a < b && c < d)
if a < b goto L1
goto L2
L1: if c < d goto L3
goto L2
L3: ...
if (x > 0)
y = 1;
if x > 0 goto L1
goto L2
L1: y = 1
L2:
if (x > 0) y = 1;
else y = 2;
if x > 0 goto L1
goto L2
L1: y = 1
goto L3
L2: y = 2
L3:
while (x < 10)
x = x + 1;
L1: if x >= 10 goto L2
x = x + 1
goto L1
L2:
Format:
(op, arg1, arg2, result)
| op | arg1 | arg2 | result |
|---|---|---|---|
| * | c | d | t1 |
| + | b | t1 | t2 |
| = | t2 | a |
Format:
(index) op arg1 arg2
| i | op | arg1 | arg2 |
|---|---|---|---|
| 0 | * | c | d |
| 1 | + | b | (0) |
| 2 | = | (1) | a |
| Feature | High-Level Code | TAC |
|---|---|---|
| Complexity | High | Simple |
| Structure | Complex | Linear |
| Optimization | Hard | Easy |
| Machine dependency | No | No |
✔ Simplifies expressions ✔ Easier optimization ✔ Helps code generation ✔ Machine-independent
❌ Needs extra memory (temporary variables) ❌ Not executable directly ❌ Requires further translation
👉 Frequently asked:
| Aspect | Three Address Code |
|---|---|
| Type | Intermediate representation |
| Format | x = y op z |
| Purpose | Simplify code |
| Output | Linear instructions |
| Use | Optimization & code generation |
| Machine dependency | No |
Open this section to load past papers