1

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'. 
3
  • Can you explain what you mean by parse? The thing to be parsed appears to be what you get from read_sentence; what should be the result of parsing? Commented Jan 5, 2016 at 20:11
  • I mean if I enter read_sentence(X). This will open a command line where an input is required. Input: cal 1 200 The code has to recognize that it has "cal" "number between 1-12" and "number between 1 and 9999" Output: calendar(1,200) Commented Jan 5, 2016 at 20:17
  • Which means you need a way to break down the input string into a list of individual words, correct? Commented Jan 5, 2016 at 21:06

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.