]>
Commit | Line | Data |
---|---|---|
40675e7c DM |
1 | /* Generate the nondeterministic finite state machine for bison, |
2 | Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc. | |
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" |
7612000c | 26 | #include "alloc.h" |
40675e7c DM |
27 | #include "gram.h" |
28 | #include "state.h" | |
a0f6b076 | 29 | #include "complain.h" |
2fa6973e | 30 | #include "closure.h" |
40675e7c DM |
31 | |
32 | extern char *nullable; | |
33 | extern short *itemset; | |
34 | extern short *itemsetend; | |
35 | ||
36 | ||
37 | int nstates; | |
38 | int final_state; | |
39 | core *first_state; | |
40 | shifts *first_shift; | |
41 | reductions *first_reduction; | |
42 | ||
2fa6973e | 43 | extern void generate_states PARAMS ((void)); |
40675e7c DM |
44 | |
45 | static core *this_state; | |
46 | static core *last_state; | |
47 | static shifts *last_shift; | |
48 | static reductions *last_reduction; | |
49 | ||
50 | static int nshifts; | |
51 | static short *shift_symbol; | |
52 | ||
53 | static short *redset; | |
54 | static short *shiftset; | |
55 | ||
56 | static short **kernel_base; | |
57 | static short **kernel_end; | |
58 | static short *kernel_items; | |
59 | ||
60 | /* hash table for states, to recognize equivalent ones. */ | |
61 | ||
62 | #define STATE_TABLE_SIZE 1009 | |
63 | static core **state_table; | |
64 | ||
2fa6973e | 65 | \f |
4a120d45 | 66 | static void |
d2729d44 | 67 | allocate_itemsets (void) |
40675e7c | 68 | { |
2fa6973e AD |
69 | short *itemp; |
70 | int symbol; | |
71 | int i; | |
72 | int count; | |
73 | short *symbol_count; | |
40675e7c DM |
74 | |
75 | count = 0; | |
2fa6973e | 76 | symbol_count = NEW2 (nsyms, short); |
40675e7c DM |
77 | |
78 | itemp = ritem; | |
79 | symbol = *itemp++; | |
80 | while (symbol) | |
81 | { | |
82 | if (symbol > 0) | |
83 | { | |
84 | count++; | |
85 | symbol_count[symbol]++; | |
86 | } | |
87 | symbol = *itemp++; | |
88 | } | |
89 | ||
2fa6973e AD |
90 | /* See comments before new_itemsets. All the vectors of items |
91 | live inside KERNEL_ITEMS. The number of active items after | |
40675e7c DM |
92 | some symbol cannot be more than the number of times that symbol |
93 | appears as an item, which is symbol_count[symbol]. | |
94 | We allocate that much space for each symbol. */ | |
95 | ||
2fa6973e AD |
96 | kernel_base = NEW2 (nsyms, short *); |
97 | kernel_items = NEW2 (count, short); | |
40675e7c DM |
98 | |
99 | count = 0; | |
100 | for (i = 0; i < nsyms; i++) | |
101 | { | |
102 | kernel_base[i] = kernel_items + count; | |
103 | count += symbol_count[i]; | |
104 | } | |
105 | ||
106 | shift_symbol = symbol_count; | |
2fa6973e | 107 | kernel_end = NEW2 (nsyms, short *); |
40675e7c DM |
108 | } |
109 | ||
110 | ||
4a120d45 | 111 | static void |
d2729d44 | 112 | allocate_storage (void) |
40675e7c | 113 | { |
2fa6973e | 114 | allocate_itemsets (); |
40675e7c | 115 | |
2fa6973e AD |
116 | shiftset = NEW2 (nsyms, short); |
117 | redset = NEW2 (nrules + 1, short); | |
118 | state_table = NEW2 (STATE_TABLE_SIZE, core *); | |
40675e7c DM |
119 | } |
120 | ||
121 | ||
4a120d45 | 122 | static void |
d2729d44 | 123 | free_storage (void) |
40675e7c | 124 | { |
2fa6973e AD |
125 | FREE (shift_symbol); |
126 | FREE (redset); | |
127 | FREE (shiftset); | |
128 | FREE (kernel_base); | |
129 | FREE (kernel_end); | |
130 | FREE (kernel_items); | |
131 | FREE (state_table); | |
40675e7c DM |
132 | } |
133 | ||
134 | ||
135 | ||
40675e7c | 136 | |
2fa6973e AD |
137 | /*----------------------------------------------------------------. |
138 | | Find which symbols can be shifted in the current state, and for | | |
139 | | each one record which items would be active after that shift. | | |
140 | | Uses the contents of itemset. | | |
141 | | | | |
142 | | shift_symbol is set to a vector of the symbols that can be | | |
143 | | shifted. For each symbol in the grammar, kernel_base[symbol] | | |
144 | | points to a vector of item numbers activated if that symbol is | | |
145 | | shifted, and kernel_end[symbol] points after the end of that | | |
146 | | vector. | | |
147 | `----------------------------------------------------------------*/ | |
40675e7c | 148 | |
4a120d45 | 149 | static void |
d2729d44 | 150 | new_itemsets (void) |
40675e7c | 151 | { |
2fa6973e AD |
152 | int i; |
153 | int shiftcount; | |
154 | short *isp; | |
155 | short *ksp; | |
156 | int symbol; | |
157 | ||
158 | #if TRACE | |
159 | fprintf (stderr, "Entering new_itemsets\n"); | |
40675e7c DM |
160 | #endif |
161 | ||
162 | for (i = 0; i < nsyms; i++) | |
163 | kernel_end[i] = NULL; | |
164 | ||
165 | shiftcount = 0; | |
166 | ||
167 | isp = itemset; | |
168 | ||
169 | while (isp < itemsetend) | |
170 | { | |
171 | i = *isp++; | |
172 | symbol = ritem[i]; | |
173 | if (symbol > 0) | |
174 | { | |
2fa6973e | 175 | ksp = kernel_end[symbol]; |
40675e7c | 176 | |
2fa6973e | 177 | if (!ksp) |
40675e7c DM |
178 | { |
179 | shift_symbol[shiftcount++] = symbol; | |
180 | ksp = kernel_base[symbol]; | |
181 | } | |
182 | ||
2fa6973e AD |
183 | *ksp++ = i + 1; |
184 | kernel_end[symbol] = ksp; | |
40675e7c DM |
185 | } |
186 | } | |
187 | ||
188 | nshifts = shiftcount; | |
189 | } | |
190 | ||
191 | ||
192 | ||
2fa6973e AD |
193 | /*-----------------------------------------------------------------. |
194 | | Subroutine of get_state. Create a new state for those items, if | | |
195 | | necessary. | | |
196 | `-----------------------------------------------------------------*/ | |
40675e7c | 197 | |
2fa6973e AD |
198 | static core * |
199 | new_state (int symbol) | |
40675e7c | 200 | { |
2fa6973e AD |
201 | int n; |
202 | core *p; | |
203 | short *isp1; | |
204 | short *isp2; | |
205 | short *iend; | |
40675e7c | 206 | |
2fa6973e AD |
207 | #if TRACE |
208 | fprintf (stderr, "Entering new_state, symbol = %d\n", symbol); | |
40675e7c DM |
209 | #endif |
210 | ||
2fa6973e AD |
211 | if (nstates >= MAXSHORT) |
212 | fatal (_("too many states (max %d)"), MAXSHORT); | |
40675e7c | 213 | |
2fa6973e AD |
214 | isp1 = kernel_base[symbol]; |
215 | iend = kernel_end[symbol]; | |
216 | n = iend - isp1; | |
40675e7c | 217 | |
2fa6973e AD |
218 | p = |
219 | (core *) xmalloc ((unsigned) (sizeof (core) + (n - 1) * sizeof (short))); | |
220 | p->accessing_symbol = symbol; | |
221 | p->number = nstates; | |
222 | p->nitems = n; | |
223 | ||
224 | isp2 = p->items; | |
225 | while (isp1 < iend) | |
226 | *isp2++ = *isp1++; | |
227 | ||
228 | last_state->next = p; | |
229 | last_state = p; | |
40675e7c | 230 | |
2fa6973e | 231 | nstates++; |
40675e7c | 232 | |
2fa6973e AD |
233 | return p; |
234 | } | |
40675e7c | 235 | |
2fa6973e AD |
236 | |
237 | /*--------------------------------------------------------------. | |
238 | | Find the state number for the state we would get to (from the | | |
239 | | current state) by shifting symbol. Create a new state if no | | |
240 | | equivalent one exists already. Used by append_states. | | |
241 | `--------------------------------------------------------------*/ | |
40675e7c | 242 | |
4a120d45 | 243 | static int |
d2729d44 | 244 | get_state (int symbol) |
40675e7c | 245 | { |
2fa6973e AD |
246 | int key; |
247 | short *isp1; | |
248 | short *isp2; | |
249 | short *iend; | |
250 | core *sp; | |
251 | int found; | |
40675e7c DM |
252 | |
253 | int n; | |
254 | ||
2fa6973e AD |
255 | #if TRACE |
256 | fprintf (stderr, "Entering get_state, symbol = %d\n", symbol); | |
40675e7c DM |
257 | #endif |
258 | ||
259 | isp1 = kernel_base[symbol]; | |
260 | iend = kernel_end[symbol]; | |
261 | n = iend - isp1; | |
262 | ||
263 | /* add up the target state's active item numbers to get a hash key */ | |
264 | key = 0; | |
265 | while (isp1 < iend) | |
266 | key += *isp1++; | |
267 | ||
268 | key = key % STATE_TABLE_SIZE; | |
269 | ||
270 | sp = state_table[key]; | |
271 | ||
272 | if (sp) | |
273 | { | |
274 | found = 0; | |
275 | while (!found) | |
276 | { | |
277 | if (sp->nitems == n) | |
278 | { | |
279 | found = 1; | |
280 | isp1 = kernel_base[symbol]; | |
281 | isp2 = sp->items; | |
282 | ||
283 | while (found && isp1 < iend) | |
284 | { | |
285 | if (*isp1++ != *isp2++) | |
286 | found = 0; | |
287 | } | |
288 | } | |
289 | ||
290 | if (!found) | |
291 | { | |
292 | if (sp->link) | |
293 | { | |
294 | sp = sp->link; | |
295 | } | |
2fa6973e | 296 | else /* bucket exhausted and no match */ |
40675e7c | 297 | { |
2fa6973e | 298 | sp = sp->link = new_state (symbol); |
40675e7c DM |
299 | found = 1; |
300 | } | |
301 | } | |
302 | } | |
303 | } | |
2fa6973e | 304 | else /* bucket is empty */ |
40675e7c | 305 | { |
2fa6973e | 306 | state_table[key] = sp = new_state (symbol); |
40675e7c DM |
307 | } |
308 | ||
36281465 | 309 | return sp->number; |
40675e7c DM |
310 | } |
311 | ||
2fa6973e AD |
312 | /*------------------------------------------------------------------. |
313 | | Use the information computed by new_itemsets to find the state | | |
314 | | numbers reached by each shift transition from the current state. | | |
315 | | | | |
316 | | shiftset is set up as a vector of state numbers of those states. | | |
317 | `------------------------------------------------------------------*/ | |
40675e7c | 318 | |
2fa6973e AD |
319 | static void |
320 | append_states (void) | |
40675e7c | 321 | { |
2fa6973e AD |
322 | int i; |
323 | int j; | |
324 | int symbol; | |
40675e7c | 325 | |
2fa6973e AD |
326 | #if TRACE |
327 | fprintf (stderr, "Entering append_states\n"); | |
328 | #endif | |
40675e7c | 329 | |
2fa6973e | 330 | /* first sort shift_symbol into increasing order */ |
40675e7c | 331 | |
2fa6973e AD |
332 | for (i = 1; i < nshifts; i++) |
333 | { | |
334 | symbol = shift_symbol[i]; | |
335 | j = i; | |
336 | while (j > 0 && shift_symbol[j - 1] > symbol) | |
337 | { | |
338 | shift_symbol[j] = shift_symbol[j - 1]; | |
339 | j--; | |
340 | } | |
341 | shift_symbol[j] = symbol; | |
342 | } | |
40675e7c | 343 | |
2fa6973e AD |
344 | for (i = 0; i < nshifts; i++) |
345 | { | |
346 | symbol = shift_symbol[i]; | |
347 | shiftset[i] = get_state (symbol); | |
348 | } | |
40675e7c DM |
349 | } |
350 | ||
351 | ||
4a120d45 | 352 | static void |
2fa6973e | 353 | new_states (void) |
40675e7c | 354 | { |
2fa6973e | 355 | core *p; |
40675e7c | 356 | |
2fa6973e | 357 | p = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); |
40675e7c DM |
358 | first_state = last_state = this_state = p; |
359 | nstates = 1; | |
360 | } | |
361 | ||
362 | ||
4a120d45 | 363 | static void |
d2729d44 | 364 | save_shifts (void) |
40675e7c | 365 | { |
2fa6973e AD |
366 | shifts *p; |
367 | short *sp1; | |
368 | short *sp2; | |
369 | short *send; | |
40675e7c | 370 | |
2fa6973e AD |
371 | p = (shifts *) xmalloc ((unsigned) (sizeof (shifts) + |
372 | (nshifts - 1) * sizeof (short))); | |
40675e7c DM |
373 | |
374 | p->number = this_state->number; | |
375 | p->nshifts = nshifts; | |
376 | ||
377 | sp1 = shiftset; | |
378 | sp2 = p->shifts; | |
379 | send = shiftset + nshifts; | |
380 | ||
381 | while (sp1 < send) | |
382 | *sp2++ = *sp1++; | |
383 | ||
384 | if (last_shift) | |
385 | { | |
386 | last_shift->next = p; | |
387 | last_shift = p; | |
388 | } | |
389 | else | |
390 | { | |
391 | first_shift = p; | |
392 | last_shift = p; | |
393 | } | |
394 | } | |
395 | ||
396 | ||
2fa6973e AD |
397 | /*------------------------------------------------------------------. |
398 | | Subroutine of augment_automaton. Create the next-to-final state, | | |
399 | | to which a shift has already been made in the initial state. | | |
400 | `------------------------------------------------------------------*/ | |
40675e7c | 401 | |
4a120d45 | 402 | static void |
2fa6973e | 403 | insert_start_shift (void) |
40675e7c | 404 | { |
2fa6973e AD |
405 | core *statep; |
406 | shifts *sp; | |
40675e7c | 407 | |
2fa6973e AD |
408 | statep = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); |
409 | statep->number = nstates; | |
410 | statep->accessing_symbol = start_symbol; | |
40675e7c | 411 | |
2fa6973e AD |
412 | last_state->next = statep; |
413 | last_state = statep; | |
40675e7c | 414 | |
2fa6973e AD |
415 | /* Make a shift from this state to (what will be) the final state. */ |
416 | sp = NEW (shifts); | |
417 | sp->number = nstates++; | |
418 | sp->nshifts = 1; | |
419 | sp->shifts[0] = nstates; | |
40675e7c | 420 | |
2fa6973e AD |
421 | last_shift->next = sp; |
422 | last_shift = sp; | |
40675e7c DM |
423 | } |
424 | ||
425 | ||
2fa6973e AD |
426 | /*------------------------------------------------------------------. |
427 | | Make sure that the initial state has a shift that accepts the | | |
428 | | grammar's start symbol and goes to the next-to-final state, which | | |
429 | | has a shift going to the final state, which has a shift to the | | |
430 | | termination state. Create such states and shifts if they don't | | |
431 | | happen to exist already. | | |
432 | `------------------------------------------------------------------*/ | |
40675e7c | 433 | |
4a120d45 | 434 | static void |
d2729d44 | 435 | augment_automaton (void) |
40675e7c | 436 | { |
2fa6973e AD |
437 | int i; |
438 | int k; | |
439 | core *statep; | |
440 | shifts *sp; | |
441 | shifts *sp2; | |
442 | shifts *sp1 = NULL; | |
40675e7c DM |
443 | |
444 | sp = first_shift; | |
445 | ||
446 | if (sp) | |
447 | { | |
448 | if (sp->number == 0) | |
449 | { | |
450 | k = sp->nshifts; | |
451 | statep = first_state->next; | |
452 | ||
453 | /* The states reached by shifts from first_state are numbered 1...K. | |
454 | Look for one reached by start_symbol. */ | |
455 | while (statep->accessing_symbol < start_symbol | |
2fa6973e | 456 | && statep->number < k) |
40675e7c DM |
457 | statep = statep->next; |
458 | ||
459 | if (statep->accessing_symbol == start_symbol) | |
460 | { | |
461 | /* We already have a next-to-final state. | |
2fa6973e | 462 | Make sure it has a shift to what will be the final state. */ |
40675e7c DM |
463 | k = statep->number; |
464 | ||
465 | while (sp && sp->number < k) | |
466 | { | |
467 | sp1 = sp; | |
468 | sp = sp->next; | |
469 | } | |
470 | ||
471 | if (sp && sp->number == k) | |
472 | { | |
2fa6973e AD |
473 | sp2 = (shifts *) xmalloc ((unsigned) (sizeof (shifts) |
474 | + | |
475 | sp->nshifts * | |
476 | sizeof (short))); | |
40675e7c DM |
477 | sp2->number = k; |
478 | sp2->nshifts = sp->nshifts + 1; | |
479 | sp2->shifts[0] = nstates; | |
480 | for (i = sp->nshifts; i > 0; i--) | |
481 | sp2->shifts[i] = sp->shifts[i - 1]; | |
482 | ||
483 | /* Patch sp2 into the chain of shifts in place of sp, | |
484 | following sp1. */ | |
485 | sp2->next = sp->next; | |
486 | sp1->next = sp2; | |
487 | if (sp == last_shift) | |
488 | last_shift = sp2; | |
2fa6973e | 489 | FREE (sp); |
40675e7c DM |
490 | } |
491 | else | |
492 | { | |
2fa6973e | 493 | sp2 = NEW (shifts); |
40675e7c DM |
494 | sp2->number = k; |
495 | sp2->nshifts = 1; | |
496 | sp2->shifts[0] = nstates; | |
497 | ||
498 | /* Patch sp2 into the chain of shifts between sp1 and sp. */ | |
499 | sp2->next = sp; | |
500 | sp1->next = sp2; | |
501 | if (sp == 0) | |
502 | last_shift = sp2; | |
503 | } | |
504 | } | |
505 | else | |
506 | { | |
507 | /* There is no next-to-final state as yet. */ | |
508 | /* Add one more shift in first_shift, | |
2fa6973e | 509 | going to the next-to-final state (yet to be made). */ |
40675e7c DM |
510 | sp = first_shift; |
511 | ||
2fa6973e AD |
512 | sp2 = (shifts *) xmalloc (sizeof (shifts) |
513 | + sp->nshifts * sizeof (short)); | |
40675e7c DM |
514 | sp2->nshifts = sp->nshifts + 1; |
515 | ||
516 | /* Stick this shift into the vector at the proper place. */ | |
517 | statep = first_state->next; | |
518 | for (k = 0, i = 0; i < sp->nshifts; k++, i++) | |
519 | { | |
520 | if (statep->accessing_symbol > start_symbol && i == k) | |
521 | sp2->shifts[k++] = nstates; | |
522 | sp2->shifts[k] = sp->shifts[i]; | |
523 | statep = statep->next; | |
524 | } | |
525 | if (i == k) | |
526 | sp2->shifts[k++] = nstates; | |
527 | ||
528 | /* Patch sp2 into the chain of shifts | |
2fa6973e | 529 | in place of sp, at the beginning. */ |
40675e7c DM |
530 | sp2->next = sp->next; |
531 | first_shift = sp2; | |
532 | if (last_shift == sp) | |
533 | last_shift = sp2; | |
534 | ||
2fa6973e | 535 | FREE (sp); |
40675e7c DM |
536 | |
537 | /* Create the next-to-final state, with shift to | |
2fa6973e AD |
538 | what will be the final state. */ |
539 | insert_start_shift (); | |
40675e7c DM |
540 | } |
541 | } | |
542 | else | |
543 | { | |
544 | /* The initial state didn't even have any shifts. | |
545 | Give it one shift, to the next-to-final state. */ | |
2fa6973e | 546 | sp = NEW (shifts); |
40675e7c DM |
547 | sp->nshifts = 1; |
548 | sp->shifts[0] = nstates; | |
549 | ||
550 | /* Patch sp into the chain of shifts at the beginning. */ | |
551 | sp->next = first_shift; | |
552 | first_shift = sp; | |
553 | ||
554 | /* Create the next-to-final state, with shift to | |
555 | what will be the final state. */ | |
2fa6973e | 556 | insert_start_shift (); |
40675e7c DM |
557 | } |
558 | } | |
559 | else | |
560 | { | |
561 | /* There are no shifts for any state. | |
2fa6973e | 562 | Make one shift, from the initial state to the next-to-final state. */ |
40675e7c | 563 | |
2fa6973e | 564 | sp = NEW (shifts); |
40675e7c DM |
565 | sp->nshifts = 1; |
566 | sp->shifts[0] = nstates; | |
567 | ||
568 | /* Initialize the chain of shifts with sp. */ | |
569 | first_shift = sp; | |
570 | last_shift = sp; | |
571 | ||
572 | /* Create the next-to-final state, with shift to | |
2fa6973e AD |
573 | what will be the final state. */ |
574 | insert_start_shift (); | |
40675e7c DM |
575 | } |
576 | ||
577 | /* Make the final state--the one that follows a shift from the | |
578 | next-to-final state. | |
579 | The symbol for that shift is 0 (end-of-file). */ | |
2fa6973e | 580 | statep = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); |
40675e7c DM |
581 | statep->number = nstates; |
582 | last_state->next = statep; | |
583 | last_state = statep; | |
584 | ||
585 | /* Make the shift from the final state to the termination state. */ | |
2fa6973e | 586 | sp = NEW (shifts); |
40675e7c DM |
587 | sp->number = nstates++; |
588 | sp->nshifts = 1; | |
589 | sp->shifts[0] = nstates; | |
590 | last_shift->next = sp; | |
591 | last_shift = sp; | |
592 | ||
593 | /* Note that the variable `final_state' refers to what we sometimes call | |
594 | the termination state. */ | |
595 | final_state = nstates; | |
596 | ||
597 | /* Make the termination state. */ | |
2fa6973e | 598 | statep = (core *) xmalloc ((unsigned) (sizeof (core) - sizeof (short))); |
40675e7c DM |
599 | statep->number = nstates++; |
600 | last_state->next = statep; | |
601 | last_state = statep; | |
602 | } | |
603 | ||
604 | ||
2fa6973e AD |
605 | /*----------------------------------------------------------------. |
606 | | Find which rules can be used for reduction transitions from the | | |
607 | | current state and make a reductions structure for the state to | | |
608 | | record their rule numbers. | | |
609 | `----------------------------------------------------------------*/ | |
610 | ||
4a120d45 | 611 | static void |
2fa6973e | 612 | save_reductions (void) |
40675e7c | 613 | { |
2fa6973e AD |
614 | short *isp; |
615 | short *rp1; | |
616 | short *rp2; | |
617 | int item; | |
618 | int count; | |
619 | reductions *p; | |
40675e7c | 620 | |
2fa6973e | 621 | short *rend; |
40675e7c | 622 | |
2fa6973e | 623 | /* Find and count the active items that represent ends of rules. */ |
40675e7c | 624 | |
2fa6973e AD |
625 | count = 0; |
626 | for (isp = itemset; isp < itemsetend; isp++) | |
627 | { | |
628 | item = ritem[*isp]; | |
629 | if (item < 0) | |
630 | redset[count++] = -item; | |
631 | } | |
40675e7c | 632 | |
2fa6973e AD |
633 | /* Make a reductions structure and copy the data into it. */ |
634 | ||
635 | if (count) | |
636 | { | |
637 | p = (reductions *) xmalloc ((unsigned) (sizeof (reductions) + | |
638 | (count - 1) * sizeof (short))); | |
639 | ||
640 | p->number = this_state->number; | |
641 | p->nreds = count; | |
642 | ||
643 | rp1 = redset; | |
644 | rp2 = p->rules; | |
645 | rend = rp1 + count; | |
646 | ||
647 | for (/* nothing */; rp1 < rend; ++rp1, ++rp2) | |
648 | *rp2 = *rp1; | |
649 | ||
650 | if (last_reduction) | |
651 | { | |
652 | last_reduction->next = p; | |
653 | last_reduction = p; | |
654 | } | |
655 | else | |
656 | { | |
657 | first_reduction = p; | |
658 | last_reduction = p; | |
659 | } | |
660 | } | |
661 | } | |
662 | ||
663 | \f | |
664 | /*-------------------------------------------------------------------. | |
665 | | Compute the nondeterministic finite state machine (see state.h for | | |
666 | | details) from the grammar. | | |
667 | `-------------------------------------------------------------------*/ | |
668 | ||
669 | void | |
670 | generate_states (void) | |
671 | { | |
672 | allocate_storage (); | |
673 | new_closure (nitems); | |
674 | new_states (); | |
675 | ||
676 | while (this_state) | |
677 | { | |
678 | /* Set up ruleset and itemset for the transitions out of this | |
679 | state. ruleset gets a 1 bit for each rule that could reduce | |
680 | now. itemset gets a vector of all the items that could be | |
681 | accepted next. */ | |
682 | closure (this_state->items, this_state->nitems); | |
683 | /* record the reductions allowed out of this state */ | |
684 | save_reductions (); | |
685 | /* find the itemsets of the states that shifts can reach */ | |
686 | new_itemsets (); | |
687 | /* find or create the core structures for those states */ | |
688 | append_states (); | |
689 | ||
690 | /* create the shifts structures for the shifts to those states, | |
691 | now that the state numbers transitioning to are known */ | |
692 | if (nshifts > 0) | |
693 | save_shifts (); | |
694 | ||
695 | /* states are queued when they are created; process them all */ | |
696 | this_state = this_state->next; | |
697 | } | |
698 | ||
699 | /* discard various storage */ | |
700 | free_closure (); | |
701 | free_storage (); | |
702 | ||
703 | /* set up initial and final states as parser wants them */ | |
704 | augment_automaton (); | |
40675e7c | 705 | } |