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