Search                        Top                                  Index

-- Analysing sentences: the procedure setup ---------------------------

LIB GRAMMAR also provides a program called SETUP which enables you to
transform your grammar and lexicon into a program which will analyse
sentences to see if they are legal, according to the grammar.

Start by typing

    setup(mygram, mylex);

You could put that in your file, after defining mygram and mylex. When
you get the colon prompt, check that analysing procedures have been
created, corresponding to the rules in mygram. E.g. type:

    s =>
    vp =>
    np =>

-- Seeing if your grammar 'accepts' a sentence -------------------------

Here is how you check whether your grammar will accept a sentence: put
the sentence in a list, then apply the procedure S to it (remember, S
was created by SETUP). Here is an example:

    s([the girl kissed the man]) ==>
    s([the computer added each number]) ==>

Use words and phrases which fit your own grammar and lexicon. Notice how
the procedure S creates a list of lists showing how the sentence is
broken down into parts.

-- Try more sentences, using the macro "---" --------------------------

Try some different sentences.

It's a chore to keep on having to type:

    s([    ]) ==>

LIB GRAMMAR defines a 'macro' abbreviation of three hyphens. To use it
just type the words of the sentence after the three hyphens, that is you
can type:

    --- the computer kissed the girl

instead of:

    s([the computer kissed the girl]) ==>

Here are some more examples of the use of the three hyphens:

    --- the big number added the computer
    --- the number added every computer

It's important that you put a space after ---.

The response <false> means that the sentence was not acceptable to the
analysing procedures.

-- Some examples with preposition phrases ------------------------------

Try some examples with prepositional phrases, for example:

    --- the man in the car kissed the cup
    --- the computer hated every number in the room

-- Using 'donouns' -----------------------------------------------------

To extend the power of the system type:

    true -> donouns;

You can then use unknown nouns and they will be accepted by the program if
the context is suitable, e.g.

    --- the fozzle teased every grumpet

-- Use "trace" to find out more ---------------------------------------

You can see in more detail what is going on if you trace your
procedures. This will show you that in order to get an analysis, the
computer tries all sorts of guesses as to what should be coming next,
which fail (result is <FALSE>), before it gets the right one. So this is
not a very intelligent language analysing system. Try:

    trace s vp np snp pp verb noun prep det;
    --- the girl hated the man in the car

Beware, you'll get a lot of printing! If you need to interrupt you can
do so using CTRL-c (I.e. hold down the button marked "Control", and
while still holding it, tap the "C" key.

-- Tracing gives more information if the sentence is unacceptable ------

You can also see what happens when an unacceptable sentence is provided. The
program makes lots of attempts, but they don't lead anywhere:

    --- the big cup in the room hated the computer

    --- each number added the car in the green room

The trace printing will show how the process of trying to find a
suitable analysis of the sentence in accordance with the grammar is a
process of SEARCH, i.e. the computer has to search for suitable way of
dividing up the sentence and linking its components to the various
rules of the grammar. Later you will learn a lot more about programs
that search for the solution to a problem.

-- How to turn off tracing ----------------------------------------------

You can suppress the amount of printout by untracing the procedures

    untrace ;

-- Using "showtree" to get a picture of a parse tree ------------------

The Pop-11 library includes a rather clever procedure called showtree
that can be used to display a parsed sentence as a tree diagram. To
make it available do

    uses showtree

That may take a little while to compile. You can then use showtree to
print out the result of parsing a sentence thus, try these

    showtree(s([the cup in the room hated the computer]));

    showtree(s([each number added the car in the room]));

I.e. apply "s" to a list of words to get a list of lists showing the
sentence structure, and apply showtree to the result of "s". Compare
that with what was previously printed out to show the tree structure.

-- Exercise: extend the grammar to allow several adjectives -----------

Can you also extend the grammar so that it will accept several
adjectives in a row before a noun, e.g.

    the happy little man
    the old old old car
    the big clever old old blue tree


    1. Introduce a grammatical category adjectival phrases ("adjphrase")
    and extend the definition of "snp" to include an adjphrase between
    "det" and "noun".

    2. Define a rule for adjphrase which allows an adjphrase to be
        Either  just an adjective
        OR      an adjphrase followed by an adjective

    That recursive definition should allow an adjphrase to have an
    arbitrary number of adjectives. Why?

Try that and use "generate" to produce examples of your extended
grammar. Also use "setup" to create a parser based on your new grammar
and use the "---" operator to test sentences with several adjectives in
a row. Use "showtree" to see what the parse tree looks like.

-- Try to stop the production or acceptance of "stupid" sentences -----

The grammar and lexicon given above allow some quite silly sentences to
be produced, using -generate-, or to be analysed after using -setup- to
create parsing programs. Try modifying the grammar and lexicon so as
to constrain the "grammatical" sentences to be more sensible.

-- Summary -------------------------------------------------------------

LIB GRAMMAR makes available the procedures SETUP and GENERATE.
LIB LEXICON defines the lexicon LEXICON, and LIB GRAMMAR1 defines the
grammar GRAMMAR1.

You can parse some more complex sentences if you do

    lib grammar2;
    lib lexicon;    ;;; unless done previously
    setup(grammar2, lexicon);

Print out GRAMMAR2 and LEXICON (using "==>" and then see if you can work
out what sorts of sentences will be accepted and parsed, e.g.

    true -> donouns;
    setup(grammar2, lexicon);
    --- he put a big dog into each car
    --- he smiled at every man who thought he liked her

Try more of your own.


Note for advanced programmers:

There is an extension to LIB GRAMMAR called LIB FACETS which enables you
to associate semantic rules (i.e. meaning rules) with a grammar. For
details see HELP * FACETS.

There is another extension, called LIB * TPARSE, which uses the Poplog
"process" mechanism to overcome a serious limitation of lib grammar,
namely that parsers produced by setup can only find a single parse for
each sentence, whereas in fact a sentence may have several different
parses. Give an example.

[Modified by A.Sloman at Birmingham 1994]