1
<HTML><HEAD><TITLE>Section A4: Grammar</TITLE></HEAD>
2
<BODY BGCOLOR="#FFFFFF">
4
<TR><TD Valign="top"><A HREF="contents.html">Contents</A><BR><A HREF="sectionA3.html">Back</A><BR><A HREF="sectionA5.html">Forward</A><TD bgcolor="#F5DEB3"><BLOCKQUOTE><H3>A4. Grammar</H3></BLOCKQUOTE><TR><TD><TD>
5
A 'verb' is a set of possible initial words in keyboard command, which are
6
treated synonymously (for example, "wear'' and "don'') together with
7
a 'grammar'. A grammar is a list of 'lines' which the parser tries to match,
8
one at a time, and accepts the first one which matches. The directive
10
Verb [<TT>meta</TT>] <I><B><verb-word-1></B></I> ... <I><B><verb-word-<I>n</I>></B></I> <I><B><grammar></B></I>
13
creates a new verb. If it is said to be <TT>meta</TT> then it will count as 'out
14
of the game': for instance "score'' or "save''. New synonyms can be added
17
Verb <I><B><new-word-1></B></I> ... <I><B><new-word-<I>n</I>></B></I> <TT>=</TT> <I><B><existing-verb-word></B></I>
20
An old verb can be modified with the directive
22
Extend [<TT>only</TT>] <I><B><existing-word-1></B></I> ... <I><B><existing-word-<I>n</I>></B></I> [<I><B><priority></B></I>] <I><B><grammar></B></I>
25
If <TT>only</TT> is specified, the existing words given (which must all be from the same
26
existing verb) are split off into a new independent copy of the verb.
27
If not, the directive extends the whole existing verb.
28
The priority can be <TT>first</TT> (insert
29
this grammar at the head of the list), <TT>last</TT> (insert it at the end) or <TT>replace</TT>
30
(throw away the old list and use this instead); the default is <TT>last</TT>.
33
A line is a list of 'tokens' together with the action generated if each
34
token matches so that the line is accepted. The syntax of a line is
36
<TT>*</TT> <I><B><token-1></B></I> <I><B><token-2></B></I> ...<I><B><token-<I>n</I>></B></I> <TT>-></TT> <I><B><action></B></I>
41
The action is named without initial <TT>##</TT> signs and if
42
an action which isn't in the standard library set is named then an action
43
routine (named with the action name followed by <TT>Sub</TT>) must be defined
44
somewhere in the game.
47
A grammar line can optionally be followed by the word
48
<TT>reverse</TT>. This signals that the action to be generated has
49
two parameters, but which have been parsed in the wrong
50
order and need to swapped over. (Note that a <TT>topic</TT> is
51
not a parameter, and nor is a preposition.)
56
A token matches a single particle of what has been typed. The possible
59
<BR><TABLE Border><TR><TD> <TABLE Border><TR><TD><TT>"</TT><I><B><word></B></I><TT>"</TT></TABLE> <TD> that literal word only
60
<TR><TD> <TABLE Border><TR><TD><TT>noun</TT></TABLE> <TD> any object in scope
61
<TR><TD> <TABLE Border><TR><TD><TT>held</TT></TABLE> <TD> object held by the player
62
<TR><TD> <TABLE Border><TR><TD><TT>multi</TT></TABLE> <TD> one or more objects in scope
63
<TR><TD> <TABLE Border><TR><TD><TT>multiheld</TT></TABLE> <TD> one or more held objects
64
<TR><TD> <TABLE Border><TR><TD><TT>multiexcept</TT></TABLE> <TD> one or more in scope, except the other
65
<TR><TD> <TABLE Border><TR><TD><TT>multiinside</TT></TABLE> <TD> one or more in scope, inside the other
66
<TR><TD> <TABLE Border><TR><TD><I><B><attribute></B></I></TABLE> <TD> any object in scope which has the attribute
67
<TR><TD> <TABLE Border><TR><TD><TT>creature</TT></TABLE> <TD> an object in scope which is <TT>animate</TT>
68
<TR><TD> <TABLE Border><TR><TD><TT>noun = </TT><I><B><Routine></B></I></TABLE> <TD> any object in scope passing the given test
69
<TR><TD> <TABLE Border><TR><TD><TT>scope = </TT><I><B><Routine></B></I></TABLE> <TD> an object in this definition of scope
70
<TR><TD> <TABLE Border><TR><TD><TT>number</TT></TABLE> <TD> a number only
71
<TR><TD> <TABLE Border><TR><TD><I><B><Routine></B></I></TABLE> <TD> refer to this general parsing routine
72
<TR><TD> <TABLE Border><TR><TD><TT>topic</TT></TABLE> <TD> any text at all
73
<TR><TD> <TABLE Border><TR><TD><TT>special</TT></TABLE> <TD> any single word or number
77
Two or more literal words (only) can be written with slash
78
signs <TT>/</TT> between them as alternatives. E.g., <TT>"in"/"on"</TT>
79
matches either the word "in'' or the word "on''.
82
For the <TABLE Border><TR><TD><TT>noun = </TT><I><B><Routine></B></I></TABLE> token,
83
the test routine must decide whether or not the
84
object in the <TT>noun</TT> variable is acceptable and return true or false.
87
For the <TABLE Border><TR><TD><TT>scope = </TT><I><B><Routine></B></I></TABLE> token,
88
the routine must look at the variable <TT>scope_stage</TT>.
89
If this is 1, then it must decide whether or not to allow a multiple object
90
(such as "all'') here and return true or false. If 2, then the routine may
91
put objects into scope by calling either <TT>PlaceInScope(obj)</TT> to put just <TT>obj</TT>
92
in, or <TT>ScopeWithin(obj)</TT> to put the contents of <TT>obj</TT> into scope. It must
93
then return either true (to prevent any other objects from entering scope) or
94
false (to let the parser put in all the usual objects). If <TT>scope_stage=3</TT>,
95
it must print a suitable message to tell the player that this token was
99
A general parsing routine can match any text it likes. It should use <TT>wn</TT>,
100
the variable holding the number of the word currently being parsed (counting
101
from the verb being word 1) and the routine <TT>NextWord()</TT> to read the next
102
word and move <TT>wn</TT> on by 1. The routine returns:
104
-1<SAMP> </SAMP> if the user's input isn't understood,<BR>
105
0<SAMP> </SAMP> if it's understood but doesn't refer to anything,<BR>
106
1<SAMP> </SAMP> if there is a numerical value resulting, or<BR>
107
<I>n</I><SAMP> </SAMP> if object <I>n</I> is understood.<BR>
110
In the case of a number, the actual value should be put into the variable
111
<TT>parsed_number</TT>.
112
On an unsuccessful match (returning -1) it doesn't matter what the final
113
value of <TT>wn</TT> is. Otherwise it should be left pointing to the next
114
thing <I> after</I> what the routine understood.
117
<HR><A HREF="contents.html">Contents</A> / <A HREF="sectionA3.html">Back</A> / <A HREF="sectionA5.html">Forward</A> <BR>
118
<A HREF="chapter1.html">Chapter I</A> / <A HREF="chapter2.html">Chapter II</A> / <A HREF="chapter3.html">Chapter III</A> / <A HREF="chapter4.html">Chapter IV</A> / <A HREF="chapter5.html">Chapter V</A> / <A HREF="chapter6.html">Chapter VI</A> / <A HREF="chapterA.html">Appendix</A><HR><SMALL><I>Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright © Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>