1 /* sym - symbol table routines */
3 /* Copyright (c) 1990 The Regents of the University of California. */
4 /* All rights reserved. */
6 /* This code is derived from software contributed to Berkeley by */
9 /* The United States Government has rights in this work pursuant */
10 /* to contract no. DE-AC03-76SF00098 between the United States */
11 /* Department of Energy and the University of California. */
13 /* This file is part of flex. */
15 /* Redistribution and use in source and binary forms, with or without */
16 /* modification, are permitted provided that the following conditions */
19 /* 1. Redistributions of source code must retain the above copyright */
20 /* notice, this list of conditions and the following disclaimer. */
21 /* 2. Redistributions in binary form must reproduce the above copyright */
22 /* notice, this list of conditions and the following disclaimer in the */
23 /* documentation and/or other materials provided with the distribution. */
25 /* Neither the name of the University nor the names of its contributors */
26 /* may be used to endorse or promote products derived from this software */
27 /* without specific prior written permission. */
29 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
36 /* Variables for symbol tables:
37 * sctbl - start-condition symbol table
38 * ndtbl - name-definition symbol table
39 * ccltab - character class text symbol table
43 struct hash_entry
*prev
, *next
;
49 typedef struct hash_entry
**hash_table
;
51 #define NAME_TABLE_HASH_SIZE 101
52 #define START_COND_HASH_SIZE 101
53 #define CCL_HASH_SIZE 101
55 static struct hash_entry
*ndtbl
[NAME_TABLE_HASH_SIZE
];
56 static struct hash_entry
*sctbl
[START_COND_HASH_SIZE
];
57 static struct hash_entry
*ccltab
[CCL_HASH_SIZE
];
60 /* declare functions that have forward references */
62 static int addsym
PROTO ((register char[], char *, int, hash_table
, int));
63 static struct hash_entry
*findsym
PROTO ((register const char *sym
,
67 static int hashfunct
PROTO ((register const char *, int));
70 /* addsym - add symbol and definitions to symbol table
72 * -1 is returned if the symbol already exists, and the change not made.
75 static int addsym (sym
, str_def
, int_def
, table
, table_size
)
82 int hash_val
= hashfunct (sym
, table_size
);
83 register struct hash_entry
*sym_entry
= table
[hash_val
];
84 register struct hash_entry
*new_entry
;
85 register struct hash_entry
*successor
;
88 if (!strcmp (sym
, sym_entry
->name
)) { /* entry already exists */
92 sym_entry
= sym_entry
->next
;
95 /* create new entry */
96 new_entry
= (struct hash_entry
*)
97 flex_alloc (sizeof (struct hash_entry
));
99 if (new_entry
== NULL
)
100 flexfatal (_("symbol table memory allocation failed"));
102 if ((successor
= table
[hash_val
]) != 0) {
103 new_entry
->next
= successor
;
104 successor
->prev
= new_entry
;
107 new_entry
->next
= NULL
;
109 new_entry
->prev
= NULL
;
110 new_entry
->name
= sym
;
111 new_entry
->str_val
= str_def
;
112 new_entry
->int_val
= int_def
;
114 table
[hash_val
] = new_entry
;
120 /* cclinstal - save the text of a character class */
122 void cclinstal (ccltxt
, cclnum
)
126 /* We don't bother checking the return status because we are not
127 * called unless the symbol is new.
130 (void) addsym ((char *) copy_unsigned_string (ccltxt
),
131 (char *) 0, cclnum
, ccltab
, CCL_HASH_SIZE
);
135 /* ccllookup - lookup the number associated with character class text
137 * Returns 0 if there's no CCL associated with the text.
140 int ccllookup (ccltxt
)
143 return findsym ((char *) ccltxt
, ccltab
, CCL_HASH_SIZE
)->int_val
;
147 /* findsym - find symbol in symbol table */
149 static struct hash_entry
*findsym (sym
, table
, table_size
)
150 register const char *sym
;
154 static struct hash_entry empty_entry
= {
155 (struct hash_entry
*) 0, (struct hash_entry
*) 0,
156 (char *) 0, (char *) 0, 0,
158 register struct hash_entry
*sym_entry
=
160 table
[hashfunct (sym
, table_size
)];
163 if (!strcmp (sym
, sym_entry
->name
))
165 sym_entry
= sym_entry
->next
;
171 /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
173 static int hashfunct (str
, hash_size
)
174 register const char *str
;
177 register int hashval
;
183 while (str
[locstr
]) {
184 hashval
= (hashval
<< 1) + (unsigned char) str
[locstr
++];
185 hashval
%= hash_size
;
192 /* ndinstal - install a name definition */
194 void ndinstal (name
, definition
)
199 if (addsym (copy_string (name
),
200 (char *) copy_unsigned_string (definition
), 0,
201 ndtbl
, NAME_TABLE_HASH_SIZE
))
202 synerr (_("name defined twice"));
206 /* ndlookup - lookup a name definition
208 * Returns a nil pointer if the name definition does not exist.
214 return (Char
*) findsym (nd
, ndtbl
, NAME_TABLE_HASH_SIZE
)->str_val
;
218 /* scextend - increase the maximum number of start conditions */
222 current_max_scs
+= MAX_SCS_INCREMENT
;
226 scset
= reallocate_integer_array (scset
, current_max_scs
);
227 scbol
= reallocate_integer_array (scbol
, current_max_scs
);
228 scxclu
= reallocate_integer_array (scxclu
, current_max_scs
);
229 sceof
= reallocate_integer_array (sceof
, current_max_scs
);
230 scname
= reallocate_char_ptr_array (scname
, current_max_scs
);
234 /* scinstal - make a start condition
237 * The start condition is "exclusive" if xcluflg is true.
240 void scinstal (str
, xcluflg
)
245 if (++lastsc
>= current_max_scs
)
248 scname
[lastsc
] = copy_string (str
);
250 if (addsym (scname
[lastsc
], (char *) 0, lastsc
,
251 sctbl
, START_COND_HASH_SIZE
))
252 format_pinpoint_message (_
253 ("start condition %s declared twice"),
256 scset
[lastsc
] = mkstate (SYM_EPSILON
);
257 scbol
[lastsc
] = mkstate (SYM_EPSILON
);
258 scxclu
[lastsc
] = xcluflg
;
259 sceof
[lastsc
] = false;
263 /* sclookup - lookup the number associated with a start condition
265 * Returns 0 if no such start condition.
271 return findsym (str
, sctbl
, START_COND_HASH_SIZE
)->int_val
;