Translate a=(a+b) (c+d)+(a+b+c)a = (a + b)\,(c + d) + (a + b + c) into Quadruple, Triple, and Indirect Triple

Translate a=(a+b) (c+d)+(a+b+c)a = (a + b)\,(c + d) + (a + b + c) into Quadruple, Triple, and Indirect Triple

Verified Sources
Nov 3, 2026

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:

a=(a+b) (c+d)+(a+b+c)a = (a+b)\,(c+d) + (a+b+c)

We compute subexpressions in a consistent evaluation order (typically following operator precedence: parentheses first, then ∗*, then ++).

Key keyword{temporaries} like t1,t2,…t1,t2,\dots 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:

  • (a+b)(a+b)
  • (c+d)(c+d)
  • (a+b+c)(a+b+c) which can be expanded as (a+b)+c(a+b)+c (since ++ is associative)

Then the full expression becomes:

a=(a+b) (c+d)⏟term1+(a+b+c)⏟term2a = \underbrace{(a+b)\,(c+d)}_{\text{term1}} + \underbrace{(a+b+c)}_{\text{term2}}

We will generate intermediate operations in this order:

  1. t1=a+bt1 = a+b
  2. t2=c+dt2 = c+d
  3. t3=t1∗t2t3 = t1*t2
  4. t4=a+bt4 = a+b
  5. t5=t4+ct5 = t4+c
  6. t6=t3+t5t6 = t3+t5
  7. a=t6a = t6

Note: Recomputing (a+b)(a+b) twice (steps 1 and 4) is normal unless you perform common subexpression elimination.

Build intermediate code (evaluation order and operator precedence)

  1. 1
    Step 1

    Create an intermediate result for (a+b)(a+b); denote it as t1t1 in quadruples (or an implicit triple index).

  2. 2
    Step 2

    Create an intermediate result for (c+d)(c+d); denote it as t2t2.

  3. 3
    Step 3

    Multiply the two previous intermediates to form t3t3.

  4. 4
    Step 4

    Compute (a+b)(a+b) again (or reuse if optimized) and then add cc to get t5t5.

  5. 5
    Step 5

    Compute t6=t3+t5t6 = t3 + t5.

  6. 6
    Step 6

    Set a=t6a = t6.

(i) Quadruple representation

Quadruple format is:

(op, arg1, arg2, result)(op,\ arg1,\ arg2,\ result)

We number temporaries explicitly as t1,t2,…t1,t2,\dots:

#oparg1arg2result
1+abt1
2+cdt2
3*t1t2t3
4+abt4
5+t4ct5
6+t3t5t6
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:

(op, arg1, arg2)(op,\ arg1,\ arg2)

and the “result” is the index of that triple. So if triple 3 is ( ∗, t1, t2)(\ *,\ t1,\ t2), then its computed value is referenced as (3)(3).

We list triples T1T1 … T7T7 (index-based):

Triple indexoparg1arg2
1+ab
2+cd
3*(1)(2)
4+ab
5+(4)c
6+(3)(5)
7=(6)-

Here:

  • triple 11 computes t1t1
  • triple 22 computes t2t2
  • triple 33 computes t3t3
  • triple 44 computes t4t4
  • triple 55 computes t5t5
  • triple 66 computes t6t6
  • triple 77 assigns a=(6)a=(6)

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:

  1. a triple table (same (op,arg1,arg2)(op,arg1,arg2) entries as triples), and
  2. an keyword{indirection table} that maps temporaries/identifiers to indices.

A common presentation:

  • You still create triples T1…T1\dots
  • 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 I[ ]I[\ ] gives which triple index a temporary points to

Triple table (base)

We reuse the triple table from (ii):

Triple indexoparg1arg2
1+ab
2+cd
3*(1)(2)
4+ab
5+(4)c
6+(3)(5)
7=(6)-

Indirection table (example)

Let temporaries correspond to the produced triple indices:

  • t1→1t1 \rightarrow 1
  • t2→2t2 \rightarrow 2
  • t3→3t3 \rightarrow 3
  • t4→4t4 \rightarrow 4
  • t5→5t5 \rightarrow 5
  • t6→6t6 \rightarrow 6

Then an “indirect triple” operand is effectively “use the value produced by the triple index stored in I[t]I[t]”.

So the operand references in the indirection style are:

  • Use I[t1]=1I[t1]=1 instead of writing t1t1
  • Use I[t2]=2I[t2]=2 instead of writing t2t2
  • etc.

A compact indirection representation for the final computation is:

  • triple 6 computes t6t6: (6): + (I[t3]) (I[t5])(6):\ +\ \big(I[t3]\big)\ \big(I[t5]\big)
  • assignment triple 7 stores into aa: (7): = (I[t6])(7):\ =\ \big(I[t6]\big)

Mermaid view (what indirection changes)

Common pitfalls and how to avoid them

From expression to intermediate code

Parse & apply precedence

1

Evaluate parentheses, then multiplication, then addition."

Generate temporaries

2

Create t1,t2,…t1,t2,\dots for intermediate results."

Emit quadruples

3

Store (op,arg1,arg2,result)."

Emit triples

4

Store (op,arg1,arg2) and reference results by index."

Emit indirect triples

5

Use 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

1 / 4
Question · Term

Quadruple format

Click to reveal
Answer · Definition

(op, arg1, arg2, result) — destination is explicit per line.

Knowledge Check

Question 1 of 4
Q1Single choice

In quadruple representation, which field contains the destination variable?