Question
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
Official answer and solution
- Official answer
- B
- From
- Official GATE 2015 answer key
Solution
Identify the basic block leaders to construct the control-flow graph (CFG), and include standard entry/exit nodes.
The three standard rules for identifying leaders:
- Instruction (1) is the first instruction Leader.
- Any target of a conditional or unconditional goto is a leader:
- Target of
goto (3)at line 9 is instruction (3) Leader. - Target of
goto (2)at line 11 is instruction (2) Leader.
- Target of
- Any instruction immediately following a conditional or unconditional goto is a leader:
- Instruction (10) follows line 9 Leader.
The leaders are instructions (1), (2), (3), and (10), partitioning the code into 4 basic blocks:
- : { (1) }
- : { (2) }
- : { (3), (4), (5), (6), (7), (8), (9) }
- : { (10), (11) }
In standard compiler textbooks (Aho, Lam, Sethi, Ullman), the CFG contains explicit Entry and Exit nodes:
Constructing the directed edges:
- (fall-through)
- (fall-through)
- (loop back on )
- (fall-through when )
- (loop back on )
- (fall-through when )
Total edges = 7 edges. Thus, there are 6 nodes and 7 edges (option B).
B
Omitting the Entry and Exit nodes, which would give 4 nodes instead of 6.
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2015 Computer Science and Information Technology (CS) Session 2
- Question
- 39 of 65, printed as CS Q.29
- Section
- Computer Science and Information Technology
Report an error in this question
GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.