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