Path: utzoo!attcan!uunet!lll-winken!ames!elroy!orion.cf.uci.edu!uci-ics!siam.ics.uci.edu!schmidt From: schmidt@siam.ics.uci.edu (Doug Schmidt) Newsgroups: comp.lang.modula2 Subject: Re: Modula 2 keywords Keywords: keywords Message-ID: <3741@paris.ics.uci.edu> Date: 12 Jan 89 01:07:57 GMT References: <3486@paris.ics.uci.edu> Sender: news@paris.ics.uci.edu Reply-To: Doug Schmidt Organization: University of California, Irvine - Dept of ICS Lines: 178 Hi, Thanks to Frode Odegard, I now have a list of Modula2 reserved words, and in return, I am distributing the following pair of C routines that recognize elements of this set of reserved words in O(1) time. The code was automatically generated, so I'm sure there are a few areas where a bit of human optimization might be useful. Please let me know if you've got any suggestions on how to improve the format and/or optimization. If you compile reserved.c and test.c together with a C compiler you'll be able to see how the routine works. thanks, Doug Schmidt ------------------------------( reserved.c )------------------------------ #define MIN_WORD_LENGTH 2 #define MAX_WORD_LENGTH 14 #define MIN_HASH_VALUE 3 #define MAX_HASH_VALUE 95 /* 40 keywords 93 is the maximum key range */ static int hash (str, len) register char *str; register int len; { static int cval[] = { 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 25, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 30, 15, 15, 0, 0, 20, 95, 20, 35, 95, 95, 0, 5, 40, 20, 40, 0, 0, 35, 10, 0, 10, 0, 95, 10, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, 95, }; register int hval; switch (hval = len) { default: case 4: hval += cval[str[3]]; case 3: case 2: case 1: hval += cval[str[0]]; hval += cval[str[len - 1]]; case 0: return (hval); } } int in_word_set (str, len) register char *str; register int len; { static char * wordlist[] = { "", "", "", "END", "ELSE", "WHILE", "", "", "MOD", "QUALIFIED", "", "MODULE", "", "VAR", "TYPE", "", "REPEAT", "", "", "CASE", "", "", "OR", "FOR", "EXIT", "", "RECORD", "BY", "DO ", "", "", "", "TO", "AND", "FROM", "", "EXPORT", "", "", "", "UNTIL", "", "OF", "", "WITH", "", "RETURN", "", "SET", "", "", "", "", "NOT", "DIV ", "", "", "IF", "", "", "ELSIF", "", "", "", "PROCEDURE", "CONST", "", "", "", "", "", "IMPORT", "", "", "", "ARRAY", "", "IN", "", "", "", "", "", "", "LOOP", "DEFINITION", "", "POINTER", "", "IMPLEMENTATION", "", "", "", "", "THEN", "BEGIN", }; if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH) { register int key = hash (str, len); if (key <= MAX_HASH_VALUE && key >= MIN_HASH_VALUE) { register char *s = wordlist[key]; return (*s == *str && ! strcmp (str + 1, s + 1)); } } return (0); } ---------------------------------( test.c )---------------------------------------- /* Tests the generated perfect has function. The -v option prints diagnostics as to whether a word is in the set or not. Without -v the program is useful for timing. */ #include #define MAX_LEN 80 int main ( int argc, char *argv [ ] ) { int verbose = ( argc > 1 ) ? 1 : 0; char buf [ MAX_LEN ]; while ( gets ( buf ) ) { if ( in_word_set ( buf, strlen ( buf ) ) && verbose ) { printf ( "in word set %s\n", buf ); } else if ( verbose ) { printf ( "NOT in word set %s\n", buf ); } } return ( 0 ); } -- schmidt@ics.uci.edu (ARPA) | Per me si va nella citta' dolente. office: (714) 856-4043 | Per me si va nell'eterno dolore. | Per me si va tra la perduta gente. | Lasciate ogni speranza o voi ch'entrate.