57 votes 57 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$. Compiler Design gatecse-2017-set1 compiler-design parsing normal numerical-answers + – Arjun asked Feb 14, 2017 edited May 4, 2021 by Arjun Arjun 20.2k views answer comment Share Follow See all 4 Comments See all 4 4 Comments reply yg92 commented Feb 13, 2017 reply Follow Share it was 10 statements. 0 votes 0 votes jatin khachane 1 commented Nov 15, 2018 reply Follow Share Why product rule is used not Sum rule ?? 3 votes 3 votes tusharp commented Nov 25, 2018 reply Follow Share It is not If e1 then e2 or e3 OR If e4 then e5 or e6. There are 10 if statements and we have conditions between them and not options. If you do OR then it will be single if statement and not 10. 6 votes 6 votes tusharp commented Nov 25, 2018 reply Follow Share A larger event of 10 statements is broken down into 1 statement each. Now each statement has 2 options. statement 1 (2 options) AND statement 2(2 options)...... = 2^10. 7 votes 7 votes Please log in or register to add a comment.
2 votes 2 votes if e1then e2 else e3 has 2 control flow paths. e1→e2 and e1→e3 One if-condition has two possibilities. So, for 10 if-conditions __, __, __, __, __, __, __, __, __, __ We have 2 possibilities for each slot. => $2*2*2*2*2*2*2*2*2*2=1024$ JashanArora answered Dec 22, 2019 JashanArora comment Share Follow See 1 comment See all 1 1 comment reply mrinmoyh commented Jan 14, 2020 reply Follow Share why control flow path goes till end ?? It should be stopped upto semicolon. I mean there are 10 if statement & for each of them have only 2 control flow. so total is 2*10 = 20. 0 votes 0 votes Please log in or register to add a comment.
1 votes 1 votes The first thing we need to know is given program(if statements) executes sequentially or in any order, so the answer is the program has been already written & given to us and now we can not decide which "if" statement will execute in which order, so it executes sequentially. Now 1st "if" statement has 2 control flow paths, either it can direct its path to "then" statement or "else" statement. Similarly 2nd "if" statement has to 2 control flow paths. Now if we look closely, using these 2 "if" statement we can have 4 control flow paths. 1) 1st "if" -> 1st "then" -> 2nd "if" -> 2nd "then" 2) 1st "if" -> 1st "then" -> 2nd "if" -> 2nd "else" 3) 1st "if" -> 1st "else" -> 2nd "if" -> 2nd "then" 4) 1st "if" -> 1st "else" -> 2nd "if" -> 2nd "else" Similarly for 10 "if" statements we can have 2^10=1024 control flow paths. Nitesh Singh 2 answered Jul 20, 2019 Nitesh Singh 2 comment Share Follow See all 0 reply Please log in or register to add a comment.
0 votes 0 votes One if statement: 2 paths (true or false) Two if statements: 2 * 2 = 4 paths (true-true, true-false, false-true, false-false) Three if statements: 2 * 2 * 2 = 8 paths Ten if statements: 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 1024 paths rajveer43 answered Jan 25 rajveer43 comment Share Follow See all 0 reply Please log in or register to add a comment.