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)._
Keep practising Theory of Computation
Browse all Theory of Computation questions for GATE CSE, or explore more Computer Science topics.
Related questions
Which class of grammars generates ALL recursively enumerable languages?Which of the following languages is CONTEXT-FREE?Every regular language is also context-free. The PROOF construction isThe CYK algorithm parses a CFG in CNF. Its time complexity for a string of length $n$ isContext-free languages are RECOGNISED by which type of machine?A CFG is AMBIGUOUS ifConsider the CFG with rule $S \to aSb \mid \varepsilon$. The language generated isWhich of these is an example of an IRREGULAR (non-regular) language?