Translate into Quadruples and Triples
Intermediate Code: Quadruples vs Triples (3-Address Code)
Given the expression we translate it into intermediate representations:
- keyword: An array of instructions of form .
- keyword: An array of instructions of form where results are referenced by index.
- keyword: IR where each instruction uses at most two operands plus an implicit destination.
- keyword: A symbol (e.g., ) storing intermediate results.
- keyword: Rule that multiplication/division bind tighter than addition/subtraction.
A standard evaluation order that respects precedence is:
- Compute the parenthesized sum .
- Multiply by and apply the unary minus.
- Divide by .
- Assign to .
The key question is how unary minus is represented:
- As a separate keyword: Often implemented as or as . Below, we use an explicit unary operation (common in textbooks/exercises), but I also show the alternative form using where needed.
Mermaid view of the data flow:
Construct triples (then map to quadruples)
- 1Step 1
Create a temp for (call it ).
- 2Step 2
Compute , then compute the unary negation .
- 3Step 3
Compute .
- 4Step 4
Final instruction sets .
Triples Representation
In triples form, each instruction is stored as: and later instructions reference prior results by index. If the triple number is , its value can be referenced as .
We will build 4 computation triples (then a final assignment as a 5th “triple-like” form).
Let:
- Triple 1:
- Triple 2:
- Triple 3: (unary negation)
- Triple 4:
- Triple 5: (assignment)
Triples list
- ⟹ value is
- ⟹
- ⟹
- ⟹
- ⟹ assign
Note: Some conventions encode unary neg as with a dummy second field. If your course uses instead, see the alternative in the accordion.
Quadruples Representation
In quadruples form, each instruction is:
We explicitly name temporaries as results of each instruction:
Quadruples table
| # | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 1 | + | c | d | t1 |
| 2 | * | b | t1 | t2 |
| 3 | neg | t2 | — | t3 |
| 4 | / | t3 | e | t4 |
| 5 | = | A | t4 | A |
This is equivalent to:
type="tip" title="Pro Tip: Choose a consistent unary minus convention" content="If your course disallows unary ops, rewrite negation as (−, 0, x). Then every operation becomes binary, making quadruples/triples uniform."
type="warning" title="Warning: Parentheses must be computed first" content="If you generate code that computes before , you violate the precedence implied by . Always start with the subexpression inside parentheses."
1: (+, c, d) 2: (*, b, [1]) 3: (neg, [2], _) 4: (/, [3], e) 5: (=, A, [4])
Common variations (to match your course format)
Computation Order Breakdown
How the IR instructions map to subexpressions
Expression Lowering Pipeline
Lower (c + d)
1Create a temp: "
Lower b * (c + d)
2Create temp: "
Lower unary minus
3Create temp: "
Lower division by e
4Create temp: "
Assign to A
5Final: "
Quadruples/Triples Quick Check
Knowledge Check
In quadruple form, which tuple includes the destination/result field explicitly?