Peter Jetta
Peter Jetta

Reputation: 27

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!

Upvotes: 1

Views: 128

Answers (1)

Sara Bean
Sara Bean

Reputation: 139

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

Upvotes: 2

Related Questions