]> git.saurik.com Git - bison.git/blame - src/LR0.c
* src/symlist.c (symbol_list_length): Return int, not unsigned
[bison.git] / src / LR0.c
CommitLineData
1dd15b6e 1/* Generate the nondeterministic finite state machine for Bison.
6fc82eaf 2
36b5e963 3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2004, 2005 Free
779e7ceb 4 Software Foundation, Inc.
40675e7c 5
2fa6973e 6 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 7
2fa6973e
AD
8 Bison is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
40675e7c 12
2fa6973e
AD
13 Bison is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
40675e7c 17
2fa6973e
AD
18 You should have received a copy of the GNU General Public License
19 along with Bison; see the file COPYING. If not, write to
0fb669f9
PE
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
40675e7c
DM
22
23
24/* See comments in state.h for the data structures that represent it.
25 The entry point is generate_states. */
26
2cec9080 27#include <config.h>
40675e7c 28#include "system.h"
add6614e
PE
29
30#include <bitset.h>
31#include <quotearg.h>
32
33#include "LR0.h"
34#include "closure.h"
35#include "complain.h"
9bfe901c 36#include "getargs.h"
40675e7c 37#include "gram.h"
add6614e 38#include "gram.h"
49701457 39#include "lalr.h"
add6614e 40#include "reader.h"
630e182b 41#include "reduce.h"
add6614e
PE
42#include "state.h"
43#include "symtab.h"
40675e7c 44
add6614e 45typedef struct state_list
32e1e0a4 46{
add6614e
PE
47 struct state_list *next;
48 state *state;
49} state_list;
32e1e0a4 50
add6614e
PE
51static state_list *first_state = NULL;
52static state_list *last_state = NULL;
32e1e0a4 53
8b752b00
AD
54
55/*------------------------------------------------------------------.
56| A state was just discovered from another state. Queue it for |
57| later examination, in order to find its transitions. Return it. |
58`------------------------------------------------------------------*/
59
add6614e
PE
60static state *
61state_list_append (symbol_number sym, size_t core_size, item_number *core)
32e1e0a4 62{
86a54ab1 63 state_list *node = xmalloc (sizeof *node);
add6614e 64 state *s = state_new (sym, core_size, core);
8b752b00 65
273a74fa 66 if (trace_flag & trace_automaton)
427c0dda 67 fprintf (stderr, "state_list_append (state = %d, symbol = %d (%s))\n",
add6614e 68 nstates, sym, symbols[sym]->tag);
8b752b00 69
32e1e0a4 70 node->next = NULL;
add6614e 71 node->state = s;
40675e7c 72
32e1e0a4
AD
73 if (!first_state)
74 first_state = node;
75 if (last_state)
76 last_state->next = node;
77 last_state = node;
8b752b00 78
add6614e 79 return s;
32e1e0a4 80}
40675e7c
DM
81
82static int nshifts;
86a54ab1 83static symbol_number *shift_symbol;
40675e7c 84
86a54ab1
PE
85static rule **redset;
86static state **shiftset;
40675e7c 87
86a54ab1
PE
88static item_number **kernel_base;
89static int *kernel_size;
90static item_number *kernel_items;
40675e7c 91
2fa6973e 92\f
4a120d45 93static void
d2729d44 94allocate_itemsets (void)
40675e7c 95{
add6614e
PE
96 symbol_number i;
97 rule_number r;
98 item_number *rhsp;
40675e7c 99
630e182b
AD
100 /* Count the number of occurrences of all the symbols in RITEMS.
101 Note that useless productions (hence useless nonterminals) are
102 browsed too, hence we need to allocate room for _all_ the
103 symbols. */
f6fbd3da
PE
104 size_t count = 0;
105 size_t *symbol_count = xcalloc (nsyms + nuseless_nonterminals,
106 sizeof *symbol_count);
40675e7c 107
4b3d3a8e 108 for (r = 0; r < nrules; ++r)
b4c4ccc2 109 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
c87d4863
AD
110 {
111 count++;
b4c4ccc2 112 symbol_count[*rhsp]++;
c87d4863 113 }
40675e7c 114
2fa6973e
AD
115 /* See comments before new_itemsets. All the vectors of items
116 live inside KERNEL_ITEMS. The number of active items after
add6614e
PE
117 some symbol S cannot be more than the number of times that S
118 appears as an item, which is SYMBOL_COUNT[S].
40675e7c
DM
119 We allocate that much space for each symbol. */
120
86a54ab1
PE
121 kernel_base = xnmalloc (nsyms, sizeof *kernel_base);
122 kernel_items = xnmalloc (count, sizeof *kernel_items);
40675e7c
DM
123
124 count = 0;
125 for (i = 0; i < nsyms; i++)
126 {
127 kernel_base[i] = kernel_items + count;
128 count += symbol_count[i];
129 }
130
630e182b 131 free (symbol_count);
86a54ab1 132 kernel_size = xnmalloc (nsyms, sizeof *kernel_size);
40675e7c
DM
133}
134
135
4a120d45 136static void
d2729d44 137allocate_storage (void)
40675e7c 138{
2fa6973e 139 allocate_itemsets ();
40675e7c 140
86a54ab1
PE
141 shiftset = xnmalloc (nsyms, sizeof *shiftset);
142 redset = xnmalloc (nrules, sizeof *redset);
c7ca99d4 143 state_hash_new ();
86a54ab1 144 shift_symbol = xnmalloc (nsyms, sizeof *shift_symbol);
40675e7c
DM
145}
146
147
4a120d45 148static void
d2729d44 149free_storage (void)
40675e7c 150{
630e182b
AD
151 free (shift_symbol);
152 free (redset);
153 free (shiftset);
154 free (kernel_base);
155 free (kernel_size);
afbb696d 156 free (kernel_items);
c7ca99d4 157 state_hash_free ();
40675e7c
DM
158}
159
160
161
40675e7c 162
32e1e0a4 163/*---------------------------------------------------------------.
add6614e 164| Find which symbols can be shifted in S, and for each one |
32e1e0a4
AD
165| record which items would be active after that shift. Uses the |
166| contents of itemset. |
167| |
168| shift_symbol is set to a vector of the symbols that can be |
169| shifted. For each symbol in the grammar, kernel_base[symbol] |
170| points to a vector of item numbers activated if that symbol is |
171| shifted, and kernel_size[symbol] is their numbers. |
172`---------------------------------------------------------------*/
40675e7c 173
4a120d45 174static void
add6614e 175new_itemsets (state *s)
40675e7c 176{
f6fbd3da 177 size_t i;
2fa6973e 178
273a74fa 179 if (trace_flag & trace_automaton)
add6614e 180 fprintf (stderr, "Entering new_itemsets, state = %d\n", s->number);
40675e7c 181
55a91a82 182 memset (kernel_size, 0, nsyms * sizeof *kernel_size);
40675e7c 183
b2872512 184 nshifts = 0;
40675e7c 185
5123689b 186 for (i = 0; i < nritemset; ++i)
5fbb0954
AD
187 if (ritem[itemset[i]] >= 0)
188 {
add6614e
PE
189 symbol_number sym = item_number_as_symbol_number (ritem[itemset[i]]);
190 if (!kernel_size[sym])
5fbb0954 191 {
add6614e 192 shift_symbol[nshifts] = sym;
5fbb0954
AD
193 nshifts++;
194 }
195
add6614e
PE
196 kernel_base[sym][kernel_size[sym]] = itemset[i] + 1;
197 kernel_size[sym]++;
5fbb0954 198 }
40675e7c
DM
199}
200
201
202
add6614e
PE
203/*--------------------------------------------------------------.
204| Find the state we would get to (from the current state) by |
205| shifting SYM. Create a new state if no equivalent one exists |
206| already. Used by append_states. |
207`--------------------------------------------------------------*/
40675e7c 208
add6614e
PE
209static state *
210get_state (symbol_number sym, size_t core_size, item_number *core)
40675e7c 211{
36b5e963 212 state *s;
40675e7c 213
273a74fa 214 if (trace_flag & trace_automaton)
427c0dda 215 fprintf (stderr, "Entering get_state, symbol = %d (%s)\n",
add6614e 216 sym, symbols[sym]->tag);
40675e7c 217
36b5e963
AD
218 s = state_hash_lookup (core_size, core);
219 if (!s)
220 s = state_list_append (sym, core_size, core);
40675e7c 221
273a74fa 222 if (trace_flag & trace_automaton)
36b5e963 223 fprintf (stderr, "Exiting get_state => %d\n", s->number);
c87d4863 224
36b5e963 225 return s;
40675e7c
DM
226}
227
640748ee
AD
228/*---------------------------------------------------------------.
229| Use the information computed by new_itemsets to find the state |
add6614e 230| numbers reached by each shift transition from S. |
640748ee
AD
231| |
232| SHIFTSET is set up as a vector of those states. |
233`---------------------------------------------------------------*/
40675e7c 234
2fa6973e 235static void
add6614e 236append_states (state *s)
40675e7c 237{
2fa6973e 238 int i;
40675e7c 239
273a74fa 240 if (trace_flag & trace_automaton)
add6614e 241 fprintf (stderr, "Entering append_states, state = %d\n", s->number);
40675e7c 242
add6614e 243 /* First sort shift_symbol into increasing order. */
40675e7c 244
2fa6973e
AD
245 for (i = 1; i < nshifts; i++)
246 {
add6614e
PE
247 symbol_number sym = shift_symbol[i];
248 int j;
86a54ab1 249 for (j = i; 0 < j && sym < shift_symbol[j - 1]; j--)
add6614e
PE
250 shift_symbol[j] = shift_symbol[j - 1];
251 shift_symbol[j] = sym;
2fa6973e 252 }
40675e7c 253
2fa6973e 254 for (i = 0; i < nshifts; i++)
458be8e0 255 {
add6614e
PE
256 symbol_number sym = shift_symbol[i];
257 shiftset[i] = get_state (sym, kernel_size[sym], kernel_base[sym]);
458be8e0 258 }
40675e7c
DM
259}
260
261
2fa6973e
AD
262/*----------------------------------------------------------------.
263| Find which rules can be used for reduction transitions from the |
264| current state and make a reductions structure for the state to |
265| record their rule numbers. |
266`----------------------------------------------------------------*/
267
4a120d45 268static void
add6614e 269save_reductions (state *s)
40675e7c 270{
30171f79 271 int count = 0;
f6fbd3da 272 size_t i;
40675e7c 273
30171f79 274 /* Find and count the active items that represent ends of rules. */
5123689b 275 for (i = 0; i < nritemset; ++i)
2fa6973e 276 {
36b5e963
AD
277 item_number item = ritem[itemset[i]];
278 if (item_number_is_rule_number (item))
279 {
280 rule_number r = item_number_as_rule_number (item);
281 redset[count++] = &rules[r];
282 if (r == 0)
283 {
284 /* This is "reduce 0", i.e., accept. */
285 assert (!final_state);
286 final_state = s;
287 }
288 }
2fa6973e 289 }
40675e7c 290
2fa6973e 291 /* Make a reductions structure and copy the data into it. */
add6614e 292 state_reductions_set (s, count, redset);
2fa6973e
AD
293}
294
295\f
82841af7 296/*---------------.
29e88316 297| Build STATES. |
82841af7 298`---------------*/
6a164e0c
AD
299
300static void
29e88316 301set_states (void)
6a164e0c 302{
86a54ab1 303 states = xcalloc (nstates, sizeof *states);
6a164e0c 304
32e1e0a4 305 while (first_state)
2cec70b9 306 {
add6614e 307 state_list *this = first_state;
32e1e0a4 308
2cec70b9 309 /* Pessimization, but simplification of the code: make sure all
8b752b00
AD
310 the states have valid transitions and reductions members,
311 even if reduced to 0. It is too soon for errs, which are
312 computed later, but set_conflicts. */
add6614e
PE
313 state *s = this->state;
314 if (!s->transitions)
315 state_transitions_set (s, 0, 0);
316 if (!s->reductions)
317 state_reductions_set (s, 0, 0);
32e1e0a4 318
add6614e 319 states[s->number] = s;
32e1e0a4
AD
320
321 first_state = this->next;
322 free (this);
2cec70b9 323 }
32e1e0a4
AD
324 first_state = NULL;
325 last_state = NULL;
6a164e0c
AD
326}
327
c7ca99d4 328
2fa6973e
AD
329/*-------------------------------------------------------------------.
330| Compute the nondeterministic finite state machine (see state.h for |
331| details) from the grammar. |
332`-------------------------------------------------------------------*/
333
334void
335generate_states (void)
336{
86a54ab1 337 item_number initial_core = 0;
add6614e 338 state_list *list = NULL;
2fa6973e 339 allocate_storage ();
9e7f6bbd 340 new_closure (nritems);
8b752b00
AD
341
342 /* Create the initial state. The 0 at the lhs is the index of the
343 item of this initial rule. */
86a54ab1 344 state_list_append (0, 1, &initial_core);
8b752b00 345
36b5e963
AD
346 /* States are queued when they are created; process them all. */
347 for (list = first_state; list; list = list->next)
2fa6973e 348 {
add6614e 349 state *s = list->state;
273a74fa 350 if (trace_flag & trace_automaton)
427c0dda 351 fprintf (stderr, "Processing state %d (reached by %s)\n",
add6614e
PE
352 s->number,
353 symbols[s->accessing_symbol]->tag);
2fa6973e
AD
354 /* Set up ruleset and itemset for the transitions out of this
355 state. ruleset gets a 1 bit for each rule that could reduce
356 now. itemset gets a vector of all the items that could be
357 accepted next. */
add6614e 358 closure (s->items, s->nitems);
32e1e0a4 359 /* Record the reductions allowed out of this state. */
add6614e 360 save_reductions (s);
32e1e0a4 361 /* Find the itemsets of the states that shifts can reach. */
add6614e 362 new_itemsets (s);
32e1e0a4 363 /* Find or create the core structures for those states. */
add6614e 364 append_states (s);
32e1e0a4
AD
365
366 /* Create the shifts structures for the shifts to those states,
367 now that the state numbers transitioning to are known. */
add6614e 368 state_transitions_set (s, nshifts, shiftset);
2fa6973e
AD
369 }
370
371 /* discard various storage */
372 free_closure ();
373 free_storage ();
374
29e88316
AD
375 /* Set up STATES. */
376 set_states ();
40675e7c 377}