Saul Garcia
Saul Garcia

Reputation: 940

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]. If there is no month given, it returns the year, if both are not specified return current month and year. Example.

Option1

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

Option2

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

Option3

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

Upvotes: 1

Views: 558

Answers (1)

Scott Hunter
Scott Hunter

Reputation: 49920

SWI-Prolog has a predicate, split_string, for splitting up strings into "words", which might be what you need for this rather simple parsing, which you could then use to decide how to invoke calendar.

Upvotes: 1

Related Questions