Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Context free grammar for balanced parethesis

I want to design a context free grammar for the following language:

L = { w e {(, )}* | w is balanced}

I proposed the following grammar:

S -> (S)S | E

Whereas the proposed solution in the lecture is:

S -> (S) | SS | E

I am not able to figure out, what could be the problem with my solution. I ran both the grammar for various cases such as:

(()()), ()()(), and (())()

and both the CFG accepts these strings.

Can someone please help, what are the cases that my CFG would not cover. Or are they both equivalent. Or the number of transitions required to reach the final state are different.

like image 248
yogeshagr Avatar asked Mar 09 '23 18:03

yogeshagr


1 Answers

Both grammars generate the same language, so neither is incorrect.

I prefer yours because it is unambiguous, but that wasn't part of the requirements. Many people seem to find the other answer easier to understand, but that wasn't part off the requirements either, and it is a highly subjective criterion.

like image 72
rici Avatar answered Mar 12 '23 08:03

rici