]> git.saurik.com Git - bison.git/blame - src/LR0.c
* src/state.h (CORE_ALLOC, SHIFTS_ALLOC, ERRS_ALLOC)
[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
f59c437a
AD
154 fprintf (stderr, "Entering new_itemsets, state = %d\n",
155 this_state->number);
40675e7c
DM
156#endif
157
158 for (i = 0; i < nsyms; i++)
159 kernel_end[i] = NULL;
160
161 shiftcount = 0;
162
163 isp = itemset;
164
165 while (isp < itemsetend)
166 {
167 i = *isp++;
168 symbol = ritem[i];
169 if (symbol > 0)
170 {
2fa6973e 171 ksp = kernel_end[symbol];
40675e7c 172
2fa6973e 173 if (!ksp)
40675e7c
DM
174 {
175 shift_symbol[shiftcount++] = symbol;
176 ksp = kernel_base[symbol];
177 }
178
2fa6973e
AD
179 *ksp++ = i + 1;
180 kernel_end[symbol] = ksp;
40675e7c
DM
181 }
182 }
183
184 nshifts = shiftcount;
185}
186
187
188
2fa6973e
AD
189/*-----------------------------------------------------------------.
190| Subroutine of get_state. Create a new state for those items, if |
191| necessary. |
192`-----------------------------------------------------------------*/
40675e7c 193
2fa6973e
AD
194static core *
195new_state (int symbol)
40675e7c 196{
2fa6973e
AD
197 int n;
198 core *p;
199 short *isp1;
200 short *isp2;
201 short *iend;
40675e7c 202
2fa6973e 203#if TRACE
6986fd9e
AD
204 fprintf (stderr, "Entering new_state, symbol = %d, state = %d\n",
205 symbol, nstates);
40675e7c
DM
206#endif
207
2fa6973e
AD
208 if (nstates >= MAXSHORT)
209 fatal (_("too many states (max %d)"), MAXSHORT);
40675e7c 210
2fa6973e
AD
211 isp1 = kernel_base[symbol];
212 iend = kernel_end[symbol];
213 n = iend - isp1;
40675e7c 214
f59c437a 215 p = CORE_ALLOC (n);
2fa6973e
AD
216 p->accessing_symbol = symbol;
217 p->number = nstates;
218 p->nitems = n;
219
220 isp2 = p->items;
221 while (isp1 < iend)
222 *isp2++ = *isp1++;
223
224 last_state->next = p;
225 last_state = p;
40675e7c 226
2fa6973e 227 nstates++;
40675e7c 228
2fa6973e
AD
229 return p;
230}
40675e7c 231
2fa6973e
AD
232
233/*--------------------------------------------------------------.
234| Find the state number for the state we would get to (from the |
235| current state) by shifting symbol. Create a new state if no |
236| equivalent one exists already. Used by append_states. |
237`--------------------------------------------------------------*/
40675e7c 238
4a120d45 239static int
d2729d44 240get_state (int symbol)
40675e7c 241{
2fa6973e
AD
242 int key;
243 short *isp1;
244 short *isp2;
245 short *iend;
246 core *sp;
247 int found;
40675e7c
DM
248
249 int n;
250
2fa6973e
AD
251#if TRACE
252 fprintf (stderr, "Entering get_state, symbol = %d\n", symbol);
40675e7c
DM
253#endif
254
255 isp1 = kernel_base[symbol];
256 iend = kernel_end[symbol];
257 n = iend - isp1;
258
259 /* add up the target state's active item numbers to get a hash key */
260 key = 0;
261 while (isp1 < iend)
262 key += *isp1++;
263
264 key = key % STATE_TABLE_SIZE;
265
266 sp = state_table[key];
267
268 if (sp)
269 {
270 found = 0;
271 while (!found)
272 {
273 if (sp->nitems == n)
274 {
275 found = 1;
276 isp1 = kernel_base[symbol];
277 isp2 = sp->items;
278
279 while (found && isp1 < iend)
280 {
281 if (*isp1++ != *isp2++)
282 found = 0;
283 }
284 }
285
286 if (!found)
287 {
288 if (sp->link)
289 {
290 sp = sp->link;
291 }
2fa6973e 292 else /* bucket exhausted and no match */
40675e7c 293 {
2fa6973e 294 sp = sp->link = new_state (symbol);
40675e7c
DM
295 found = 1;
296 }
297 }
298 }
299 }
2fa6973e 300 else /* bucket is empty */
40675e7c 301 {
2fa6973e 302 state_table[key] = sp = new_state (symbol);
40675e7c
DM
303 }
304
36281465 305 return sp->number;
40675e7c
DM
306}
307
2fa6973e
AD
308/*------------------------------------------------------------------.
309| Use the information computed by new_itemsets to find the state |
310| numbers reached by each shift transition from the current state. |
311| |
312| shiftset is set up as a vector of state numbers of those states. |
313`------------------------------------------------------------------*/
40675e7c 314
2fa6973e
AD
315static void
316append_states (void)
40675e7c 317{
2fa6973e
AD
318 int i;
319 int j;
320 int symbol;
40675e7c 321
2fa6973e
AD
322#if TRACE
323 fprintf (stderr, "Entering append_states\n");
324#endif
40675e7c 325
2fa6973e 326 /* first sort shift_symbol into increasing order */
40675e7c 327
2fa6973e
AD
328 for (i = 1; i < nshifts; i++)
329 {
330 symbol = shift_symbol[i];
331 j = i;
332 while (j > 0 && shift_symbol[j - 1] > symbol)
333 {
334 shift_symbol[j] = shift_symbol[j - 1];
335 j--;
336 }
337 shift_symbol[j] = symbol;
338 }
40675e7c 339
2fa6973e
AD
340 for (i = 0; i < nshifts; i++)
341 {
342 symbol = shift_symbol[i];
343 shiftset[i] = get_state (symbol);
344 }
40675e7c
DM
345}
346
347
4a120d45 348static void
2fa6973e 349new_states (void)
40675e7c 350{
2fa6973e 351 core *p;
40675e7c 352
f59c437a 353 p = CORE_ALLOC (0);
40675e7c
DM
354 first_state = last_state = this_state = p;
355 nstates = 1;
356}
357
358
4a120d45 359static void
d2729d44 360save_shifts (void)
40675e7c 361{
2fa6973e
AD
362 shifts *p;
363 short *sp1;
364 short *sp2;
365 short *send;
40675e7c 366
f59c437a 367 p = SHIFTS_ALLOC (nshifts);
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
f59c437a 403 statep = CORE_ALLOC (0);
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. */
f59c437a 411 sp = SHIFTS_ALLOC (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 {
f59c437a 468 sp2 = SHIFTS_ALLOC (sp->nshifts + 1);
40675e7c
DM
469 sp2->number = k;
470 sp2->nshifts = sp->nshifts + 1;
471 sp2->shifts[0] = nstates;
472 for (i = sp->nshifts; i > 0; i--)
473 sp2->shifts[i] = sp->shifts[i - 1];
474
475 /* Patch sp2 into the chain of shifts in place of sp,
476 following sp1. */
477 sp2->next = sp->next;
478 sp1->next = sp2;
479 if (sp == last_shift)
480 last_shift = sp2;
d7913476 481 XFREE (sp);
40675e7c
DM
482 }
483 else
484 {
f59c437a 485 sp2 = SHIFTS_ALLOC (1);
40675e7c
DM
486 sp2->number = k;
487 sp2->nshifts = 1;
488 sp2->shifts[0] = nstates;
489
490 /* Patch sp2 into the chain of shifts between sp1 and sp. */
491 sp2->next = sp;
492 sp1->next = sp2;
493 if (sp == 0)
494 last_shift = sp2;
495 }
496 }
497 else
498 {
499 /* There is no next-to-final state as yet. */
500 /* Add one more shift in first_shift,
2fa6973e 501 going to the next-to-final state (yet to be made). */
40675e7c
DM
502 sp = first_shift;
503
f59c437a 504 sp2 = SHIFTS_ALLOC (sp->nshifts + 1);
40675e7c
DM
505 sp2->nshifts = sp->nshifts + 1;
506
507 /* Stick this shift into the vector at the proper place. */
508 statep = first_state->next;
509 for (k = 0, i = 0; i < sp->nshifts; k++, i++)
510 {
511 if (statep->accessing_symbol > start_symbol && i == k)
512 sp2->shifts[k++] = nstates;
513 sp2->shifts[k] = sp->shifts[i];
514 statep = statep->next;
515 }
516 if (i == k)
517 sp2->shifts[k++] = nstates;
518
519 /* Patch sp2 into the chain of shifts
2fa6973e 520 in place of sp, at the beginning. */
40675e7c
DM
521 sp2->next = sp->next;
522 first_shift = sp2;
523 if (last_shift == sp)
524 last_shift = sp2;
525
d7913476 526 XFREE (sp);
40675e7c
DM
527
528 /* Create the next-to-final state, with shift to
2fa6973e
AD
529 what will be the final state. */
530 insert_start_shift ();
40675e7c
DM
531 }
532 }
533 else
534 {
535 /* The initial state didn't even have any shifts.
536 Give it one shift, to the next-to-final state. */
f59c437a 537 sp = SHIFTS_ALLOC (1);
40675e7c
DM
538 sp->nshifts = 1;
539 sp->shifts[0] = nstates;
540
541 /* Patch sp into the chain of shifts at the beginning. */
542 sp->next = first_shift;
543 first_shift = sp;
544
545 /* Create the next-to-final state, with shift to
546 what will be the final state. */
2fa6973e 547 insert_start_shift ();
40675e7c
DM
548 }
549 }
550 else
551 {
552 /* There are no shifts for any state.
2fa6973e 553 Make one shift, from the initial state to the next-to-final state. */
40675e7c 554
f59c437a 555 sp = SHIFTS_ALLOC (1);
40675e7c
DM
556 sp->nshifts = 1;
557 sp->shifts[0] = nstates;
558
559 /* Initialize the chain of shifts with sp. */
560 first_shift = sp;
561 last_shift = sp;
562
563 /* Create the next-to-final state, with shift to
2fa6973e
AD
564 what will be the final state. */
565 insert_start_shift ();
40675e7c
DM
566 }
567
568 /* Make the final state--the one that follows a shift from the
569 next-to-final state.
570 The symbol for that shift is 0 (end-of-file). */
f59c437a 571 statep = CORE_ALLOC (0);
40675e7c
DM
572 statep->number = nstates;
573 last_state->next = statep;
574 last_state = statep;
575
576 /* Make the shift from the final state to the termination state. */
f59c437a 577 sp = SHIFTS_ALLOC (1);
40675e7c
DM
578 sp->number = nstates++;
579 sp->nshifts = 1;
580 sp->shifts[0] = nstates;
581 last_shift->next = sp;
582 last_shift = sp;
583
584 /* Note that the variable `final_state' refers to what we sometimes call
585 the termination state. */
586 final_state = nstates;
587
588 /* Make the termination state. */
f59c437a 589 statep = CORE_ALLOC (0);
40675e7c
DM
590 statep->number = nstates++;
591 last_state->next = statep;
592 last_state = statep;
593}
594
595
2fa6973e
AD
596/*----------------------------------------------------------------.
597| Find which rules can be used for reduction transitions from the |
598| current state and make a reductions structure for the state to |
599| record their rule numbers. |
600`----------------------------------------------------------------*/
601
4a120d45 602static void
2fa6973e 603save_reductions (void)
40675e7c 604{
2fa6973e
AD
605 short *isp;
606 short *rp1;
607 short *rp2;
608 int item;
609 int count;
610 reductions *p;
40675e7c 611
2fa6973e 612 short *rend;
40675e7c 613
2fa6973e 614 /* Find and count the active items that represent ends of rules. */
40675e7c 615
2fa6973e
AD
616 count = 0;
617 for (isp = itemset; isp < itemsetend; isp++)
618 {
619 item = ritem[*isp];
620 if (item < 0)
621 redset[count++] = -item;
622 }
40675e7c 623
2fa6973e
AD
624 /* Make a reductions structure and copy the data into it. */
625
626 if (count)
627 {
f59c437a 628 p = REDUCTIONS_ALLOC (count);
2fa6973e
AD
629
630 p->number = this_state->number;
631 p->nreds = count;
632
633 rp1 = redset;
634 rp2 = p->rules;
635 rend = rp1 + count;
636
637 for (/* nothing */; rp1 < rend; ++rp1, ++rp2)
638 *rp2 = *rp1;
639
640 if (last_reduction)
641 {
642 last_reduction->next = p;
643 last_reduction = p;
644 }
645 else
646 {
647 first_reduction = p;
648 last_reduction = p;
649 }
650 }
651}
652
653\f
654/*-------------------------------------------------------------------.
655| Compute the nondeterministic finite state machine (see state.h for |
656| details) from the grammar. |
657`-------------------------------------------------------------------*/
658
659void
660generate_states (void)
661{
662 allocate_storage ();
663 new_closure (nitems);
664 new_states ();
665
666 while (this_state)
667 {
668 /* Set up ruleset and itemset for the transitions out of this
669 state. ruleset gets a 1 bit for each rule that could reduce
670 now. itemset gets a vector of all the items that could be
671 accepted next. */
672 closure (this_state->items, this_state->nitems);
673 /* record the reductions allowed out of this state */
674 save_reductions ();
675 /* find the itemsets of the states that shifts can reach */
676 new_itemsets ();
677 /* find or create the core structures for those states */
678 append_states ();
679
680 /* create the shifts structures for the shifts to those states,
681 now that the state numbers transitioning to are known */
682 if (nshifts > 0)
683 save_shifts ();
684
685 /* states are queued when they are created; process them all */
686 this_state = this_state->next;
687 }
688
689 /* discard various storage */
690 free_closure ();
691 free_storage ();
692
693 /* set up initial and final states as parser wants them */
694 augment_automaton ();
40675e7c 695}