]>
Commit | Line | Data |
---|---|---|
40675e7c | 1 | /* Generate the nondeterministic finite state machine for bison, |
97db7bd4 | 2 | Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc. |
40675e7c | 3 | |
2fa6973e | 4 | This file is part of Bison, the GNU Compiler Compiler. |
40675e7c | 5 | |
2fa6973e AD |
6 | Bison is free software; you can redistribute it and/or modify |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
40675e7c | 10 | |
2fa6973e AD |
11 | Bison is distributed in the hope that it will be useful, |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
40675e7c | 15 | |
2fa6973e AD |
16 | You should have received a copy of the GNU General Public License |
17 | along with Bison; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
40675e7c DM |
20 | |
21 | ||
22 | /* See comments in state.h for the data structures that represent it. | |
23 | The entry point is generate_states. */ | |
24 | ||
40675e7c | 25 | #include "system.h" |
9bfe901c | 26 | #include "getargs.h" |
c87d4863 | 27 | #include "reader.h" |
40675e7c DM |
28 | #include "gram.h" |
29 | #include "state.h" | |
a0f6b076 | 30 | #include "complain.h" |
2fa6973e | 31 | #include "closure.h" |
403b315b | 32 | #include "LR0.h" |
630e182b | 33 | #include "reduce.h" |
40675e7c | 34 | |
40675e7c DM |
35 | int nstates; |
36 | int final_state; | |
342b8b6e AD |
37 | core *first_state = NULL; |
38 | shifts *first_shift = NULL; | |
39 | reductions *first_reduction = NULL; | |
40675e7c | 40 | |
342b8b6e AD |
41 | static core *this_state = NULL; |
42 | static core *last_state = NULL; | |
43 | static shifts *last_shift = NULL; | |
44 | static reductions *last_reduction = NULL; | |
40675e7c DM |
45 | |
46 | static int nshifts; | |
342b8b6e | 47 | static short *shift_symbol = NULL; |
40675e7c | 48 | |
342b8b6e AD |
49 | static short *redset = NULL; |
50 | static short *shiftset = NULL; | |
40675e7c | 51 | |
342b8b6e | 52 | static short **kernel_base = NULL; |
6255b435 | 53 | static int *kernel_size = NULL; |
342b8b6e | 54 | static short *kernel_items = NULL; |
40675e7c DM |
55 | |
56 | /* hash table for states, to recognize equivalent ones. */ | |
57 | ||
58 | #define STATE_TABLE_SIZE 1009 | |
342b8b6e | 59 | static core **state_table = NULL; |
40675e7c | 60 | |
2fa6973e | 61 | \f |
4a120d45 | 62 | static void |
d2729d44 | 63 | allocate_itemsets (void) |
40675e7c | 64 | { |
2fa6973e | 65 | int i; |
40675e7c | 66 | |
630e182b AD |
67 | /* Count the number of occurrences of all the symbols in RITEMS. |
68 | Note that useless productions (hence useless nonterminals) are | |
69 | browsed too, hence we need to allocate room for _all_ the | |
70 | symbols. */ | |
71 | int count = 0; | |
72 | short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals); | |
40675e7c | 73 | |
c87d4863 AD |
74 | for (i = 0; ritem[i]; ++i) |
75 | if (ritem[i] > 0) | |
76 | { | |
77 | count++; | |
78 | symbol_count[ritem[i]]++; | |
79 | } | |
40675e7c | 80 | |
2fa6973e AD |
81 | /* See comments before new_itemsets. All the vectors of items |
82 | live inside KERNEL_ITEMS. The number of active items after | |
40675e7c DM |
83 | some symbol cannot be more than the number of times that symbol |
84 | appears as an item, which is symbol_count[symbol]. | |
85 | We allocate that much space for each symbol. */ | |
86 | ||
d7913476 | 87 | kernel_base = XCALLOC (short *, nsyms); |
342b8b6e AD |
88 | if (count) |
89 | kernel_items = XCALLOC (short, count); | |
40675e7c DM |
90 | |
91 | count = 0; | |
92 | for (i = 0; i < nsyms; i++) | |
93 | { | |
94 | kernel_base[i] = kernel_items + count; | |
95 | count += symbol_count[i]; | |
96 | } | |
97 | ||
630e182b | 98 | free (symbol_count); |
0e41b407 | 99 | kernel_size = XCALLOC (int, nsyms); |
40675e7c DM |
100 | } |
101 | ||
102 | ||
4a120d45 | 103 | static void |
d2729d44 | 104 | allocate_storage (void) |
40675e7c | 105 | { |
2fa6973e | 106 | allocate_itemsets (); |
40675e7c | 107 | |
d7913476 AD |
108 | shiftset = XCALLOC (short, nsyms); |
109 | redset = XCALLOC (short, nrules + 1); | |
110 | state_table = XCALLOC (core *, STATE_TABLE_SIZE); | |
40675e7c DM |
111 | } |
112 | ||
113 | ||
4a120d45 | 114 | static void |
d2729d44 | 115 | free_storage (void) |
40675e7c | 116 | { |
630e182b AD |
117 | free (shift_symbol); |
118 | free (redset); | |
119 | free (shiftset); | |
120 | free (kernel_base); | |
121 | free (kernel_size); | |
d7913476 | 122 | XFREE (kernel_items); |
630e182b | 123 | free (state_table); |
40675e7c DM |
124 | } |
125 | ||
126 | ||
127 | ||
40675e7c | 128 | |
2fa6973e AD |
129 | /*----------------------------------------------------------------. |
130 | | Find which symbols can be shifted in the current state, and for | | |
131 | | each one record which items would be active after that shift. | | |
132 | | Uses the contents of itemset. | | |
133 | | | | |
134 | | shift_symbol is set to a vector of the symbols that can be | | |
135 | | shifted. For each symbol in the grammar, kernel_base[symbol] | | |
136 | | points to a vector of item numbers activated if that symbol is | | |
125ecb56 | 137 | | shifted, and kernel_size[symbol] is their numbers. | |
2fa6973e | 138 | `----------------------------------------------------------------*/ |
40675e7c | 139 | |
4a120d45 | 140 | static void |
d2729d44 | 141 | new_itemsets (void) |
40675e7c | 142 | { |
2fa6973e | 143 | int i; |
2fa6973e | 144 | |
9bfe901c | 145 | if (trace_flag) |
c87d4863 AD |
146 | fprintf (stderr, "Entering new_itemsets, state = %d\n", |
147 | this_state->number); | |
40675e7c DM |
148 | |
149 | for (i = 0; i < nsyms; i++) | |
125ecb56 | 150 | kernel_size[i] = 0; |
40675e7c | 151 | |
630e182b | 152 | shift_symbol = XCALLOC (short, nsyms); |
b2872512 | 153 | nshifts = 0; |
40675e7c | 154 | |
b2872512 | 155 | for (i = 0; i < nitemset; ++i) |
40675e7c | 156 | { |
97db7bd4 | 157 | int symbol = ritem[itemset[i]]; |
40675e7c DM |
158 | if (symbol > 0) |
159 | { | |
125ecb56 | 160 | if (!kernel_size[symbol]) |
40675e7c | 161 | { |
b2872512 AD |
162 | shift_symbol[nshifts] = symbol; |
163 | nshifts++; | |
40675e7c DM |
164 | } |
165 | ||
125ecb56 AD |
166 | kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1; |
167 | kernel_size[symbol]++; | |
40675e7c DM |
168 | } |
169 | } | |
40675e7c DM |
170 | } |
171 | ||
172 | ||
173 | ||
2fa6973e AD |
174 | /*-----------------------------------------------------------------. |
175 | | Subroutine of get_state. Create a new state for those items, if | | |
176 | | necessary. | | |
177 | `-----------------------------------------------------------------*/ | |
40675e7c | 178 | |
2fa6973e AD |
179 | static core * |
180 | new_state (int symbol) | |
40675e7c | 181 | { |
2fa6973e | 182 | core *p; |
40675e7c | 183 | |
9bfe901c | 184 | if (trace_flag) |
c87d4863 AD |
185 | fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n", |
186 | this_state->number, symbol, tags[symbol]); | |
40675e7c | 187 | |
2fa6973e AD |
188 | if (nstates >= MAXSHORT) |
189 | fatal (_("too many states (max %d)"), MAXSHORT); | |
40675e7c | 190 | |
125ecb56 | 191 | p = CORE_ALLOC (kernel_size[symbol]); |
2fa6973e AD |
192 | p->accessing_symbol = symbol; |
193 | p->number = nstates; | |
125ecb56 | 194 | p->nitems = kernel_size[symbol]; |
2fa6973e | 195 | |
125ecb56 | 196 | shortcpy (p->items, kernel_base[symbol], kernel_size[symbol]); |
2fa6973e AD |
197 | |
198 | last_state->next = p; | |
199 | last_state = p; | |
2fa6973e | 200 | nstates++; |
40675e7c | 201 | |
2fa6973e AD |
202 | return p; |
203 | } | |
40675e7c | 204 | |
2fa6973e AD |
205 | |
206 | /*--------------------------------------------------------------. | |
207 | | Find the state number for the state we would get to (from the | | |
208 | | current state) by shifting symbol. Create a new state if no | | |
97db7bd4 | 209 | | equivalent one exists already. Used by append_states. | |
2fa6973e | 210 | `--------------------------------------------------------------*/ |
40675e7c | 211 | |
4a120d45 | 212 | static int |
d2729d44 | 213 | get_state (int symbol) |
40675e7c | 214 | { |
2fa6973e | 215 | int key; |
97db7bd4 | 216 | int i; |
2fa6973e | 217 | core *sp; |
40675e7c | 218 | |
9bfe901c | 219 | if (trace_flag) |
c87d4863 AD |
220 | fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n", |
221 | this_state->number, symbol, tags[symbol]); | |
40675e7c | 222 | |
97db7bd4 AD |
223 | /* Add up the target state's active item numbers to get a hash key. |
224 | */ | |
40675e7c | 225 | key = 0; |
125ecb56 | 226 | for (i = 0; i < kernel_size[symbol]; ++i) |
97db7bd4 | 227 | key += kernel_base[symbol][i]; |
40675e7c | 228 | key = key % STATE_TABLE_SIZE; |
40675e7c DM |
229 | sp = state_table[key]; |
230 | ||
231 | if (sp) | |
232 | { | |
97db7bd4 | 233 | int found = 0; |
40675e7c DM |
234 | while (!found) |
235 | { | |
125ecb56 | 236 | if (sp->nitems == kernel_size[symbol]) |
40675e7c DM |
237 | { |
238 | found = 1; | |
125ecb56 | 239 | for (i = 0; i < kernel_size[symbol]; ++i) |
97db7bd4 AD |
240 | if (kernel_base[symbol][i] != sp->items[i]) |
241 | found = 0; | |
40675e7c DM |
242 | } |
243 | ||
244 | if (!found) | |
245 | { | |
246 | if (sp->link) | |
247 | { | |
248 | sp = sp->link; | |
249 | } | |
2fa6973e | 250 | else /* bucket exhausted and no match */ |
40675e7c | 251 | { |
2fa6973e | 252 | sp = sp->link = new_state (symbol); |
40675e7c DM |
253 | found = 1; |
254 | } | |
255 | } | |
256 | } | |
257 | } | |
2fa6973e | 258 | else /* bucket is empty */ |
40675e7c | 259 | { |
2fa6973e | 260 | state_table[key] = sp = new_state (symbol); |
40675e7c DM |
261 | } |
262 | ||
c87d4863 AD |
263 | if (trace_flag) |
264 | fprintf (stderr, "Exiting get_state => %d\n", sp->number); | |
265 | ||
36281465 | 266 | return sp->number; |
40675e7c DM |
267 | } |
268 | ||
2fa6973e AD |
269 | /*------------------------------------------------------------------. |
270 | | Use the information computed by new_itemsets to find the state | | |
271 | | numbers reached by each shift transition from the current state. | | |
272 | | | | |
273 | | shiftset is set up as a vector of state numbers of those states. | | |
274 | `------------------------------------------------------------------*/ | |
40675e7c | 275 | |
2fa6973e AD |
276 | static void |
277 | append_states (void) | |
40675e7c | 278 | { |
2fa6973e AD |
279 | int i; |
280 | int j; | |
281 | int symbol; | |
40675e7c | 282 | |
9bfe901c | 283 | if (trace_flag) |
c87d4863 AD |
284 | fprintf (stderr, "Entering append_states, state = %d\n", |
285 | this_state->number); | |
40675e7c | 286 | |
2fa6973e | 287 | /* first sort shift_symbol into increasing order */ |
40675e7c | 288 | |
2fa6973e AD |
289 | for (i = 1; i < nshifts; i++) |
290 | { | |
291 | symbol = shift_symbol[i]; | |
292 | j = i; | |
293 | while (j > 0 && shift_symbol[j - 1] > symbol) | |
294 | { | |
295 | shift_symbol[j] = shift_symbol[j - 1]; | |
296 | j--; | |
297 | } | |
298 | shift_symbol[j] = symbol; | |
299 | } | |
40675e7c | 300 | |
2fa6973e | 301 | for (i = 0; i < nshifts; i++) |
97db7bd4 | 302 | shiftset[i] = get_state (shift_symbol[i]); |
40675e7c DM |
303 | } |
304 | ||
305 | ||
4a120d45 | 306 | static void |
2fa6973e | 307 | new_states (void) |
40675e7c | 308 | { |
97db7bd4 | 309 | first_state = last_state = this_state = CORE_ALLOC (0); |
40675e7c DM |
310 | nstates = 1; |
311 | } | |
312 | ||
313 | ||
4a38e613 AD |
314 | /*------------------------------------------------------------. |
315 | | Save the NSHIFTS of SHIFTSET into the current linked list. | | |
316 | `------------------------------------------------------------*/ | |
317 | ||
4a120d45 | 318 | static void |
d2729d44 | 319 | save_shifts (void) |
40675e7c | 320 | { |
4a38e613 | 321 | shifts *p = shifts_new (nshifts); |
40675e7c DM |
322 | |
323 | p->number = this_state->number; | |
40675e7c | 324 | |
300f275f | 325 | shortcpy (p->shifts, shiftset, nshifts); |
40675e7c DM |
326 | |
327 | if (last_shift) | |
97db7bd4 | 328 | last_shift->next = p; |
40675e7c | 329 | else |
97db7bd4 AD |
330 | first_shift = p; |
331 | last_shift = p; | |
40675e7c DM |
332 | } |
333 | ||
334 | ||
2fa6973e AD |
335 | /*------------------------------------------------------------------. |
336 | | Subroutine of augment_automaton. Create the next-to-final state, | | |
337 | | to which a shift has already been made in the initial state. | | |
338 | `------------------------------------------------------------------*/ | |
40675e7c | 339 | |
4a120d45 | 340 | static void |
2fa6973e | 341 | insert_start_shift (void) |
40675e7c | 342 | { |
2fa6973e AD |
343 | core *statep; |
344 | shifts *sp; | |
40675e7c | 345 | |
f59c437a | 346 | statep = CORE_ALLOC (0); |
2fa6973e AD |
347 | statep->number = nstates; |
348 | statep->accessing_symbol = start_symbol; | |
40675e7c | 349 | |
2fa6973e AD |
350 | last_state->next = statep; |
351 | last_state = statep; | |
40675e7c | 352 | |
2fa6973e | 353 | /* Make a shift from this state to (what will be) the final state. */ |
4a38e613 | 354 | sp = shifts_new (1); |
2fa6973e | 355 | sp->number = nstates++; |
2fa6973e | 356 | sp->shifts[0] = nstates; |
40675e7c | 357 | |
2fa6973e AD |
358 | last_shift->next = sp; |
359 | last_shift = sp; | |
40675e7c DM |
360 | } |
361 | ||
362 | ||
2fa6973e AD |
363 | /*------------------------------------------------------------------. |
364 | | Make sure that the initial state has a shift that accepts the | | |
365 | | grammar's start symbol and goes to the next-to-final state, which | | |
366 | | has a shift going to the final state, which has a shift to the | | |
367 | | termination state. Create such states and shifts if they don't | | |
368 | | happen to exist already. | | |
369 | `------------------------------------------------------------------*/ | |
40675e7c | 370 | |
4a120d45 | 371 | static void |
d2729d44 | 372 | augment_automaton (void) |
40675e7c | 373 | { |
2fa6973e AD |
374 | core *statep; |
375 | shifts *sp; | |
2fa6973e | 376 | shifts *sp1 = NULL; |
40675e7c DM |
377 | |
378 | sp = first_shift; | |
379 | ||
d954473d | 380 | if (!sp->nshifts) |
40675e7c | 381 | { |
b178c8cc AD |
382 | /* There are no shifts for any state. Make one shift, from the |
383 | initial state to the next-to-final state. */ | |
40675e7c | 384 | |
b178c8cc AD |
385 | sp = shifts_new (1); |
386 | sp->shifts[0] = nstates; | |
40675e7c | 387 | |
b178c8cc AD |
388 | /* Initialize the chain of shifts with sp. */ |
389 | first_shift = sp; | |
390 | last_shift = sp; | |
40675e7c | 391 | |
b178c8cc AD |
392 | /* Create the next-to-final state, with shift to |
393 | what will be the final state. */ | |
394 | insert_start_shift (); | |
395 | } | |
396 | else if (sp->number == 0) | |
397 | { | |
398 | statep = first_state->next; | |
40675e7c | 399 | |
b178c8cc AD |
400 | /* The states reached by shifts from FIRST_STATE are numbered |
401 | 1..(SP->NSHIFTS). Look for one reached by START_SYMBOL. */ | |
402 | while (statep->accessing_symbol < start_symbol | |
403 | && statep->number < sp->nshifts) | |
404 | statep = statep->next; | |
40675e7c | 405 | |
b178c8cc AD |
406 | if (statep->accessing_symbol == start_symbol) |
407 | { | |
408 | /* We already have a next-to-final state. | |
409 | Make sure it has a shift to what will be the final state. */ | |
410 | while (sp && sp->number < statep->number) | |
411 | { | |
412 | sp1 = sp; | |
413 | sp = sp->next; | |
414 | } | |
40675e7c | 415 | |
b178c8cc AD |
416 | if (sp && sp->number == statep->number) |
417 | { | |
418 | int i; | |
419 | shifts *sp2 = shifts_new (sp->nshifts + 1); | |
420 | sp2->number = statep->number; | |
421 | sp2->shifts[0] = nstates; | |
422 | for (i = sp->nshifts; i > 0; i--) | |
423 | sp2->shifts[i] = sp->shifts[i - 1]; | |
424 | ||
425 | /* Patch sp2 into the chain of shifts in place of sp, | |
426 | following sp1. */ | |
40675e7c | 427 | sp2->next = sp->next; |
b178c8cc AD |
428 | sp1->next = sp2; |
429 | if (sp == last_shift) | |
40675e7c | 430 | last_shift = sp2; |
d7913476 | 431 | XFREE (sp); |
b178c8cc AD |
432 | } |
433 | else | |
434 | { | |
435 | shifts *sp2 = shifts_new (1); | |
436 | sp2->number = statep->number; | |
437 | sp2->shifts[0] = nstates; | |
438 | ||
439 | /* Patch sp2 into the chain of shifts between sp1 and sp. */ | |
440 | sp2->next = sp; | |
441 | sp1->next = sp2; | |
442 | if (sp == 0) | |
443 | last_shift = sp2; | |
40675e7c DM |
444 | } |
445 | } | |
446 | else | |
447 | { | |
b178c8cc AD |
448 | int i, k; |
449 | shifts *sp2; | |
40675e7c | 450 | |
b178c8cc AD |
451 | /* There is no next-to-final state as yet. */ |
452 | /* Add one more shift in first_shift, | |
453 | going to the next-to-final state (yet to be made). */ | |
454 | sp = first_shift; | |
455 | ||
456 | sp2 = shifts_new (sp->nshifts + 1); | |
457 | ||
458 | /* Stick this shift into the vector at the proper place. */ | |
459 | statep = first_state->next; | |
460 | for (k = 0, i = 0; i < sp->nshifts; k++, i++) | |
461 | { | |
462 | if (statep->accessing_symbol > start_symbol && i == k) | |
463 | sp2->shifts[k++] = nstates; | |
464 | sp2->shifts[k] = sp->shifts[i]; | |
465 | statep = statep->next; | |
466 | } | |
467 | if (i == k) | |
468 | sp2->shifts[k++] = nstates; | |
469 | ||
470 | /* Patch sp2 into the chain of shifts | |
471 | in place of sp, at the beginning. */ | |
472 | sp2->next = sp->next; | |
473 | first_shift = sp2; | |
474 | if (last_shift == sp) | |
475 | last_shift = sp2; | |
476 | ||
477 | XFREE (sp); | |
40675e7c DM |
478 | |
479 | /* Create the next-to-final state, with shift to | |
480 | what will be the final state. */ | |
2fa6973e | 481 | insert_start_shift (); |
40675e7c DM |
482 | } |
483 | } | |
484 | else | |
485 | { | |
b178c8cc AD |
486 | /* The initial state didn't even have any shifts. |
487 | Give it one shift, to the next-to-final state. */ | |
4a38e613 | 488 | sp = shifts_new (1); |
40675e7c DM |
489 | sp->shifts[0] = nstates; |
490 | ||
b178c8cc AD |
491 | /* Patch sp into the chain of shifts at the beginning. */ |
492 | sp->next = first_shift; | |
40675e7c | 493 | first_shift = sp; |
40675e7c DM |
494 | |
495 | /* Create the next-to-final state, with shift to | |
b178c8cc | 496 | what will be the final state. */ |
2fa6973e | 497 | insert_start_shift (); |
40675e7c DM |
498 | } |
499 | ||
500 | /* Make the final state--the one that follows a shift from the | |
501 | next-to-final state. | |
502 | The symbol for that shift is 0 (end-of-file). */ | |
f59c437a | 503 | statep = CORE_ALLOC (0); |
40675e7c DM |
504 | statep->number = nstates; |
505 | last_state->next = statep; | |
506 | last_state = statep; | |
507 | ||
508 | /* Make the shift from the final state to the termination state. */ | |
4a38e613 | 509 | sp = shifts_new (1); |
40675e7c | 510 | sp->number = nstates++; |
40675e7c DM |
511 | sp->shifts[0] = nstates; |
512 | last_shift->next = sp; | |
513 | last_shift = sp; | |
514 | ||
515 | /* Note that the variable `final_state' refers to what we sometimes call | |
516 | the termination state. */ | |
517 | final_state = nstates; | |
518 | ||
519 | /* Make the termination state. */ | |
f59c437a | 520 | statep = CORE_ALLOC (0); |
40675e7c DM |
521 | statep->number = nstates++; |
522 | last_state->next = statep; | |
523 | last_state = statep; | |
524 | } | |
525 | ||
526 | ||
2fa6973e AD |
527 | /*----------------------------------------------------------------. |
528 | | Find which rules can be used for reduction transitions from the | | |
529 | | current state and make a reductions structure for the state to | | |
530 | | record their rule numbers. | | |
531 | `----------------------------------------------------------------*/ | |
532 | ||
4a120d45 | 533 | static void |
2fa6973e | 534 | save_reductions (void) |
40675e7c | 535 | { |
2fa6973e | 536 | int count; |
fb908786 | 537 | int i; |
40675e7c | 538 | |
2fa6973e | 539 | /* Find and count the active items that represent ends of rules. */ |
40675e7c | 540 | |
2fa6973e | 541 | count = 0; |
b2872512 | 542 | for (i = 0; i < nitemset; ++i) |
2fa6973e | 543 | { |
fb908786 | 544 | int item = ritem[itemset[i]]; |
2fa6973e AD |
545 | if (item < 0) |
546 | redset[count++] = -item; | |
547 | } | |
40675e7c | 548 | |
2fa6973e AD |
549 | /* Make a reductions structure and copy the data into it. */ |
550 | ||
551 | if (count) | |
552 | { | |
fb908786 | 553 | reductions *p = REDUCTIONS_ALLOC (count); |
2fa6973e AD |
554 | |
555 | p->number = this_state->number; | |
556 | p->nreds = count; | |
557 | ||
300f275f | 558 | shortcpy (p->rules, redset, count); |
2fa6973e AD |
559 | |
560 | if (last_reduction) | |
97db7bd4 | 561 | last_reduction->next = p; |
2fa6973e | 562 | else |
97db7bd4 AD |
563 | first_reduction = p; |
564 | last_reduction = p; | |
2fa6973e AD |
565 | } |
566 | } | |
567 | ||
568 | \f | |
569 | /*-------------------------------------------------------------------. | |
570 | | Compute the nondeterministic finite state machine (see state.h for | | |
571 | | details) from the grammar. | | |
572 | `-------------------------------------------------------------------*/ | |
573 | ||
574 | void | |
575 | generate_states (void) | |
576 | { | |
577 | allocate_storage (); | |
578 | new_closure (nitems); | |
579 | new_states (); | |
580 | ||
581 | while (this_state) | |
582 | { | |
23cbcc6c AD |
583 | if (trace_flag) |
584 | fprintf (stderr, "Processing state %d (reached by %s)\n", | |
585 | this_state->number, tags[this_state->accessing_symbol]); | |
2fa6973e AD |
586 | /* Set up ruleset and itemset for the transitions out of this |
587 | state. ruleset gets a 1 bit for each rule that could reduce | |
588 | now. itemset gets a vector of all the items that could be | |
589 | accepted next. */ | |
590 | closure (this_state->items, this_state->nitems); | |
591 | /* record the reductions allowed out of this state */ | |
592 | save_reductions (); | |
593 | /* find the itemsets of the states that shifts can reach */ | |
594 | new_itemsets (); | |
595 | /* find or create the core structures for those states */ | |
596 | append_states (); | |
597 | ||
598 | /* create the shifts structures for the shifts to those states, | |
599 | now that the state numbers transitioning to are known */ | |
d954473d | 600 | save_shifts (); |
2fa6973e AD |
601 | |
602 | /* states are queued when they are created; process them all */ | |
603 | this_state = this_state->next; | |
604 | } | |
605 | ||
606 | /* discard various storage */ | |
607 | free_closure (); | |
608 | free_storage (); | |
609 | ||
610 | /* set up initial and final states as parser wants them */ | |
611 | augment_automaton (); | |
40675e7c | 612 | } |