I am studying for my finals & I was reading context free grammars article from wikipedia and came across following example.
S → SS- (1st production rule)
S → (S) - (2nd production rule)
S → () - (3rd production rule)
I am well aware of left and right derivation. When I tried to solve this problem I start with start symbol
S-> SS -> (S)S-> ()S-> ()(S) -> ()()
but when I looked the answer, it was like this
S → SS → SSS → (S)SS → ((S))SS → ((SS))S(S)
→ ((()S))S(S) → ((()()))S(S) → ((()()))()(S)
→ ((()()))()(())
I am not sure what goes wrong with my answer? Is it necessary to use first production rule twice? Could anybody help me with this.
What problem? The wikipedia article doesn’t pose any problem. It just shows a grammar describing the language of well-matched parentheses and gives an example of a word in that language and how to derive it.
That’s a perfectly valid derivation.
That’s not the answer (there was no question). It’s just an example.
There’s nothing wrong with your derivation. Both
()()and((()()))()(())are valid words in the language.You can apply the production rules as often as you want (assuming of course the non-terminal you want to replace is present in the term) and in any order you want. It all just depends on which word you want to derive.