]> git.saurik.com Git - bison.git/blame - src/LR0.c
* src/output.c: Comment changes.
[bison.git] / src / LR0.c
CommitLineData
40675e7c 1/* Generate the nondeterministic finite state machine for bison,
97db7bd4 2 Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc.
40675e7c 3
2fa6973e 4 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 5
2fa6973e
AD
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.
40675e7c 10
2fa6973e
AD
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.
40675e7c 15
2fa6973e
AD
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. */
40675e7c
DM
20
21
22/* See comments in state.h for the data structures that represent it.
23 The entry point is generate_states. */
24
40675e7c 25#include "system.h"
9bfe901c 26#include "getargs.h"
c87d4863 27#include "reader.h"
40675e7c
DM
28#include "gram.h"
29#include "state.h"
a0f6b076 30#include "complain.h"
2fa6973e 31#include "closure.h"
403b315b 32#include "LR0.h"
49701457 33#include "lalr.h"
630e182b 34#include "reduce.h"
40675e7c 35
40675e7c
DM
36int nstates;
37int final_state;
6a164e0c 38static state_t *first_state = NULL;
40675e7c 39
f693ad14
AD
40static state_t *this_state = NULL;
41static state_t *last_state = NULL;
40675e7c
DM
42
43static int nshifts;
342b8b6e 44static short *shift_symbol = NULL;
40675e7c 45
342b8b6e
AD
46static short *redset = NULL;
47static short *shiftset = NULL;
40675e7c 48
342b8b6e 49static short **kernel_base = NULL;
6255b435 50static int *kernel_size = NULL;
342b8b6e 51static short *kernel_items = NULL;
40675e7c
DM
52
53/* hash table for states, to recognize equivalent ones. */
54
f693ad14
AD
55#define STATE_HASH_SIZE 1009
56static state_t **state_hash = NULL;
40675e7c 57
2fa6973e 58\f
4a120d45 59static void
d2729d44 60allocate_itemsets (void)
40675e7c 61{
2fa6973e 62 int i;
40675e7c 63
630e182b
AD
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);
40675e7c 70
75142d45
AD
71 for (i = 0; i < nritems; ++i)
72 if (ritem[i] >= 0)
c87d4863
AD
73 {
74 count++;
75 symbol_count[ritem[i]]++;
76 }
40675e7c 77
2fa6973e
AD
78 /* See comments before new_itemsets. All the vectors of items
79 live inside KERNEL_ITEMS. The number of active items after
40675e7c
DM
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
d7913476 84 kernel_base = XCALLOC (short *, nsyms);
342b8b6e
AD
85 if (count)
86 kernel_items = XCALLOC (short, count);
40675e7c
DM
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
630e182b 95 free (symbol_count);
0e41b407 96 kernel_size = XCALLOC (int, nsyms);
40675e7c
DM
97}
98
99
4a120d45 100static void
d2729d44 101allocate_storage (void)
40675e7c 102{
2fa6973e 103 allocate_itemsets ();
40675e7c 104
d7913476
AD
105 shiftset = XCALLOC (short, nsyms);
106 redset = XCALLOC (short, nrules + 1);
f693ad14 107 state_hash = XCALLOC (state_t *, STATE_HASH_SIZE);
40675e7c
DM
108}
109
110
4a120d45 111static void
d2729d44 112free_storage (void)
40675e7c 113{
630e182b
AD
114 free (shift_symbol);
115 free (redset);
116 free (shiftset);
117 free (kernel_base);
118 free (kernel_size);
d7913476 119 XFREE (kernel_items);
f693ad14 120 free (state_hash);
40675e7c
DM
121}
122
123
124
40675e7c 125
2fa6973e
AD
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 |
125ecb56 134| shifted, and kernel_size[symbol] is their numbers. |
2fa6973e 135`----------------------------------------------------------------*/
40675e7c 136
4a120d45 137static void
d2729d44 138new_itemsets (void)
40675e7c 139{
2fa6973e 140 int i;
2fa6973e 141
9bfe901c 142 if (trace_flag)
c87d4863
AD
143 fprintf (stderr, "Entering new_itemsets, state = %d\n",
144 this_state->number);
40675e7c
DM
145
146 for (i = 0; i < nsyms; i++)
125ecb56 147 kernel_size[i] = 0;
40675e7c 148
630e182b 149 shift_symbol = XCALLOC (short, nsyms);
b2872512 150 nshifts = 0;
40675e7c 151
b2872512 152 for (i = 0; i < nitemset; ++i)
40675e7c 153 {
97db7bd4 154 int symbol = ritem[itemset[i]];
75142d45 155 if (symbol >= 0)
40675e7c 156 {
125ecb56 157 if (!kernel_size[symbol])
40675e7c 158 {
b2872512
AD
159 shift_symbol[nshifts] = symbol;
160 nshifts++;
40675e7c
DM
161 }
162
125ecb56
AD
163 kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
164 kernel_size[symbol]++;
40675e7c
DM
165 }
166 }
40675e7c
DM
167}
168
169
170
2fa6973e
AD
171/*-----------------------------------------------------------------.
172| Subroutine of get_state. Create a new state for those items, if |
173| necessary. |
174`-----------------------------------------------------------------*/
40675e7c 175
f693ad14 176static state_t *
2fa6973e 177new_state (int symbol)
40675e7c 178{
f693ad14 179 state_t *p;
40675e7c 180
9bfe901c 181 if (trace_flag)
c87d4863
AD
182 fprintf (stderr, "Entering new_state, state = %d, symbol = %d (%s)\n",
183 this_state->number, symbol, tags[symbol]);
40675e7c 184
2fa6973e
AD
185 if (nstates >= MAXSHORT)
186 fatal (_("too many states (max %d)"), MAXSHORT);
40675e7c 187
f693ad14 188 p = STATE_ALLOC (kernel_size[symbol]);
2fa6973e
AD
189 p->accessing_symbol = symbol;
190 p->number = nstates;
125ecb56 191 p->nitems = kernel_size[symbol];
2fa6973e 192
125ecb56 193 shortcpy (p->items, kernel_base[symbol], kernel_size[symbol]);
2fa6973e
AD
194
195 last_state->next = p;
196 last_state = p;
2fa6973e 197 nstates++;
40675e7c 198
30171f79
AD
199 /* If this is the eoftoken, then this is the final state. */
200 if (symbol == 0)
201 final_state = p->number;
202
2fa6973e
AD
203 return p;
204}
40675e7c 205
2fa6973e
AD
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 |
97db7bd4 210| equivalent one exists already. Used by append_states. |
2fa6973e 211`--------------------------------------------------------------*/
40675e7c 212
4a120d45 213static int
d2729d44 214get_state (int symbol)
40675e7c 215{
2fa6973e 216 int key;
97db7bd4 217 int i;
f693ad14 218 state_t *sp;
40675e7c 219
9bfe901c 220 if (trace_flag)
c87d4863
AD
221 fprintf (stderr, "Entering get_state, state = %d, symbol = %d (%s)\n",
222 this_state->number, symbol, tags[symbol]);
40675e7c 223
97db7bd4
AD
224 /* Add up the target state's active item numbers to get a hash key.
225 */
40675e7c 226 key = 0;
125ecb56 227 for (i = 0; i < kernel_size[symbol]; ++i)
97db7bd4 228 key += kernel_base[symbol][i];
f693ad14
AD
229 key = key % STATE_HASH_SIZE;
230 sp = state_hash[key];
40675e7c
DM
231
232 if (sp)
233 {
97db7bd4 234 int found = 0;
40675e7c
DM
235 while (!found)
236 {
125ecb56 237 if (sp->nitems == kernel_size[symbol])
40675e7c
DM
238 {
239 found = 1;
125ecb56 240 for (i = 0; i < kernel_size[symbol]; ++i)
97db7bd4
AD
241 if (kernel_base[symbol][i] != sp->items[i])
242 found = 0;
40675e7c
DM
243 }
244
245 if (!found)
246 {
247 if (sp->link)
248 {
249 sp = sp->link;
250 }
2fa6973e 251 else /* bucket exhausted and no match */
40675e7c 252 {
2fa6973e 253 sp = sp->link = new_state (symbol);
40675e7c
DM
254 found = 1;
255 }
256 }
257 }
258 }
2fa6973e 259 else /* bucket is empty */
40675e7c 260 {
f693ad14 261 state_hash[key] = sp = new_state (symbol);
40675e7c
DM
262 }
263
c87d4863
AD
264 if (trace_flag)
265 fprintf (stderr, "Exiting get_state => %d\n", sp->number);
266
36281465 267 return sp->number;
40675e7c
DM
268}
269
2fa6973e
AD
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`------------------------------------------------------------------*/
40675e7c 276
2fa6973e
AD
277static void
278append_states (void)
40675e7c 279{
2fa6973e
AD
280 int i;
281 int j;
282 int symbol;
40675e7c 283
9bfe901c 284 if (trace_flag)
c87d4863
AD
285 fprintf (stderr, "Entering append_states, state = %d\n",
286 this_state->number);
40675e7c 287
2fa6973e 288 /* first sort shift_symbol into increasing order */
40675e7c 289
2fa6973e
AD
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 }
40675e7c 301
2fa6973e 302 for (i = 0; i < nshifts; i++)
97db7bd4 303 shiftset[i] = get_state (shift_symbol[i]);
40675e7c
DM
304}
305
306
4a120d45 307static void
2fa6973e 308new_states (void)
40675e7c 309{
f693ad14 310 first_state = last_state = this_state = STATE_ALLOC (0);
40675e7c
DM
311 nstates = 1;
312}
313
314
4a38e613
AD
315/*------------------------------------------------------------.
316| Save the NSHIFTS of SHIFTSET into the current linked list. |
317`------------------------------------------------------------*/
318
4a120d45 319static void
d2729d44 320save_shifts (void)
40675e7c 321{
4a38e613 322 shifts *p = shifts_new (nshifts);
300f275f 323 shortcpy (p->shifts, shiftset, nshifts);
80e25d4d 324 this_state->shifts = p;
40675e7c
DM
325}
326
327
2fa6973e
AD
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
4a120d45 334static void
2fa6973e 335save_reductions (void)
40675e7c 336{
30171f79 337 int count = 0;
fb908786 338 int i;
40675e7c 339
30171f79
AD
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;
40675e7c 344
30171f79 345 /* Find and count the active items that represent ends of rules. */
b2872512 346 for (i = 0; i < nitemset; ++i)
2fa6973e 347 {
fb908786 348 int item = ritem[itemset[i]];
2fa6973e
AD
349 if (item < 0)
350 redset[count++] = -item;
351 }
40675e7c 352
2fa6973e 353 /* Make a reductions structure and copy the data into it. */
80dac38c
AD
354 this_state->reductions = reductions_new (count);
355 shortcpy (this_state->reductions->rules, redset, count);
2fa6973e
AD
356}
357
358\f
6a164e0c
AD
359/*--------------------.
360| Build STATE_TABLE. |
361`--------------------*/
362
363static void
364set_state_table (void)
365{
2cec70b9 366 state_t *sp;
d200e455 367 state_table = XCALLOC (state_t *, nstates);
6a164e0c 368
2cec70b9
AD
369 for (sp = first_state; sp; sp = sp->next)
370 {
371 /* Pessimization, but simplification of the code: make sure all
80dac38c
AD
372 the states have a shifts, errs, and reductions, even if
373 reduced to 0. */
2cec70b9
AD
374 if (!sp->shifts)
375 sp->shifts = shifts_new (0);
376 if (!sp->errs)
377 sp->errs = errs_new (0);
80dac38c
AD
378 if (!sp->reductions)
379 sp->reductions = reductions_new (0);
2cec70b9 380
6a164e0c 381 state_table[sp->number] = sp;
2cec70b9 382 }
6a164e0c
AD
383}
384
2fa6973e
AD
385/*-------------------------------------------------------------------.
386| Compute the nondeterministic finite state machine (see state.h for |
387| details) from the grammar. |
388`-------------------------------------------------------------------*/
389
390void
391generate_states (void)
392{
393 allocate_storage ();
394 new_closure (nitems);
395 new_states ();
396
397 while (this_state)
398 {
23cbcc6c
AD
399 if (trace_flag)
400 fprintf (stderr, "Processing state %d (reached by %s)\n",
401 this_state->number, tags[this_state->accessing_symbol]);
2fa6973e
AD
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 */
d954473d 416 save_shifts ();
2fa6973e
AD
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
6a164e0c
AD
426 /* Set up STATE_TABLE. */
427 set_state_table ();
40675e7c 428}