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
where relop is a relational operator e.g., 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 then else
has control flow paths. and .
View worked solution
Answer: 1024
To find the number of control flow paths in a program containing ten if terminals, we analyze the structure of the program generated by the given grammar.
1. Program Structure
The grammar rule for statements is:
Here, represents the empty statement . Since the recursion occurs at the end of the rule, a program containing ten if terminals consists of a sequence of independent if-then-else statements executed one after another:
2. Paths per Statement
As given in the example, a single if-then-else statement:
has exactly control flow paths:
- Path 1: (when the condition is true)
- Path 2: (when the condition is false)
3. Total Control Flow Paths
Since the statements are executed sequentially, the choices made in each statement are independent of the others. By the product rule of combinatorics, the total number of control flow paths through the program is:
Free practice · Your answers on this page are not saved.
