Any string of terminals that can be generated by the following context free…
2021
Any string of terminals that can be generated by the following context free grammar (where S is start nonterminal symbol)
\(S \rightarrow XY \\ X \rightarrow 0X |1X|0 \\ Y \rightarrow Y0|Y1|0\)
- A.
has at least one 1
- B.
should end with 0
- C.
has no consecutive 0’s or 1’s
- D.
has at least two 0’s
Attempted by 49 students.
Show answer & explanation
Correct answer: D
Solution: The grammar generates exactly the set of binary strings that contain at least two 0's.
Characterize X: The nonterminal X has productions X → 0 | 0X | 1X. This yields all nonempty binary strings that end with 0, i.e. X = (0|1)*0.
Characterize Y: The nonterminal Y has productions Y → 0 | Y0 | Y1. Starting from the base 0 and appending symbols to the right produces all nonempty binary strings that start with 0, i.e. Y = 0(0|1)*.
Combine: Since S → XY, every generated string is a concatenation of a string that ends with 0 and a string that starts with 0, so every string produced contains at least two 0's. In regular-expression form: S = (0|1)*0 0(0|1)*.
Conversely: Any binary string with at least two 0's can be split between the first and second 0; the prefix (ending at the first 0) belongs to X and the suffix (starting at the second 0) belongs to Y. Thus the grammar generates exactly the set of binary strings that have at least two 0's.
Example generated strings: "00" (X -> 0, Y -> 0), "10100" (choose X to end at the first 0, Y to start at the second 0).
A video solution is available for this question — log in and enroll to watch it.