用prolog进行解析

时间:2016-01-05 20:04:35

标签: parsing prolog swi-prolog

我正在尝试用prolog解析:

我需要运行一个代码,该代码接收命令中的文本并根据输入进行解析。

命令 cal 返回日历(月,年),其中月份∈[1-12]和年份∈[1-9999]。如果没有给出月份,则返回年份,如果两者都未指定,则返回当前月份和年份。 实施例。

选项1

?- read_sentence(X).
|: cal 1 2000
X = calendar(1,2000).

选项2

?- read_sentence(X).
|: cal  2000
X = calendar(2000).`

2选项

?- read_sentence(X).
|: cal
X = calendar(1,2016).

到目前为止,我能够阅读并打印出来,但我不知道如何解析,甚至不知道从哪里开始。

read_sentence(X) :- get0(C),
   read_sentence(X, L,C),
   name(X, L).
read_sentence(_, [], X) :-
   member(X, `.\n\t`), !.
read_sentence(X, [C|L], C) :-
   get0(C1),
   read_sentence(X, L, C1).

其中:

?- read_sentence(X).
|: Hello  there
X = 'Hello  there'.

1 个答案:

答案 0 :(得分:1)

SWI-Prolog有一个谓词split_string,用于将字符串拆分为“单词”,这可能是您需要的这种相当简单的解析,然后您可以使用它来决定如何调用{{1 }}