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