20 |
|
|
21 |
|
|
22 |
#include "system.h" |
#include "system.h" |
23 |
|
#include "hash.h" |
24 |
#include "complain.h" |
#include "complain.h" |
25 |
#include "gram.h" |
#include "gram.h" |
26 |
#include "state.h" |
#include "state.h" |
31 |
`-------------------*/ |
`-------------------*/ |
32 |
|
|
33 |
|
|
34 |
/*---------------------------------. |
/*---------------------------------------. |
35 |
| Create a new array of N shitfs. | |
| Create a new array of N shifts/gotos. | |
36 |
`---------------------------------*/ |
`---------------------------------------*/ |
37 |
|
|
38 |
#define SHIFTS_ALLOC(Nshifts) \ |
#define SHIFTS_ALLOC(Nshifts) \ |
39 |
(shifts *) xcalloc ((unsigned) (sizeof (shifts) \ |
(shifts *) xcalloc ((unsigned) (sizeof (shifts) \ |
117 |
accessing symbol: EOF. */ |
accessing symbol: EOF. */ |
118 |
state_t *final_state = NULL; |
state_t *final_state = NULL; |
119 |
|
|
120 |
|
#define STATE_ALLOC(Nitems) \ |
121 |
|
(state_t *) xcalloc ((unsigned) (sizeof (state_t) \ |
122 |
|
+ (Nitems - 1) * sizeof (item_number_t)), 1) |
123 |
|
|
124 |
/*------------------------------------------------------------. |
/*------------------------------------------------------------. |
125 |
| Create a new state with ACCESSING_SYMBOL, for those items. | |
| Create a new state with ACCESSING_SYMBOL, for those items. | |
126 |
`------------------------------------------------------------*/ |
`------------------------------------------------------------*/ |
134 |
if (nstates >= STATE_NUMBER_MAX) |
if (nstates >= STATE_NUMBER_MAX) |
135 |
fatal (_("too many states (max %d)"), STATE_NUMBER_MAX); |
fatal (_("too many states (max %d)"), STATE_NUMBER_MAX); |
136 |
|
|
|
#define STATE_ALLOC(Nitems) \ |
|
|
(state_t *) xcalloc ((unsigned) (sizeof (state_t) \ |
|
|
+ (Nitems - 1) * sizeof (item_number_t)), 1) |
|
|
|
|
137 |
res = STATE_ALLOC (core_size); |
res = STATE_ALLOC (core_size); |
138 |
res->accessing_symbol = accessing_symbol; |
res->accessing_symbol = accessing_symbol; |
139 |
res->number = nstates; |
res->number = nstates; |
178 |
fprintf (out, "]"); |
fprintf (out, "]"); |
179 |
} |
} |
180 |
} |
} |
181 |
|
|
182 |
|
|
183 |
|
/*----------------------. |
184 |
|
| A state hash table. | |
185 |
|
`----------------------*/ |
186 |
|
|
187 |
|
/* Initial capacity of states hash table. */ |
188 |
|
#define HT_INITIAL_CAPACITY 257 |
189 |
|
|
190 |
|
static struct hash_table *state_table = NULL; |
191 |
|
|
192 |
|
/* Two states are equal if they have the same core items. */ |
193 |
|
static bool |
194 |
|
state_compare (const state_t *s1, const state_t *s2) |
195 |
|
{ |
196 |
|
int i; |
197 |
|
|
198 |
|
if (s1->nitems != s2->nitems) |
199 |
|
return FALSE; |
200 |
|
|
201 |
|
for (i = 0; i < s1->nitems; ++i) |
202 |
|
if (s1->items[i] != s2->items[i]) |
203 |
|
return FALSE; |
204 |
|
|
205 |
|
return TRUE; |
206 |
|
} |
207 |
|
|
208 |
|
static unsigned int |
209 |
|
state_hash (const state_t *state, unsigned int tablesize) |
210 |
|
{ |
211 |
|
/* Add up the state's item numbers to get a hash key. */ |
212 |
|
int key = 0; |
213 |
|
int i; |
214 |
|
for (i = 0; i < state->nitems; ++i) |
215 |
|
key += state->items[i]; |
216 |
|
return key % tablesize; |
217 |
|
} |
218 |
|
|
219 |
|
|
220 |
|
/*-------------------------------. |
221 |
|
| Create the states hash table. | |
222 |
|
`-------------------------------*/ |
223 |
|
|
224 |
|
void |
225 |
|
state_hash_new (void) |
226 |
|
{ |
227 |
|
state_table = hash_initialize (HT_INITIAL_CAPACITY, |
228 |
|
NULL, |
229 |
|
(Hash_hasher) state_hash, |
230 |
|
(Hash_comparator) state_compare, |
231 |
|
(Hash_data_freer) NULL); |
232 |
|
} |
233 |
|
|
234 |
|
|
235 |
|
/*---------------------------------------------. |
236 |
|
| Free the states hash table, not the states. | |
237 |
|
`---------------------------------------------*/ |
238 |
|
|
239 |
|
void |
240 |
|
state_hash_free (void) |
241 |
|
{ |
242 |
|
hash_free (state_table); |
243 |
|
} |
244 |
|
|
245 |
|
|
246 |
|
/*---------------------------------------. |
247 |
|
| Insert STATE in the state hash table. | |
248 |
|
`---------------------------------------*/ |
249 |
|
|
250 |
|
void |
251 |
|
state_hash_insert (state_t *state) |
252 |
|
{ |
253 |
|
hash_insert (state_table, state); |
254 |
|
} |
255 |
|
|
256 |
|
|
257 |
|
/*------------------------------------------------------------------. |
258 |
|
| Find the state associated to the CORE, and return it. If it does | |
259 |
|
| not exist yet, return NULL. | |
260 |
|
`------------------------------------------------------------------*/ |
261 |
|
|
262 |
|
state_t * |
263 |
|
state_hash_lookup (size_t core_size, item_number_t *core) |
264 |
|
{ |
265 |
|
state_t *probe = STATE_ALLOC (core_size); |
266 |
|
state_t *entry; |
267 |
|
|
268 |
|
probe->nitems = core_size; |
269 |
|
memcpy (probe->items, core, core_size * sizeof (core[0])); |
270 |
|
entry = hash_lookup (state_table, probe); |
271 |
|
free (probe); |
272 |
|
return entry; |
273 |
|
} |
274 |
|
|
275 |
|
/* All the decorated states, indexed by the state number. */ |
276 |
|
state_t **states = NULL; |
277 |
|
|
278 |
|
|
279 |
|
/*----------------------. |
280 |
|
| Free all the states. | |
281 |
|
`----------------------*/ |
282 |
|
|
283 |
|
void |
284 |
|
states_free (void) |
285 |
|
{ |
286 |
|
state_number_t i; |
287 |
|
|
288 |
|
for (i = 0; i < nstates; ++i) |
289 |
|
{ |
290 |
|
free (states[i]->shifts); |
291 |
|
XFREE (states[i]->reductions); |
292 |
|
free (states[i]->errs); |
293 |
|
free (states[i]); |
294 |
|
} |
295 |
|
XFREE (states); |
296 |
|
} |