Search code examples
grammarcontext-free-grammarautomata

How to make this CFG conflict free?


So I have the following grammar, symbols (a, b, c, d, !):

S → N!
N → aNd | aMd | aNdN | aMdN
M → bM | cM | b | c

Essentially 'a' and 'd' are brackets and in between have to contain one or more 'b' and/or 'c'. Can have multiple brackets as long as they still contain one or more 'b' and/or 'c'. Must end with !.

So this grammar works but I am struggling to make it conflict-free. The conflicts are with both non terminals N and M, where you can both shift and reduce the same character. I have tried to work around by introducing epsilons and new non-terminals but always have something get in the way.

Can reduce 'd' and shift 'd' in non terminal N, also shift/reduce 'b' and 'c' in non terminal M.

Examples of strings derived from the grammar:

abccdaaccdd!
aaabddd!
aabddaccd!
aabbbddabccbcd!
aacbcdacbdd!
acbbccd!

I assume my grammar is unambiguous as I don't see anything wrong with it?

How can I make this conflict free?

Thanks!


Solution

  • Have you tried to swap bM in the rule to the Mb?