GATE Computer ScienceHardGATE CS (PYQ pattern)

Partition into basic blocks (leaders: the first statement, every jump target, every statement right after a jump): 1: i = 0; 2: L1: if i > n goto L3; 3: i = i + 1; 4: goto L1; 5: x = 5; 6: y = x; 7: L3: return i. How many basic blocks and control-flow edges are there, and how many blocks are unreachable from the entry?

  1. A. 6 blocks, 6 edges, 2 unreachable
  2. B. 5 blocks, 5 edges, 1 unreachable
  3. C. 5 blocks, 4 edges, 1 unreachable
  4. D. 4 blocks, 4 edges, 0 unreachable

Correct answer: B. 5 blocks, 5 edges, 1 unreachable

Leaders: 1, 2 (target L1), 3 (after the conditional jump), 5 (after goto), 7 (target L3). Blocks {1}, {2}, {3, 4}, {5, 6}, {7}. Edges: {1}→{2}, {2}→{7}, {2}→{3,4}, {3,4}→{2}, and the fall-through {5,6}→{7}: 5 edges. Nothing jumps to 5 and {3,4} ends in goto, so {5, 6} is unreachable (its edge into {7} still exists in the graph).

More Compiler Design questions

Exams that ask this topic