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