Debian Codegraph @

terps/advsys/advprs.c

raw symbols SCIP
1
2 /* advprs.c - adventure parser */
3 /*
4 Copyright (c) 1986, by David Michael Betz
5 All rights reserved
6 */
7
8 #include "header.h"
9 #include "advint.h"
10 #include "advdbs.h"
11
12 /* parser result variables */
13 int nouns[20];
14 int *adjectives[20];
15 static int actor,action,dobject,ndobjects,iobject;
16 static int flag;
17
18 /* external routines */
19 extern char *trm_get(char *line);
20
21 /* external variables */
22 extern char line[]; /* line buffer */
23
24 /* local variables */
25 static char *lptr; /* line pointer */
26 static int words[100]; /* word table */
27 static char *wtext[100];/* word text table */
28 static int *wptr; /* word pointer */
29 static int wcnt; /* word count */
30
31 static int verbs[3]; /* words in the verb phrase */
32 static int nnums[20]; /* noun word numbers */
33 static int nptr; /* noun pointer (actually, an index) */
34 static int adjs[100]; /* adjective lists */
35 static int anums[100]; /* adjective word numbers */
36 static int aptr; /* adjective pointer (actually, an index) */
37
38
39 /* Function Headers */
40 int parse1();
41 int getverb();
42 int getnoun();
43 int get_line();
44 int skip_spaces();
45 void show_noun(int n);
46 int get_word();
47 int spacep(int ch);
48 void parse_error();
49
50 /* parse - read and parse an input line */
51 int parse()
52 {
53 if (!parse1())
54 return (FALSE);
55 setvalue(V_ACTOR,actor);
56 setvalue(V_ACTION,action);
57 setvalue(V_DOBJECT,dobject);
58 setvalue(V_NDOBJECTS,ndobjects);
59 setvalue(V_IOBJECT,iobject);
60 return (TRUE);
61 }
62
63 /* next - get the next command (next direct object) */
64 int next()
65 {
66 if (getvalue(V_NDOBJECTS) > 1) {
67 setvalue(V_ACTOR,actor);
68 setvalue(V_ACTION,action);
69 setvalue(V_DOBJECT,getvalue(V_DOBJECT) + 1);
70 setvalue(V_NDOBJECTS,getvalue(V_NDOBJECTS) - 1);
71 setvalue(V_IOBJECT,iobject);
72 return (TRUE);
73 }
74 else
75 return (FALSE);
76 }
77
78 /* parse1 - the main parser */
79 int parse1()
80 {
81 int noun1,cnt1,noun2,cnt2;
82 int preposition,flag;
83
84 /* initialize */
85 noun1 = noun2 = NIL; cnt1 = cnt2 = 0;
86 nptr = aptr = 0;
87 preposition = 0;
88 flag = 0;
89
90 /* initialize the parser result variables */
91 actor = action = dobject = iobject = NIL;
92 ndobjects = 0;
93
94 /* get an input line */
95 if (!get_line())
96 return (FALSE);
97
98 /* check for actor */
99 if (wtype(*wptr) == WT_ADJECTIVE || wtype(*wptr) == WT_NOUN) {
100 if ((actor = getnoun()) == NIL)
101 return (FALSE);
102 flag |= A_ACTOR;
103 }
104
105 /* get verb phrase */
106 if (!getverb())
107 return (FALSE);
108
109 /* direct object, preposition and indirect object */
110 if (*wptr) {
111
112 /* get the first set of noun phrases (direct objects) */
113 noun1 = nptr+1;
114 for (;;) {
115
116 /* get the next direct object */
117 if (getnoun() == NIL)
118 return (FALSE);
119 ++cnt1;
120
121 /* check for more direct objects */
122 if (*wptr == NIL || wtype(*wptr) != WT_CONJUNCTION)
123 break;
124 wptr++;
125 }
126
127 /* get the preposition and indirect object */
128 if (*wptr) {
129
130 /* get the preposition */
131 if (wtype(*wptr) == WT_PREPOSITION)
132 preposition = *wptr++;
133
134 /* get the second set of noun phrases (indirect object) */
135 noun2 = nptr+1;
136 for (;;) {
137
138 /* get the next direct object */
139 if (getnoun() == NIL)
140 return (FALSE);
141 ++cnt2;
142
143 /* check for more direct objects */
144 if (*wptr == NIL || wtype(*wptr) != WT_CONJUNCTION)
145 break;
146 wptr++;
147 }
148 }
149
150 /* make sure this is the end of the sentence */
151 if (*wptr) {
152 parse_error();
153 return (FALSE);
154 }
155 }
156
157 /* setup the direct and indirect objects */
158 if (preposition) {
159 if (cnt2 > 1) {
160 parse_error();
161 return (FALSE);
162 }
163 dobject = noun1;
164 ndobjects = cnt1;
165 iobject = noun2;
166 }
167 else if (noun2) {
168 if (cnt1 > 1) {
169 parse_error();
170 return (FALSE);
171 }
172 preposition = findword("to");
173 dobject = noun2;
174 ndobjects = cnt2;
175 iobject = noun1;
176 }
177 else {
178 dobject = noun1;
179 ndobjects = cnt1;
180 }
181
182 /* setup the flags for the action lookup */
183 if (dobject) flag |= A_DOBJECT;
184 if (iobject) flag |= A_IOBJECT;
185
186 /* find the action */
187 if ((action = findaction(verbs,preposition,flag)) == NIL) {
188 parse_error();
189 return (FALSE);
190 }
191
192 /* return successfully */
193 return (TRUE);
194 }
195
196 /* getverb - get a verb phrase and return the action it refers to */
197 int getverb()
198 {
199 /* get the verb */
200 if (*wptr == NIL || wtype(*wptr) != WT_VERB) {
201 parse_error();
202 return (NIL);
203 }
204 verbs[0] = *wptr++;
205 verbs[1] = NIL;
206
207 /* check for a word following the verb */
208 if (*wptr) {
209 verbs[1] = *wptr;
210 verbs[2] = NIL;
211 if (checkverb(verbs))
212 wptr++;
213 else {
214 verbs[1] = words[wcnt-1];
215 if (checkverb(verbs))
216 words[--wcnt] = NIL;
217 else {
218 verbs[1] = NIL;
219 if (!checkverb(verbs)) {
220 parse_error();
221 return (NIL);
222 }
223 }
224 }
225 }
226 return (T);
227 }
228
229 /* getnoun - get a noun phrase and return the object it refers to */
230 int getnoun()
231 {
232 /* initialize the adjective list pointer */
233 adjectives[nptr] = adjs + aptr;
234
235 /* get the optional article */
236 if (*wptr != NIL && wtype(*wptr) == WT_ARTICLE)
237 wptr++;
238
239 /* get optional adjectives */
240 while (*wptr != NIL && wtype(*wptr) == WT_ADJECTIVE) {
241 adjs[aptr] = *wptr++;
242 anums[aptr] = wptr - words - 1;
243 aptr++;
244 }
245 adjs[aptr++] = 0;
246
247 /* get the noun itself */
248 if (*wptr == NIL || wtype(*wptr) != WT_NOUN) {
249 parse_error();
250 return (NIL);
251 }
252
253 /* save the noun */
254 nouns[nptr] = *wptr++;
255 nnums[nptr] = wptr - words - 1;
256 return (++nptr);
257 }
258
259 /* get_line - get the input line and lookup each word */
260 int get_line()
261 {
262 /* read an input line */
263 trm_chr('>');
264 if ((lptr = trm_get(line)) == NULL) {
265 trm_str("Speak up! I can't hear you!\n");
266 return (FALSE);
267 }
268
269 /* get each word on the line */
270 for (wcnt = 0; skip_spaces(); wcnt++)
271 if (get_word() == NIL)
272 return (FALSE);
273 words[wcnt] = NIL;
274
275 /* check for a blank line */
276 if (wcnt == 0) {
277 trm_str("Speak up! I can't hear you!\n");
278 return (FALSE);
279 }
280
281 /* point to the first word and return successfully */
282 wptr = words;
283 return (TRUE);
284 }
285
286 /* skip_spaces - skip leading spaces */
287 int skip_spaces()
288 {
289 while (spacep(*lptr))
290 lptr++;
291 return (*lptr != EOS);
292 }
293
294 /* show_noun - show a noun phrase */
295 void show_noun(int n)
296 {
297 int adj,*p;
298
299 /* print the adjectives */
300 for (p = adjectives[n-1], adj = FALSE; *p; p++, adj = TRUE) {
301 if (adj) trm_chr(' ');
302 trm_str(wtext[anums[p-adjs]]);
303 }
304
305 /* print the noun */
306 if (adj) trm_chr(' ');
307 trm_str(wtext[nnums[n-1]]);
308 }
309
310 /* get_word - get the next word */
311 int get_word()
312 {
313 int ch;
314
315 /* get the next word */
316 for (wtext[wcnt] = lptr; (ch = *lptr) != EOS && !spacep(ch); )
317 *lptr++ = (isupper(ch) ? tolower(ch) : ch);
318 if (*lptr != EOS) *lptr++ = EOS;
319
320 /* look up the word */
321 if (words[wcnt] = findword(wtext[wcnt]))
322 return (words[wcnt]);
323 else {
324 trm_str("I don't know the word \"");
325 trm_str(wtext[wcnt]);
326 trm_str("\".\n");
327 return (NIL);
328 }
329 }
330
331 /* spacep - is this character a space? */
332 int spacep(int ch)
333 {
334 return (ch == ' ' || ch == ',' || ch == '.');
335 }
336
337 /* parse_error - announce a parsing error */
338 void parse_error()
339 {
340 trm_str("I don't understand.\n");
341 }