# Which of the following words is in the language of the grammar G?

This is taken from a practice quiz by my university.

I ruled out that aabbbaab is not part of the grammar:

S → aSb → aaSbb... This shows that I can't make this word because it would have to have atleast two b's at the end in order for me to add more letters

Then I tried to see if abbaabb can be derived from the grammar and I experienced the same thing:

S → aSb → aaSbb... This shows that I can't make a word with two b's at the end without having at least two a's in the beginning

Finally, I tried to do aabaabbb:

S → aSb → aaSbb → aaSSbb → aaSaSbbb... I decided it can't be this one because even if I make aaSaSbbb into ϵ, I still need to find a way to make aaSaSbbb into ba so I can derive the word. It doesn't seem like I can do that.

So by elimination I thought the correct answer was aabaabb but I was wrong.

Please show me how to work out whether a word can be derived from a grammar.

• The language is a Dyck language. Perhaps if you write a as { and b as }, the pattern will be clearer.
– rici
Mar 2, 2021 at 23:40
• As a first cut, it's clear that any word derivable in this grammar must have the same number of a's as b's. This eliminates half the possibilities. @rici's answer, of course, is more precise. Mar 3, 2021 at 0:41

This is what we have at our hand.

Now let us see what can be get from the second production in red.

From the production in Blue we have

And the parsing can be stopped using $$S\rightarrow \epsilon$$

So from the above two work outs we find that the $$a$$'s and $$b$$'s are properly nested. (and can be mapped to the problem of valid parenthesization )

That being said let us work the options out:

Alternatively, during the examination, it is hard to get this detailed thought in mind, so one can try to draw the parse tree for any one of the words, trying to do so, you shall get the intuition of the semantics of the language of the grammar.