DCG:零次或多次、零次或一次、一次或多次? [英] DCG : zero-or-more, zero-or-one , one-or-more occurrences?
本文介绍了DCG:零次或多次、零次或一次、一次或多次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在DCG中如何实现:零或多、零或一、一或多个实例?
我说的是伪代码中的以下内容:
sentence --> word+
float --> int+, ['.'], int+
nilORa --> a?
nilORaaaa --> a*
推荐答案
您可以使用谓词的子句集(或者,在本例中,同一个Dcg的非终结符的dcg产生式集合--dcg产生式是Horn子句的另一种表示法)所提供的or-非确定性。
将应首先执行的生产移到顶部。例如,贪婪地收集至少一个word
,但可能更多:
sentence --> word, sentence.
sentence --> word.
根据语法中决定论的程度,您甚至可以删除:
sentence --> word, !, sentence.
sentence --> word.
浮点数也是如此。digits
至少为一位数字。我认为库中已经有digit
的定义:
float --> digits, ['.'], digits.
digits --> digit, digits.
digits --> digit.
nilORa
是a
--或者可能不是:
nilORa --> a.
nilORa --> [].
nilORaaaa
是a
后跟nilORaaaa
--或者可能不是:
nilORaaaa --> a, nilORaaaa.
nilORaaaa --> [].
您还应该能够部署;
我认为:
nilORaaaa --> (a, nilORaaa) ; [].
这篇关于DCG:零次或多次、零次或一次、一次或多次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文