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:

{}{}
{,{}{}}
{{},{}
wkf
  • 842
  • 9
  • 17

2 Answers2

4

Done by hand:

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

Or, instead of just winging it we could use a more systematic approach and apply the algorithm from Wikipedia on removing immediate left recursion:

L ::= { L } L1 | L1
L1 ::= ε | , L L1

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
0

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
  • 12,034
  • 15
  • 57
  • 79