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