简体   繁体   English

用序言解析

[英]Parsing with prolog

I am trying to parse with prolog: 我正在尝试用序言进行解析:

I need to run a code which recieves a text in the command and parse it depending of the input. 我需要运行一个接收命令中文本的代码,并根据输入来解析它。

Command cal returns calendar(Month, Year) where month ∈ [1-12] and year ∈ [1-9999]. 命令cal返回日历(月,年) ,其中月份∈[1-12]和年份∈[1-9999]。 If there is no month given, it returns the year, if both are not specified return current month and year. 如果没有给出月份,则返回年份;如果未指定月份,则返回当前月份和年份。 Example. 例。

Option1 选项1

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

Option2 选项2

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

Option3 2选项

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

So far I am able to read the sentence, and print it, but I have no idea how to parse or even where to begin. 到目前为止,我能够阅读并打印该句子,但是我不知道如何解析甚至从哪里开始。

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).

Which does: 哪个:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM