Consider the following expression: X = (A + B) * (C – D) + (E + F) * (G – H)…

2025

Consider the following expression:

X = (A + B) * (C – D) + (E + F) * (G – H)

Perform the following tasks in sequence:

  • Convert the expression into postfix notation.

  • Generate the corresponding Three-Address Code (TAC) using minimal temporary variables.

  • Then apply simple local optimizations like common subexpression elimination and dead code elimination, and provide the optimized TAC.

Attempted by 38 students.

Show answer & explanation
  1. The given arithmetic expression is
    X = (A + B) * (C − D) + (E + F) * (G − H)

The first step is to convert the expression into postfix notation. In postfix notation, operators appear after their operands and parentheses are removed.

(A + B) → A B +
(C − D) → C D − *
(E + F) → E F +
(G − H) → G H − *

Substituting these in the original structure:

(A+B)(C−D) → A B + C D −
(E+F)(G−H) → E F + G H −

Final postfix expression:

A B + C D − * E F + G H − * +

  1. Next step is generating Three Address Code (TAC). Standard TAC generation translates the evaluation steps sequentially using temporary variables (tn):

t1 = A + B
t2 = C − D
t3 = t1 * t2
t4 = E + F
t5 = G − H
t6 = t4 * t5

t7 = t3 + t6
X = t7

  1. Now apply simple local optimizations.

To optimize, we look for Common Subexpression Elimination (CSE) and Dead Code Elimination (DCE) opportunities:

  • CSE Analysis: Examine the independent subexpressions: (A+B), (C-D), (E+F), and (G-H). None of these literal variable pairs are identical. Therefore, no common subexpressions exist in this specific equation.

  • DCE & Register Reuse Optimization: We can drastically minimize the number of temporary variables by immediately reusing registers/variables once their previous values are consumed, and by directly assigning the final addition to X (eliminating the dead/redundant final assignment instruction).

Optimized TAC Sequence


t1 = A + B
t2 = C - D
t1 = t1 * t2 (Reuse t1 )
t2 = E + F (Reuse t2 )
t3 = G - H
t2 = t2 * t3 (Reuse t2 )
X = t1 + t2 (Direct assignment to target)

This reduces the temporary variable footprint from 7 down to 3.

Explore the full course: Niacl Ao It Specialist