Consider the following grammar: stmt → if expr then expr else expr; stmt | Ò…

2017

Consider the following grammar:

stmt → if expr then expr else expr; stmt | Ò

expr → term relop term | term

term → id | number

id → a | c

number →[0−9] 

where relop is a relational operator (e.g., <,>,…), Ò refers to the empty statement, and ifthenelse 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 e1then e2else e3

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

Answer: 1024Answer: 1024 Explanation: Each if statement creates two possible control-flow branches: the then branch and the else branch. When multiple if statements are…

Attempted by 5 students.

Show answer & explanation

Correct answer: 1024

Answer: 1024

Explanation:

  • Each if statement creates two possible control-flow branches: the then branch and the else branch.

  • When multiple if statements are present and executed (whether nested or sequential), the total number of distinct control-flow paths is the product of the branches from each if.

  • With ten if terminals, the number of paths is 2 multiplied by itself 10 times: 2^10 = 1024.

Quick examples:

  • 1 if → 2 paths.

  • 2 ifs → 4 paths.

  • 3 ifs → 8 paths.

Therefore, for 10 if terminals the number of control-flow paths is 1024.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…