1 /* Perfect hash generated by command line:
2  * ./a.out 1
3  */
4 #define MIN_HASH_VALUE	2
5 #define MAX_HASH_VALUE	118
6 #define MIN_KEYWORD_LEN 2
7 #define MAX_KEYWORD_LEN 11
8 
9 static unsigned char Keyword_Hash_Table [256] =
10 {
11   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
12   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
13   119,   1, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
14     9,   7,   1,   8,   2, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
15   119, 119,   0,   0, 119,   0, 119, 119, 119,   7, 119,   0,   0, 119, 119,   0,
16   119, 119,   0,   0,   0,   0, 119, 119,   0, 119, 119, 119, 119, 119, 119,   2,
17   119,  41,   1,   1,   9,   0,  55,   8,   0,   0, 119,   0,  27,   0,   0,   0,
18     7,   2,   0,  21,   0,   0,   0,   3,   2,   0, 119, 119, 119, 119, 119, 119,
19   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
20   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
21   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
22   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
23   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
24   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
25   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119,
26   119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119, 119
27 };
28 
keyword_hash(char * s,unsigned int len)29 static unsigned char keyword_hash (char *s, unsigned int len)
30 {
31    unsigned int sum;
32 
33    sum = len;
34    while (len)
35      {
36 	len--;
37 	sum += (unsigned int) Keyword_Hash_Table [(unsigned char)s[len]];
38      }
39    return sum;
40 }
41 
42 typedef struct
43 {
44    char *name;
45    unsigned int type;
46 }
47 Keyword_Table_Type;
48 
49 static Keyword_Table_Type Keyword_Table [/* 117 */] =
50 {
51    {"or",	OR_TOKEN},
52    {"not",	NOT_TOKEN},
53    {NULL,0},
54    {"xor",	BXOR_TOKEN},
55    {"return",	RETURN_TOKEN},
56    {"exch",	EXCH_TOKEN},
57    {NULL,0},
58    {"continue",	CONT_TOKEN},
59    {NULL,0},
60    {"do",	DO_TOKEN},
61    {"mod",	MOD_TOKEN},
62    {"ERROR_BLOCK",	ERRBLK_TOKEN},
63    {"USER_BLOCK2",	USRBLK2_TOKEN},
64    {"USER_BLOCK4",	USRBLK4_TOKEN},
65    {"__tmp",	TMP_TOKEN},
66    {"pop",	POP_TOKEN},
67    {NULL,0},
68    {"EXIT_BLOCK",	EXITBLK_TOKEN},
69    {"USER_BLOCK1",	USRBLK1_TOKEN},
70    {"USER_BLOCK3",	USRBLK3_TOKEN},
71    {"USER_BLOCK0",	USRBLK0_TOKEN},
72    {NULL,0},
73    {"shr",	SHR_TOKEN},
74    {"chs",	CHS_TOKEN},
75    {"sqr",	SQR_TOKEN},
76    {NULL,0},
77    {"struct",	STRUCT_TOKEN},
78    {NULL,0},
79    {NULL,0},
80    {"switch",	SWITCH_TOKEN},
81    {"mul2",	MUL2_TOKEN},
82    {"sign",	SIGN_TOKEN},
83    {"using",	USING_TOKEN},
84    {"while",	WHILE_TOKEN},
85    {NULL,0},
86    {NULL,0},
87    {"loop",	LOOP_TOKEN},
88    {NULL,0},
89    {NULL,0},
90    {NULL,0},
91    {"public",	PUBLIC_TOKEN},
92    {NULL,0},
93    {NULL,0},
94    {NULL,0},
95    {NULL,0},
96    {"break",	BREAK_TOKEN},
97    {NULL,0},
98    {"do_while",	DOWHILE_TOKEN},
99    {NULL,0},
100    {"shl",	SHL_TOKEN},
101    {"else",	ELSE_TOKEN},
102    {"and",	AND_TOKEN},
103    {"orelse",	ORELSE_TOKEN},
104    {"private",	PRIVATE_TOKEN},
105    {NULL,0},
106    {"if",	IF_TOKEN},
107    {"for",	FOR_TOKEN},
108    {"!if",	IFNOT_TOKEN},
109    {NULL,0},
110    {"_for",	_FOR_TOKEN},
111    {"forever",	FOREVER_TOKEN},
112    {NULL,0},
113    {NULL,0},
114    {NULL,0},
115    {"abs",	ABS_TOKEN},
116    {"case",	CASE_TOKEN},
117    {NULL,0},
118    {"static",	STATIC_TOKEN},
119    {"define",	DEFINE_TOKEN},
120    {NULL,0},
121    {NULL,0},
122    {NULL,0},
123    {NULL,0},
124    {NULL,0},
125    {NULL,0},
126    {NULL,0},
127    {"typedef",	TYPEDEF_TOKEN},
128    {NULL,0},
129    {NULL,0},
130    {NULL,0},
131    {NULL,0},
132    {NULL,0},
133    {NULL,0},
134    {NULL,0},
135    {NULL,0},
136    {NULL,0},
137    {NULL,0},
138    {NULL,0},
139    {NULL,0},
140    {NULL,0},
141    {NULL,0},
142    {NULL,0},
143    {NULL,0},
144    {NULL,0},
145    {NULL,0},
146    {NULL,0},
147    {NULL,0},
148    {NULL,0},
149    {NULL,0},
150    {NULL,0},
151    {NULL,0},
152    {NULL,0},
153    {"foreach",	FOREACH_TOKEN},
154    {"andelse",	ANDELSE_TOKEN},
155    {NULL,0},
156    {NULL,0},
157    {NULL,0},
158    {NULL,0},
159    {NULL,0},
160    {NULL,0},
161    {NULL,0},
162    {NULL,0},
163    {NULL,0},
164    {NULL,0},
165    {NULL,0},
166    {NULL,0},
167    {"variable",	VARIABLE_TOKEN},
168 };
169 
is_keyword(char * str,unsigned int len)170 static Keyword_Table_Type *is_keyword (char *str, unsigned int len)
171 {
172    unsigned int hash;
173    char *name;
174    Keyword_Table_Type *kw;
175 
176    if ((len < MIN_KEYWORD_LEN)
177        || (len > MAX_KEYWORD_LEN))
178      return NULL;
179 
180    hash = keyword_hash (str, len);
181    if ((hash > MAX_HASH_VALUE) || (hash < MIN_HASH_VALUE))
182      return NULL;
183 
184    kw = &Keyword_Table[hash - MIN_HASH_VALUE];
185    if ((NULL != (name = kw->name))
186        && (*str == *name)
187        && (0 == strcmp (str, name)))
188      return kw;
189    return NULL;
190 }
191