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