GATE / CS ARCHIVE

GATE CSE 1992 / QUESTION 11(b)

Eager Boolean expression quadruples

Compiler Design · Intermediate Code · Full GATE CSE 1992 paper

GATE 1992, Q11(b) Descriptive
Report issue

Intermediate Code

Question 11(b), GATE 1992: Generate three-address quadruples in compiler generation order for (a+b) > (c+d) or a > c and b < d. Use usual precedence; partial/short-circuit evaluation is forbidden.
Tap to enlarge
Answer & solution

Answer: Seven quadruples, ending in t7 = t3 or (t4 and t5); all operands are evaluated. (no official key was published)

The parse is ((a+b)>(c+d)) or ((a>c) and (b<d)), because arithmetic binds before comparison and and binds before or. Generate code in a left-to-right postorder traversal, storing each intermediate value in a fresh temporary. A quadruple is (operator, argument1, argument2, result):

1. (+, a, b, t1)
2. (+, c, d, t2)
3. (>, t1, t2, t3)
4. (>, a, c, t4)
5. (<, b, d, t5)
6. (and, t4, t5, t6)
7. (or, t3, t6, t7)

Thus t1=a+b, t2=c+d, and t3 is the first Boolean comparison. Independently t4 and t5 compute both comparisons in the second arm; t6 combines them, and t7 is the final Boolean value. All comparison and Boolean results are represented as Boolean values (or conventional 0/1 encodings), not jump destinations. Every operation executes even if t3 is already true or t4 is false. A jump-based short-circuit sequence would violate the explicit prohibition on partial evaluation.

There are seven internal nodes in the expression tree and one quadruple per node. Temporary names and the order of independent child computations can differ in valid implementations; this is one conventional compiler generation order, not a uniquely mandated register allocation.

Original paper, page 8