[MUD-Dev] string parsing

Chris Gray cg at ami-cg.GraySage.Edmonton.AB.CA
Sat Nov 1 11:08:25 CET 1997


[Felix C:]

:    object find_obj_in_obj(mixed *parsetree)
:    {
:	/*
:	 * parsetree = ({ obj1name, preposition, obj2name })
:	 *
:	 * find obj1 in obj2 (the code below has been simplified)
:	 */
:	return present(parsetree[0], present(parsetree[2]));
:    }
:
:Not actually very hackish, is it?  Finding an object in another object
:is a fairly common operation.

Well, are you going to have find_obj_on_obj, find_obj_beside_obj,
find_obj_under_obj, etc.? Wouldn't you also want 'present' to be able
to properly parse a noun phrase? That's where I was thinking you might
end up recursively calling parse_string. Nothing really bad here, I
guess - there probably aren't any truly tidy alternatives that work better.

:>    give [ARTICLE] ADJECTIVE* NOUN to [ARTICLE] ADJECTIVE* NOUN [PUNCTUATION]
:
:It looks as if your system used regular expressions, rather than a
:context-free grammar (a context-free grammar is just a set of
:production rules as in the example above).  The latter is much more
:powerful.  There is a great deal that you can find out merely by
:checking the syntax, which cuts down on the need for function calls
:that you mentioned.

Hmm. The syntax might have confused the issue. The metasymbols []* apply
to the word-types - they aren't used on characters like in a traditional
regexp. So, the above rule is read as:

    the word 'give', followed by
    an optional word of type ARTICLE, followed by
    zero or more words of type ADJECTIVE, followed by
    a word of type NOUN, followed by
    the word 'to', followed by
    an optional word of type ARTICLE, followed by
    zero or more words of type ADJECTIVE, followed by
    a word of type NOUN, followed by
    an optional word of type PUNCTUATION

I guess you could think of it as a regexp of words, but I never did.

:What is your new system like?

Erff. Full docs at: ftp.myrias.com:/pub/cg/MUD/AmigaMUD/docs.tar.Z
(Not sure of the filename at the end.) Briefly:

A grammar is basically a hash table of words. It will contain "separator
words" like 'in', 'on', 'from', etc. as well as the "verbs" that will be
the first word of a command. Associated with each verb word is a list of
the forms of that verb (only Verb0, Verb1 and Verb2 forms). Each such
element includes a reference to any "separator word" needed, and a
referenced to the MUD-code routine to handle the semantics of that verb
form. Synonyms can be entered - they just point at another entry. The
verb forms are:

    VerbTail - no handling of the rest of the command - it is available
	to the verb routine, word by word or as a single string. E.g.
	SAY hello to the mouse
    Verb0 - no noun-phrase allowed. e.g. NORTH; GET UP
    Verb1 - direct object wanted. Several can be given, separated by commas
	or 'and', and each will be passed to the handler in turn. E.g.
	GET the large rock; PUT DOWN the book and the rod.
    Verb2 - direct and indirect objects wanted. First one can be several,
	separated by commas or 'and'. E.g.
	PUT the sword, big orange pillow INTO trophy case.

The same word can be all of the last 3 kinds, and it will be disambiguated
based on the separator words. The set of articles and punctuation handled
is fixed. Builtin 'Parse' parses an input string against a grammar - it
handles several commands at one time from the string. I have a canonical
form for noun phrases, e.g. "pillow;big,orange", and I have a builtin to
match them (allowing several alternatives for the in-db parameter). This
is used within another builtin which will look at a list of objects in the
database, and try for a match with a given input noun-phrase. In my current
scenario, there is an input command handler which checks for special forms
(leading ", leading :, etc.) as well as aliases, before it passes the
input to 'Parse', with the main grammar.

There is more detailed documentation, and examples, in the 'Parsing'
section of the "ProgConcepts.txt" file in the docs archive.

--
Chris Gray   cg at ami-cg.GraySage.Edmonton.AB.CA



More information about the mud-dev-archive mailing list