问题
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:
{}{}
{,{}{}}
{{},{}
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/1095774/how-can-i-eliminate-left-recursion-in-the-following-grammar