🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.modula2
Article #4565

Re: Modula 2 keywords

#4565
From: Doug Schmidt
Date: Fri, 13 Jan 1989 02:01
163 lines
3940 bytes
Hi,

   Just for completeness, here is a perfect hash function for Modula 3
keywords!  Now there are no more reasons not to build a modula 3 compiler
and donate it to the FSF! ;-).

Doug

p.s. Again, let me know if there are any problems.

----------------------------------------
#define MIN_WORD_LENGTH 2
#define MAX_WORD_LENGTH 9
#define MIN_HASH_VALUE 6
#define MAX_HASH_VALUE 157
/*
53 keywords
152 is the maximum key range
*/
static int hash (str,len)
register char	*str;
register int	len;
{
	static int cval[] = {
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157,  20,  25,  10,  35,   0,
		  45, 157,  10,  55, 157,  30,  15,   5,   0,  60,
		   0, 157,   0,  10,  10,  20,  55,   0,  35,   0,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157, 157, 157,
		 157, 157, 157, 157, 157, 157, 157, 157,
	};
	register int hval;

	switch (hval = len) {
		default:
		case 2:
			hval += cval[str[1]];
		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[] = {
		 "", "", "", "", "", "",
		 "RETURN", 
		 "",
		 "READONLY", 
		 "PROCEDURE", 
		 "", "", "",
		 "TRY", 
		 "TYPE", 
		 "WHILE", 
		 "REPEAT", 
		 "",
		 "TYPECASE", 
		 "ELSE", 
		 "", "",
		 "METHODS", 
		 "SET", 
		 "THEN", 
		 "ARRAY", 
		 "UNSAFE", 
		 "BY", 
		 "", "",
		 "BEGIN", 
		 "", "", "",
		 "CASE", 
		 "",
		 "RAISES", 
		 "",
		 "END", 
		 "",
		 "UNTIL", 
		 "RECORD", 
		 "", "",
		 "EXCEPTION", 
		 "", "", "",
		 "REF", 
		 "EXIT", 
		 "",
		 "EXCEPT", 
		 "EXPORTS", 
		 "",
		 "FROM", 
		 "", "",
		 "IN", 
		 "AND", 
		 "", "",
		 "INLINE", 
		 "OR", 
		 "UNTRACED", 
		 "INTERFACE", 
		 "ELSIF", 
		 "", "", "",
		 "WITH", 
		 "",
		 "MODULE", 
		 "",
		 "NOT", 
		 "EVAL", 
		 "",
		 "IMPORT", 
		 "",
		 "VAR", 
		 "",
		 "VALUE", 
		 "", "", "", "",
		 "CONST", 
		 "", "", "", "", "", "", "", "",
		 "BITS", 
		 "", "", "", "", "", "",
		 "OBJECT", 
		 "",
		 "MOD", 
		 "", "", "",
		 "FINALLY", 
		 "FOR", 
		 "LOCK", 
		 "", "", "", "", "", "", "", "", "",
		 "", "", "", "", "", "", "", "", "",
		 "", "", "", "",
		 "TO", 
		 "", "", "", "", "", "", "", "", "",
		 "", "", "", "", "",
		 "IF", 
		 "DIV", 
		 "", "", "",
		 "OF", 
		 "", "", "", "",
		 "DO", 
	};

	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);
}
--
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.
Technical Details
Message-ID: <3997@paris.ics.uci.edu>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!attcan!uunet!lll-winken!ames!ucsd!orion.cf.uci.edu!uci-ics!siam.ics.uci.edu!schmidt
References: <3486@paris.ics.uci.edu> <3741@paris.ics.uci.edu> <3995@paris.ics.uci.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!attcan!uunet!lll-winken!ames!ucsd!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: <3997@paris.ics.uci.edu>
Date: 13 Jan 89 02:01:44 GMT
References: <3486@paris.ics.uci.edu> <3741@paris.ics.uci.edu> <3995@paris.ics.uci.edu>
Sender: news@paris.ics.uci.edu
Reply-To: Doug Schmidt <schmidt@siam.ics.uci.edu>
Organization: University of California, Irvine - Dept of ICS
Lines: 163