1. Gyanastra
  2. Compiler Design
  3. Intermediate Code Generation
  4. Three Address Code and SDT for Generating Three Address Code
Free lesson, no account needed 7 min read 14 practice questions after it
Asked in past papers7 times
  • GATE CSE2021201720162015200320011989

Compiler Design · Intermediate Code Generation

Three Address Code and SDT for Generating Three Address Code

Overview

Three Address Code (TAC) is an intermediate representation in which every instruction contains at most three addresses (two operands and one result).
A Syntax-Directed Translation (SDT) — a grammar with semantic rules attached to its productions — converts a multi-address statement such as x = a + b * c into a sequence of three-address statements.

Key concepts

  • Three Address Code (TAC): an intermediate representation of a program in which every instruction contains at most three addresses (two operands and one result).
  • Four-address code: a statement such as x = a + b * c uses four addresses — x, a, b, c — and must be converted into a sequence of three-address statements.
  • Syntax-Directed Translation (SDT): a grammar plus semantic rules attached to productions; the rules use the place attribute and the gen function.
  • place: an attribute used as a placeholder; it holds the name of the variable or temporary that stores the value of that non-terminal.
  • gen(...): writes one line of three-address code to the output.
  • New temporary variable: a fresh temporary (T1, T2, ...) created for the left-hand-side non-terminal of a production.

How it works

  1. Use the grammar:
S -> id = E
E -> E + T | T
T -> T * F | F
F -> id

This grammar generates all statements of the form x = a + b * c (identifier = expression over + and *). 2. Attach the semantic rules:

ProductionSemantic Rule
S → id = ES.code: gen(id.name = E.place)
E → E1 + TE.place = new temporary variable; gen(E.place = E1.place + T.place)
E → TE.place = T.place
T → T1 * FT.place = new temporary variable; gen(T.place = T1.place * F.place)
T → FT.place = F.place
F → idF.place = id.name
  1. Build the parse tree and evaluate bottom-up (as in bottom-up parsing). At each reduction, the semantic rule of the production used for that reduction fires; non-reductions (shifts) do nothing.
Figure. Notice that T * F -> T generates T1 = b * c, E + T -> E generates T2 = a + T1, and id = E -> S generates x = T2. The semantic rules fire at these reductions.

Formulas

  • F.place = id.name — the place of F is the identifier's name.
  • T.place = F.place — place copied upward for T -> F.
  • E.place = T.place — place copied upward for E -> T.
  • T.place = new temporary; gen(T.place = T1.place * F.place) — for T -> T1 * F.
  • E.place = new temporary; gen(E.place = E1.place + T.place) — for E -> E1 + T.
  • gen(id.name = E.place) — for S -> id = E, produces the final assignment.

Worked examples

Input: x = a + b * c (four-address code).

Parse tree: S at the root, with children id (x), =, and E. The E is E + T; the left E is T → F → id (a). The right T is T * F; its left T is F → id (b), and its F is id (c).

Step-by-step reductions and actions:

StepReductionSemantic actionResult
1id → F (for a)F.place = id.nameF.place = a
2F → TT.place = F.placeT.place = a
3T → EE.place = T.placeE.place = a
4id → F (for b)F.place = id.nameF.place = b
5F → TT.place = F.placeT.place = b
6id → F (for c)F.place = id.nameF.place = c
7T * F → TT.place = new temp (T1); gen(T.place = T1.place * F.place)Output: T1 = b * c
8E + T → EE.place = new temp (T2); gen(T2 = E1.place + T.place)Output: T2 = a + T1
9id = E → Sgen(id.name = E.place)Output: x = T2
  • Step 7: the left-side T is a fresh non-terminal and the right-side T is the one holding b. A new temporary T1 is created for the left-side T, and T1 = b * c is generated — the multiplication part is evaluated first.
  • Step 8: a new temporary T2 is created for the left-side E, and T2 = a + T1 is generated — the two evaluated parts are added.
  • Step 9: id.name is x and E.place is T2, so x = T2 is generated.

Final output (three address code):

T1 = b * c
T2 = a + T1
x  = T2

Every line of the output contains a maximum of three addresses, so the four-address input has been converted into three-address code.

Comparison

Input statementOutput (TAC)
Formx = a + b * cT1 = b * c, T2 = a + T1, x = T2
Addresses per line4 (x, a, b, c)at most 3 per line
Number of lines13

Revision summary

  • Three address code = intermediate representation where each instruction has at most three addresses.
  • F -> id remembers the variable name; E -> T and T -> F simply copy place upward.
  • E -> E1 + T and T -> T1 * F create a new temporary, set the LHS place to it, and gen the operation.
  • S -> id = E generates the final assignment via gen(id.name = E.place).
  • Evaluation is bottom-up: semantic rules fire at each reduction; shifts do nothing.
  • For x = a + b * c the output is T1 = b * c, T2 = a + T1, x = T2.
  • For programs containing if, else and other statements, the grammar must be written for all those statement forms and rules written for each of them.

Previous-year exam insights

  • In SSA form every variable is assigned exactly once; each assignment creates a new version (subscripted name), and uses are renamed to the reaching definition, with phi-functions at join points of control flow.
    Asked inGATE 2017GATE 2016GATE 2015
  • The minimum number of temporaries/variables needed for a three-address code in SSA form equals the number of distinct assignment targets (each definition needs its own name); re-definitions of the same variable each require a fresh version.
    Asked inGATE 2016GATE 2015
  • Common subexpression elimination removes recomputation of an expression whose operands have not been redefined since the earlier computation; it is invalid if any operand was reassigned in between.
    Asked inGATE 2021
  • For an expression tree, the number of temporaries in straight-line three-address code can be reduced by reusing a temporary once its value is no longer needed (Sethi–Ullman numbering); the minimum equals the maximum number of simultaneously live temporaries.
    Asked inGATE 2015
  • In an SDT evaluated during bottom-up (LR) parsing, semantic actions attached at the end of a production fire at reduction; synthesized attributes are computed from children, inherited attributes from parent/siblings.
    Asked inGATE 2003
  • newtemp() returns a distinct fresh temporary on each call, and gen() emits one three-address instruction; the place of a non-terminal is the name (variable or temporary) holding its value.
    Asked inGATE 2003
Pages 1–2 of 2