GATE / CS ARCHIVE

GATE CSE 2017 SET 1 / QUESTION 43

GATE CSE 2017 Set 1 Q43

Compiler Design · Intermediate Code · Full GATE CSE 2017 Set 1 paper

GATE 2017 Set 1, Q43 2 marks · NAT
Report issue

Intermediate Code

Question 43, GATE 2017: Grammar: stmt→if expr then expr else expr; stmt | δ; expr→term relop term | term; term→id | number; id→a|b|c; number→[0–9]. δ is the empty statement and relop a relational operator. A program contains ten if terminals. Find its number of control-flow paths, counting the two branches of one if as two paths.
Tap to enlarge
Answer & solution

Official answer: 1024.0

Our answer: 1024

1. The stmt grammar appends one if statement followed by another stmt. Thus ten if terminals describe ten sequential binary choices, not a nesting with an unspecified shape.

2. After the first if there are two structural paths. Each subsequent if doubles the existing path count because every current path can take either its then or else branch.

3. Therefore P0=1 and Pn=2Pn−1, giving P10=210=1024. This counts syntactic control-flow graph paths, as the question’s example does; repeated or constant conditions can make some paths infeasible for actual executions.

Original paper, page 19 · Answer key