views:

564

answers:

2

Here's the grammar, which is supposed to describe a language of nested braces with commas as delimiters:

L ::= {L} | L,L |

A few more examples of strings I'd expect the grammar to accept and reject:

Accept:

{,{,,{,}},,{,}}
{{{{}}}}
{,{}}

Reject:

{}{}
{,{}{}}
{{},{}
+4  A: 
John Kugelman
Right on, now I feel stupid. Thanks!
wkf
Can't think of a way to derive `{{},}` from this grammar
Johannes Schaub - litb
@litb: the outer {} is the first rule {L} the {}, is firstly second rule L,L with the first of those {} and the second empty.
Simeon Pilgrim
@Simeon, yep it works now with the updated grammar :)
Johannes Schaub - litb
nod, I only just clicked you where meaning JK's grammar vs. wkf's my bad.
Simeon Pilgrim
A: 

First of all, that grammar won't accept your first example, since it requires the commas to be after the close brace and before the open brace. I would suggest to re-write it as

L::= {L} | ,L

This won't get rid of the left recursion, but it will at least match your acceptable answers.

a_m0d
Not true. His grammar will accept `{,}`, where the ',' is produced by L1=L2,L3 with L2 and L3 being empty.
John Kugelman
Your right - I didn't realise that that was the point of the empty rule at the end
a_m0d