Question
Consider the following grammar:
stmt -> if expr then expr else expr; stmt | 0
expr -> term relop term | term
term -> id | number
id -> a | b | c
number -> [0-9]
where relop is a relational operator (e.g., ), 0 refers to the empty statement, and if, then, else are terminals.
Consider a program following the above grammar containing ten if terminals. The number of control flow paths in is ______.
For example, the program
if e1 then e2 else e3
has 2 control flow paths, and .
Official answer and solution
- Official answer
- 1024
- From
- Official GATE 2017 answer key
Solution
Determine the control flow branching factor of each if statement generated by the grammar and compute the total number of paths for ten sequential if statements.
Consider the production for statements:
stmt -> if expr then expr else expr; stmt | 0
Each individual if expr then expr else expr; construct represents a conditional statement that branches into two mutually exclusive control flow execution paths:
- Path 1: through the
thenbranch. - Path 2: through the
elsebranch.
The grammar rule stmt -> if expr ...; stmt chains these statements sequentially. A program containing ten if terminals consists of a sequence of 10 sequential conditional statements.
Because each of the 10 if statements branches independently into 2 paths, the total number of distinct control flow paths from the start of the program to the end is given by the product rule:
1024.0
The answer is the one in the official GATE answer key. The solution is this site's own explanation.
Source
- Paper
- GATE 2017 Computer Science and Information Technology (CS) Session 1
- Question
- 43 of 65, printed as CS Q.43
- 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.