edited by
233 views
3 votes
3 votes

Which of the following are context-free grammar for the language that contains all strings (including empty string) with matched parenthesis?

  1. $S \rightarrow (\; )$
    $S \rightarrow SS$
    $S \rightarrow (S)$
    $S \rightarrow \varepsilon$
  2. $S \rightarrow (\; )$
    $S \rightarrow SS$
    $S \rightarrow (S)$
  3. $S \rightarrow (\; )$
    $S \rightarrow SS$
    $S \rightarrow \varepsilon$
  4. $S \rightarrow (S )$
    $S \rightarrow SS$
    $S \rightarrow \varepsilon$
edited by

1 Answer

Best answer
1 votes
1 votes
Starting symbol $\rightarrow S$

Non-terminal variables$ = \{(,)\}$

Production rules:

$S \rightarrow (\; )$
$S \rightarrow SS$
$S \rightarrow (S)$
$S \rightarrow \varepsilon$

Here, the first rule  is redundant as we can have it using rules $3$ and $4.$

Option B cannot generate empty string and option C cannot generate nested parenthesis.

So, the correct answer is A;D
edited by
Answer:

Related questions