Intermediate Code Generation Formula Sheet: TAC, Quadruples and Backpatching Revision Notes
Revise intermediate code generation through compact formulas, representation tables and verified traces. Follow one expression from TAC to DAG reuse, then patch a Boolean branch exactly.
KnowledgeGate Team
Exam prep & CS education

You remember TAC, quadruples, triples and backpatching, but under pressure mix row formats, temporary counts and jump lists. This sheet gives one formula set, traced expression and exact backpatching trace. Since conventions vary, name yours before counting rows or temporaries.
Intermediate Code Generation Formula Sheet at a Glance
The pipeline is:
source program → syntax tree or DAG → intermediate code → machine-independent optimisation → target code
An intermediate representation (IR) bridges front end and back end; TAC is one linear IR. With four sources and three targets, direct translation needs 4 × 3 = 12 combinations; a shared IR needs 4 + 3 = 7 front-end and back-end components.
Term | Quick recall |
|---|---|
TAC | At most one right-hand-side operator per emitted instruction. |
Temporary | Stores an intermediate result; count emitted names. |
Quadruple |
|
Triple |
|
Indirect triple | A pointer list sets order; changing pointers reorders independent work. |
Need full teaching? Read the complete guide to intermediate code generation.
TAC Forms and Syntax-Directed Translation Rules
TAC family | Form or example |
|---|---|
Binary |
|
Unary |
|
Copy |
|
Unconditional jump |
|
Conditional jump |
|
Indexed load and store |
|
Address and pointer |
|
Procedure sequence |
|
Here, n counts arguments; || means code concatenation, not Boolean OR:
E → E1 op E2:E.place = newtemp()andE.code = E1.code || E2.code || gen(E.place = E1.place op E2.place)E → id:E.place = idandE.code = εS → id = E:S.code = E.code || gen(id = E.place)
See syntax-directed translation worked examples for dependency order.
For y = -a + b * c, a = 4, b = 2, c = 6: t1 = uminus a = -4, t2 = b * c = 12, t3 = t1 + t2 = 8, y = t3. Convention: 4 rows, 3 temporaries; unary minus gets its own row.
Worked Example: Expression to TAC, DAG, Quadruples and Triples
Use x = (a + b) * (c - d) + (a + b) with a = 2, b = 3, c = 9, d = 5.
Naive tree TAC: 6 rows, 5 temporaries
t1 = a + b = 2 + 3 = 5t2 = c - d = 9 - 5 = 4t3 = t1 * t2 = 5 * 4 = 20t4 = a + b = 2 + 3 = 5t5 = t3 + t4 = 20 + 5 = 25x = t5 = 25
DAG-reuse TAC: 5 rows, 4 temporaries
t1 = a + b = 5t2 = c - d = 4t3 = t1 * t2 = 20t4 = t3 + t1 = 25x = t4 = 25
Reuse is safe because a and b do not change.
The optimised representations are:
Row | Quadruple | Triple |
|---|---|---|
0 |
|
|
1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
Triple (0) means row 0's result. Pointers start [0, 1, 2, 3, 4]. Rows 0 and 1 are independent, so [1, 0, 2, 3, 4] is valid with later references unchanged.

