We now try an approach which is less non-deterministic. We will start by looking at:
sentence(In,Out) [-5pt]The idea is that sentence/2 takes in a list of words as input, finds a legal sentence and returns a result consisting of the input list minus all the words that formed the legal sentence.
We can define it:
sentence(S,S0):-Here is a rough semantics for sentence/2.noun_phrase(S,S1),
verb_phrase(S1,S0).
A sentence can be found at the front of a list of words if there is a noun phrase at the front of the list and a verb phrase immediately following.
This declarative reading should help to bridge the gap between what we want to be a sentence and the procedure for finding a sentence.
Here is the rest of the parser:
noun_phrase(NP,NP0):-As you can see, there is a remarkable sameness about each rule which, once you see what is going on, is fairly tedious to type in every time. So we turn to a facility that is built in to Prolog.determiner(NP,NP1),
noun(NP1,NP0).
verb_phrase(VP,VP0):-
verb(VP,VP1),
noun_phrase(VP1,VP0).
determiner([aRest],Rest).
determiner([theRest],Rest).
noun([manRest],Rest).
noun([cakeRest],Rest).
verb([ateRest],Rest).