]> git.saurik.com Git - bison.git/blame - src/LR0.c
* src/LR0.h (first_shift, first_reduction): Remove.
[bison.git] / src / LR0.c
CommitLineData
40675e7c 1/* Generate the nondeterministic finite state machine for bison,
97db7bd4 2 Copyright 1984, 1986, 1989, 2000, 2001 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"
9bfe901c 26#include "getargs.h"
c87d4863 27#include "reader.h"
40675e7c
DM
28#include "gram.h"
29#include "state.h"
a0f6b076 30#include "complain.h"
2fa6973e 31#include "closure.h"
403b315b 32#include "LR0.h"
49701457 33#include "lalr.h"
630e182b 34#include "reduce.h"
40675e7c 35
40675e7c
DM
36int nstates;
37int final_state;
f693ad14 38state_t *first_state = NULL;
342b8b6e 39shifts *first_shift = NULL;
40675e7c 40
f693ad14
AD
41static state_t *this_state = NULL;
42static state_t *last_state = NULL;
342b8b6e 43static shifts *last_shift = NULL;
40675e7c
DM
44
45static int nshifts;
342b8b6e 46static short *shift_symbol = NULL;
40675e7c 47
342b8b6e
AD
48static short *redset = NULL;
49static short *shiftset = NULL;
40675e7c 50
342b8b6e 51static short **kernel_base = NULL;
6255b435 52static int *kernel_size = NULL;
342b8b6e 53static short *kernel_items = NULL;
40675e7c
DM
54
55/* hash table for states, to recognize equivalent ones. */
56
f693ad14
AD
57#define STATE_HASH_SIZE 1009
58static state_t **state_hash = NULL;
40675e7c 59
2fa6973e 60\f
4a120d45 61static void
d2729d44 62allocate_itemsets (void)
40675e7c 63{
2fa6973e 64 int i;
40675e7c 65
630e182b
AD
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);
40675e7c 72
c87d4863
AD
73 for (i = 0; ritem[i]; ++i)
74 if (ritem[i] > 0)
75 {
76 count++;
77 symbol_count[ritem[i]]++;
78 }
40675e7c 79
2fa6973e
AD
80 /* See comments before new_itemsets. All the vectors of items
81 live inside KERNEL_ITEMS. The number of active items after
40675e7c
DM
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
d7913476 86 kernel_base = XCALLOC (short *, nsyms);
342b8b6e
AD
87 if (count)
88 kernel_items = XCALLOC (short, count);
40675e7c
DM
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
630e182b 97 free (symbol_count);
0e41b407 98 kernel_size = XCALLOC (int, nsyms);
40675e7c
DM
99}
100
101
4a120d45 102static void
d2729d44 103allocate_storage (void)
40675e7c 104{
2fa6973e 105 allocate_itemsets ();
40675e7c 106
d7913476
AD
107 shiftset = XCALLOC (short, nsyms);
108 redset = XCALLOC (short, nrules + 1);
f693ad14 109 state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
40675e7c
DM
110}
111
112
4a120d45 113static void
d2729d44 114free_storage (void)
40675e7c 115{
630e182b
AD
116 free (shift_symbol);
117 free (redset);
118 free (shiftset);
119 free (kernel_base);
120 free (kernel_size);
d7913476 121 XFREE (kernel_items);
f693ad14 122 free (state_hash);
40675e7c
DM
123}
124
125
126
40675e7c 127
2fa6973e
AD
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 |
125ecb56 136| shifted, and kernel_size[symbol] is their numbers. |
2fa6973e 137`----------------------------------------------------------------*/
40675e7c 138
4a120d45 139static void
d2729d44 140new_itemsets (void)
40675e7c 141{
2fa6973e 142 int i;
2fa6973e 143
9bfe901c 144 if (trace_flag)
c87d4863
AD
145 fprintf (stderr, "Entering new_itemsets, state = %d\n",
146 this_state->number);
40675e7c
DM
147
148 for (i = 0; i < nsyms; i++)
125ecb56 149 kernel_size[i] = 0;
40675e7c 150
630e182b 151 shift_symbol = XCALLOC (short, nsyms);
b2872512 152 nshifts = 0;
40675e7c 153
b2872512 154 for (i = 0; i < nitemset; ++i)
40675e7c 155 {
97db7bd4 156 int symbol = ritem[itemset[i]];
40675e7c
DM
157 if (symbol > 0)
158 {
125ecb56 159 if (!kernel_size[symbol])
40675e7c 160 {
b2872512
AD
161 shift_symbol[nshifts] = symbol;
162 nshifts++;
40675e7c
DM
163 }
164
125ecb56
AD
165 kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
166 kernel_size[symbol]++;
40675e7c
DM
167 }
168 }
40675e7c
DM
169}
170
171
172
2fa6973e
AD
173/*-----------------------------------------------------------------.
174| Subroutine of get_state. Create a new state for those items, if |
175| necessary. |
176`-----------------------------------------------------------------*/
40675e7c 177
f693ad14 178static state_t *
2fa6973e 179new_state (int symbol)
40675e7c 180{
f693ad14 181 state_t *p;
40675e7c 182
9bfe901c 183 if (trace_flag)
c87d4863
AD
184 fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
185 this_state->number, symbol, tags[symbol]);
40675e7c 186
2fa6973e
AD
187 if (nstates >= MAXSHORT)
188 fatal (_("too many states (max %d)"), MAXSHORT);
40675e7c 189
f693ad14 190 p = STATE_ALLOC (kernel_size[symbol]);
2fa6973e
AD
191 p->accessing_symbol = symbol;
192 p->number = nstates;
125ecb56 193 p->nitems = kernel_size[symbol];
2fa6973e 194
125ecb56 195 shortcpy (p->items, kernel_base[symbol], kernel_size[symbol]);
2fa6973e
AD
196
197 last_state->next = p;
198 last_state = p;
2fa6973e 199 nstates++;
40675e7c 200
2fa6973e
AD
201 return p;
202}
40675e7c 203
2fa6973e
AD
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 |
97db7bd4 208| equivalent one exists already. Used by append_states. |
2fa6973e 209`--------------------------------------------------------------*/
40675e7c 210
4a120d45 211static int
d2729d44 212get_state (int symbol)
40675e7c 213{
2fa6973e 214 int key;
97db7bd4 215 int i;
f693ad14 216 state_t *sp;
40675e7c 217
9bfe901c 218 if (trace_flag)
c87d4863
AD
219 fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n",
220 this_state->number, symbol, tags[symbol]);
40675e7c 221
97db7bd4
AD
222 /* Add up the target state's active item numbers to get a hash key.
223 */
40675e7c 224 key = 0;
125ecb56 225 for (i = 0; i < kernel_size[symbol]; ++i)
97db7bd4 226 key += kernel_base[symbol][i];
f693ad14
AD
227 key = key % STATE_HASH_SIZE;
228 sp = state_hash[key];
40675e7c
DM
229
230 if (sp)
231 {
97db7bd4 232 int found = 0;
40675e7c
DM
233 while (!found)
234 {
125ecb56 235 if (sp->nitems == kernel_size[symbol])
40675e7c
DM
236 {
237 found = 1;
125ecb56 238 for (i = 0; i < kernel_size[symbol]; ++i)
97db7bd4
AD
239 if (kernel_base[symbol][i] != sp->items[i])
240 found = 0;
40675e7c
DM
241 }
242
243 if (!found)
244 {
245 if (sp->link)
246 {
247 sp = sp->link;
248 }
2fa6973e 249 else /* bucket exhausted and no match */
40675e7c 250 {
2fa6973e 251 sp = sp->link = new_state (symbol);
40675e7c
DM
252 found = 1;
253 }
254 }
255 }
256 }
2fa6973e 257 else /* bucket is empty */
40675e7c 258 {
f693ad14 259 state_hash[key] = sp = new_state (symbol);
40675e7c
DM
260 }
261
c87d4863
AD
262 if (trace_flag)
263 fprintf (stderr, "Exiting get_state => %d\n", sp->number);
264
36281465 265 return sp->number;
40675e7c
DM
266}
267
2fa6973e
AD
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`------------------------------------------------------------------*/
40675e7c 274
2fa6973e
AD
275static void
276append_states (void)
40675e7c 277{
2fa6973e
AD
278 int i;
279 int j;
280 int symbol;
40675e7c 281
9bfe901c 282 if (trace_flag)
c87d4863
AD
283 fprintf (stderr, "Entering append_states, state = %d\n",
284 this_state->number);
40675e7c 285
2fa6973e 286 /* first sort shift_symbol into increasing order */
40675e7c 287
2fa6973e
AD
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 }
40675e7c 299
2fa6973e 300 for (i = 0; i < nshifts; i++)
97db7bd4 301 shiftset[i] = get_state (shift_symbol[i]);
40675e7c
DM
302}
303
304
4a120d45 305static void
2fa6973e 306new_states (void)
40675e7c 307{
f693ad14 308 first_state = last_state = this_state = STATE_ALLOC (0);
40675e7c
DM
309 nstates = 1;
310}
311
312
4a38e613
AD
313/*------------------------------------------------------------.
314| Save the NSHIFTS of SHIFTSET into the current linked list. |
315`------------------------------------------------------------*/
316
4a120d45 317static void
d2729d44 318save_shifts (void)
40675e7c 319{
4a38e613 320 shifts *p = shifts_new (nshifts);
40675e7c
DM
321
322 p->number = this_state->number;
300f275f 323 shortcpy (p->shifts, shiftset, nshifts);
80e25d4d 324 this_state->shifts = p;
40675e7c
DM
325
326 if (last_shift)
97db7bd4 327 last_shift->next = p;
40675e7c 328 else
97db7bd4
AD
329 first_shift = p;
330 last_shift = p;
40675e7c
DM
331}
332
333
2fa6973e
AD
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. |
0ab3728b
AD
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. |
2fa6973e 341`------------------------------------------------------------------*/
40675e7c 342
4a120d45 343static void
0ab3728b 344insert_start_shifting_state (void)
40675e7c 345{
f693ad14 346 state_t *statep;
2fa6973e 347 shifts *sp;
40675e7c 348
f693ad14 349 statep = STATE_ALLOC (0);
2fa6973e 350 statep->number = nstates;
0ab3728b
AD
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 */
2fa6973e 357 statep->accessing_symbol = start_symbol;
40675e7c 358
2fa6973e
AD
359 last_state->next = statep;
360 last_state = statep;
40675e7c 361
2fa6973e 362 /* Make a shift from this state to (what will be) the final state. */
4a38e613 363 sp = shifts_new (1);
80e25d4d 364 statep->shifts = sp;
2fa6973e 365 sp->number = nstates++;
2fa6973e 366 sp->shifts[0] = nstates;
40675e7c 367
2fa6973e
AD
368 last_shift->next = sp;
369 last_shift = sp;
40675e7c
DM
370}
371
372
0ab3728b
AD
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
379static void
380insert_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);
80e25d4d 396 statep->shifts = sp;
0ab3728b
AD
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
409static void
410insert_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
2fa6973e
AD
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`------------------------------------------------------------------*/
40675e7c 436
4a120d45 437static void
d2729d44 438augment_automaton (void)
40675e7c 439{
0ab3728b 440 if (!first_shift->nshifts)
40675e7c 441 {
b178c8cc
AD
442 /* There are no shifts for any state. Make one shift, from the
443 initial state to the next-to-final state. */
40675e7c 444
0ab3728b 445 shifts *sp = shifts_new (1);
80e25d4d 446 first_state->shifts = sp;
b178c8cc 447 sp->shifts[0] = nstates;
40675e7c 448
b178c8cc
AD
449 /* Initialize the chain of shifts with sp. */
450 first_shift = sp;
451 last_shift = sp;
40675e7c 452
b178c8cc
AD
453 /* Create the next-to-final state, with shift to
454 what will be the final state. */
0ab3728b 455 insert_start_shifting_state ();
b178c8cc 456 }
0ab3728b 457 else if (first_shift->number == 0)
b178c8cc 458 {
0ab3728b
AD
459 state_t *statep = first_state->next;
460 shifts *sp = first_shift;
461 shifts *sp1 = NULL;
b178c8cc
AD
462 /* The states reached by shifts from FIRST_STATE are numbered
463 1..(SP->NSHIFTS). Look for one reached by START_SYMBOL. */
0ab3728b 464 while (statep->accessing_symbol != start_symbol
b178c8cc
AD
465 && statep->number < sp->nshifts)
466 statep = statep->next;
40675e7c 467
b178c8cc
AD
468 if (statep->accessing_symbol == start_symbol)
469 {
470 /* We already have a next-to-final state.
471 Make sure it has a shift to what will be the final state. */
472 while (sp && sp->number < statep->number)
473 {
474 sp1 = sp;
475 sp = sp->next;
476 }
40675e7c 477
b178c8cc
AD
478 if (sp && sp->number == statep->number)
479 {
480 int i;
481 shifts *sp2 = shifts_new (sp->nshifts + 1);
482 sp2->number = statep->number;
80e25d4d 483 statep->shifts = sp2;
b178c8cc
AD
484 sp2->shifts[0] = nstates;
485 for (i = sp->nshifts; i > 0; i--)
486 sp2->shifts[i] = sp->shifts[i - 1];
487
488 /* Patch sp2 into the chain of shifts in place of sp,
489 following sp1. */
40675e7c 490 sp2->next = sp->next;
b178c8cc
AD
491 sp1->next = sp2;
492 if (sp == last_shift)
40675e7c 493 last_shift = sp2;
d7913476 494 XFREE (sp);
b178c8cc
AD
495 }
496 else
497 {
498 shifts *sp2 = shifts_new (1);
499 sp2->number = statep->number;
80e25d4d 500 statep->shifts = sp2;
b178c8cc
AD
501 sp2->shifts[0] = nstates;
502
503 /* Patch sp2 into the chain of shifts between sp1 and sp. */
504 sp2->next = sp;
505 sp1->next = sp2;
506 if (sp == 0)
507 last_shift = sp2;
40675e7c
DM
508 }
509 }
510 else
511 {
b178c8cc
AD
512 int i, k;
513 shifts *sp2;
0ab3728b 514 sp = first_shift;
40675e7c 515
b178c8cc
AD
516 /* There is no next-to-final state as yet. */
517 /* Add one more shift in first_shift,
518 going to the next-to-final state (yet to be made). */
b178c8cc 519 sp2 = shifts_new (sp->nshifts + 1);
80e25d4d 520 first_state->shifts = sp2;
b178c8cc
AD
521 /* Stick this shift into the vector at the proper place. */
522 statep = first_state->next;
523 for (k = 0, i = 0; i < sp->nshifts; k++, i++)
524 {
525 if (statep->accessing_symbol > start_symbol && i == k)
526 sp2->shifts[k++] = nstates;
527 sp2->shifts[k] = sp->shifts[i];
528 statep = statep->next;
529 }
530 if (i == k)
531 sp2->shifts[k++] = nstates;
532
533 /* Patch sp2 into the chain of shifts
534 in place of sp, at the beginning. */
535 sp2->next = sp->next;
536 first_shift = sp2;
537 if (last_shift == sp)
538 last_shift = sp2;
539
540 XFREE (sp);
40675e7c
DM
541
542 /* Create the next-to-final state, with shift to
543 what will be the final state. */
0ab3728b 544 insert_start_shifting_state ();
40675e7c
DM
545 }
546 }
547 else
548 {
b178c8cc
AD
549 /* The initial state didn't even have any shifts.
550 Give it one shift, to the next-to-final state. */
0ab3728b 551 shifts *sp = shifts_new (1);
80e25d4d 552 first_state->shifts = sp;
40675e7c
DM
553 sp->shifts[0] = nstates;
554
b178c8cc
AD
555 /* Patch sp into the chain of shifts at the beginning. */
556 sp->next = first_shift;
40675e7c 557 first_shift = sp;
40675e7c
DM
558
559 /* Create the next-to-final state, with shift to
b178c8cc 560 what will be the final state. */
0ab3728b 561 insert_start_shifting_state ();
40675e7c
DM
562 }
563
0ab3728b
AD
564 insert_eof_shifting_state ();
565 insert_accepting_state ();
40675e7c
DM
566}
567
568
2fa6973e
AD
569/*----------------------------------------------------------------.
570| Find which rules can be used for reduction transitions from the |
571| current state and make a reductions structure for the state to |
572| record their rule numbers. |
573`----------------------------------------------------------------*/
574
4a120d45 575static void
2fa6973e 576save_reductions (void)
40675e7c 577{
2fa6973e 578 int count;
fb908786 579 int i;
40675e7c 580
2fa6973e 581 /* Find and count the active items that represent ends of rules. */
40675e7c 582
2fa6973e 583 count = 0;
b2872512 584 for (i = 0; i < nitemset; ++i)
2fa6973e 585 {
fb908786 586 int item = ritem[itemset[i]];
2fa6973e
AD
587 if (item < 0)
588 redset[count++] = -item;
589 }
40675e7c 590
2fa6973e
AD
591 /* Make a reductions structure and copy the data into it. */
592
593 if (count)
594 {
fb908786 595 reductions *p = REDUCTIONS_ALLOC (count);
2fa6973e 596 p->nreds = count;
300f275f 597 shortcpy (p->rules, redset, count);
2fa6973e 598
49701457 599 this_state->reductions = p;
2fa6973e
AD
600 }
601}
602
603\f
604/*-------------------------------------------------------------------.
605| Compute the nondeterministic finite state machine (see state.h for |
606| details) from the grammar. |
607`-------------------------------------------------------------------*/
608
609void
610generate_states (void)
611{
612 allocate_storage ();
613 new_closure (nitems);
614 new_states ();
615
616 while (this_state)
617 {
23cbcc6c
AD
618 if (trace_flag)
619 fprintf (stderr, "Processing state %d (reached by %s)\n",
620 this_state->number, tags[this_state->accessing_symbol]);
2fa6973e
AD
621 /* Set up ruleset and itemset for the transitions out of this
622 state. ruleset gets a 1 bit for each rule that could reduce
623 now. itemset gets a vector of all the items that could be
624 accepted next. */
625 closure (this_state->items, this_state->nitems);
626 /* record the reductions allowed out of this state */
627 save_reductions ();
628 /* find the itemsets of the states that shifts can reach */
629 new_itemsets ();
630 /* find or create the core structures for those states */
631 append_states ();
632
633 /* create the shifts structures for the shifts to those states,
634 now that the state numbers transitioning to are known */
d954473d 635 save_shifts ();
2fa6973e
AD
636
637 /* states are queued when they are created; process them all */
638 this_state = this_state->next;
639 }
640
641 /* discard various storage */
642 free_closure ();
643 free_storage ();
644
645 /* set up initial and final states as parser wants them */
646 augment_automaton ();
40675e7c 647}