DEV Community

GTU Hero
GTU Hero

Posted on

Three Address Code, Quadruples, Triples and Indirect Triples: One Expression, Every Representation

Most compiler courses spend weeks on parsing and then rush intermediate code generation in one lecture. But in the GTU Compiler Design exam it is an easy, almost mechanical question, if you know the format. Let us take one expression and write it in all four forms side by side.

Disclosure: I help build GTU Hero (link at the end). The expressions below are standard practice examples and the tables are my own working.

Why intermediate code at all

Without an intermediate form, M languages and N machines need M x N compilers; with one, M front ends plus N back ends. Three address code (TAC) is the classic choice because every instruction has at most one operator on the right side, which maps neatly to machine instructions and makes optimisation easy.

The practice expression

a = b * -c + b * -c
Enter fullscreen mode Exit fullscreen mode

The unary minus binds tighter than *, and * binds tighter than +.

Step 1: three address code

t1 = minus c
t2 = b * t1
t3 = minus c
t4 = b * t3
t5 = t2 + t4
a  = t5
Enter fullscreen mode Exit fullscreen mode

Each line has at most three addresses (two operands and one result). Temporaries t1 to t5 are compiler generated names.

Notice that t1 and t3 compute the same thing, and so do t2 and t4. An optimiser would apply common subexpression elimination and produce:

t1 = minus c
t2 = b * t1
t5 = t2 + t2
a  = t5
Enter fullscreen mode Exit fullscreen mode

Mention this in one line in the exam, it shows you know why TAC is useful.

Step 2: quadruples

Each instruction becomes a record with four fields: op, arg1, arg2, result.

# op arg1 arg2 result
(0) minus c t1
(1) * b t1 t2
(2) minus c t3
(3) * b t3 t4
(4) + t2 t4 t5
(5) = t5 a

Pros: the result is named explicitly, so instructions can be moved around during optimisation without breaking references. Cons: lots of temporary names in the symbol table.

Step 3: triples

Drop the result field. Refer to a computed value by the position of the instruction that computed it.

# op arg1 arg2
(0) minus c
(1) * b (0)
(2) minus c
(3) * b (2)
(4) + (1) (3)
(5) = a (4)

Pros: no temporaries, less space. Cons: if the optimiser moves instruction (2) somewhere else, every reference to (2) must be updated. Yahi triples ki sabse badi problem hai.

Note the assignment row: in triples, a goes in arg1 and the value (4) in arg2, the common textbook convention.

Step 4: indirect triples

Keep the triples as they are, and add a separate list of pointers that defines execution order.

Statement list points to
35 (0)
36 (1)
37 (2)
38 (3)
39 (4)
40 (5)

(The numbers 35 to 40 are arbitrary positions in the statement list.) To reorder code, the optimiser just reorders this pointer list. The triples themselves never move, so their references stay valid. That is the advantage over plain triples, so state it explicitly in your answer.

Bonus: TAC for an if statement

Practice example: if (x < y) z = x + 1; else z = y * 2;

100: if x < y goto 104
101: t1 = y * 2
102: z = t1
103: goto 106
104: t2 = x + 1
105: z = t2
106: ...
Enter fullscreen mode Exit fullscreen mode

Practise

Try x = (a + b) * (a + b) - c / d in all four forms. You should get 6 TAC lines (counting the final assignment to x) before optimisation and 5 after removing the repeated a + b. Then hunt for intermediate code questions in the compiler design previous year papers on GTU Hero. The page has 10 papers under code 3170701, every one of them 70 marks, covering Winter 2021 through Summer 2026.

Top comments (0)