Prolog - 大于 x 的数字 [英] Prolog - Numbers greater than x

查看:45
本文介绍了Prolog - 大于 x 的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Prolog 算术,并且有一个生成抽象语法树的程序,例如 plus(num(1),num(2)) 就是 1+2.这是通过使用 DCG 来完成的.在本例中,plus(num(1),num(2)) 与前缀列表表示 [+,1,2] 相同.

I'm working with Prolog arithmetic and have a program that generates an abstract syntax tree, such as plus(num(1),num(2)) which simply is 1+2. This is done by using DCG. In this example plus(num(1),num(2)) is the same as the prefix list representation [+,1,2].

我的问题是我只想允许 num(x) 大于 3.例如允许 num(4) 但不允许 num(1).

My problem is that I only want to allow num(x) greater than 3. For example num(4) is allowed but not num(1).

我这样做:

num(num(4)) --> [4].
num(num(5)) --> [5].
num(num(6)) --> [6].
num(num(7)) --> [7].

等等.但想做类似 num(num(x)) -->[x]. 对于大于 3 的数字.知道如何解决这个问题吗?

etc. but would like to do something like num(num(x)) --> [x]. for numbers greater than 3. Any idea as to how to approach this problem?

推荐答案

怎么样:

num(num(X)) --> [X], {X > 3}

{}/1 可用于嵌入条件和副作用在 DCG 语法规则中.DCG 可以在许多 Prolog 系统中找到但目前还没有一个成熟的标准.但大多数 Prolog系统确实有 {}/1.例如在这里定义:

The {}/1 can be used to embed conditions and side effects in DCG grammar rules. DCGs are found in many Prolog systems but there is not yet a ripe standard. But most of the Prolog systems do have the {}/1. It is for example defined here:

定语从句语法规则
克劳斯·戴斯勒
2013 年 8 月 20 日
http://www.complang.tuwien.ac.at/ulrich/iso-prolog/dcgs/dcgsdin130820.pdf

在词汇表中,{}/1 作为语法正文目标".在较旧的出版物 该结构被称为辅助动作"、程序调用"或额外条件",这可能比只是将其称为语法正文目标,因为我们希望看到所有语法体的组成部分作为语法体目标.查看这里的例子:

In the glossary the {}/1 goes as "grammar body goal". In older publications the construct was called "auxiliary action", "procedure call" or "extra conditions", which is probably more sensible than just calling it grammar body goal, since we would like to see all constituents of a grammar body as grammar body goals. See for example here:

用于语言分析的定语从句语法
费尔南多 C. N. 佩雷拉和大卫 H. D. 沃伦
人工智能 13 (1980), 231-278
http://cgi.di.uoa.gr/~takis/pereira-warren.pdf

再见

这篇关于Prolog - 大于 x 的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