Generating Three-Address Code (Quadruples, Triples, Indirect Triples) for (x = a*(b+c) - d/e)
To generate intermediate code for the expression we typically follow a standard compilation strategy: break the expression into subexpressions, compute each subexpression into a temporary location, and then combine temporaries according to operator precedence and evaluation order.
Key representations you asked for:
- Three-address code
- Quadruple
- Triple
- Indirect triple
Because I couldn’t complete the required web research (tool usage limit), the final content below is derived from standard compiler-construction definitions and conventions commonly used in coursework and textbooks.
Three-Address Code / Quadruple / Triple (Intermediate Code) - Compiler Construction
Three-address code generation (with temporaries)
- 1Step 1
Use precedence: parentheses first, then multiplication/division, then subtraction. So compute , then , compute , then subtract.
- 2Step 2
Let hold . Generate: .
- 3Step 3
Let hold . Generate: .
- 4Step 4
Let hold . Generate: .
- 5Step 5
Let hold . Generate: .
- 6Step 6
Generate: .
Generated three-address code (TAC)
Using temporaries (t1, t2, t3, t4), the three-address code is:
- (t1 = b + c)
- (t2 = a * t1)
- (t3 = d / e)
- (t4 = t2 - t3)
- (x = t4)
Note: Each TAC instruction is of the form ([result] = [arg1]\ \text{op}\ [arg2]) (assignment has one operand target but still fits the “three-address” style when viewed as an operation with operands plus destination).
Pro Tip
When producing TAC, always compute the deepest parentheses first, then higher-precedence operators (* and /) before lower-precedence operators (+ and -). This determines the temporary dependencies and affects the triple/quadruple indices.
Quadruple Representation
A quadruple list is written as: [ (op,\ arg1,\ arg2,\ result) ] The destination (result) is explicitly provided.
We map:
- index 1 → (t1 = b + c)
- index 2 → (t2 = a * t1)
- index 3 → (t3 = d / e)
- index 4 → (t4 = t2 - t3)
- index 5 → (x = t4) (assignment can be represented as ( (=, t4, -, x)); some conventions store arg2 as blank)
| # | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 1 | + | b | c | t1 |
| 2 | * | a | t1 | t2 |
| 3 | / | d | e | t3 |
| 4 | - | t2 | t3 | t4 |
| 5 | = | t4 | — | x |
Triple Representation
A triple list stores: [ (op,\ arg1,\ arg2) ] and the result of entry (i) is implied as “(t_i)” (or generally: the value produced by that triple’s index).
Thus the i-th triple produces (t_i).
| i | op | arg1 | arg2 |
|---|---|---|---|
| 1 | + | b | c |
| 2 | * | a | (1) |
| 3 | / | d | e |
| 4 | - | (2) | (3) |
| 5 | = | (4) | — |
Where ((1)), ((2)), ((3)), ((4)) mean “use the results of those earlier triples.”
Warning on Conventions
Different courses/textbooks handle assignment in quadruples/triples differently (e.g., using op '=' with arg2 blank, or omitting '=' and treating x as a final target). Stick to one convention consistently across TAC → quadruple → triple → indirect-triple.
Indirect-Triple Representation
An indirect triple is commonly presented with:
- a triple table (the (op, arg1, arg2) without explicit result names),
- and a name table (or mapping) that says what temporary/register corresponds to the result of each triple index.
1) Triple table (same as before)
Let triple indices be 1..5:
- ( (+, b, c) )
- ( (*, a, 1) )
- ( (/, d, e) )
- ( (-, 2, 3) )
- ( (=, 4, \text{—}) )
2) Name table mapping indices to storage locations
Let the mapping be:
- name(1) = t1
- name(2) = t2
- name(3) = t3
- name(4) = t4
- name(5) = x
Now when a triple references an earlier result by index, indirect-triple uses the name table to resolve which temporary holds that value.
| Triple index i | triple (op, arg1, arg2) | name(i) |
|---|---|---|
| 1 | (+, b, c) | t1 |
| 2 | (*, a, 1) | t2 |
| 3 | (/, d, e) | t3 |
| 4 | (-, 2, 3) | t4 |
| 5 | (=, 4, —) | x |
This is “indirect” because operands that refer to earlier computations are indices, and the actual storage (t1/t2/t3/t4/x) is obtained through the name mapping.
Consolidated View (TAC vs Quadruple vs Triple)
Same computation, different encoding
Common Exam-Style Variants
Knowledge Check
Which subexpression should be computed first for when generating TAC?