12.3 Practical Session

In this practical session, we want to combine what we learned today with some bits and pieces that we met earlier in the course. The goal is to write a program for running a DCG grammar on a testsuite, so that the performance of the grammar can be checked. A testsuite is a file that contains lots of possible inputs for a program, in our case a file that contains lots of lists representing grammatical or ungrammatical sentences, such as [the,woman,shoots,the,cow,under,the,shower] or [him,shoots,woman]. The test program should take this file, run the grammar on each of the sentences and store the results in another file. We can then look at the output file to check whether the grammar answered everywhere the way it should. When developing grammars, testsuites like this are extremely useful to make sure that the changes we make to the grammar don't have any unwanted effects.

12.3.1 Step 1

Take the DCG that you built in the practical session of Chapter 8 and turn it into a module, exporting the predicate s/3, i.e. the predicate that lets you parse sentences and returns the parse tree in its first argument.

12.3.2 Step 2

In the practical session of Chapter 9, you had to write a program for pretty printing parse trees onto the screen. Turn that into a module as well.

12.3.3 Step 3

Now, modify the program, so that it prints the tree not to the screen but to a given stream. That means that the predicate pptree should now be a two-place predicate taking the Prolog representation of a parse tree and a stream as arguments.

12.3.4 Step 4

Import both modules into a file and define a two-place predicate test which takes a list representing a sentence (such as [a,woman,shoots]), parses it and writes the result to the file specified by the second argument of test. Check that everything is working as it should.

12.3.5 Step 5

Finally, modify test/2, so that it takes a filename instead of a sentence as its first argument and then reads in the sentences given in the file one by one, parses them and writes the sentence as well as the parsing result into the output file. If, e.g, your input file looked like this:


the output file should look similar to this:

[the, cow, under, the, table, shoots]
[a, dead, woman, likes, he]

12.3.6 Step 6

Now, if you are in for some real Prolog hacking, try to write a module that reads in sentences terminated by a full stop or a line break from a file, so that you can give your testsuite as

the cow under the table shoots .
a dead woman likes he .

instead of


Patrick Blackburn, Johan Bos and Kristina Striegnitz
Version 1.2.5 (20030212)