next up previous contents
Next: Adding Arbitrary Prolog Up: Parsing in Prolog Previous: To Use the

How to Extract a Parse Tree

We can add an extra argument which can be used to return a result.

 
sentence([[np,NP],[vp,VP]]) 		  --> 		 noun_phrase(NP), verb_phrase(VP).

noun_phrase([[det,Det],[noun,Noun]]) --> determiner(Det), noun(Noun).

determiner(the) --> [the].

and so on

What we have done above is declare predicates sentence/3, noun_phrase/3, verb_phrase/3, determiner/3 and so on. The explicit argument is the first and the two others are added when the clause is read in by Prolog. Basically, Prolog expands a grammar rule with n arguments into a corresponding clause with n+2 arguments.

So what structure is returned from solving the goal:

 
sentence(Structure,[the,man,ate,a,cake],[])
 [-5pt]

The result is:
 
[[np,[[det,the],[noun,man]]],[vp,[...
 [-5pt]

Not too easy to read!
We can improve on this representation if we are allowed to use Prolog terms as arguments. For example, in foo(happy(fred),12) the term happy(fred) is one of the arguments of foo/2. Such a term is known as a compound term. We discuss this at greater length in chapter 10.

With the help of compound terms, we could tidy up our representation of sentence structure to something akin to:

 
sentence([np([det(the),noun(man)]),vp([...
 [-5pt]



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