Contents
Back
Forward

A4. Grammar

A 'verb' is a set of possible initial words in keyboard command, which are treated synonymously (for example, "wear'' and "don'') together with a 'grammar'. A grammar is a list of 'lines' which the parser tries to match, one at a time, and accepts the first one which matches. The directive
Verb [meta] <verb-word-1> ... <verb-word-n> <grammar>
creates a new verb. If it is said to be meta then it will count as 'out of the game': for instance "score'' or "save''. New synonyms can be added to an old verb with:
Verb <new-word-1> ... <new-word-n> = <existing-verb-word>
An old verb can be modified with the directive
Extend [only] <existing-word-1> ... <existing-word-n> [<priority>] <grammar>
If only is specified, the existing words given (which must all be from the same existing verb) are split off into a new independent copy of the verb. If not, the directive extends the whole existing verb. The priority can be first (insert this grammar at the head of the list), last (insert it at the end) or replace (throw away the old list and use this instead); the default is last.

A line is a list of 'tokens' together with the action generated if each token matches so that the line is accepted. The syntax of a line is

* <token-1> <token-2> ...<token-n> -> <action>
where 0 <= n <= 31. The action is named without initial ## signs and if an action which isn't in the standard library set is named then an action routine (named with the action name followed by Sub) must be defined somewhere in the game.

A grammar line can optionally be followed by the word reverse. This signals that the action to be generated has two parameters, but which have been parsed in the wrong order and need to swapped over. (Note that a topic is not a parameter, and nor is a preposition.)

A token matches a single particle of what has been typed. The possible tokens are:
"<word>"
that literal word only
noun
any object in scope
held
object held by the player
multi
one or more objects in scope
multiheld
one or more held objects
multiexcept
one or more in scope, except the other
multiinside
one or more in scope, inside the other
<attribute>
any object in scope which has the attribute
creature
an object in scope which is animate
noun = <Routine>
any object in scope passing the given test
scope = <Routine>
an object in this definition of scope
number
a number only
<Routine>
refer to this general parsing routine
topic
any text at all
special
any single word or number

Two or more literal words (only) can be written with slash signs / between them as alternatives. E.g., "in"/"on" matches either the word "in'' or the word "on''.

For the
noun = <Routine>
token, the test routine must decide whether or not the object in the noun variable is acceptable and return true or false.

For the
scope = <Routine>
token, the routine must look at the variable scope_stage. If this is 1, then it must decide whether or not to allow a multiple object (such as "all'') here and return true or false. If 2, then the routine may put objects into scope by calling either PlaceInScope(obj) to put just obj in, or ScopeWithin(obj) to put the contents of obj into scope. It must then return either true (to prevent any other objects from entering scope) or false (to let the parser put in all the usual objects). If scope_stage=3, it must print a suitable message to tell the player that this token was misunderstood.

A general parsing routine can match any text it likes. It should use wn, the variable holding the number of the word currently being parsed (counting from the verb being word 1) and the routine NextWord() to read the next word and move wn on by 1. The routine returns:

-1 if the user's input isn't understood,
0 if it's understood but doesn't refer to anything,
1 if there is a numerical value resulting, or
n if object n is understood.
In the case of a number, the actual value should be put into the variable parsed_number. On an unsuccessful match (returning -1) it doesn't matter what the final value of wn is. Otherwise it should be left pointing to the next thing after what the routine understood.


Contents / Back / Forward
Chapter I / Chapter II / Chapter III / Chapter IV / Chapter V / Chapter VI / Appendix
Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.