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