Let's break down the expression and construct Three Address Code (TAC) and represent it in:
1. Quadruple (or "Quartet") format
2. Triple format
3. Indirect triple format
Given Expression: a = b + c * (d - e)
1. t1 = d - e
2. t2 = c * t1
3. t3 = b + t2
4. a = t3
t1 = d - e
t2 = c * t1
t3 = b + t2
a = t3
Each line represents one operation with at most three addresses (two operands and one result).
2. Quadruple (or Quartet) RepresentationIndex | Operator | Arg1 | Arg2 | Result |
0 | - | d | e | t1 |
1 | * | c | t1 | t2 |
2 | + | b | t2 | t3 |
3 | = | t3 | - | a |
- Each instruction is stored with four fields.
- Easy to optimize as all results are named (no ambiguity).
3. Triple RepresentationIndex | Operator | Arg1 | Arg2 |
0 | - | d | e |
1 | * | c | (0) |
2 | + | b | (1) |
3 | = | (2) | - |
Instead of variable names like t1, t2, etc., this format uses position indexes.
More compact, but harder to modify if instructions are moved.
4. Indirect Triple RepresentationAn indirect triple maintains a separate list of instruction pointers (indexes), which reference the triple table.
Instruction Pointer Table:
Index | Operator |
0 | ->(0) |
1 | ->(1) |
2 | ->(2) |
3 | ->(3) |
Triple Table (same as above):
Index | Operator | Arg1 | Arg2 |
0 | - | d | e |
1 | * | c | (0) |
2 | + | b | (1) |
3 | = | (2) | - |
Indirect triples allow easier code reordering or optimization, since the instruction pointer list can change without modifying the instructions themselves.