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