I am relatively new to CFG and am wondering if one of you might be able to help me determine if the current solution I have is correct. I was asked to generate the context free grammar which accepts the language of nested brackets such that if the last token is a right square bracket, it closes all remaining open left brackets.
Some examples of those strings accepted would be: (((], (()(] (()()) Some examples of those strings not accepted would be: (]), )(, ())
At current my grammar is as follows:
- S -> epsilon
- S -> ()
- S -> (]
- S -> (S)
- S -> (S]
- S -> (S
- S -> SS
From all the examples I have tried, including those listed as both accepted and non accepted states above, I think that my CFG is correct. I'm wondering if there is any way to check this more concretely than simply randomly trying a large number of possibilities, and or if any of you can spot any errors?
Thanks in advance!
UPDATE:
I believe I have found the desired solution, though again I could be wrong. Starting with the base cases for the problem I have:
- S->(]
- S->(()]
- S->()(]
From my inductive step
- S->(I]
- S->(I()]
- S->((I)]
- S->(()I]
- S->(I)(]
- S->()I(]
S->()(I]
I->()
- I->II
- I->(
- I->(I)
((])
, which should be forbidden because the ] already closes all (. – sepp2kS
. LikeS -> (X), X -> ...
. – sepp2k