# Recursive-descent parser for the grammar S -> S(S)S | ε

I'm studying (for self-betterment - I don't go to school) the 2nd edition of Compilers: Principles, Techniques and Tools by Aho et al. I'm not sure how to do Exercise 2.4.1 (b), which is to construct a recursive-descent parser, starting with the grammar:

$S\to S(S)S \mid \epsilon$.

It seem to me that the simpler grammar

$S\to (S)S \mid \epsilon$

can generate the same language as the given grammar, and if that's the case, I can construct a parser for it in a similar manner to the example that was shown in the previous section, without difficulty. Is that the right approach? (Keep in mind that I'm only in chapter 2, at around page 70 of this 1000+ page book, so the solution shouldn't be too "advanced".)

• Indeed it's the same language. And you can't really deal with that left $S$ without altering the grammar, so this should be a correct way. – Karolis Juodelė Jun 18 '14 at 20:00
• @KarolisJuodelė in that case the original grammar is ambiguous, and the parse will reflect only one possible derivation. That might not be enough – vonbrand Jun 19 '14 at 8:23
• Thanks for the replies. @vonbrand The original grammar is clearly ambiguous. But if I understand correctly, that doesn't necessarily prevent us from being able to check whether a particular string could be derived from it. Is that right? – Aky Jun 19 '14 at 12:09
• @Aky, it depends on what you want. If it is just to check membership in the language, it is clearly enough. If you need a particular derivation (or all of them, as may be) it isn't. – vonbrand Jun 19 '14 at 12:22
• @vonbrand Given how (not so) far I am into the book, I don't think I'm expected to show anything besides membership. So I'm happy with that. – Aky Jun 19 '14 at 12:25