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