|<< Prev||- Up -|
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
[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.
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.
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.
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.
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.
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]
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 .
|<< Prev||- Up -|