Home › GATE CSE › computerscience › Theory 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)._
Related questions
Which class of grammars generates ALL recursively enumerable languages?What is a LEFTMOST DERIVATION?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$ isA LEFT-RECURSIVE rule in a CFG has the form $A \to A\alpha$ for some non-empty $\alpha$. WContext-free languages are CLOSED under which of the following operations?Is the language $L = \{a^n b^n c^n \mid n \geq 0\}$ context-free?