]> git.saurik.com Git - bison.git/blame - src/LR0.c
* src/LR0.c (new_state): Complete trace code.
[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 202#if TRACE
6986fd9e
AD
203 fprintf (stderr, "Entering new_state, symbol = %d, state = %d\n",
204 symbol, nstates);
40675e7c
DM
205#endif
206
2fa6973e
AD
207 if (nstates >= MAXSHORT)
208 fatal (_("too many states (max %d)"), MAXSHORT);
40675e7c 209
2fa6973e
AD
210 isp1 = kernel_base[symbol];
211 iend = kernel_end[symbol];
212 n = iend - isp1;
40675e7c 213
2fa6973e 214 p =
d7913476 215 (core *) xcalloc ((unsigned) (sizeof (core) + (n - 1) * sizeof (short)), 1);
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
d7913476 353 p = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1);
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
d7913476
AD
367 p = (shifts *) xcalloc ((unsigned) (sizeof (shifts) +
368 (nshifts - 1) * sizeof (short)), 1);
40675e7c
DM
369
370 p->number = this_state->number;
371 p->nshifts = nshifts;
372
373 sp1 = shiftset;
374 sp2 = p->shifts;
375 send = shiftset + nshifts;
376
377 while (sp1 < send)
378 *sp2++ = *sp1++;
379
380 if (last_shift)
381 {
382 last_shift->next = p;
383 last_shift = p;
384 }
385 else
386 {
387 first_shift = p;
388 last_shift = p;
389 }
390}
391
392
2fa6973e
AD
393/*------------------------------------------------------------------.
394| Subroutine of augment_automaton. Create the next-to-final state, |
395| to which a shift has already been made in the initial state. |
396`------------------------------------------------------------------*/
40675e7c 397
4a120d45 398static void
2fa6973e 399insert_start_shift (void)
40675e7c 400{
2fa6973e
AD
401 core *statep;
402 shifts *sp;
40675e7c 403
d7913476 404 statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1);
2fa6973e
AD
405 statep->number = nstates;
406 statep->accessing_symbol = start_symbol;
40675e7c 407
2fa6973e
AD
408 last_state->next = statep;
409 last_state = statep;
40675e7c 410
2fa6973e 411 /* Make a shift from this state to (what will be) the final state. */
d7913476 412 sp = XCALLOC (shifts, 1);
2fa6973e
AD
413 sp->number = nstates++;
414 sp->nshifts = 1;
415 sp->shifts[0] = nstates;
40675e7c 416
2fa6973e
AD
417 last_shift->next = sp;
418 last_shift = sp;
40675e7c
DM
419}
420
421
2fa6973e
AD
422/*------------------------------------------------------------------.
423| Make sure that the initial state has a shift that accepts the |
424| grammar's start symbol and goes to the next-to-final state, which |
425| has a shift going to the final state, which has a shift to the |
426| termination state. Create such states and shifts if they don't |
427| happen to exist already. |
428`------------------------------------------------------------------*/
40675e7c 429
4a120d45 430static void
d2729d44 431augment_automaton (void)
40675e7c 432{
2fa6973e
AD
433 int i;
434 int k;
435 core *statep;
436 shifts *sp;
437 shifts *sp2;
438 shifts *sp1 = NULL;
40675e7c
DM
439
440 sp = first_shift;
441
442 if (sp)
443 {
444 if (sp->number == 0)
445 {
446 k = sp->nshifts;
447 statep = first_state->next;
448
449 /* The states reached by shifts from first_state are numbered 1...K.
450 Look for one reached by start_symbol. */
451 while (statep->accessing_symbol < start_symbol
2fa6973e 452 && statep->number < k)
40675e7c
DM
453 statep = statep->next;
454
455 if (statep->accessing_symbol == start_symbol)
456 {
457 /* We already have a next-to-final state.
2fa6973e 458 Make sure it has a shift to what will be the final state. */
40675e7c
DM
459 k = statep->number;
460
461 while (sp && sp->number < k)
462 {
463 sp1 = sp;
464 sp = sp->next;
465 }
466
467 if (sp && sp->number == k)
468 {
d7913476 469 sp2 = (shifts *) xcalloc ((unsigned) (sizeof (shifts)
2fa6973e
AD
470 +
471 sp->nshifts *
d7913476 472 sizeof (short)), 1);
40675e7c
DM
473 sp2->number = k;
474 sp2->nshifts = sp->nshifts + 1;
475 sp2->shifts[0] = nstates;
476 for (i = sp->nshifts; i > 0; i--)
477 sp2->shifts[i] = sp->shifts[i - 1];
478
479 /* Patch sp2 into the chain of shifts in place of sp,
480 following sp1. */
481 sp2->next = sp->next;
482 sp1->next = sp2;
483 if (sp == last_shift)
484 last_shift = sp2;
d7913476 485 XFREE (sp);
40675e7c
DM
486 }
487 else
488 {
d7913476 489 sp2 = XCALLOC (shifts, 1);
40675e7c
DM
490 sp2->number = k;
491 sp2->nshifts = 1;
492 sp2->shifts[0] = nstates;
493
494 /* Patch sp2 into the chain of shifts between sp1 and sp. */
495 sp2->next = sp;
496 sp1->next = sp2;
497 if (sp == 0)
498 last_shift = sp2;
499 }
500 }
501 else
502 {
503 /* There is no next-to-final state as yet. */
504 /* Add one more shift in first_shift,
2fa6973e 505 going to the next-to-final state (yet to be made). */
40675e7c
DM
506 sp = first_shift;
507
d7913476
AD
508 sp2 = (shifts *) xcalloc (sizeof (shifts)
509 + sp->nshifts * sizeof (short), 1);
40675e7c
DM
510 sp2->nshifts = sp->nshifts + 1;
511
512 /* Stick this shift into the vector at the proper place. */
513 statep = first_state->next;
514 for (k = 0, i = 0; i < sp->nshifts; k++, i++)
515 {
516 if (statep->accessing_symbol > start_symbol && i == k)
517 sp2->shifts[k++] = nstates;
518 sp2->shifts[k] = sp->shifts[i];
519 statep = statep->next;
520 }
521 if (i == k)
522 sp2->shifts[k++] = nstates;
523
524 /* Patch sp2 into the chain of shifts
2fa6973e 525 in place of sp, at the beginning. */
40675e7c
DM
526 sp2->next = sp->next;
527 first_shift = sp2;
528 if (last_shift == sp)
529 last_shift = sp2;
530
d7913476 531 XFREE (sp);
40675e7c
DM
532
533 /* Create the next-to-final state, with shift to
2fa6973e
AD
534 what will be the final state. */
535 insert_start_shift ();
40675e7c
DM
536 }
537 }
538 else
539 {
540 /* The initial state didn't even have any shifts.
541 Give it one shift, to the next-to-final state. */
d7913476 542 sp = XCALLOC (shifts, 1);
40675e7c
DM
543 sp->nshifts = 1;
544 sp->shifts[0] = nstates;
545
546 /* Patch sp into the chain of shifts at the beginning. */
547 sp->next = first_shift;
548 first_shift = sp;
549
550 /* Create the next-to-final state, with shift to
551 what will be the final state. */
2fa6973e 552 insert_start_shift ();
40675e7c
DM
553 }
554 }
555 else
556 {
557 /* There are no shifts for any state.
2fa6973e 558 Make one shift, from the initial state to the next-to-final state. */
40675e7c 559
d7913476 560 sp = XCALLOC (shifts, 1);
40675e7c
DM
561 sp->nshifts = 1;
562 sp->shifts[0] = nstates;
563
564 /* Initialize the chain of shifts with sp. */
565 first_shift = sp;
566 last_shift = sp;
567
568 /* Create the next-to-final state, with shift to
2fa6973e
AD
569 what will be the final state. */
570 insert_start_shift ();
40675e7c
DM
571 }
572
573 /* Make the final state--the one that follows a shift from the
574 next-to-final state.
575 The symbol for that shift is 0 (end-of-file). */
d7913476 576 statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1);
40675e7c
DM
577 statep->number = nstates;
578 last_state->next = statep;
579 last_state = statep;
580
581 /* Make the shift from the final state to the termination state. */
d7913476 582 sp = XCALLOC (shifts, 1);
40675e7c
DM
583 sp->number = nstates++;
584 sp->nshifts = 1;
585 sp->shifts[0] = nstates;
586 last_shift->next = sp;
587 last_shift = sp;
588
589 /* Note that the variable `final_state' refers to what we sometimes call
590 the termination state. */
591 final_state = nstates;
592
593 /* Make the termination state. */
d7913476 594 statep = (core *) xcalloc ((unsigned) (sizeof (core) - sizeof (short)), 1);
40675e7c
DM
595 statep->number = nstates++;
596 last_state->next = statep;
597 last_state = statep;
598}
599
600
2fa6973e
AD
601/*----------------------------------------------------------------.
602| Find which rules can be used for reduction transitions from the |
603| current state and make a reductions structure for the state to |
604| record their rule numbers. |
605`----------------------------------------------------------------*/
606
4a120d45 607static void
2fa6973e 608save_reductions (void)
40675e7c 609{
2fa6973e
AD
610 short *isp;
611 short *rp1;
612 short *rp2;
613 int item;
614 int count;
615 reductions *p;
40675e7c 616
2fa6973e 617 short *rend;
40675e7c 618
2fa6973e 619 /* Find and count the active items that represent ends of rules. */
40675e7c 620
2fa6973e
AD
621 count = 0;
622 for (isp = itemset; isp < itemsetend; isp++)
623 {
624 item = ritem[*isp];
625 if (item < 0)
626 redset[count++] = -item;
627 }
40675e7c 628
2fa6973e
AD
629 /* Make a reductions structure and copy the data into it. */
630
631 if (count)
632 {
d7913476
AD
633 p = (reductions *) xcalloc ((unsigned) (sizeof (reductions) +
634 (count - 1) * sizeof (short)), 1);
2fa6973e
AD
635
636 p->number = this_state->number;
637 p->nreds = count;
638
639 rp1 = redset;
640 rp2 = p->rules;
641 rend = rp1 + count;
642
643 for (/* nothing */; rp1 < rend; ++rp1, ++rp2)
644 *rp2 = *rp1;
645
646 if (last_reduction)
647 {
648 last_reduction->next = p;
649 last_reduction = p;
650 }
651 else
652 {
653 first_reduction = p;
654 last_reduction = p;
655 }
656 }
657}
658
659\f
660/*-------------------------------------------------------------------.
661| Compute the nondeterministic finite state machine (see state.h for |
662| details) from the grammar. |
663`-------------------------------------------------------------------*/
664
665void
666generate_states (void)
667{
668 allocate_storage ();
669 new_closure (nitems);
670 new_states ();
671
672 while (this_state)
673 {
674 /* Set up ruleset and itemset for the transitions out of this
675 state. ruleset gets a 1 bit for each rule that could reduce
676 now. itemset gets a vector of all the items that could be
677 accepted next. */
678 closure (this_state->items, this_state->nitems);
679 /* record the reductions allowed out of this state */
680 save_reductions ();
681 /* find the itemsets of the states that shifts can reach */
682 new_itemsets ();
683 /* find or create the core structures for those states */
684 append_states ();
685
686 /* create the shifts structures for the shifts to those states,
687 now that the state numbers transitioning to are known */
688 if (nshifts > 0)
689 save_shifts ();
690
691 /* states are queued when they are created; process them all */
692 this_state = this_state->next;
693 }
694
695 /* discard various storage */
696 free_closure ();
697 free_storage ();
698
699 /* set up initial and final states as parser wants them */
700 augment_automaton ();
40675e7c 701}