Array, Pointer and Procedure-Call Formulas
Definitions: base(A) is base address, L1/L2 lower bounds, w element width, and N1/N2 row and column counts.
Layout | Address formula |
|---|---|
One dimension |
|
Two-dimensional row major |
|
Two-dimensional column major |
|
For zero-based 4 × 5 array A, let base(A) = 1000, w = 4; find A[2][3]:
Row major:
((2 × 5) + 3) × 4 = 13 × 4 = 52, so the address is1000 + 52 = 1052.Column major:
((3 × 4) + 2) × 4 = 14 × 4 = 56, so the address is1000 + 56 = 1056.
Row-major TAC: t1 = 2 * 5 = 10, t2 = t1 + 3 = 13, t3 = t2 * 4 = 52, t4 = 1000 + t3 = 1052, x = *t4. Index 13 is not byte offset 52.
Call mnemonic: param a; param b; t = call f, 2; return t. Do not invent unstated conventions.
Boolean Translation and Backpatching Rules
makelist(i) creates a one-jump list, merge(p1,p2) combines unresolved lists, and backpatch(p,i) fills target i into each jump in p.
B1 && B2: patchB1.truelistto the first instruction ofB2; true list isB2.truelist; false list ismerge(B1.falselist, B2.falselist).B1 || B2: patchB1.falselistto the first instruction ofB2; true list ismerge(B1.truelist, B2.truelist); false list isB2.falselist.!B1: swap the true and false lists.
For a = 7, b = 4, c = 2, d = 5, trace if (a < b || c < d) x = 1; else x = 0:
100: if a < b goto _
101: goto _
102: if c < d goto _
103: goto _
104: x = 1
105: goto _
106: x = 0
107: nextPatch [101] → 102, true list [100,102] → 104, false list [103] → 106, and [105] → 107. Path: 100 false → 101 → 102 true → 104 → 105 → 107; hence x = 1.

How GATE CS Tests Intermediate Code Generation
The official GATE 2024 Computer Science and Information Technology syllabus lists syntax-directed translation, runtime environments and intermediate code generation under Compiler Design. It assigns no fixed marks or yearly weightage to the topic.
Practice asks for TAC counts, representation conversion, safe reuse, array offsets, backpatch lists and SDT dependency order. Place these within your wider GATE CS preparation.
In 60 seconds: circle operators and side effects, state the convention, emit one operator per row, mark temporaries, check redefinitions before DAG reuse, then convert.
Common Mistakes and the Two-Minute Final Check
Mistake | Correction | ||||
|---|---|---|---|---|---|
Hiding unary minus inside a binary row | Emit a separate unary instruction. | ||||
Equating instructions with temporaries | Count rows and | ||||
Reusing after an operand changes | Check intervening definitions and side effects. | ||||
Reading a quadruple result as a row reference | Quadruples name results; triples reference rows. | ||||
Moving triple rows without fixing references | Reorder the indirect-triple pointer list instead. | ||||
Forgetting array element width | Find the element index, then byte offset. | ||||
Patching | ` |
| ` continues on false. | ||
Forcing every Boolean to | Control-flow translation can branch directly. |
Final check: a + b = 5, result 25, DAG reuse 5 rows and 4 temporaries, no operand redefinition. Trace, table and diagram must agree.
Short Version and the Next Revision Step
At most one right-hand-side operator per TAC row.
placenames a result;codeconcatenates emitted rows.Quadruples name results.
Triples reference row numbers.
DAGs reuse only safe common subexpressions.
Backpatching fills unresolved jump targets.
Concept gap? Continue with GATE Guidance by Sanchit Sir. Ready for timed practice? Use the GATE Test Series, then study code optimisation worked traces.
Cover the formulas, regenerate the main TAC and backpatch rows from memory, then check every count and target.
Keep learning

Grammar and CFG Formula Sheet: Rules, Worked Example and GATE Revision Notes
Revise CFGs in problem-solving order through precise rules, use cues, one consistent worked grammar, an LL(1) table, normal forms and a final audit routine.

Grammar and CFG Mistakes for GATE: 7 Traps and How to Fix Them
Seven small grammars expose the errors that make CFG answers go wrong, with reproducible checks for classification, ambiguity, FIRST sets and CNF.

Code Optimization Mistakes in Compiler Design: How to Avoid Losing GATE Marks
A rewrite that looks familiar is not necessarily safe. Trace six common optimization errors, learn why each one changes program meaning, and use a five-step check before accepting an answer.

Compiler Design Semester Guide: Trace One Program from Tokens to Target Code
Follow one assignment through lexical analysis, parsing, semantic checks, TAC, optimization and abstract target instructions, with every hand-off made visible.