]>
git.saurik.com Git - bison.git/blob - src/LR0.c
7770ecbc32b16ebc624787bcbd727c3a10cd0b4a
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
22 /* See comments in state.h for the data structures that represent it.
23 The entry point is generate_states. */
38 state_t
*first_state
= NULL
;
39 shifts
*first_shift
= NULL
;
41 static state_t
*this_state
= NULL
;
42 static state_t
*last_state
= NULL
;
43 static shifts
*last_shift
= NULL
;
46 static short *shift_symbol
= NULL
;
48 static short *redset
= NULL
;
49 static short *shiftset
= NULL
;
51 static short **kernel_base
= NULL
;
52 static int *kernel_size
= NULL
;
53 static short *kernel_items
= NULL
;
55 /* hash table for states, to recognize equivalent ones. */
57 #define STATE_HASH_SIZE 1009
58 static state_t
**state_hash
= NULL
;
62 allocate_itemsets (void)
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
71 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
73 for (i
= 0; ritem
[i
]; ++i
)
77 symbol_count
[ritem
[i
]]++;
80 /* See comments before new_itemsets. All the vectors of items
81 live inside KERNEL_ITEMS. The number of active items after
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. */
86 kernel_base
= XCALLOC (short *, nsyms
);
88 kernel_items
= XCALLOC (short, count
);
91 for (i
= 0; i
< nsyms
; i
++)
93 kernel_base
[i
] = kernel_items
+ count
;
94 count
+= symbol_count
[i
];
98 kernel_size
= XCALLOC (int, nsyms
);
103 allocate_storage (void)
105 allocate_itemsets ();
107 shiftset
= XCALLOC (short, nsyms
);
108 redset
= XCALLOC (short, nrules
+ 1);
109 state_hash
= XCALLOC (state_t
*, STATE_HASH_SIZE
);
121 XFREE (kernel_items
);
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. |
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 |
136 | shifted, and kernel_size[symbol] is their numbers. |
137 `----------------------------------------------------------------*/
145 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
148 for (i
= 0; i
< nsyms
; i
++)
151 shift_symbol
= XCALLOC (short, nsyms
);
154 for (i
= 0; i
< nitemset
; ++i
)
156 int symbol
= ritem
[itemset
[i
]];
159 if (!kernel_size
[symbol
])
161 shift_symbol
[nshifts
] = symbol
;
165 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
166 kernel_size
[symbol
]++;
173 /*-----------------------------------------------------------------.
174 | Subroutine of get_state. Create a new state for those items, if |
176 `-----------------------------------------------------------------*/
179 new_state (int symbol
)
184 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
185 this_state
->number
, symbol
, tags
[symbol
]);
187 if (nstates
>= MAXSHORT
)
188 fatal (_("too many states (max %d)"), MAXSHORT
);
190 p
= STATE_ALLOC (kernel_size
[symbol
]);
191 p
->accessing_symbol
= symbol
;
193 p
->nitems
= kernel_size
[symbol
];
195 shortcpy (p
->items
, kernel_base
[symbol
], kernel_size
[symbol
]);
197 last_state
->next
= p
;
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 |
208 | equivalent one exists already. Used by append_states. |
209 `--------------------------------------------------------------*/
212 get_state (int symbol
)
219 fprintf (stderr
, "Entering get_state, state = %d, symbol = %d (%s)\n",
220 this_state
->number
, symbol
, tags
[symbol
]);
222 /* Add up the target state's active item numbers to get a hash key.
225 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
226 key
+= kernel_base
[symbol
][i
];
227 key
= key
% STATE_HASH_SIZE
;
228 sp
= state_hash
[key
];
235 if (sp
->nitems
== kernel_size
[symbol
])
238 for (i
= 0; i
< kernel_size
[symbol
]; ++i
)
239 if (kernel_base
[symbol
][i
] != sp
->items
[i
])
249 else /* bucket exhausted and no match */
251 sp
= sp
->link
= new_state (symbol
);
257 else /* bucket is empty */
259 state_hash
[key
] = sp
= new_state (symbol
);
263 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
268 /*------------------------------------------------------------------.
269 | Use the information computed by new_itemsets to find the state |
270 | numbers reached by each shift transition from the current state. |
272 | shiftset is set up as a vector of state numbers of those states. |
273 `------------------------------------------------------------------*/
283 fprintf (stderr
, "Entering append_states, state = %d\n",
286 /* first sort shift_symbol into increasing order */
288 for (i
= 1; i
< nshifts
; i
++)
290 symbol
= shift_symbol
[i
];
292 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
294 shift_symbol
[j
] = shift_symbol
[j
- 1];
297 shift_symbol
[j
] = symbol
;
300 for (i
= 0; i
< nshifts
; i
++)
301 shiftset
[i
] = get_state (shift_symbol
[i
]);
308 first_state
= last_state
= this_state
= STATE_ALLOC (0);
313 /*------------------------------------------------------------.
314 | Save the NSHIFTS of SHIFTSET into the current linked list. |
315 `------------------------------------------------------------*/
320 shifts
*p
= shifts_new (nshifts
);
322 p
->number
= this_state
->number
;
324 shortcpy (p
->shifts
, shiftset
, nshifts
);
327 last_shift
->next
= p
;
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. |
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. |
341 `------------------------------------------------------------------*/
344 insert_start_shifting_state (void)
349 statep
= STATE_ALLOC (0);
350 statep
->number
= nstates
;
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
357 statep
->accessing_symbol
= start_symbol
;
359 last_state
->next
= statep
;
362 /* Make a shift from this state to (what will be) the final state. */
364 sp
->number
= nstates
++;
365 sp
->shifts
[0] = nstates
;
367 last_shift
->next
= sp
;
372 /*-----------------------------------------------------------------.
373 | Subroutine of augment_automaton. Create the final state, which |
374 | shifts `0', the end of file. The initial state shifts the start |
375 | symbol, and goes to here. |
376 `-----------------------------------------------------------------*/
379 insert_eof_shifting_state (void)
384 /* Make the final state--the one that follows a shift from the
386 The symbol for that shift is 0 (end-of-file). */
387 statep
= STATE_ALLOC (0);
388 statep
->number
= nstates
;
390 last_state
->next
= statep
;
393 /* Make the shift from the final state to the termination state. */
395 sp
->number
= nstates
++;
396 sp
->shifts
[0] = nstates
;
398 last_shift
->next
= sp
;
403 /*---------------------------------------------------------------.
404 | Subroutine of augment_automaton. Create the accepting state. |
405 `---------------------------------------------------------------*/
408 insert_accepting_state (void)
412 /* Note that the variable `final_state' refers to what we sometimes
413 call the termination state. */
414 final_state
= nstates
;
416 /* Make the termination state. */
417 statep
= STATE_ALLOC (0);
418 statep
->number
= nstates
++;
419 last_state
->next
= statep
;
427 /*------------------------------------------------------------------.
428 | Make sure that the initial state has a shift that accepts the |
429 | grammar's start symbol and goes to the next-to-final state, which |
430 | has a shift going to the final state, which has a shift to the |
431 | termination state. Create such states and shifts if they don't |
432 | happen to exist already. |
433 `------------------------------------------------------------------*/
436 augment_automaton (void)
438 if (!first_shift
->nshifts
)
440 /* There are no shifts for any state. Make one shift, from the
441 initial state to the next-to-final state. */
443 shifts
*sp
= shifts_new (1);
444 sp
->shifts
[0] = nstates
;
446 /* Initialize the chain of shifts with sp. */
450 /* Create the next-to-final state, with shift to
451 what will be the final state. */
452 insert_start_shifting_state ();
454 else if (first_shift
->number
== 0)
456 state_t
*statep
= first_state
->next
;
457 shifts
*sp
= first_shift
;
459 /* The states reached by shifts from FIRST_STATE are numbered
460 1..(SP->NSHIFTS). Look for one reached by START_SYMBOL. */
461 while (statep
->accessing_symbol
!= start_symbol
462 && statep
->number
< sp
->nshifts
)
463 statep
= statep
->next
;
465 if (statep
->accessing_symbol
== start_symbol
)
467 /* We already have a next-to-final state.
468 Make sure it has a shift to what will be the final state. */
469 while (sp
&& sp
->number
< statep
->number
)
475 if (sp
&& sp
->number
== statep
->number
)
478 shifts
*sp2
= shifts_new (sp
->nshifts
+ 1);
479 sp2
->number
= statep
->number
;
480 sp2
->shifts
[0] = nstates
;
481 for (i
= sp
->nshifts
; i
> 0; i
--)
482 sp2
->shifts
[i
] = sp
->shifts
[i
- 1];
484 /* Patch sp2 into the chain of shifts in place of sp,
486 sp2
->next
= sp
->next
;
488 if (sp
== last_shift
)
494 shifts
*sp2
= shifts_new (1);
495 sp2
->number
= statep
->number
;
496 sp2
->shifts
[0] = nstates
;
498 /* Patch sp2 into the chain of shifts between sp1 and sp. */
511 /* There is no next-to-final state as yet. */
512 /* Add one more shift in first_shift,
513 going to the next-to-final state (yet to be made). */
514 sp2
= shifts_new (sp
->nshifts
+ 1);
516 /* Stick this shift into the vector at the proper place. */
517 statep
= first_state
->next
;
518 for (k
= 0, i
= 0; i
< sp
->nshifts
; k
++, i
++)
520 if (statep
->accessing_symbol
> start_symbol
&& i
== k
)
521 sp2
->shifts
[k
++] = nstates
;
522 sp2
->shifts
[k
] = sp
->shifts
[i
];
523 statep
= statep
->next
;
526 sp2
->shifts
[k
++] = nstates
;
528 /* Patch sp2 into the chain of shifts
529 in place of sp, at the beginning. */
530 sp2
->next
= sp
->next
;
532 if (last_shift
== sp
)
537 /* Create the next-to-final state, with shift to
538 what will be the final state. */
539 insert_start_shifting_state ();
544 /* The initial state didn't even have any shifts.
545 Give it one shift, to the next-to-final state. */
546 shifts
*sp
= shifts_new (1);
547 sp
->shifts
[0] = nstates
;
549 /* Patch sp into the chain of shifts at the beginning. */
550 sp
->next
= first_shift
;
553 /* Create the next-to-final state, with shift to
554 what will be the final state. */
555 insert_start_shifting_state ();
558 insert_eof_shifting_state ();
559 insert_accepting_state ();
563 /*----------------------------------------------------------------.
564 | Find which rules can be used for reduction transitions from the |
565 | current state and make a reductions structure for the state to |
566 | record their rule numbers. |
567 `----------------------------------------------------------------*/
570 save_reductions (void)
575 /* Find and count the active items that represent ends of rules. */
578 for (i
= 0; i
< nitemset
; ++i
)
580 int item
= ritem
[itemset
[i
]];
582 redset
[count
++] = -item
;
585 /* Make a reductions structure and copy the data into it. */
589 reductions
*p
= REDUCTIONS_ALLOC (count
);
591 shortcpy (p
->rules
, redset
, count
);
593 this_state
->reductions
= p
;
598 /*-------------------------------------------------------------------.
599 | Compute the nondeterministic finite state machine (see state.h for |
600 | details) from the grammar. |
601 `-------------------------------------------------------------------*/
604 generate_states (void)
607 new_closure (nitems
);
613 fprintf (stderr
, "Processing state %d (reached by %s)\n",
614 this_state
->number
, tags
[this_state
->accessing_symbol
]);
615 /* Set up ruleset and itemset for the transitions out of this
616 state. ruleset gets a 1 bit for each rule that could reduce
617 now. itemset gets a vector of all the items that could be
619 closure (this_state
->items
, this_state
->nitems
);
620 /* record the reductions allowed out of this state */
622 /* find the itemsets of the states that shifts can reach */
624 /* find or create the core structures for those states */
627 /* create the shifts structures for the shifts to those states,
628 now that the state numbers transitioning to are known */
631 /* states are queued when they are created; process them all */
632 this_state
= this_state
->next
;
635 /* discard various storage */
639 /* set up initial and final states as parser wants them */
640 augment_automaton ();