【问题标题】:Parsing with prolog用 prolog 解析
【发布时间】:2016-01-06 05:43:31
【问题描述】:

我正在尝试用 prolog 解析:

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

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

选项1

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

选项2

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

选项3

?- 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'.

【问题讨论】:

  • 你能解释一下parse是什么意思吗?要解析的东西似乎是你从read_sentence 得到的东西;解析的结果应该是什么?
  • 我的意思是如果我输入 read_sentence(X)。这将打开一个需要输入的命令行。输入:cal 1 200 代码必须识别它有“cal”“1-12 之间的数字”和“1 到 9999 之间的数字”输出:calendar(1,200)
  • 这意味着您需要一种将输入字符串分解为单个单词列表的方法,对吗?

标签: parsing prolog swi-prolog


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多