]> git.saurik.com Git - bison.git/blob - src/LR0.c
* src/state.h, src/state.c (transitions_t): Holds state_t*'s, not
[bison.git] / src / LR0.c
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001, 2002 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 "bitset.h"
27 #include "quotearg.h"
28 #include "symtab.h"
29 #include "gram.h"
30 #include "getargs.h"
31 #include "reader.h"
32 #include "gram.h"
33 #include "state.h"
34 #include "complain.h"
35 #include "closure.h"
36 #include "LR0.h"
37 #include "lalr.h"
38 #include "reduce.h"
39
40 typedef struct state_list_s
41 {
42 struct state_list_s *next;
43 state_t *state;
44 } state_list_t;
45
46 static state_list_t *first_state = NULL;
47 static state_list_t *last_state = NULL;
48
49
50 /*------------------------------------------------------------------.
51 | A state was just discovered from another state. Queue it for |
52 | later examination, in order to find its transitions. Return it. |
53 `------------------------------------------------------------------*/
54
55 static state_t *
56 state_list_append (symbol_number_t symbol,
57 size_t core_size, item_number_t *core)
58 {
59 state_list_t *node = XMALLOC (state_list_t, 1);
60 state_t *state = state_new (symbol, core_size, core);
61
62 if (trace_flag)
63 fprintf (stderr, "state_list_append (state = %d, symbol = %d (%s))\n",
64 nstates, symbol, symbols[symbol]->tag);
65
66 /* If this is the endtoken, and this is not the initial state, then
67 this is the final state. */
68 if (symbol == 0 && first_state)
69 final_state = state;
70
71 node->next = NULL;
72 node->state = state;
73
74 if (!first_state)
75 first_state = node;
76 if (last_state)
77 last_state->next = node;
78 last_state = node;
79
80 return state;
81 }
82
83 static int nshifts;
84 static symbol_number_t *shift_symbol = NULL;
85
86 static rule_t **redset = NULL;
87 static state_t **shiftset = NULL;
88
89 static item_number_t **kernel_base = NULL;
90 static int *kernel_size = NULL;
91 static item_number_t *kernel_items = NULL;
92
93 \f
94 static void
95 allocate_itemsets (void)
96 {
97 symbol_number_t i;
98 rule_number_t r;
99 item_number_t *rhsp;
100
101 /* Count the number of occurrences of all the symbols in RITEMS.
102 Note that useless productions (hence useless nonterminals) are
103 browsed too, hence we need to allocate room for _all_ the
104 symbols. */
105 int count = 0;
106 short *symbol_count = XCALLOC (short, nsyms + nuseless_nonterminals);
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 cannot be more than the number of times that symbol
118 appears as an item, which is SYMBOL_COUNT[SYMBOL].
119 We allocate that much space for each symbol. */
120
121 kernel_base = XCALLOC (item_number_t *, nsyms);
122 if (count)
123 kernel_items = XCALLOC (item_number_t, count);
124
125 count = 0;
126 for (i = 0; i < nsyms; i++)
127 {
128 kernel_base[i] = kernel_items + count;
129 count += symbol_count[i];
130 }
131
132 free (symbol_count);
133 kernel_size = XCALLOC (int, nsyms);
134 }
135
136
137 static void
138 allocate_storage (void)
139 {
140 allocate_itemsets ();
141
142 shiftset = XCALLOC (state_t *, nsyms);
143 redset = XCALLOC (rule_t *, nrules);
144 state_hash_new ();
145 shift_symbol = XCALLOC (symbol_number_t, nsyms);
146 }
147
148
149 static void
150 free_storage (void)
151 {
152 free (shift_symbol);
153 free (redset);
154 free (shiftset);
155 free (kernel_base);
156 free (kernel_size);
157 XFREE (kernel_items);
158 state_hash_free ();
159 }
160
161
162
163
164 /*---------------------------------------------------------------.
165 | Find which symbols can be shifted in STATE, and for each one |
166 | record which items would be active after that shift. Uses the |
167 | contents of itemset. |
168 | |
169 | shift_symbol is set to a vector of the symbols that can be |
170 | shifted. For each symbol in the grammar, kernel_base[symbol] |
171 | points to a vector of item numbers activated if that symbol is |
172 | shifted, and kernel_size[symbol] is their numbers. |
173 `---------------------------------------------------------------*/
174
175 static void
176 new_itemsets (state_t *state)
177 {
178 int i;
179
180 if (trace_flag)
181 fprintf (stderr, "Entering new_itemsets, state = %d\n",
182 state->number);
183
184 for (i = 0; i < nsyms; i++)
185 kernel_size[i] = 0;
186
187 nshifts = 0;
188
189 for (i = 0; i < nritemset; ++i)
190 if (ritem[itemset[i]] >= 0)
191 {
192 symbol_number_t symbol
193 = item_number_as_symbol_number (ritem[itemset[i]]);
194 if (!kernel_size[symbol])
195 {
196 shift_symbol[nshifts] = symbol;
197 nshifts++;
198 }
199
200 kernel_base[symbol][kernel_size[symbol]] = itemset[i] + 1;
201 kernel_size[symbol]++;
202 }
203 }
204
205
206
207 /*-----------------------------------------------------------------.
208 | Find the state we would get to (from the current state) by |
209 | shifting SYMBOL. Create a new state if no equivalent one exists |
210 | already. Used by append_states. |
211 `-----------------------------------------------------------------*/
212
213 static state_t *
214 get_state (symbol_number_t symbol, size_t core_size, item_number_t *core)
215 {
216 state_t *sp;
217
218 if (trace_flag)
219 fprintf (stderr, "Entering get_state, symbol = %d (%s)\n",
220 symbol, symbols[symbol]->tag);
221
222 sp = state_hash_lookup (core_size, core);
223 if (!sp)
224 sp = state_list_append (symbol, core_size, core);
225
226 if (trace_flag)
227 fprintf (stderr, "Exiting get_state => %d\n", sp->number);
228
229 return sp;
230 }
231
232 /*---------------------------------------------------------------.
233 | Use the information computed by new_itemsets to find the state |
234 | numbers reached by each shift transition from STATE. |
235 | |
236 | SHIFTSET is set up as a vector of those states. |
237 `---------------------------------------------------------------*/
238
239 static void
240 append_states (state_t *state)
241 {
242 int i;
243 int j;
244 symbol_number_t symbol;
245
246 if (trace_flag)
247 fprintf (stderr, "Entering append_states, state = %d\n",
248 state->number);
249
250 /* first sort shift_symbol into increasing order */
251
252 for (i = 1; i < nshifts; i++)
253 {
254 symbol = shift_symbol[i];
255 j = i;
256 while (j > 0 && shift_symbol[j - 1] > symbol)
257 {
258 shift_symbol[j] = shift_symbol[j - 1];
259 j--;
260 }
261 shift_symbol[j] = symbol;
262 }
263
264 for (i = 0; i < nshifts; i++)
265 {
266 symbol = shift_symbol[i];
267 shiftset[i] = get_state (symbol,
268 kernel_size[symbol], kernel_base[symbol]);
269 }
270 }
271
272
273 /*----------------------------------------------------------------.
274 | Find which rules can be used for reduction transitions from the |
275 | current state and make a reductions structure for the state to |
276 | record their rule numbers. |
277 `----------------------------------------------------------------*/
278
279 static void
280 save_reductions (state_t *state)
281 {
282 int count = 0;
283 int i;
284
285 /* If this is the final state, we want it to have no reductions at
286 all, although it has one for `START_SYMBOL $end .'. */
287 if (final_state && state->number == final_state->number)
288 return;
289
290 /* Find and count the active items that represent ends of rules. */
291 for (i = 0; i < nritemset; ++i)
292 {
293 int item = ritem[itemset[i]];
294 if (item < 0)
295 redset[count++] = &rules[item_number_as_rule_number (item)];
296 }
297
298 /* Make a reductions structure and copy the data into it. */
299 state_reductions_set (state, count, redset);
300 }
301
302 \f
303 /*---------------.
304 | Build STATES. |
305 `---------------*/
306
307 static void
308 set_states (void)
309 {
310 states = XCALLOC (state_t *, nstates);
311
312 while (first_state)
313 {
314 state_list_t *this = first_state;
315
316 /* Pessimization, but simplification of the code: make sure all
317 the states have valid transitions and reductions members,
318 even if reduced to 0. It is too soon for errs, which are
319 computed later, but set_conflicts. */
320 state_t *state = this->state;
321 if (!state->transitions)
322 state_transitions_set (state, 0, 0);
323 if (!state->reductions)
324 state_reductions_set (state, 0, 0);
325
326 states[state->number] = state;
327
328 first_state = this->next;
329 free (this);
330 }
331 first_state = NULL;
332 last_state = NULL;
333 }
334
335
336 /*-------------------------------------------------------------------.
337 | Compute the nondeterministic finite state machine (see state.h for |
338 | details) from the grammar. |
339 `-------------------------------------------------------------------*/
340
341 void
342 generate_states (void)
343 {
344 state_list_t *list = NULL;
345 allocate_storage ();
346 new_closure (nritems);
347
348 /* Create the initial state. The 0 at the lhs is the index of the
349 item of this initial rule. */
350 kernel_base[0][0] = 0;
351 kernel_size[0] = 1;
352 state_list_append (0, kernel_size[0], kernel_base[0]);
353
354 list = first_state;
355
356 while (list)
357 {
358 state_t *state = list->state;
359 if (trace_flag)
360 fprintf (stderr, "Processing state %d (reached by %s)\n",
361 state->number,
362 symbols[state->accessing_symbol]->tag);
363 /* Set up ruleset and itemset for the transitions out of this
364 state. ruleset gets a 1 bit for each rule that could reduce
365 now. itemset gets a vector of all the items that could be
366 accepted next. */
367 closure (state->items, state->nitems);
368 /* Record the reductions allowed out of this state. */
369 save_reductions (state);
370 /* Find the itemsets of the states that shifts can reach. */
371 new_itemsets (state);
372 /* Find or create the core structures for those states. */
373 append_states (state);
374
375 /* Create the shifts structures for the shifts to those states,
376 now that the state numbers transitioning to are known. */
377 state_transitions_set (state, nshifts, shiftset);
378
379 /* States are queued when they are created; process them all.
380 */
381 list = list->next;
382 }
383
384 /* discard various storage */
385 free_closure ();
386 free_storage ();
387
388 /* Set up STATES. */
389 set_states ();
390 }