tl:dr-我只有一行序言,它在一个版本的Prolog(SWI)中工作正常,但在另一个版本(TuProlog)中却不行。

我正在将脚本从SWI序言移植到Tuprolog。 (TuProlog最近进行了一次重大更新,两个版本的行为相同)

当我使用下面的Java设置将脚本放入TuProlog时,出现错误“无法将enitire字符串作为一个术语读取”。

因此,我缩减了脚本(有效地使用二进制搜索),直到将脚本缩减为:

iterm3(Term) --> "'", notquote(Cs), "'", { name(Term1,Cs), Term = q(Term1) }.


尽管swipl很好,但具有以下输出...

cobrakai:~ josephreddington$ swipl -s /Users/josephreddington/Documents/workspace/com.plancomps.prolog.helloworld/caml-light-dynamics/Tools/Prolog/temp.pl% library(swi_hooks) compiled into pce_swi_hooks 0.00 sec, 3,992 bytes% /Users/josephreddington/Documents/workspace/com.plancomps.prolog.helloworld/caml-light-dynamics/Tools/Prolog/temp.pl compiled 0.00 sec, 1,720 bytes
Welcome to SWI-Prolog (Multi-threaded, 64 bits, Version 5.10.5)
Copyright (c) 1990-2011 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.

For help, use ?- help(Topic). or ?- apropos(Word).

?-


但是在Tuprolog中仍然返回“不能将enitire字符串作为一个术语读取”-有人可以告诉我为什么会发生这种情况吗?

附录:使用的代码:

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import alice.tuprolog.NoMoreSolutionException;
import alice.tuprolog.NoSolutionException;
import alice.tuprolog.Prolog;
import alice.tuprolog.SolveInfo;
import alice.tuprolog.Theory;

public class EntireStringForStackOverflow {
    public static void main(String[] args) throws Exception {
        Prolog engine = new Prolog();
        engine.loadLibrary("alice.tuprolog.lib.DCGLibrary");
        engine.addTheory(new Theory(readFile("temp.pl")));
    }

    private static String readFile(String file) throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = null;
        StringBuilder stringBuilder = new StringBuilder();
        String ls = System.getProperty("line.separator");
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line);
            stringBuilder.append(ls);
        }
        return stringBuilder.toString();
    }
}

最佳答案

我想可能是单引号需要在Tuprolog中引用。我会尝试

iterm3(Term) --> "\'", notquote(Cs), "\'", { name(Term1,Cs), Term = q(Term1) }.


编辑现在,我必须承认我不知道tuProlog DCG的文档可能在哪里,而且我也不能花太多时间搜索它(实际上我可以阅读)。语法的另一种修改,您可以在其中看到我为什么建议上面无用的修改:

iterm3(Term) --> ['\''], notquote(Cs), ['\''], { name(Term1,Cs), Term = q(Term1) }.


也就是说,通过尝试失败验证tuProlog中是否禁止双引号常量...

09-11 21:43