]> git.saurik.com Git - bison.git/blob - src/LR0.c
65c9ec07f5313d4b4d02f5ba941c08813da8c020
[bison.git] / src / LR0.c
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001, 2002 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 "bitset.h"
27 #include "quotearg.h"
28 #include "symtab.h"
29 #include "getargs.h"
30 #include "reader.h"
31 #include "gram.h"
32 #include "state.h"
33 #include "complain.h"
34 #include "closure.h"
35 #include "LR0.h"
36 #include "lalr.h"
37 #include "reduce.h"
38
39 unsigned int nstates = 0;
40 /* Initialize the final state to -1, otherwise, it might be set to 0
41 by default, and since we don't compute the reductions of the final
42 state, we end up not computing the reductions of the initial state,
43 which is of course needed.
44
45 FINAL_STATE is properly set by new_state when it recognizes the
46 accessing symbol: EOF. */
47 int final_state = -1;
48 static state_t *first_state = NULL;
49
50 static state_t *this_state = NULL;
51 static state_t *last_state = NULL;
52
53 static int nshifts;
54 static short *shift_symbol = NULL;
55
56 static short *redset = NULL;
57 static short *shiftset = NULL;
58
59 static item_number_t **kernel_base = NULL;
60 static int *kernel_size = NULL;
61 static item_number_t *kernel_items = NULL;
62
63 /* hash table for states, to recognize equivalent ones. */
64
65 #define STATE_HASH_SIZE 1009
66 static state_t **state_hash = NULL;
67
68 \f
69 static void
70 allocate_itemsets (void)
71 {
72 int i, r;
73 item_number_t *rhsp;
74
75 /* Count the number of occurrences of all the symbols in RITEMS.
76 Note that useless productions (hence useless nonterminals) are
77 browsed too, hence we need to allocate room for _all_ the
78 symbols. */
79 int count = 0;
80 short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals);
81
82 for (r = 1; r < nrules + 1; ++r)
83 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
84 {
85 count++;
86 symbol_count[*rhsp]++;
87 }
88
89 /* See comments before new_itemsets. All the vectors of items
90 live inside KERNEL_ITEMS. The number of active items after
91 some symbol cannot be more than the number of times that symbol
92 appears as an item, which is symbol_count[symbol].
93 We allocate that much space for each symbol. */
94
95 kernel_base = XCALLOC (item_number_t *, nsyms);
96 if (count)
97 kernel_items = XCALLOC (item_number_t, count);
98
99 count = 0;
100 for (i = 0; i < nsyms; i++)
101 {
102 kernel_base[i] = kernel_items + count;
103 count += symbol_count[i];
104 }
105
106 free (symbol_count);
107 kernel_size = XCALLOC (int, nsyms);
108 }
109
110
111 static void
112 allocate_storage (void)
113 {
114 allocate_itemsets ();
115
116 shiftset = XCALLOC (short, nsyms);
117 redset = XCALLOC (short, nrules + 1);
118 state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
119 shift_symbol = XCALLOC (short, nsyms);
120 }
121
122
123 static void
124 free_storage (void)
125 {
126 free (shift_symbol);
127 free (redset);
128 free (shiftset);
129 free (kernel_base);
130 free (kernel_size);
131 XFREE (kernel_items);
132 free (state_hash);
133 }
134
135
136
137
138 /*----------------------------------------------------------------.
139 | Find which symbols can be shifted in the current state, and for |
140 | each one record which items would be active after that shift. |
141 | Uses the contents of itemset. |
142 | |
143 | shift_symbol is set to a vector of the symbols that can be |
144 | shifted. For each symbol in the grammar, kernel_base[symbol] |
145 | points to a vector of item numbers activated if that symbol is |
146 | shifted, and kernel_size[symbol] is their numbers. |
147 `----------------------------------------------------------------*/
148
149 static void
150 new_itemsets (void)
151 {
152 int i;
153
154 if (trace_flag)
155 fprintf (stderr, "Entering new_itemsets, state = %d\n",
156 this_state->number);
157
158 for (i = 0; i < nsyms; i++)
159 kernel_size[i] = 0;
160
161 nshifts = 0;
162
163 for (i = 0; i < nritemset; ++i)
164 {
165 int symbol = ritem[itemset[i]];
166 if (symbol >= 0)
167 {
168 if (!kernel_size[symbol])
169 {
170 shift_symbol[nshifts] = symbol;
171 nshifts++;
172 }
173
174 kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
175 kernel_size[symbol]++;
176 }
177 }
178 }
179
180
181
182 /*-----------------------------------------------------------------.
183 | Subroutine of get_state. Create a new state for those items, if |
184 | necessary. |
185 `-----------------------------------------------------------------*/
186
187 static state_t *
188 new_state (int symbol)
189 {
190 state_t *p;
191
192 if (trace_flag)
193 fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
194 nstates, symbol, quotearg_style (escape_quoting_style,
195 symbols[symbol]->tag));
196
197 if (nstates >= SHRT_MAX)
198 fatal (_("too many states (max %d)"), SHRT_MAX);
199
200 p = STATE_ALLOC (kernel_size[symbol]);
201 p->accessing_symbol = symbol;
202 p->number = nstates;
203 p->nitems = kernel_size[symbol];
204
205 memcpy (p->items, kernel_base[symbol],
206 kernel_size[symbol] * sizeof (kernel_base[symbol][0]));
207
208 /* If this is the eoftoken, and this is not the initial state, then
209 this is the final state. */
210 if (symbol == 0 && first_state)
211 final_state = p->number;
212
213 if (!first_state)
214 first_state = p;
215 if (last_state)
216 last_state->next = p;
217 last_state = p;
218
219 nstates++;
220
221 return p;
222 }
223
224
225 /*--------------------------------------------------------------.
226 | Find the state number for the state we would get to (from the |
227 | current state) by shifting symbol. Create a new state if no |
228 | equivalent one exists already. Used by append_states. |
229 `--------------------------------------------------------------*/
230
231 static int
232 get_state (int symbol)
233 {
234 int key;
235 int i;
236 state_t *sp;
237
238 if (trace_flag)
239 fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n",
240 this_state->number, symbol, quotearg_style (escape_quoting_style,
241 symbols[symbol]->tag));
242
243 /* Add up the target state's active item numbers to get a hash key.
244 */
245 key = 0;
246 for (i = 0; i < kernel_size[symbol]; ++i)
247 key += kernel_base[symbol][i];
248 key = key % STATE_HASH_SIZE;
249 sp = state_hash[key];
250
251 if (sp)
252 {
253 int found = 0;
254 while (!found)
255 {
256 if (sp->nitems == kernel_size[symbol])
257 {
258 found = 1;
259 for (i = 0; i < kernel_size[symbol]; ++i)
260 if (kernel_base[symbol][i] != sp->items[i])
261 found = 0;
262 }
263
264 if (!found)
265 {
266 if (sp->link)
267 {
268 sp = sp->link;
269 }
270 else /* bucket exhausted and no match */
271 {
272 sp = sp->link = new_state (symbol);
273 found = 1;
274 }
275 }
276 }
277 }
278 else /* bucket is empty */
279 {
280 state_hash[key] = sp = new_state (symbol);
281 }
282
283 if (trace_flag)
284 fprintf (stderr, "Exiting get_state => %d\n", sp->number);
285
286 return sp->number;
287 }
288
289 /*------------------------------------------------------------------.
290 | Use the information computed by new_itemsets to find the state |
291 | numbers reached by each shift transition from the current state. |
292 | |
293 | shiftset is set up as a vector of state numbers of those states. |
294 `------------------------------------------------------------------*/
295
296 static void
297 append_states (void)
298 {
299 int i;
300 int j;
301 int symbol;
302
303 if (trace_flag)
304 fprintf (stderr, "Entering append_states, state = %d\n",
305 this_state->number);
306
307 /* first sort shift_symbol into increasing order */
308
309 for (i = 1; i < nshifts; i++)
310 {
311 symbol = shift_symbol[i];
312 j = i;
313 while (j > 0 && shift_symbol[j - 1] > symbol)
314 {
315 shift_symbol[j] = shift_symbol[j - 1];
316 j--;
317 }
318 shift_symbol[j] = symbol;
319 }
320
321 for (i = 0; i < nshifts; i++)
322 shiftset[i] = get_state (shift_symbol[i]);
323 }
324
325
326 static void
327 new_states (void)
328 {
329 /* The 0 at the lhs is the index of the item of this initial rule. */
330 kernel_base[0][0] = 0;
331 kernel_size[0] = 1;
332 this_state = new_state (0);
333 }
334
335
336 /*------------------------------------------------------------.
337 | Save the NSHIFTS of SHIFTSET into the current linked list. |
338 `------------------------------------------------------------*/
339
340 static void
341 save_shifts (void)
342 {
343 shifts *p = shifts_new (nshifts);
344 memcpy (p->shifts, shiftset, nshifts * sizeof (shiftset[0]));
345 this_state->shifts = p;
346 }
347
348
349 /*----------------------------------------------------------------.
350 | Find which rules can be used for reduction transitions from the |
351 | current state and make a reductions structure for the state to |
352 | record their rule numbers. |
353 `----------------------------------------------------------------*/
354
355 static void
356 save_reductions (void)
357 {
358 int count = 0;
359 int i;
360
361 /* If this is the final state, we want it to have no reductions at
362 all, although it has one for `START_SYMBOL EOF .'. */
363 if (this_state->number == final_state)
364 return;
365
366 /* Find and count the active items that represent ends of rules. */
367 for (i = 0; i < nritemset; ++i)
368 {
369 int item = ritem[itemset[i]];
370 if (item < 0)
371 redset[count++] = -item;
372 }
373
374 /* Make a reductions structure and copy the data into it. */
375 this_state->reductions = reductions_new (count);
376 memcpy (this_state->reductions->rules, redset, count * sizeof (redset[0]));
377 }
378
379 \f
380 /*---------------.
381 | Build STATES. |
382 `---------------*/
383
384 static void
385 set_states (void)
386 {
387 state_t *sp;
388 states = XCALLOC (state_t *, nstates);
389
390 for (sp = first_state; sp; sp = sp->next)
391 {
392 /* Pessimization, but simplification of the code: make sure all
393 the states have a shifts, errs, and reductions, even if
394 reduced to 0. */
395 if (!sp->shifts)
396 sp->shifts = shifts_new (0);
397 if (!sp->errs)
398 sp->errs = errs_new (0);
399 if (!sp->reductions)
400 sp->reductions = reductions_new (0);
401
402 states[sp->number] = sp;
403 }
404 }
405
406 /*-------------------------------------------------------------------.
407 | Compute the nondeterministic finite state machine (see state.h for |
408 | details) from the grammar. |
409 `-------------------------------------------------------------------*/
410
411 void
412 generate_states (void)
413 {
414 allocate_storage ();
415 new_closure (nritems);
416 new_states ();
417
418 while (this_state)
419 {
420 if (trace_flag)
421 fprintf (stderr, "Processing state %d (reached by %s)\n",
422 this_state->number,
423 quotearg_style (escape_quoting_style,
424 symbols[this_state->accessing_symbol]->tag));
425 /* Set up ruleset and itemset for the transitions out of this
426 state. ruleset gets a 1 bit for each rule that could reduce
427 now. itemset gets a vector of all the items that could be
428 accepted next. */
429 closure (this_state->items, this_state->nitems);
430 /* record the reductions allowed out of this state */
431 save_reductions ();
432 /* find the itemsets of the states that shifts can reach */
433 new_itemsets ();
434 /* find or create the core structures for those states */
435 append_states ();
436
437 /* create the shifts structures for the shifts to those states,
438 now that the state numbers transitioning to are known */
439 save_shifts ();
440
441 /* states are queued when they are created; process them all */
442 this_state = this_state->next;
443 }
444
445 /* discard various storage */
446 free_closure ();
447 free_storage ();
448
449 /* Set up STATES. */
450 set_states ();
451 }