edited by
34,576 views
85 85 votes

Consider the following grammar:

  • stmt $\rightarrow$ if expr then expr else expr; stmt | $Ò$
  • expr $\rightarrow$ term relop term | term
  • term $\rightarrow$ id | number
  • id $\rightarrow$ a | b | c
  • number $\rightarrow [0-9]$ 

where relop is a relational operator $($e.g., $< , >,\ldots),$ $Ò$ 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 $e_1$ then $e_2$ else $e_3$ 
has $2$ control flow paths. $e_1 \rightarrow e_2$ and $e_1 \rightarrow e_3$. 

14 Answers

Best answer
130 130 votes

This question is picked from area of Counting in Combinatorics.

Given:
if $e_1$ then $e_2$ else $e_3$ has $2$ control flow paths $e_1 \rightarrow e_2$ and $e_1 \rightarrow e_3$.
(Meaning of "how many control flow" for if structure is clearly mentioned)

What is asked:
Number of control flow paths for $10$ if terminals?

Solution:
To get $10$ if's we need to use grammar to get,
         if <expr> then <expr> else <expr> ; stmt
         if <expr> then <expr> else <expr> ; if <expr> then <expr> else <expr> ; stmt
         ..............
         ..............
         ..............
         (keep doing it $10$ times to get $10$ if's)
Observe that there is a semi-colon after every if structure.

We know that every if structure has $2$ control flows as given in question. Hence,
        We have $2$ control flow choices for $1$st if terminal.
        We have $2$ control flow choices for $2$nd if terminal.
        ............
        ............
        ............
        We have $2$ control flow choices for $10$th if terminal.
        
By using multiplicative law of counting we get,
        Total choices as $2*2*2*2*2$......$10$ times $= 2^{10} = 1024$

Once again, one need not know "what control flow" is, but needs to know "how many control flows" are in if structure which is given in question.

edited by
55 55 votes
I will take a small example to make it clear.

Taking 2 if terminals

If expr then e1 else e2;

If expr then e3 else e4;

So there can be 4 control flow paths.

e1 e3

e1  e4

e2  e3

e2  e4

Now u can consider  10 if terminals.

The total control flow paths=2^10=1024.
45 45 votes

A control flow path/control flow graph is a graphical representation of all paths that might be traversed through a program during its execution.

Now each $if$ statement has 2 outcomes ~ either true or false.

As per the grammar each if statement is independent of the other.

Consider 3 if statements

if e1 then e2 $(T)$ else e3 $(F)$;

if e4 then e5 $(T)$ else e6 $(F)$;

if e7 then e8 $(T)$ else e9 $(F)$;

Now following are the paths the program goes through during its execution :

$1. \ TTT$

$2. \ TTF$

$3. \ TFT$

$4. \ TFF$

$5. \ FTT$

$6. \ FTF$

$7. \ FFT$

$8. \ FFF$

So for $n = 3$ we have $2^{3} = 8$ control flow paths, hence for $n = 10$ we will have total $2^{10} = 1024$ control flow paths.

1 flag
18 18 votes

each if-else condition leads to two different paths, there are 10 if-else conditions one after the other, therefore totally 210  = 1024 paths possible

9 9 votes

people considering 2.2....=210  but tha happens when each 2 possiblity depends on the prevois possibility(counting rule---like topological sorting)

here for the Non-terminal expr no NESTING is done..

it will be like this..

for 3 if statement 

        every if statement is independent of previous one

so 20 should be the answer 

Answer:
Position:
Show:

Related questions

32 32 votes
8 answers 8 answers
12.1k
12.1k views
Arjun asked Feb 14, 2017
12,143 views
Consider the following grammar:$P\rightarrow xQRS$$Q\rightarrow yz\mid z$$R\rightarrow w\mid \varepsilon$$S\rightarrow y$What is FOLLOW($Q$)?$\left \{ R \right \}$$\left ...
75 75 votes
4 answers 4 answers
30.0k
30.0k views
Arjun asked Feb 14, 2017
29,979 views
Consider the expression $(a-1) * (((b+c)/3)+d)$. Let $X$ be the minimum number of registers required by an optimal code generation (without any register spill) algorithm ...
48 48 votes
5 answers 5 answers
17.5k
17.5k views
Arjun asked Feb 14, 2017
17,506 views
Consider the following intermediate program in three address codep = a - b q = p * c p = u * v q = p + qWhich one of the following corresponds to a static single assignme...
89 89 votes
12 answers 12 answers
28.7k
28.7k views
Arjun asked Feb 14, 2017
28,663 views
A cache memory unit with capacity of $N$ words and block size of $B$ words is to be designed. If it is designed as a direct mapped cache, the length of the $\textsf{TAG}$...