Below I have a Definite Clause Grammar that should accept the string aabccc
, however when I tested the grammar, the only string I was able to get accepted was abc
.
I'm pretty sure I've gotten rid of left-hand recursion, so I'm not sure what's going wrong.
s --> x, y, z.
x --> [a], x.
x --> [a].
y --> [b], y.
y --> [b].
z --> [c], z.
z --> [c].
Also, would I be able to define the above grammar as...
s --> x, y, z.
x --> [a], x; [a].
y --> [b], y; [b].
z --> [c], z; [c].
Both version of your grammar work as expected:
Maybe the issue was that you're trying to call it not with e.g. a
[a,a,b,b,c,c,c]
list of tokens but with aaabccc
atom? If that's the case, you can use the standardatom_chars/2
built-in predicate to convert an atom into a list of characters: