
(Compiler Construction β exam-ready, simple explanation with examples, rules, and key points)
(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 view and download past papers
Click on Show Past Papers to load available exam papers.