Consider the intermediate code given below. (1) i=1 (2) j=1 (3) t1 = 5 * i (4)…

2015

Consider the intermediate code given below.

(1) i=1 (2) j=1 (3) t1 = 5 * i (4) t2 = t1 + j (5) t3 = 4 * t2 (6) t4 = t3 (7) a[t4] = -1 (8) j = j + 1 (9) if j <= 5 goto (3) (10) i = i +1 (11) if i < 5 goto (2)

The number of nodes and edges in the control-flow-graph constructed for the above code, respectively, are

Answer: B. 6 and 7Solution overview: construct the control-flow graph by identifying leaders, forming basic blocks, then counting nodes and edges (including explicit entry and…

  1. A.

    5 and 7

  2. B.

    6 and 7

  3. C.

    5 and 5

  4. D.

    7 and 8

Attempted by 51 students.

Show answer & explanation

Correct answer: B

Solution overview: construct the control-flow graph by identifying leaders, forming basic blocks, then counting nodes and edges (including explicit entry and exit nodes).

  • Leaders (statement numbers): first statement (1); targets of jumps (3) and (2); statement following the conditional at (9) is (10). So leaders are 1, 2, 3, and 10.

  • Basic blocks formed from leaders:

    • Block 1: statement (1) alone.

    • Block 2: statement (2) alone.

    • Block 3: statements (3) through (9) (includes the conditional at (9)).

    • Block 4: statements (10) and (11).

  • Count nodes: 4 basic blocks plus an explicit entry and exit node gives 6 nodes.

  • Count edges (succinctly):

    • Entry → Block containing (1).

    • Block (1) → Block (2).

    • Block (2) → Block (3).

    • From the conditional at (9) inside Block (3): true branch goes back to the start of Block (3) (a self-loop).

    • From the conditional at (9): false branch goes to Block (4) (statement (10)).

    • From the conditional at (11) in Block (4): true branch goes to Block (2).

    • From the conditional at (11): false branch goes to Exit.

  • Total edges counted above = 7.

Answer: 6 nodes and 7 edges.

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

Explore the full course: Compiler Design

Loading lesson…