next up previous contents
Next: Prolog Grammar Rules Up: Parsing in Prolog Previous: First Attempt at

A Second Approach

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

noun_phrase(S S1)

verb_phrase(S1 S0).

Here is a rough semantics for sentence/2.
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):-

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

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.



Paul Brna
Mon May 24 20:14:48 BST 1999