Definite Clause Grammar - Prolog

最后都变了- 提交于 2019-12-11 06:59:18

问题


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].

回答1:


Both version of your grammar work as expected:

?- phrase(s, [a,a,b,b,c,c,c], R).
R = [] .

?- phrase(s, [a,a,b,b,c,c,c]).
true .

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 a aabccc atom? If that's the case, you can use the standard atom_chars/2 built-in predicate to convert an atom into a list of characters:

?- atom_chars(aabccc, Tokens).
Tokens = [a, a, b, c, c, c].


来源:https://stackoverflow.com/questions/26871678/definite-clause-grammar-prolog

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!