46 |
static state_list_t *first_state = NULL; |
static state_list_t *first_state = NULL; |
47 |
static state_list_t *last_state = NULL; |
static state_list_t *last_state = NULL; |
48 |
|
|
49 |
static void |
|
50 |
state_list_append (state_t *state) |
/*------------------------------------------------------------------. |
51 |
|
| A state was just discovered from another state. Queue it for | |
52 |
|
| later examination, in order to find its transitions. Return it. | |
53 |
|
`------------------------------------------------------------------*/ |
54 |
|
|
55 |
|
static state_t * |
56 |
|
state_list_append (symbol_number_t symbol, |
57 |
|
size_t core_size, item_number_t *core) |
58 |
{ |
{ |
59 |
state_list_t *node = XMALLOC (state_list_t, 1); |
state_list_t *node = XMALLOC (state_list_t, 1); |
60 |
|
state_t *state = state_new (symbol, core_size, core); |
61 |
|
|
62 |
|
if (trace_flag) |
63 |
|
fprintf (stderr, "state_list_append (state = %d, symbol = %d (%s))\n", |
64 |
|
nstates, symbol, symbols[symbol]->tag); |
65 |
|
|
66 |
|
/* If this is the eoftoken, and this is not the initial state, then |
67 |
|
this is the final state. */ |
68 |
|
if (symbol == 0 && first_state) |
69 |
|
final_state = state; |
70 |
|
|
71 |
node->next = NULL; |
node->next = NULL; |
72 |
node->state = state; |
node->state = state; |
73 |
|
|
76 |
if (last_state) |
if (last_state) |
77 |
last_state->next = node; |
last_state->next = node; |
78 |
last_state = node; |
last_state = node; |
79 |
|
|
80 |
|
return state; |
81 |
} |
} |
82 |
|
|
83 |
static int nshifts; |
static int nshifts; |
204 |
|
|
205 |
|
|
206 |
|
|
|
/*-----------------------------------------------------------------. |
|
|
| Subroutine of get_state. Create a new state for those items, if | |
|
|
| necessary. | |
|
|
`-----------------------------------------------------------------*/ |
|
|
|
|
|
static state_t * |
|
|
new_state (symbol_number_t symbol, size_t core_size, item_number_t *core) |
|
|
{ |
|
|
state_t *res; |
|
|
|
|
|
if (trace_flag) |
|
|
fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n", |
|
|
nstates, symbol, symbols[symbol]->tag); |
|
|
|
|
|
res = state_new (symbol, core_size, core); |
|
|
state_hash_insert (res); |
|
|
|
|
|
/* If this is the eoftoken, and this is not the initial state, then |
|
|
this is the final state. */ |
|
|
if (symbol == 0 && first_state) |
|
|
final_state = res; |
|
|
|
|
|
state_list_append (res); |
|
|
return res; |
|
|
} |
|
|
|
|
|
|
|
207 |
/*--------------------------------------------------------------. |
/*--------------------------------------------------------------. |
208 |
| Find the state number for the state we would get to (from the | |
| Find the state number for the state we would get to (from the | |
209 |
| current state) by shifting symbol. Create a new state if no | |
| current state) by shifting symbol. Create a new state if no | |
221 |
|
|
222 |
sp = state_hash_lookup (core_size, core); |
sp = state_hash_lookup (core_size, core); |
223 |
if (!sp) |
if (!sp) |
224 |
sp = new_state (symbol, core_size, core); |
sp = state_list_append (symbol, core_size, core); |
225 |
|
|
226 |
if (trace_flag) |
if (trace_flag) |
227 |
fprintf (stderr, "Exiting get_state => %d\n", sp->number); |
fprintf (stderr, "Exiting get_state => %d\n", sp->number); |
270 |
} |
} |
271 |
|
|
272 |
|
|
|
static void |
|
|
new_states (void) |
|
|
{ |
|
|
/* The 0 at the lhs is the index of the item of this initial rule. */ |
|
|
kernel_base[0][0] = 0; |
|
|
kernel_size[0] = 1; |
|
|
state_list_append (new_state (0, kernel_size[0], kernel_base[0])); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
273 |
/*----------------------------------------------------------------. |
/*----------------------------------------------------------------. |
274 |
| Find which rules can be used for reduction transitions from the | |
| Find which rules can be used for reduction transitions from the | |
275 |
| current state and make a reductions structure for the state to | |
| current state and make a reductions structure for the state to | |
314 |
state_list_t *this = first_state; |
state_list_t *this = first_state; |
315 |
|
|
316 |
/* Pessimization, but simplification of the code: make sure all |
/* Pessimization, but simplification of the code: make sure all |
317 |
the states have a shifts, errs, and reductions, even if |
the states have valid transitions and reductions members, |
318 |
reduced to 0. */ |
even if reduced to 0. It is too soon for errs, which are |
319 |
|
computed later, but set_conflicts. */ |
320 |
state_t *state = this->state; |
state_t *state = this->state; |
321 |
if (!state->shifts) |
if (!state->transitions) |
322 |
state_transitions_set (state, 0, 0); |
state_transitions_set (state, 0, 0); |
|
if (!state->errs) |
|
|
state->errs = errs_new (0); |
|
323 |
if (!state->reductions) |
if (!state->reductions) |
324 |
state_reductions_set (state, 0, 0); |
state_reductions_set (state, 0, 0); |
325 |
|
|
344 |
state_list_t *list = NULL; |
state_list_t *list = NULL; |
345 |
allocate_storage (); |
allocate_storage (); |
346 |
new_closure (nritems); |
new_closure (nritems); |
347 |
new_states (); |
|
348 |
|
/* Create the initial state. The 0 at the lhs is the index of the |
349 |
|
item of this initial rule. */ |
350 |
|
kernel_base[0][0] = 0; |
351 |
|
kernel_size[0] = 1; |
352 |
|
state_list_append (0, kernel_size[0], kernel_base[0]); |
353 |
|
|
354 |
list = first_state; |
list = first_state; |
355 |
|
|
356 |
while (list) |
while (list) |