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