prolog dcg 限制 [英] prolog dcg restriction
问题描述
我想使用 DCG 作为生成器.截至目前,语法是
I would like to use DCGs as a generator. As of now, the syntax is
s-->a,b.
a-->[].
a-->a,c.
c-->[t1].
c-->[t2].
b-->[t3].
b-->[t4].
我想生成所有s
,其中a
的长度是
I would like to generate all s
where the length of a
is < someNumber
.
使用?-短语(a,X),length(X,Y),Y<4.
我可以得到少于4个项目的所有a
.但是,当所有组合都用完时,系统(SWI-Prolog 6.2.5)似乎会停滞.有时以前,在这里提出了类似的问题.但是,作为 Prolog 的新手,我无法使其与上述语法一起使用.有什么想法吗?
Using ?- phrase(a,X),length(X,Y),Y<4.
i can get all a
with less than 4 items. However, when all combinations are exhausted, the system (SWI-Prolog 6.2.5) seems to stall.
Sometimes ago, a similar question was asked here. However, being new to Prolog i am not able to make it work with the grammar above. Any ideas?
更新:(canrememberthename) 的评论不知何故被删除了.无论如何,建议使用 between(1,4,Y),length(X,Y),phrase(a,X).
来设置限制.在将我的代码更改为 a-->c,a.
Update: There was a comment by (canrememberthename) which got deleted, somehow. Anyway, it was suggested to use between(1,4,Y),length(X,Y),phrase(a,X).
to set limits. This worked nicely, after changing my code to a-->c,a.
推荐答案
非终结符 a//0 both 是左递归和 'epsilon'(生成空序列),phrase/2 将空生产后立即循环.
the nonterminal a//0 is both left recursive and 'epsilon' (generate the empty sequence), and phrase/2 will loop immediately after the empty production.
您可以解决边界列表的长度问题:
You can solve your problem bounding list' length:
?- between(1,4,Y),length(X,Y),phrase(a,X).
并且,正如您已经完成的那样,删除左递归.
and, as you have already done, removing the left recursion.
这篇关于prolog dcg 限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!