GATE Computer ScienceHardGATE CS (PYQ pattern)
The statement r = (a + b) * (b + a) − (a + b) / c is turned into a DAG (+ and * commutative, − and / not), and three-address code is generated with one instruction t = x op y per interior node followed by a separate copy r = t for the assignment. How many nodes (leaves plus interior) does the DAG have, and how many instructions are generated, counting the copy?
- A. 8 nodes, 6 instructions
- B. 13 nodes, 7 instructions
- C. 7 nodes, 5 instructions
- D. 6 nodes, 5 instructions
Correct answer: C. 7 nodes, 5 instructions
Leaves a, b, c (3). a + b and b + a are one node because + is commutative; then (a+b)*(a+b), (a+b)/c and the final −: 4 interior nodes, 7 nodes in all. Code: t1 = a + b; t2 = t1 * t1; t3 = t1 / c; t4 = t2 − t3; r = t4: 5 instructions. Without commutativity there would be 8 nodes and 6 instructions; the expression tree has 13 nodes.
More Compiler Design questions
- For S′ → S; S → a S a | b S b | c, the canonical LR(1) states with identical cores are merged to obtain LALR(1). The LALR state whose only…
- Which of the following about intermediate representations is/are true? I. In triples, an operand that is a result of another statement is…
- Backpatching with no fall-through optimisation, numbering from 100, for: if (!(p < q)) x = 1; while (i < n) { if (a < b || c < d) k = k +…
- An LR parser for E → id | E [ E ] | ( E ) parses "( id ) [ id ]". Each shift and each reduce is one move (the final accept is not a move)…
- SDD: S → ( S S ) | a. Inherited attribute S.d: the root has S.d = 0 and each child of a node gets parent.d + 1. Synthesised S.v: for a →…
- A Pascal-like scanner uses longest match (ties to the earlier rule) with rules: FLOAT = [0-9]+.[0-9]; INT = [0-9]+; RANGE = ..; DOT =…