Translate into Quadruple, Triple, and Indirect Triple
We translate the expression into three common intermediate-code formats used in compiler design: keyword-style Quadruple , keyword-style Triple , and keyword-style Indirect Triple.
Let the target expression be:
We compute subexpressions in a consistent evaluation order (typically following operator precedence: parentheses first, then , then ).
Key keyword{temporaries} like will appear as results in the Quadruple form and implicitly as indices in the Triple form.
Compiler Intermediate Code: Quadruples and Triples (Triple Representation Basics)
Expression decomposition
We identify the parenthesized parts:
- which can be expanded as (since is associative)
Then the full expression becomes:
We will generate intermediate operations in this order:
Note: Recomputing twice (steps 1 and 4) is normal unless you perform common subexpression elimination.
Build intermediate code (evaluation order and operator precedence)
- 1Step 1
Create an intermediate result for ; denote it as in quadruples (or an implicit triple index).
- 2Step 2
Create an intermediate result for ; denote it as .
- 3Step 3
Multiply the two previous intermediates to form .
- 4Step 4
Compute again (or reuse if optimized) and then add to get .
- 5Step 5
Compute .
- 6Step 6
Set .
(i) Quadruple representation
Quadruple format is:
We number temporaries explicitly as :
| # | op | arg1 | arg2 | result |
|---|---|---|---|---|
| 1 | + | a | b | t1 |
| 2 | + | c | d | t2 |
| 3 | * | t1 | t2 | t3 |
| 4 | + | a | b | t4 |
| 5 | + | t4 | c | t5 |
| 6 | + | t3 | t5 | t6 |
| 7 | = | t6 | - | a |
Notes
- For assignment, we often write with one meaningful operand; arg2 can be shown as “-”.
- This form makes the destination explicit each row.
(ii) Triple representation
Triple format stores:
and the “result” is the index of that triple. So if triple 3 is , then its computed value is referenced as .
We list triples … (index-based):
| Triple index | op | arg1 | arg2 |
|---|---|---|---|
| 1 | + | a | b |
| 2 | + | c | d |
| 3 | * | (1) | (2) |
| 4 | + | a | b |
| 5 | + | (4) | c |
| 6 | + | (3) | (5) |
| 7 | = | (6) | - |
Here:
- triple computes
- triple computes
- triple computes
- triple computes
- triple computes
- triple computes
- triple assigns
keyword{Index referencing} is why there is no explicit “result” field per row.
type="tip" title="Pro Tip: Keep (a+b) consistent" content="If your course expects reuse, you can avoid recomputing (a+b) by reusing triple/quadruple results. The tables above recompute (a+b) twice because that’s the simplest translation without optimization."
(iii) Indirect triple representation
Indirect Triple is typically implemented using:
- a triple table (same entries as triples), and
- an keyword{indirection table} that maps temporaries/identifiers to indices.
A common presentation:
- You still create triples
- But when an operand refers to a temporary, you store a pointer to the triple entry rather than the temporary name itself.
A clear way to show it is:
- triple entries are the same as part (ii)
- an indirection array gives which triple index a temporary points to
Triple table (base)
We reuse the triple table from (ii):
| Triple index | op | arg1 | arg2 |
|---|---|---|---|
| 1 | + | a | b |
| 2 | + | c | d |
| 3 | * | (1) | (2) |
| 4 | + | a | b |
| 5 | + | (4) | c |
| 6 | + | (3) | (5) |
| 7 | = | (6) | - |
Indirection table (example)
Let temporaries correspond to the produced triple indices:
Then an “indirect triple” operand is effectively “use the value produced by the triple index stored in ”.
So the operand references in the indirection style are:
- Use instead of writing
- Use instead of writing
- etc.
A compact indirection representation for the final computation is:
- triple 6 computes :
- assignment triple 7 stores into :
Mermaid view (what indirection changes)
Common pitfalls and how to avoid them
From expression to intermediate code
Parse & apply precedence
1Evaluate parentheses, then multiplication, then addition."
Generate temporaries
2Create for intermediate results."
Emit quadruples
3Store (op,arg1,arg2,result)."
Emit triples
4Store (op,arg1,arg2) and reference results by index."
Emit indirect triples
5Use pointers/indirection to reference triple results."
Number of generated operations
Operations count for each representation (for this translation order).
Quadruple vs Triple vs Indirect Triple
Knowledge Check
In quadruple representation, which field contains the destination variable?