Intermediate Code Generation Mistakes: 7 Mark-Losing Errors and Exact Fixes
Seven tempting shortcuts can corrupt an otherwise neat intermediate-code answer. Trace each wrong result, then apply the exact repair for TAC, arrays, records, jumps, loops and expression reuse.
KnowledgeGate Team
Exam prep & CS education

You can lose an otherwise neat intermediate-code answer through one unchecked assumption: precedence, operand width, record references, branch targets or expression availability. A wrong trace can look orderly, so compare each value and jump with the rule that produces it. The Intermediate Code Generation Formula Sheet: TAC, Quadruples and Backpatching Revision Notes is the compact reference for forms and formulas; use it before tracing the seven error patterns here.
1. Mistake 1: Generating TAC from left-to-right reading instead of the parse
Why it happens. You scan the expression as text and emit a temporary as soon as you see an operator.
What goes wrong. Precedence and associativity disappear. For x = a + b * c - d / e, use a=8, b=3, c=4, d=10, e=2. Correct postorder TAC gives:
t1 = b*c = 12
t2 = a+t1 = 20
t3 = d/e = 5
t4 = t2-t3 = 15
x = t4The wrong left-to-right chain gives u1=a+b=11, u2=u1*c=44, u3=u2-d=34, u4=u3/e=17, then x=u4.
What to do instead. Add implied parentheses or sketch the AST, then emit one temporary per operator in postorder. The numerical check is 15, never 17.
2. Mistake 2: Packing several operations into one three-address statement
Why it happens. You copy a source-level subexpression and only add a temporary name.
What goes wrong. t1=-(p+q)*r contains several operators, so it is not three-address code and cannot become reliable records. For y = -(p + q) * r, with p=2, q=5, r=3, write:
t1 = p+q = 7
t2 = uminus t1 = -7
t3 = t2*r = -21
y = t3uminus is one unary operator, not an operator needing a second operand.
What to do instead. Count operator nodes, keep at most one operator on each right-hand side, and retain the final copy when assignments are counted. The Intermediate Code Generation in Compiler Design: Three-Address Code, Quadruples, Triples and Worked Examples lays out the full instruction set and representations; this check targets the packed-expression error.
3. Mistake 3: Using the wrong dimension or forgetting element width in array addresses
Why it happens. Row count, column count, index and byte width get mixed into one memorised formula.
What goes wrong. For zero-based row-major A[4][5], base 1000, 4-byte integers, i=2 and j=3, the linear index is 2*5+3=13, displacement is 13*4=52, and address is 1052:
t1 = i*5 = 10
t2 = t1+j = 13
t3 = t2*4 = 52
t4 = 1000+t3 = 1052
x = *t4Multiplying by 4 rows produces 1000+((2*4)+3)*4=1044. Omitting byte width produces 1013. Both errors look orderly because every temporary is still well formed.
What to do instead. Before TAC, write the layout, lower bound, varying dimension and element width. Here the multiplier is 5 columns, followed by 4 bytes.
4. Mistake 4: Mixing temporary names and instruction references across record formats
Why it happens. Quadruples and triples are remembered as interchangeable tables.
What goes wrong. For x=(a-b)*(c+d), with a=9, b=4, c=2, d=6, the value is 5*8=40. TAC is t1=a-b, t2=c+d, t3=t1*t2, x=t3.
Quadruples use (op,arg1,arg2,result):
(-,a,b,t1)
(+,c,d,t2)
(*,t1,t2,t3)
(=,t3,-,x)Triples use (op,arg1,arg2) and producing-row references:
0: (-,a,b)
1: (+,c,d)
2: (*,(0),(1))
3: (=,(2),x)A correct number does not rescue the wrong table convention.
What to do instead. Write the table header first. Quadruples name results; triples refer to the rows that produced them.
5. Mistake 5: Evaluating a Boolean expression as a value when jumps are required

Why it happens. You treat relational operators like arithmetic operators.
What goes wrong. t1=a<b; t2=c<d; t3=t1&&t2 hides short-circuit flow. It also fails to expose the lists that backpatching must join. For if (a < b && c < d) x=1; else x=0, use a=2, b=5, c=7, d=3:
100: if a<b goto 102
101: goto 106
102: if c<d goto 104
103: goto 106
104: x=1
105: goto 107
106: x=0
107: nextThe first comparison is true and the second is false, so execution follows 100 -> 102 -> 103 -> 106 -> 107 and finishes with x=0.

What to do instead. Patch the first comparison's true exit to the second comparison. Merge both false exits for the else label, then patch the second comparison's true exit to the then label.
6. Mistake 6: Guessing branch targets before the final instruction layout
Why it happens. You fill numeric targets while instructions are still being inserted.
What goes wrong. For i=1, s=0; while (i<3) { s=s+i; i=i+1; }, the correct layout is:
200: if i<3 goto 202
201: goto 207
202: t1=s+i
203: s=t1
204: t2=i+1
205: i=t2
206: goto 200
207: nextAt the condition, (i,s) is (1,0), then (2,1), then (3,3). The loop exits with s=3. Sending the false branch to 206 repeatedly returns to the failed test. Using 202 as the back edge repeats the body without retesting.
What to do instead. Use symbolic labels or makelist, merge and backpatch. Assign instruction numbers only after the layout is final.
7. Mistake 7: Reusing or optimising an expression after an operand changes
Why it happens. Identical text looks like an available common subexpression. Textual equality alone proves nothing about availability.
What goes wrong. Start with a=4, b=6: t1=a+b gives 10; a=a+1 makes a=5; t2=a+b must give 11. Replacing t2 with t1 returns 10, because redefining a kills the earlier expression.
What to do instead. Generate unoptimised TAC first and optimise only when asked. Invalidate an expression whenever either operand may be redefined on the path. The Code Optimization Mistakes in Compiler Design: How to Avoid Losing GATE Marks examines multi-block legality and other optimisation failures; this check stops at the first invalid reuse.
8. Reverse-trace the final answer before you submit
Read the last assignment backwards. For each temporary, confirm its producing operation, then check the operand values, array stride, record references and branch target that feed it. In a Boolean or loop trace, follow both one true path and one false or exit path; in an optimisation question, mark every redefinition before reusing a value.
With the worked values above, the checkpoints are expression result 15, array address 1052, Boolean result x=0 and loop result s=3. Practise the full compiler sequence in the Compiler Design course.
Keep learning
Run-Time Environment Mistakes: 7 Mark-Losing Errors and Exact Fixes
Definitions are not enough when a trace depends on storage duration, calling order, lexical nesting and frame-address rules. Repair seven common errors through small, checkable examples.
Lexical Analysis Mistakes for GATE: 7 Mark-Losing Errors and Exact Fixes
These seven repairs show exactly where lexical analysis answers go wrong. Trace one shared source, separate token counts from lexemes and categories, and keep each compiler phase in its lane.
Control Flow in C: 7 Mark-Losing Mistakes and Precise Fixes
Trace seven common C control-flow mistakes step by step, see exactly where execution changes course, and learn a short routine for checking every branch and loop.
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.





