parse.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /* $NetBSD: parse.c,v 1.14 2004/01/27 20:30:29 jsm Exp $ */
  2. /*
  3. * Copyright (c) 1983, 1993
  4. * The Regents of the University of California. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the University nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. #include <sys/cdefs.h>
  31. #ifndef lint
  32. #if 0
  33. static char sccsid[] = "@(#)parse.c 8.2 (Berkeley) 4/28/95";
  34. #else
  35. __RCSID("$NetBSD: parse.c,v 1.14 2004/01/27 20:30:29 jsm Exp $");
  36. #endif
  37. #endif /* not lint */
  38. #include "extern.h"
  39. #define HASHSIZE 256
  40. #define HASHMUL 81
  41. #define HASHMASK (HASHSIZE - 1)
  42. static int hash(const char *);
  43. static void install(struct wlist *);
  44. static struct wlist *lookup(const char *);
  45. static struct wlist *hashtab[HASHSIZE];
  46. void
  47. wordinit()
  48. {
  49. struct wlist *w;
  50. for (w = wlist; w->string; w++)
  51. install(w);
  52. }
  53. static int
  54. hash(s)
  55. const char *s;
  56. {
  57. int hashval = 0;
  58. while (*s) {
  59. hashval += *s++;
  60. hashval *= HASHMUL;
  61. hashval &= HASHMASK;
  62. }
  63. return hashval;
  64. }
  65. static struct wlist *
  66. lookup(s)
  67. const char *s;
  68. {
  69. struct wlist *wp;
  70. for (wp = hashtab[hash(s)]; wp != NULL; wp = wp->next)
  71. if (*s == *wp->string && strcmp(s, wp->string) == 0)
  72. return wp;
  73. return NULL;
  74. }
  75. static void
  76. install(wp)
  77. struct wlist *wp;
  78. {
  79. int hashval;
  80. if (lookup(wp->string) == NULL) {
  81. hashval = hash(wp->string);
  82. wp->next = hashtab[hashval];
  83. hashtab[hashval] = wp;
  84. } else
  85. printf("Multiply defined %s.\n", wp->string);
  86. }
  87. void
  88. parse()
  89. {
  90. struct wlist *wp;
  91. int n;
  92. int flag;
  93. wordnumber = 0; /* for cypher */
  94. for (n = 0; n <= wordcount; n++) {
  95. if ((wp = lookup(words[n])) == NULL) {
  96. wordvalue[n] = -1;
  97. wordtype[n] = -1;
  98. } else {
  99. wordvalue[n] = wp->value;
  100. wordtype[n] = wp->article;
  101. }
  102. }
  103. /* We never use adjectives for anything, so yank them all. */
  104. for (n = 1; n < wordcount; n++)
  105. if (wordtype[n] == ADJS) {
  106. int i;
  107. for (i = n + 1; i < wordcount; i++) {
  108. wordtype[i - 1] = wordtype[i];
  109. wordvalue[i - 1] = wordvalue[i];
  110. strcpy(words[i - 1], words[i]);
  111. }
  112. wordcount--;
  113. }
  114. /* Don't let a comma mean AND if followed by a verb. */
  115. for (n = 0; n < wordcount; n++)
  116. if (wordvalue[n] == AND && words[n][0] == ','
  117. && wordtype[n + 1] == VERB) {
  118. wordvalue[n] = -1;
  119. wordtype[n] = -1;
  120. }
  121. /* Trim "AND AND" which can happen naturally at the end of a
  122. * comma-delimited list.
  123. */
  124. for (n = 1; n < wordcount; n++)
  125. if (wordvalue[n - 1] == AND && wordvalue[n] == AND) {
  126. int i;
  127. for (i = n + 1; i < wordcount; i++) {
  128. wordtype[i - 1] = wordtype[i];
  129. wordvalue[i - 1] = wordvalue[i];
  130. strcpy(words[i - 1], words[i]);
  131. }
  132. wordcount--;
  133. }
  134. /* If there is a sequence (NOUN | OBJECT) AND EVERYTHING
  135. * then move all the EVERYTHINGs to the beginning, since that's where
  136. * they're expected. We can't get rid of the NOUNs and OBJECTs in
  137. * case they aren't in EVERYTHING (i.e. not here or nonexistent).
  138. */
  139. flag = 1;
  140. while (flag) {
  141. flag = 0;
  142. for (n = 1; n < wordcount; n++)
  143. if ((wordtype[n - 1] == NOUNS || wordtype[n - 1] == OBJECT) &&
  144. wordvalue[n] == AND && wordvalue[n + 1] == EVERYTHING) {
  145. char tmpword[WORDLEN];
  146. wordvalue[n + 1] = wordvalue[n - 1];
  147. wordvalue[n - 1] = EVERYTHING;
  148. wordtype[n + 1] = wordtype[n - 1];
  149. wordtype[n - 1] = OBJECT;
  150. strcpy(tmpword, words[n - 1]);
  151. strcpy(words[n - 1], words[n + 1]);
  152. strcpy(words[n + 1], tmpword);
  153. flag = 1;
  154. }
  155. /* And trim EVERYTHING AND EVERYTHING. */
  156. for (n = 1; n < wordcount; n++)
  157. if (wordvalue[n - 1] == EVERYTHING &&
  158. wordvalue[n] == AND && wordvalue[n + 1] == EVERYTHING) {
  159. int i;
  160. for (i = n + 1; i < wordcount; i++) {
  161. wordtype[i - 1] = wordtype[i + 1];
  162. wordvalue[i - 1] = wordvalue[i + 1];
  163. strcpy(words[i - 1], words[i + 1]);
  164. }
  165. wordcount--;
  166. wordcount--;
  167. flag = 1;
  168. }
  169. }
  170. }