Computer Science 3675
Fall 2000
Programming Assignment 6

Due: Monday, Nov 6.

The following is a very simple grammar for a tiny subset of English.

   <sentence>            ::= <noun-phrase> <verb-phrase>
                           | <noun-phrase> is <adjective>
   <noun-phrase>         ::= <proper-noun> 
                           | <determiner> <common-noun>
                           | <determiner> <adjective> <common-noun>
   <verb-phrase>         ::= <transitive-verb> <noun-phrase>
   <determiner>          ::= a | the
   <proper-noun>         ::= archie | jughead | betty | veronica
   <common-noun>         ::= malt | hamburger | dog | shop
   <transitive-verb>     ::= eats | likes
   <adjective>           ::= happy | hungry | sleepy
For example, here is a way to derive sentence "archie likes betty".
  <sentence>    => <noun-phrase> <verb-phrase>
                => <proper-noun> <verb-phrase>
                => archie <verb-phrase>
                => archie <transitive-verb> <noun-phrase>
                => archie likes <noun-phrase>
                => archie likes <proper-noun>
                => archie likes betty
A parser for a grammar does this process backwards. It takes a purported sentence as input, and either finds a derivation of the sentence (and so claims that the input is, in fact, a sentence) or claims that the input is not a sentence at all.

Logic programming makes it easy to write parsers for grammars. There is more than one form of "logic grammar". We will use one that is fairly easy to understand.

A purported sentence is a list of symbols. Sometimes, it is convenient to specify a list as a difference of two other lists. If A and B are lists, where B is a suffix of A, then the list A-B is defined to be the list X such that A = X ++ B. For example, [archie,likes,the,hamburger] - [the,hamburger] = [archie,likes]. That is, A-B is the result of removing list B from the end of list A. It is only defined when B is a suffix of A.

You might imagine implementing a grammar by writing a predicate for each nonterminal, where A(X) is true if list X can be generated by nonterminal A. It turns out to be more convenient to use difference lists. For each nonterminal A, you create a predicate A so that A(X,Y) is true if list X-Y is something that can be generated by nonterminal A. Precisely, you write predicate A so that A(X,Y) means

  1. X and Y are lists and Y is a suffix of X. That is, there is a list Z such that X = Z ++ Y.

  2. The list Z from (1) is a phrase that can be generated by nonterminal A.
For example, in our English grammar, properNoun([archie, eats, the, hamburger], [eats, the, hamburger]) is true since nonterminal properNoun can derive the phrase [archie]. Similarly, verbPhrase([eats, the, hamburger], [ ]) is true.

A logic grammar consists of a predicate for each nonterminal and a collection of axioms, one for each production in the grammar. For example, here is a fact for <sentence> that comes from the first production.

   Astarte:  case Sentence(?x,?y). <- Var{logic} r. 
                                      NounPhrase(x,r). 
				      VerbPhrase(r,y).

   Prolog:   sentence(X,Y) :- nounPhrase(X,R), verbPhrase(R,Y)
This fact says that a sentence can be a noun phrase followed by a verb phrase. Look at it carefully. It says that, to extract a sentence from the front of list X, leaving behind Y, you can do the following.

  1. Extract a noun phrase NP from the front of X, leaving behind R. That is, find a suffix R of L so that L = NP ++ R (for some list NP) and NP is a noun phrase. Notice that NP is never explicitly constructed here, but it is implicitly constructed by finding the suffix R that comes after it.

  2. Extract a verb phrase VP from the front of R, leaving behind Y. Again, VP is never explicitly constructed.
Lexical facts are very simple. For example, the fact that eats is a transitive verb can be stated as follows.
    Astarte: case TransitiveVerb("eats"::?r, ?r) <- ()
    Prolog: transitiveVerb([eats|R], R).
This says that you can extract a transitive verb, namely eats, from the front of list X, leaving behind R, provided X has the form eats::R.

Using these methods, write a logic program that tells whether a list is a sentence, using the simple grammar above. Run your program on a few examples, including at least two sentences and at least two non-sentences. To test whether "Archie likes the hamburger" is a sentence, write

  Astarte:  Sentence(["Archie", "likes", "the", "hamburger"], []).
  Prolog:   sentence([archie, likes, the, hamburger], []).
Alternatively, in Astarte you can write the sentence and explode it:
  Sentence(wsExplode "Archie likes the hamburger", []).
The Prolog interpreter will respond with Yes or No. The Astarte line will succeed if the list is a sentence, and will fail if it is not. To print whether "Archie likes the hamburger" is a sentence, you might write a tester
  Define TestSentence ?s. =
    Try
      Sentence(s,[]).
    then
      Writeln["yes"].
    else
      Writeln["no"].
    %Try
  %Define

For this exercise, use either Astarte or Prolog. To write in Astarte, read logic programming in Astarte. You must use logic programming style for this program. To use Prolog, use the following notes.

Writing and running Prolog programs

In prolog, you write [A|B] for the list whose head is A and whose tail is B.

Be sure to put a period at the end of every axiom. In Prolog, variables start with upper case letters, and constants start with lower case letters. Be careful about that. Predicate names also start with lower case letters. If you write Archie, you are writing a variable, not a constant. If you say

        properNoun([Archie|R] , R).
you are claiming that every word is a proper noun, since variable Archie can stand for any word at all.

Use the pl command on the machines in Austin 320 to run SWI Prolog. To use pl, first compile your file. If your Prolog program is in file myprog.pl, type

  pl -o myprog -c myprog.pl
This only compiles your program into an intermediate code. To run your program, use command
  myprog
You will be prompted for goals. Each goal must end on a period. After a result is shown, you can type a semicolon to ask for more solutions.

What to turn in

Turn the source of your program, using the handin program.