Skip to content
GATEiit

GATE CSE 2017 Session 1 Q53: Number of control flow paths in a program with ten if-then-else statements

Syllabus topic: Lexical analysis, parsing and syntax-directed translation

Question

Consider the following grammar:

stmt    -> if expr then expr else expr; stmt | ε
expr    -> term relop term | term
term    -> id | number
id      -> a | b | c
number  -> [0-9]

where relop is a relational operator (e.g., <, >, ...), ε refers to the empty statement, and if, then, else are terminals.

Consider a program P following the above grammar containing ten if terminals. The number of control flow paths in P is ______. For example, the program

if e1 then e2 else e3

has 2 control flow paths, e1→e2 and e1→e3.

Try it: numerical answer: type the value

Numerical answer. No negative marking.
Show answer and explanation

Answer: 1024 (official GATE 2017 final answer key)

Concept used: The grammar only allows a sequence of if-then-else statements (no nesting, since branches are expressions), and each one independently doubles the number of paths.

Concept notes

Loading notes…

First step: See that a program with ten if terminals is ten if-then-else statements one after another, each with 2 choices, and multiply.

Full step-by-step solution

Loading…

Answer from the official final answer key. Found a mistake? Use Report issue and we will fix it.

More questions on Lexical analysis, parsing and syntax-directed translation

Every PYQ on this topic →

Share this question

WhatsAppDownload image

Source: official GATE 2017 Session 1 CS question paper and final answer key, organised by IIT Roorkee. Question paper · Answer key