]> git.saurik.com Git - bison.git/blob - src/LR0.c
* LR0.c (new_itemsets): Fix wording in comments: say item index rather
[bison.git] / src / LR0.c
1 /* Generate the nondeterministic finite state machine for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007
4 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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.
12
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.
17
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
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
22
23
24 /* See comments in state.h for the data structures that represent it.
25 The entry point is generate_states. */
26
27 #include <config.h>
28 #include "system.h"
29
30 #include <bitset.h>
31 #include <quotearg.h>
32
33 #include "LR0.h"
34 #include "closure.h"
35 #include "complain.h"
36 #include "getargs.h"
37 #include "gram.h"
38 #include "gram.h"
39 #include "lalr.h"
40 #include "reader.h"
41 #include "reduce.h"
42 #include "state.h"
43 #include "symtab.h"
44
45 typedef struct state_list
46 {
47 struct state_list *next;
48 state *state;
49 } state_list;
50
51 static state_list *first_state = NULL;
52 static state_list *last_state = NULL;
53
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
60 static state *
61 state_list_append (symbol_number sym, size_t core_size, item_number *core)
62 {
63 state_list *node = xmalloc (sizeof *node);
64 state *s = state_new (sym, core_size, core);
65
66 if (trace_flag & trace_automaton)
67 fprintf (stderr, "state_list_append (state = %d, symbol = %d (%s))\n",
68 nstates, sym, symbols[sym]->tag);
69
70 node->next = NULL;
71 node->state = s;
72
73 if (!first_state)
74 first_state = node;
75 if (last_state)
76 last_state->next = node;
77 last_state = node;
78
79 return s;
80 }
81
82 static int nshifts;
83 static symbol_number *shift_symbol;
84
85 static rule **redset;
86 static state **shiftset;
87
88 static item_number **kernel_base;
89 static int *kernel_size;
90 static item_number *kernel_items;
91
92 \f
93 static void
94 allocate_itemsets (void)
95 {
96 symbol_number i;
97 rule_number r;
98 item_number *rhsp;
99
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. */
104 size_t count = 0;
105 size_t *symbol_count = xcalloc (nsyms + nuseless_nonterminals,
106 sizeof *symbol_count);
107
108 for (r = 0; r < nrules; ++r)
109 for (rhsp = rules[r].rhs; *rhsp >= 0; ++rhsp)
110 {
111 count++;
112 symbol_count[*rhsp]++;
113 }
114
115 /* See comments before new_itemsets. All the vectors of items
116 live inside KERNEL_ITEMS. The number of active items after
117 some symbol S cannot be more than the number of times that S
118 appears as an item, which is SYMBOL_COUNT[S].
119 We allocate that much space for each symbol. */
120
121 kernel_base = xnmalloc (nsyms, sizeof *kernel_base);
122 kernel_items = xnmalloc (count, sizeof *kernel_items);
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
131 free (symbol_count);
132 kernel_size = xnmalloc (nsyms, sizeof *kernel_size);
133 }
134
135
136 static void
137 allocate_storage (void)
138 {
139 allocate_itemsets ();
140
141 shiftset = xnmalloc (nsyms, sizeof *shiftset);
142 redset = xnmalloc (nrules, sizeof *redset);
143 state_hash_new ();
144 shift_symbol = xnmalloc (nsyms, sizeof *shift_symbol);
145 }
146
147
148 static void
149 free_storage (void)
150 {
151 free (shift_symbol);
152 free (redset);
153 free (shiftset);
154 free (kernel_base);
155 free (kernel_size);
156 free (kernel_items);
157 state_hash_free ();
158 }
159
160
161
162
163 /*---------------------------------------------------------------.
164 | Find which symbols can be shifted in S, and for each one |
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 | |
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. |
176 `---------------------------------------------------------------*/
177
178 static void
179 new_itemsets (state *s)
180 {
181 size_t i;
182
183 if (trace_flag & trace_automaton)
184 fprintf (stderr, "Entering new_itemsets, state = %d\n", s->number);
185
186 memset (kernel_size, 0, nsyms * sizeof *kernel_size);
187
188 nshifts = 0;
189
190 for (i = 0; i < nitemset; ++i)
191 if (item_number_is_symbol_number (ritem[itemset[i]]))
192 {
193 symbol_number sym = item_number_as_symbol_number (ritem[itemset[i]]);
194 if (!kernel_size[sym])
195 {
196 shift_symbol[nshifts] = sym;
197 nshifts++;
198 }
199
200 kernel_base[sym][kernel_size[sym]] = itemset[i] + 1;
201 kernel_size[sym]++;
202 }
203 }
204
205
206
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 `--------------------------------------------------------------*/
212
213 static state *
214 get_state (symbol_number sym, size_t core_size, item_number *core)
215 {
216 state *s;
217
218 if (trace_flag & trace_automaton)
219 fprintf (stderr, "Entering get_state, symbol = %d (%s)\n",
220 sym, symbols[sym]->tag);
221
222 s = state_hash_lookup (core_size, core);
223 if (!s)
224 s = state_list_append (sym, core_size, core);
225
226 if (trace_flag & trace_automaton)
227 fprintf (stderr, "Exiting get_state => %d\n", s->number);
228
229 return s;
230 }
231
232 /*---------------------------------------------------------------.
233 | Use the information computed by new_itemsets to find the state |
234 | numbers reached by each shift transition from S. |
235 | |
236 | SHIFTSET is set up as a vector of those states. |
237 `---------------------------------------------------------------*/
238
239 static void
240 append_states (state *s)
241 {
242 int i;
243
244 if (trace_flag & trace_automaton)
245 fprintf (stderr, "Entering append_states, state = %d\n", s->number);
246
247 /* First sort shift_symbol into increasing order. */
248
249 for (i = 1; i < nshifts; i++)
250 {
251 symbol_number sym = shift_symbol[i];
252 int j;
253 for (j = i; 0 < j && sym < shift_symbol[j - 1]; j--)
254 shift_symbol[j] = shift_symbol[j - 1];
255 shift_symbol[j] = sym;
256 }
257
258 for (i = 0; i < nshifts; i++)
259 {
260 symbol_number sym = shift_symbol[i];
261 shiftset[i] = get_state (sym, kernel_size[sym], kernel_base[sym]);
262 }
263 }
264
265
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
272 static void
273 save_reductions (state *s)
274 {
275 int count = 0;
276 size_t i;
277
278 /* Find and count the active items that represent ends of rules. */
279 for (i = 0; i < nitemset; ++i)
280 {
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. */
289 aver (!final_state);
290 final_state = s;
291 }
292 }
293 }
294
295 /* Make a reductions structure and copy the data into it. */
296 state_reductions_set (s, count, redset);
297 }
298
299 \f
300 /*---------------.
301 | Build STATES. |
302 `---------------*/
303
304 static void
305 set_states (void)
306 {
307 states = xcalloc (nstates, sizeof *states);
308
309 while (first_state)
310 {
311 state_list *this = first_state;
312
313 /* Pessimization, but simplification of the code: make sure all
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. */
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);
322
323 states[s->number] = s;
324
325 first_state = this->next;
326 free (this);
327 }
328 first_state = NULL;
329 last_state = NULL;
330 }
331
332
333 /*-------------------------------------------------------------------.
334 | Compute the nondeterministic finite state machine (see state.h for |
335 | details) from the grammar. |
336 `-------------------------------------------------------------------*/
337
338 void
339 generate_states (void)
340 {
341 item_number initial_core = 0;
342 state_list *list = NULL;
343 allocate_storage ();
344 new_closure (nritems);
345
346 /* Create the initial state. The 0 at the lhs is the index of the
347 item of this initial rule. */
348 state_list_append (0, 1, &initial_core);
349
350 /* States are queued when they are created; process them all. */
351 for (list = first_state; list; list = list->next)
352 {
353 state *s = list->state;
354 if (trace_flag & trace_automaton)
355 fprintf (stderr, "Processing state %d (reached by %s)\n",
356 s->number,
357 symbols[s->accessing_symbol]->tag);
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. */
360 closure (s->items, s->nitems);
361 /* Record the reductions allowed out of this state. */
362 save_reductions (s);
363 /* Find the itemsets of the states that shifts can reach. */
364 new_itemsets (s);
365 /* Find or create the core structures for those states. */
366 append_states (s);
367
368 /* Create the shifts structures for the shifts to those states,
369 now that the state numbers transitioning to are known. */
370 state_transitions_set (s, nshifts, shiftset);
371 }
372
373 /* discard various storage */
374 free_closure ();
375 free_storage ();
376
377 /* Set up STATES. */
378 set_states ();
379 }