]>
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" |
40675e7c | 33 | |
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; |
125ecb56 | 53 | static size_t *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 AD |
65 | int i; |
66 | int count; | |
342b8b6e | 67 | short *symbol_count = NULL; |
40675e7c DM |
68 | |
69 | count = 0; | |
d7913476 | 70 | symbol_count = XCALLOC (short, nsyms); |
40675e7c | 71 | |
c87d4863 AD |
72 | for (i = 0; ritem[i]; ++i) |
73 | if (ritem[i] > 0) | |
74 | { | |
75 | count++; | |
76 | symbol_count[ritem[i]]++; | |
77 | } | |
40675e7c | 78 | |
2fa6973e AD |
79 | /* See comments before new_itemsets. All the vectors of items |
80 | live inside KERNEL_ITEMS. The number of active items after | |
40675e7c DM |
81 | some symbol cannot be more than the number of times that symbol |
82 | appears as an item, which is symbol_count[symbol]. | |
83 | We allocate that much space for each symbol. */ | |
84 | ||
d7913476 | 85 | kernel_base = XCALLOC (short *, nsyms); |
342b8b6e AD |
86 | if (count) |
87 | kernel_items = XCALLOC (short, count); | |
40675e7c DM |
88 | |
89 | count = 0; | |
90 | for (i = 0; i < nsyms; i++) | |
91 | { | |
92 | kernel_base[i] = kernel_items + count; | |
93 | count += symbol_count[i]; | |
94 | } | |
95 | ||
96 | shift_symbol = symbol_count; | |
125ecb56 | 97 | kernel_size = XCALLOC (size_t, nsyms); |
40675e7c DM |
98 | } |
99 | ||
100 | ||
4a120d45 | 101 | static void |
d2729d44 | 102 | allocate_storage (void) |
40675e7c | 103 | { |
2fa6973e | 104 | allocate_itemsets (); |
40675e7c | 105 | |
d7913476 AD |
106 | shiftset = XCALLOC (short, nsyms); |
107 | redset = XCALLOC (short, nrules + 1); | |
108 | state_table = XCALLOC (core *, STATE_TABLE_SIZE); | |
40675e7c DM |
109 | } |
110 | ||
111 | ||
4a120d45 | 112 | static void |
d2729d44 | 113 | free_storage (void) |
40675e7c | 114 | { |
d7913476 AD |
115 | XFREE (shift_symbol); |
116 | XFREE (redset); | |
117 | XFREE (shiftset); | |
118 | XFREE (kernel_base); | |
125ecb56 | 119 | XFREE (kernel_size); |
d7913476 AD |
120 | XFREE (kernel_items); |
121 | XFREE (state_table); | |
40675e7c DM |
122 | } |
123 | ||
124 | ||
125 | ||
40675e7c | 126 | |
2fa6973e AD |
127 | /*----------------------------------------------------------------. |
128 | | Find which symbols can be shifted in the current state, and for | | |
129 | | each one record which items would be active after that shift. | | |
130 | | Uses the contents of itemset. | | |
131 | | | | |
132 | | shift_symbol is set to a vector of the symbols that can be | | |
133 | | shifted. For each symbol in the grammar, kernel_base[symbol] | | |
134 | | points to a vector of item numbers activated if that symbol is | | |
125ecb56 | 135 | | shifted, and kernel_size[symbol] is their numbers. | |
2fa6973e | 136 | `----------------------------------------------------------------*/ |
40675e7c | 137 | |
4a120d45 | 138 | static void |
d2729d44 | 139 | new_itemsets (void) |
40675e7c | 140 | { |
2fa6973e AD |
141 | int i; |
142 | int shiftcount; | |
2fa6973e | 143 | |
9bfe901c | 144 | if (trace_flag) |
c87d4863 AD |
145 | fprintf (stderr, "Entering new_itemsets, state = %d\n", |
146 | this_state->number); | |
40675e7c DM |
147 | |
148 | for (i = 0; i < nsyms; i++) | |
125ecb56 | 149 | kernel_size[i] = 0; |
40675e7c DM |
150 | |
151 | shiftcount = 0; | |
152 | ||
fb908786 | 153 | for (i = 0; i < itemsetsize; ++i) |
40675e7c | 154 | { |
97db7bd4 | 155 | int symbol = ritem[itemset[i]]; |
40675e7c DM |
156 | if (symbol > 0) |
157 | { | |
125ecb56 | 158 | if (!kernel_size[symbol]) |
40675e7c | 159 | { |
97db7bd4 | 160 | shift_symbol[shiftcount] = symbol; |
97db7bd4 | 161 | shiftcount++; |
40675e7c DM |
162 | } |
163 | ||
125ecb56 AD |
164 | kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1; |
165 | kernel_size[symbol]++; | |
40675e7c DM |
166 | } |
167 | } | |
168 | ||
169 | nshifts = shiftcount; | |
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; |
2fa6973e | 216 | short *isp2; |
97db7bd4 | 217 | int i; |
2fa6973e | 218 | core *sp; |
40675e7c | 219 | |
9bfe901c | 220 | if (trace_flag) |
c87d4863 AD |
221 | fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n", |
222 | this_state->number, symbol, tags[symbol]); | |
40675e7c | 223 | |
97db7bd4 AD |
224 | /* Add up the target state's active item numbers to get a hash key. |
225 | */ | |
40675e7c | 226 | key = 0; |
125ecb56 | 227 | for (i = 0; i < kernel_size[symbol]; ++i) |
97db7bd4 | 228 | key += kernel_base[symbol][i]; |
40675e7c | 229 | key = key % STATE_TABLE_SIZE; |
40675e7c DM |
230 | sp = state_table[key]; |
231 | ||
232 | if (sp) | |
233 | { | |
97db7bd4 | 234 | int found = 0; |
40675e7c DM |
235 | while (!found) |
236 | { | |
125ecb56 | 237 | if (sp->nitems == kernel_size[symbol]) |
40675e7c | 238 | { |
97db7bd4 | 239 | int i; |
40675e7c | 240 | found = 1; |
125ecb56 | 241 | for (i = 0; i < kernel_size[symbol]; ++i) |
97db7bd4 AD |
242 | if (kernel_base[symbol][i] != sp->items[i]) |
243 | found = 0; | |
40675e7c DM |
244 | } |
245 | ||
246 | if (!found) | |
247 | { | |
248 | if (sp->link) | |
249 | { | |
250 | sp = sp->link; | |
251 | } | |
2fa6973e | 252 | else /* bucket exhausted and no match */ |
40675e7c | 253 | { |
2fa6973e | 254 | sp = sp->link = new_state (symbol); |
40675e7c DM |
255 | found = 1; |
256 | } | |
257 | } | |
258 | } | |
259 | } | |
2fa6973e | 260 | else /* bucket is empty */ |
40675e7c | 261 | { |
2fa6973e | 262 | state_table[key] = sp = new_state (symbol); |
40675e7c DM |
263 | } |
264 | ||
c87d4863 AD |
265 | if (trace_flag) |
266 | fprintf (stderr, "Exiting get_state => %d\n", sp->number); | |
267 | ||
36281465 | 268 | return sp->number; |
40675e7c DM |
269 | } |
270 | ||
2fa6973e AD |
271 | /*------------------------------------------------------------------. |
272 | | Use the information computed by new_itemsets to find the state | | |
273 | | numbers reached by each shift transition from the current state. | | |
274 | | | | |
275 | | shiftset is set up as a vector of state numbers of those states. | | |
276 | `------------------------------------------------------------------*/ | |
40675e7c | 277 | |
2fa6973e AD |
278 | static void |
279 | append_states (void) | |
40675e7c | 280 | { |
2fa6973e AD |
281 | int i; |
282 | int j; | |
283 | int symbol; | |
40675e7c | 284 | |
9bfe901c | 285 | if (trace_flag) |
c87d4863 AD |
286 | fprintf (stderr, "Entering append_states, state = %d\n", |
287 | this_state->number); | |
40675e7c | 288 | |
2fa6973e | 289 | /* first sort shift_symbol into increasing order */ |
40675e7c | 290 | |
2fa6973e AD |
291 | for (i = 1; i < nshifts; i++) |
292 | { | |
293 | symbol = shift_symbol[i]; | |
294 | j = i; | |
295 | while (j > 0 && shift_symbol[j - 1] > symbol) | |
296 | { | |
297 | shift_symbol[j] = shift_symbol[j - 1]; | |
298 | j--; | |
299 | } | |
300 | shift_symbol[j] = symbol; | |
301 | } | |
40675e7c | 302 | |
2fa6973e | 303 | for (i = 0; i < nshifts; i++) |
97db7bd4 | 304 | shiftset[i] = get_state (shift_symbol[i]); |
40675e7c DM |
305 | } |
306 | ||
307 | ||
4a120d45 | 308 | static void |
2fa6973e | 309 | new_states (void) |
40675e7c | 310 | { |
97db7bd4 | 311 | first_state = last_state = this_state = CORE_ALLOC (0); |
40675e7c DM |
312 | nstates = 1; |
313 | } | |
314 | ||
315 | ||
4a120d45 | 316 | static void |
d2729d44 | 317 | save_shifts (void) |
40675e7c | 318 | { |
97db7bd4 | 319 | shifts *p = SHIFTS_ALLOC (nshifts); |
40675e7c DM |
320 | |
321 | p->number = this_state->number; | |
322 | p->nshifts = nshifts; | |
323 | ||
300f275f | 324 | shortcpy (p->shifts, shiftset, nshifts); |
40675e7c DM |
325 | |
326 | if (last_shift) | |
97db7bd4 | 327 | last_shift->next = p; |
40675e7c | 328 | else |
97db7bd4 AD |
329 | first_shift = p; |
330 | last_shift = p; | |
40675e7c DM |
331 | } |
332 | ||
333 | ||
2fa6973e AD |
334 | /*------------------------------------------------------------------. |
335 | | Subroutine of augment_automaton. Create the next-to-final state, | | |
336 | | to which a shift has already been made in the initial state. | | |
337 | `------------------------------------------------------------------*/ | |
40675e7c | 338 | |
4a120d45 | 339 | static void |
2fa6973e | 340 | insert_start_shift (void) |
40675e7c | 341 | { |
2fa6973e AD |
342 | core *statep; |
343 | shifts *sp; | |
40675e7c | 344 | |
f59c437a | 345 | statep = CORE_ALLOC (0); |
2fa6973e AD |
346 | statep->number = nstates; |
347 | statep->accessing_symbol = start_symbol; | |
40675e7c | 348 | |
2fa6973e AD |
349 | last_state->next = statep; |
350 | last_state = statep; | |
40675e7c | 351 | |
2fa6973e | 352 | /* Make a shift from this state to (what will be) the final state. */ |
f59c437a | 353 | sp = SHIFTS_ALLOC (1); |
2fa6973e AD |
354 | sp->number = nstates++; |
355 | sp->nshifts = 1; | |
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 | int i; |
375 | int k; | |
376 | core *statep; | |
377 | shifts *sp; | |
378 | shifts *sp2; | |
379 | shifts *sp1 = NULL; | |
40675e7c DM |
380 | |
381 | sp = first_shift; | |
382 | ||
383 | if (sp) | |
384 | { | |
385 | if (sp->number == 0) | |
386 | { | |
387 | k = sp->nshifts; | |
388 | statep = first_state->next; | |
389 | ||
390 | /* The states reached by shifts from first_state are numbered 1...K. | |
391 | Look for one reached by start_symbol. */ | |
392 | while (statep->accessing_symbol < start_symbol | |
2fa6973e | 393 | && statep->number < k) |
40675e7c DM |
394 | statep = statep->next; |
395 | ||
396 | if (statep->accessing_symbol == start_symbol) | |
397 | { | |
398 | /* We already have a next-to-final state. | |
2fa6973e | 399 | Make sure it has a shift to what will be the final state. */ |
40675e7c DM |
400 | k = statep->number; |
401 | ||
402 | while (sp && sp->number < k) | |
403 | { | |
404 | sp1 = sp; | |
405 | sp = sp->next; | |
406 | } | |
407 | ||
408 | if (sp && sp->number == k) | |
409 | { | |
f59c437a | 410 | sp2 = SHIFTS_ALLOC (sp->nshifts + 1); |
40675e7c DM |
411 | sp2->number = k; |
412 | sp2->nshifts = sp->nshifts + 1; | |
413 | sp2->shifts[0] = nstates; | |
414 | for (i = sp->nshifts; i > 0; i--) | |
415 | sp2->shifts[i] = sp->shifts[i - 1]; | |
416 | ||
417 | /* Patch sp2 into the chain of shifts in place of sp, | |
418 | following sp1. */ | |
419 | sp2->next = sp->next; | |
420 | sp1->next = sp2; | |
421 | if (sp == last_shift) | |
422 | last_shift = sp2; | |
d7913476 | 423 | XFREE (sp); |
40675e7c DM |
424 | } |
425 | else | |
426 | { | |
f59c437a | 427 | sp2 = SHIFTS_ALLOC (1); |
40675e7c DM |
428 | sp2->number = k; |
429 | sp2->nshifts = 1; | |
430 | sp2->shifts[0] = nstates; | |
431 | ||
432 | /* Patch sp2 into the chain of shifts between sp1 and sp. */ | |
433 | sp2->next = sp; | |
434 | sp1->next = sp2; | |
435 | if (sp == 0) | |
436 | last_shift = sp2; | |
437 | } | |
438 | } | |
439 | else | |
440 | { | |
441 | /* There is no next-to-final state as yet. */ | |
442 | /* Add one more shift in first_shift, | |
2fa6973e | 443 | going to the next-to-final state (yet to be made). */ |
40675e7c DM |
444 | sp = first_shift; |
445 | ||
f59c437a | 446 | sp2 = SHIFTS_ALLOC (sp->nshifts + 1); |
40675e7c DM |
447 | sp2->nshifts = sp->nshifts + 1; |
448 | ||
449 | /* Stick this shift into the vector at the proper place. */ | |
450 | statep = first_state->next; | |
451 | for (k = 0, i = 0; i < sp->nshifts; k++, i++) | |
452 | { | |
453 | if (statep->accessing_symbol > start_symbol && i == k) | |
454 | sp2->shifts[k++] = nstates; | |
455 | sp2->shifts[k] = sp->shifts[i]; | |
456 | statep = statep->next; | |
457 | } | |
458 | if (i == k) | |
459 | sp2->shifts[k++] = nstates; | |
460 | ||
461 | /* Patch sp2 into the chain of shifts | |
2fa6973e | 462 | in place of sp, at the beginning. */ |
40675e7c DM |
463 | sp2->next = sp->next; |
464 | first_shift = sp2; | |
465 | if (last_shift == sp) | |
466 | last_shift = sp2; | |
467 | ||
d7913476 | 468 | XFREE (sp); |
40675e7c DM |
469 | |
470 | /* Create the next-to-final state, with shift to | |
2fa6973e AD |
471 | what will be the final state. */ |
472 | insert_start_shift (); | |
40675e7c DM |
473 | } |
474 | } | |
475 | else | |
476 | { | |
477 | /* The initial state didn't even have any shifts. | |
478 | Give it one shift, to the next-to-final state. */ | |
f59c437a | 479 | sp = SHIFTS_ALLOC (1); |
40675e7c DM |
480 | sp->nshifts = 1; |
481 | sp->shifts[0] = nstates; | |
482 | ||
483 | /* Patch sp into the chain of shifts at the beginning. */ | |
484 | sp->next = first_shift; | |
485 | first_shift = sp; | |
486 | ||
487 | /* Create the next-to-final state, with shift to | |
488 | what will be the final state. */ | |
2fa6973e | 489 | insert_start_shift (); |
40675e7c DM |
490 | } |
491 | } | |
492 | else | |
493 | { | |
494 | /* There are no shifts for any state. | |
2fa6973e | 495 | Make one shift, from the initial state to the next-to-final state. */ |
40675e7c | 496 | |
f59c437a | 497 | sp = SHIFTS_ALLOC (1); |
40675e7c DM |
498 | sp->nshifts = 1; |
499 | sp->shifts[0] = nstates; | |
500 | ||
501 | /* Initialize the chain of shifts with sp. */ | |
502 | first_shift = sp; | |
503 | last_shift = sp; | |
504 | ||
505 | /* Create the next-to-final state, with shift to | |
2fa6973e AD |
506 | what will be the final state. */ |
507 | insert_start_shift (); | |
40675e7c DM |
508 | } |
509 | ||
510 | /* Make the final state--the one that follows a shift from the | |
511 | next-to-final state. | |
512 | The symbol for that shift is 0 (end-of-file). */ | |
f59c437a | 513 | statep = CORE_ALLOC (0); |
40675e7c DM |
514 | statep->number = nstates; |
515 | last_state->next = statep; | |
516 | last_state = statep; | |
517 | ||
518 | /* Make the shift from the final state to the termination state. */ | |
f59c437a | 519 | sp = SHIFTS_ALLOC (1); |
40675e7c DM |
520 | sp->number = nstates++; |
521 | sp->nshifts = 1; | |
522 | sp->shifts[0] = nstates; | |
523 | last_shift->next = sp; | |
524 | last_shift = sp; | |
525 | ||
526 | /* Note that the variable `final_state' refers to what we sometimes call | |
527 | the termination state. */ | |
528 | final_state = nstates; | |
529 | ||
530 | /* Make the termination state. */ | |
f59c437a | 531 | statep = CORE_ALLOC (0); |
40675e7c DM |
532 | statep->number = nstates++; |
533 | last_state->next = statep; | |
534 | last_state = statep; | |
535 | } | |
536 | ||
537 | ||
2fa6973e AD |
538 | /*----------------------------------------------------------------. |
539 | | Find which rules can be used for reduction transitions from the | | |
540 | | current state and make a reductions structure for the state to | | |
541 | | record their rule numbers. | | |
542 | `----------------------------------------------------------------*/ | |
543 | ||
4a120d45 | 544 | static void |
2fa6973e | 545 | save_reductions (void) |
40675e7c | 546 | { |
2fa6973e | 547 | int count; |
fb908786 | 548 | int i; |
40675e7c | 549 | |
2fa6973e | 550 | /* Find and count the active items that represent ends of rules. */ |
40675e7c | 551 | |
2fa6973e | 552 | count = 0; |
fb908786 | 553 | for (i = 0; i < itemsetsize; ++i) |
2fa6973e | 554 | { |
fb908786 | 555 | int item = ritem[itemset[i]]; |
2fa6973e AD |
556 | if (item < 0) |
557 | redset[count++] = -item; | |
558 | } | |
40675e7c | 559 | |
2fa6973e AD |
560 | /* Make a reductions structure and copy the data into it. */ |
561 | ||
562 | if (count) | |
563 | { | |
fb908786 | 564 | reductions *p = REDUCTIONS_ALLOC (count); |
2fa6973e AD |
565 | |
566 | p->number = this_state->number; | |
567 | p->nreds = count; | |
568 | ||
300f275f | 569 | shortcpy (p->rules, redset, count); |
2fa6973e AD |
570 | |
571 | if (last_reduction) | |
97db7bd4 | 572 | last_reduction->next = p; |
2fa6973e | 573 | else |
97db7bd4 AD |
574 | first_reduction = p; |
575 | last_reduction = p; | |
2fa6973e AD |
576 | } |
577 | } | |
578 | ||
579 | \f | |
580 | /*-------------------------------------------------------------------. | |
581 | | Compute the nondeterministic finite state machine (see state.h for | | |
582 | | details) from the grammar. | | |
583 | `-------------------------------------------------------------------*/ | |
584 | ||
585 | void | |
586 | generate_states (void) | |
587 | { | |
588 | allocate_storage (); | |
589 | new_closure (nitems); | |
590 | new_states (); | |
591 | ||
592 | while (this_state) | |
593 | { | |
594 | /* Set up ruleset and itemset for the transitions out of this | |
595 | state. ruleset gets a 1 bit for each rule that could reduce | |
596 | now. itemset gets a vector of all the items that could be | |
597 | accepted next. */ | |
598 | closure (this_state->items, this_state->nitems); | |
599 | /* record the reductions allowed out of this state */ | |
600 | save_reductions (); | |
601 | /* find the itemsets of the states that shifts can reach */ | |
602 | new_itemsets (); | |
603 | /* find or create the core structures for those states */ | |
604 | append_states (); | |
605 | ||
606 | /* create the shifts structures for the shifts to those states, | |
607 | now that the state numbers transitioning to are known */ | |
608 | if (nshifts > 0) | |
609 | save_shifts (); | |
610 | ||
611 | /* states are queued when they are created; process them all */ | |
612 | this_state = this_state->next; | |
613 | } | |
614 | ||
615 | /* discard various storage */ | |
616 | free_closure (); | |
617 | free_storage (); | |
618 | ||
619 | /* set up initial and final states as parser wants them */ | |
620 | augment_automaton (); | |
40675e7c | 621 | } |