Practice free →
HomeGATE CSEcomputerscienceTheory of Computation › Consider the grammar $S \to (S) \mid SS \mid \va…

Consider the grammar $S \to (S) \mid SS \mid \varepsilon$. The language generated is

Aall strings of parentheses
Bstrings starting with an open paren
Cstrings of only open parentheses
DWELL-MATCHED (balanced) strings of parentheses
Answer & Solution
Correct answer: D. WELL-MATCHED (balanced) strings of parentheses
1. Trace derivations from $S$: - $S \Rightarrow \varepsilon$ - $S \Rightarrow (S) \Rightarrow ()$ - $S \Rightarrow (S) \Rightarrow ((S)) \Rightarrow (())$ - $S \Rightarrow SS \Rightarrow (S)S \Rightarrow ()()$ - $S \Rightarrow (SS) \Rightarrow (()())$ 2. Every derived string has EQUAL counts of $($ and $)$ AND every prefix has at-least-as-many opens as closes. These are the DYCK / WELL-MATCHED parens. 3. Bad strings like $)($ cannot be derived: $)($ has $)$ before $($, violating the well-matched property. 4. This is the classic CFG for parens. Used as a building block in EVERY compiler. 5. Other options are too broad or too narrow. _Source: Jeff Erickson, "Models of Computation", §5.3 (CFG examples — Dyck language)._
Solve this in the app — GATE CSE practice & 24k+ MCQs →
Related questions