| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "header.h" |
| 9 |
#include "advint.h" |
| 10 |
#include "advdbs.h" |
| 11 |
|
| 12 |
|
| 13 |
int nouns[20]; |
| 14 |
int *adjectives[20]; |
| 15 |
static int actor,action,dobject,ndobjects,iobject; |
| 16 |
static int flag; |
| 17 |
|
| 18 |
|
| 19 |
extern char *trm_get(char *line); |
| 20 |
|
| 21 |
|
| 22 |
extern char line[]; |
| 23 |
|
| 24 |
|
| 25 |
static char *lptr; |
| 26 |
static int words[100]; |
| 27 |
static char *wtext[100]; |
| 28 |
static int *wptr; |
| 29 |
static int wcnt; |
| 30 |
|
| 31 |
static int verbs[3]; |
| 32 |
static int nnums[20]; |
| 33 |
static int nptr; |
| 34 |
static int adjs[100]; |
| 35 |
static int anums[100]; |
| 36 |
static int aptr; |
| 37 |
|
| 38 |
|
| 39 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 79 |
int parse1() |
| 80 |
{ |
| 81 |
int noun1,cnt1,noun2,cnt2; |
| 82 |
int preposition,flag; |
| 83 |
|
| 84 |
|
| 85 |
noun1 = noun2 = NIL; cnt1 = cnt2 = 0; |
| 86 |
nptr = aptr = 0; |
| 87 |
preposition = 0; |
| 88 |
flag = 0; |
| 89 |
|
| 90 |
|
| 91 |
actor = action = dobject = iobject = NIL; |
| 92 |
ndobjects = 0; |
| 93 |
|
| 94 |
|
| 95 |
if (!get_line()) |
| 96 |
return (FALSE); |
| 97 |
|
| 98 |
|
| 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 |
|
| 106 |
if (!getverb()) |
| 107 |
return (FALSE); |
| 108 |
|
| 109 |
|
| 110 |
if (*wptr) { |
| 111 |
|
| 112 |
|
| 113 |
noun1 = nptr+1; |
| 114 |
for (;;) { |
| 115 |
|
| 116 |
|
| 117 |
if (getnoun() == NIL) |
| 118 |
return (FALSE); |
| 119 |
++cnt1; |
| 120 |
|
| 121 |
|
| 122 |
if (*wptr == NIL || wtype(*wptr) != WT_CONJUNCTION) |
| 123 |
break; |
| 124 |
wptr++; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
if (*wptr) { |
| 129 |
|
| 130 |
|
| 131 |
if (wtype(*wptr) == WT_PREPOSITION) |
| 132 |
preposition = *wptr++; |
| 133 |
|
| 134 |
|
| 135 |
noun2 = nptr+1; |
| 136 |
for (;;) { |
| 137 |
|
| 138 |
|
| 139 |
if (getnoun() == NIL) |
| 140 |
return (FALSE); |
| 141 |
++cnt2; |
| 142 |
|
| 143 |
|
| 144 |
if (*wptr == NIL || wtype(*wptr) != WT_CONJUNCTION) |
| 145 |
break; |
| 146 |
wptr++; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
if (*wptr) { |
| 152 |
parse_error(); |
| 153 |
return (FALSE); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 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 |
|
| 183 |
if (dobject) flag |= A_DOBJECT; |
| 184 |
if (iobject) flag |= A_IOBJECT; |
| 185 |
|
| 186 |
|
| 187 |
if ((action = findaction(verbs,preposition,flag)) == NIL) { |
| 188 |
parse_error(); |
| 189 |
return (FALSE); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
return (TRUE); |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
int getverb() |
| 198 |
{ |
| 199 |
|
| 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 |
|
| 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 |
|
| 230 |
int getnoun() |
| 231 |
{ |
| 232 |
|
| 233 |
adjectives[nptr] = adjs + aptr; |
| 234 |
|
| 235 |
|
| 236 |
if (*wptr != NIL && wtype(*wptr) == WT_ARTICLE) |
| 237 |
wptr++; |
| 238 |
|
| 239 |
|
| 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 |
|
| 248 |
if (*wptr == NIL || wtype(*wptr) != WT_NOUN) { |
| 249 |
parse_error(); |
| 250 |
return (NIL); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
nouns[nptr] = *wptr++; |
| 255 |
nnums[nptr] = wptr - words - 1; |
| 256 |
return (++nptr); |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
int get_line() |
| 261 |
{ |
| 262 |
|
| 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 |
|
| 270 |
for (wcnt = 0; skip_spaces(); wcnt++) |
| 271 |
if (get_word() == NIL) |
| 272 |
return (FALSE); |
| 273 |
words[wcnt] = NIL; |
| 274 |
|
| 275 |
|
| 276 |
if (wcnt == 0) { |
| 277 |
trm_str("Speak up! I can't hear you!\n"); |
| 278 |
return (FALSE); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
wptr = words; |
| 283 |
return (TRUE); |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
int skip_spaces() |
| 288 |
{ |
| 289 |
while (spacep(*lptr)) |
| 290 |
lptr++; |
| 291 |
return (*lptr != EOS); |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
void show_noun(int n) |
| 296 |
{ |
| 297 |
int adj,*p; |
| 298 |
|
| 299 |
|
| 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 |
|
| 306 |
if (adj) trm_chr(' '); |
| 307 |
trm_str(wtext[nnums[n-1]]); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
int get_word() |
| 312 |
{ |
| 313 |
int ch; |
| 314 |
|
| 315 |
|
| 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 |
|
| 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 |
|
| 332 |
int spacep(int ch) |
| 333 |
{ |
| 334 |
return (ch == ' ' || ch == ',' || ch == '.'); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
void parse_error() |
| 339 |
{ |
| 340 |
trm_str("I don't understand.\n"); |
| 341 |
} |