]> git.saurik.com Git - bison.git/blame - src/LR0.c
* LR0.c (new_itemsets): Fix wording in comments: say item index rather
[bison.git] / src / LR0.c
CommitLineData
1dd15b6e 1/* Generate the nondeterministic finite state machine for Bison.
6fc82eaf 2
75ad86ee 3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007
4f82b42a 4 Free 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. |
71b61d4d 172| |
6ce2d93a
JD
173| itemset is sorted on item index in ritem, which is sorted on |
174| rule number. Compute each kernel_base[symbol] with the same |
175| sort. |
32e1e0a4 176`---------------------------------------------------------------*/
40675e7c 177
4a120d45 178static void
add6614e 179new_itemsets (state *s)
40675e7c 180{
f6fbd3da 181 size_t i;
2fa6973e 182
273a74fa 183 if (trace_flag & trace_automaton)
add6614e 184 fprintf (stderr, "Entering new_itemsets, state = %d\n", s->number);
40675e7c 185
55a91a82 186 memset (kernel_size, 0, nsyms * sizeof *kernel_size);
40675e7c 187
b2872512 188 nshifts = 0;
40675e7c 189
b09f4f48
JD
190 for (i = 0; i < nitemset; ++i)
191 if (item_number_is_symbol_number (ritem[itemset[i]]))
5fbb0954 192 {
add6614e
PE
193 symbol_number sym = item_number_as_symbol_number (ritem[itemset[i]]);
194 if (!kernel_size[sym])
5fbb0954 195 {
add6614e 196 shift_symbol[nshifts] = sym;
5fbb0954
AD
197 nshifts++;
198 }
199
add6614e
PE
200 kernel_base[sym][kernel_size[sym]] = itemset[i] + 1;
201 kernel_size[sym]++;
5fbb0954 202 }
40675e7c
DM
203}
204
205
206
add6614e
PE
207/*--------------------------------------------------------------.
208| Find the state we would get to (from the current state) by |
209| shifting SYM. Create a new state if no equivalent one exists |
210| already. Used by append_states. |
211`--------------------------------------------------------------*/
40675e7c 212
add6614e
PE
213static state *
214get_state (symbol_number sym, size_t core_size, item_number *core)
40675e7c 215{
36b5e963 216 state *s;
40675e7c 217
273a74fa 218 if (trace_flag & trace_automaton)
427c0dda 219 fprintf (stderr, "Entering get_state, symbol = %d (%s)\n",
add6614e 220 sym, symbols[sym]->tag);
40675e7c 221
36b5e963
AD
222 s = state_hash_lookup (core_size, core);
223 if (!s)
224 s = state_list_append (sym, core_size, core);
40675e7c 225
273a74fa 226 if (trace_flag & trace_automaton)
36b5e963 227 fprintf (stderr, "Exiting get_state => %d\n", s->number);
c87d4863 228
36b5e963 229 return s;
40675e7c
DM
230}
231
640748ee
AD
232/*---------------------------------------------------------------.
233| Use the information computed by new_itemsets to find the state |
add6614e 234| numbers reached by each shift transition from S. |
640748ee
AD
235| |
236| SHIFTSET is set up as a vector of those states. |
237`---------------------------------------------------------------*/
40675e7c 238
2fa6973e 239static void
add6614e 240append_states (state *s)
40675e7c 241{
2fa6973e 242 int i;
40675e7c 243
273a74fa 244 if (trace_flag & trace_automaton)
add6614e 245 fprintf (stderr, "Entering append_states, state = %d\n", s->number);
40675e7c 246
add6614e 247 /* First sort shift_symbol into increasing order. */
40675e7c 248
2fa6973e
AD
249 for (i = 1; i < nshifts; i++)
250 {
add6614e
PE
251 symbol_number sym = shift_symbol[i];
252 int j;
86a54ab1 253 for (j = i; 0 < j && sym < shift_symbol[j - 1]; j--)
add6614e
PE
254 shift_symbol[j] = shift_symbol[j - 1];
255 shift_symbol[j] = sym;
2fa6973e 256 }
40675e7c 257
2fa6973e 258 for (i = 0; i < nshifts; i++)
458be8e0 259 {
add6614e
PE
260 symbol_number sym = shift_symbol[i];
261 shiftset[i] = get_state (sym, kernel_size[sym], kernel_base[sym]);
458be8e0 262 }
40675e7c
DM
263}
264
265
2fa6973e
AD
266/*----------------------------------------------------------------.
267| Find which rules can be used for reduction transitions from the |
268| current state and make a reductions structure for the state to |
269| record their rule numbers. |
270`----------------------------------------------------------------*/
271
4a120d45 272static void
add6614e 273save_reductions (state *s)
40675e7c 274{
30171f79 275 int count = 0;
f6fbd3da 276 size_t i;
40675e7c 277
30171f79 278 /* Find and count the active items that represent ends of rules. */
b09f4f48 279 for (i = 0; i < nitemset; ++i)
2fa6973e 280 {
36b5e963
AD
281 item_number item = ritem[itemset[i]];
282 if (item_number_is_rule_number (item))
283 {
284 rule_number r = item_number_as_rule_number (item);
285 redset[count++] = &rules[r];
286 if (r == 0)
287 {
288 /* This is "reduce 0", i.e., accept. */
4f82b42a 289 aver (!final_state);
36b5e963
AD
290 final_state = s;
291 }
292 }
2fa6973e 293 }
40675e7c 294
2fa6973e 295 /* Make a reductions structure and copy the data into it. */
add6614e 296 state_reductions_set (s, count, redset);
2fa6973e
AD
297}
298
299\f
82841af7 300/*---------------.
29e88316 301| Build STATES. |
82841af7 302`---------------*/
6a164e0c
AD
303
304static void
29e88316 305set_states (void)
6a164e0c 306{
86a54ab1 307 states = xcalloc (nstates, sizeof *states);
6a164e0c 308
32e1e0a4 309 while (first_state)
2cec70b9 310 {
add6614e 311 state_list *this = first_state;
32e1e0a4 312
2cec70b9 313 /* Pessimization, but simplification of the code: make sure all
8b752b00
AD
314 the states have valid transitions and reductions members,
315 even if reduced to 0. It is too soon for errs, which are
316 computed later, but set_conflicts. */
add6614e
PE
317 state *s = this->state;
318 if (!s->transitions)
319 state_transitions_set (s, 0, 0);
320 if (!s->reductions)
321 state_reductions_set (s, 0, 0);
32e1e0a4 322
add6614e 323 states[s->number] = s;
32e1e0a4
AD
324
325 first_state = this->next;
326 free (this);
2cec70b9 327 }
32e1e0a4
AD
328 first_state = NULL;
329 last_state = NULL;
6a164e0c
AD
330}
331
c7ca99d4 332
2fa6973e
AD
333/*-------------------------------------------------------------------.
334| Compute the nondeterministic finite state machine (see state.h for |
335| details) from the grammar. |
336`-------------------------------------------------------------------*/
337
338void
339generate_states (void)
340{
86a54ab1 341 item_number initial_core = 0;
add6614e 342 state_list *list = NULL;
2fa6973e 343 allocate_storage ();
9e7f6bbd 344 new_closure (nritems);
8b752b00
AD
345
346 /* Create the initial state. The 0 at the lhs is the index of the
347 item of this initial rule. */
86a54ab1 348 state_list_append (0, 1, &initial_core);
8b752b00 349
36b5e963
AD
350 /* States are queued when they are created; process them all. */
351 for (list = first_state; list; list = list->next)
2fa6973e 352 {
add6614e 353 state *s = list->state;
273a74fa 354 if (trace_flag & trace_automaton)
427c0dda 355 fprintf (stderr, "Processing state %d (reached by %s)\n",
add6614e
PE
356 s->number,
357 symbols[s->accessing_symbol]->tag);
71b61d4d
JD
358 /* Set up itemset for the transitions out of this state. itemset gets a
359 vector of all the items that could be accepted next. */
add6614e 360 closure (s->items, s->nitems);
32e1e0a4 361 /* Record the reductions allowed out of this state. */
add6614e 362 save_reductions (s);
32e1e0a4 363 /* Find the itemsets of the states that shifts can reach. */
add6614e 364 new_itemsets (s);
32e1e0a4 365 /* Find or create the core structures for those states. */
add6614e 366 append_states (s);
32e1e0a4
AD
367
368 /* Create the shifts structures for the shifts to those states,
369 now that the state numbers transitioning to are known. */
add6614e 370 state_transitions_set (s, nshifts, shiftset);
2fa6973e
AD
371 }
372
373 /* discard various storage */
374 free_closure ();
375 free_storage ();
376
29e88316
AD
377 /* Set up STATES. */
378 set_states ();
40675e7c 379}