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