The least number of temporary variables required to create a three-address…

2015

The least number of temporary variables required to create a three-address code in static single assignment form for the expression \(q + r / 3 + s – t * 5 + u * v /w\)  is _______________.

Attempted by 68 students.

Show answer & explanation

Correct answer: 8

Key idea: each binary operation in the expression requires a distinct temporary in static single assignment form because each result must be assigned exactly once.

  • Apply operator precedence to group the expression as: q + (r / 3) + s - (t * 5) + ((u * v) / w).

  • Count the binary operations (each needs one temporary):

  1. r / 3

  2. t * 5

  3. u * v

  4. (u * v) / w

  5. q + (r / 3)

  6. (previous) + s

  7. (previous) - (t * 5)

  8. (previous) + ((u * v) / w)

One convenient three-address code (SSA) ordering that uses one temporary per operation is:

  1. t1 = r / 3

  2. t2 = t * 5

  3. t3 = u * v

  4. t4 = t3 / w

  5. t5 = q + t1

  6. t6 = t5 + s

  7. t7 = t6 - t2

  8. t8 = t7 + t4

Therefore, the least number of temporary variables required is 8.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir