]>
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 | ||
8b752b00 AD |
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) | |
32e1e0a4 AD |
58 | { |
59 | state_list_t *node = XMALLOC (state_list_t, 1); | |
8b752b00 AD |
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 | ||
88bce5a2 | 66 | /* If this is the endtoken, and this is not the initial state, then |
8b752b00 AD |
67 | this is the final state. */ |
68 | if (symbol == 0 && first_state) | |
69 | final_state = state; | |
70 | ||
32e1e0a4 AD |
71 | node->next = NULL; |
72 | node->state = state; | |
40675e7c | 73 | |
32e1e0a4 AD |
74 | if (!first_state) |
75 | first_state = node; | |
76 | if (last_state) | |
77 | last_state->next = node; | |
78 | last_state = node; | |
8b752b00 AD |
79 | |
80 | return state; | |
32e1e0a4 | 81 | } |
40675e7c DM |
82 | |
83 | static int nshifts; | |
a49aecd5 | 84 | static symbol_number_t *shift_symbol = NULL; |
40675e7c | 85 | |
640748ee AD |
86 | static rule_t **redset = NULL; |
87 | static state_t **shiftset = NULL; | |
40675e7c | 88 | |
62a3e4f0 | 89 | static item_number_t **kernel_base = NULL; |
6255b435 | 90 | static int *kernel_size = NULL; |
62a3e4f0 | 91 | static item_number_t *kernel_items = NULL; |
40675e7c | 92 | |
2fa6973e | 93 | \f |
4a120d45 | 94 | static void |
d2729d44 | 95 | allocate_itemsets (void) |
40675e7c | 96 | { |
9222837b AD |
97 | symbol_number_t i; |
98 | rule_number_t r; | |
62a3e4f0 | 99 | item_number_t *rhsp; |
40675e7c | 100 | |
630e182b AD |
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); | |
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 | |
40675e7c | 117 | some symbol cannot be more than the number of times that symbol |
8a731ca8 | 118 | appears as an item, which is SYMBOL_COUNT[SYMBOL]. |
40675e7c DM |
119 | We allocate that much space for each symbol. */ |
120 | ||
62a3e4f0 | 121 | kernel_base = XCALLOC (item_number_t *, nsyms); |
342b8b6e | 122 | if (count) |
62a3e4f0 | 123 | kernel_items = XCALLOC (item_number_t, count); |
40675e7c DM |
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 | ||
630e182b | 132 | free (symbol_count); |
0e41b407 | 133 | kernel_size = XCALLOC (int, nsyms); |
40675e7c DM |
134 | } |
135 | ||
136 | ||
4a120d45 | 137 | static void |
d2729d44 | 138 | allocate_storage (void) |
40675e7c | 139 | { |
2fa6973e | 140 | allocate_itemsets (); |
40675e7c | 141 | |
640748ee AD |
142 | shiftset = XCALLOC (state_t *, nsyms); |
143 | redset = XCALLOC (rule_t *, nrules); | |
c7ca99d4 | 144 | state_hash_new (); |
a49aecd5 | 145 | shift_symbol = XCALLOC (symbol_number_t, nsyms); |
40675e7c DM |
146 | } |
147 | ||
148 | ||
4a120d45 | 149 | static void |
d2729d44 | 150 | free_storage (void) |
40675e7c | 151 | { |
630e182b AD |
152 | free (shift_symbol); |
153 | free (redset); | |
154 | free (shiftset); | |
155 | free (kernel_base); | |
156 | free (kernel_size); | |
d7913476 | 157 | XFREE (kernel_items); |
c7ca99d4 | 158 | state_hash_free (); |
40675e7c DM |
159 | } |
160 | ||
161 | ||
162 | ||
40675e7c | 163 | |
32e1e0a4 AD |
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 | `---------------------------------------------------------------*/ | |
40675e7c | 174 | |
4a120d45 | 175 | static void |
32e1e0a4 | 176 | new_itemsets (state_t *state) |
40675e7c | 177 | { |
2fa6973e | 178 | int i; |
2fa6973e | 179 | |
9bfe901c | 180 | if (trace_flag) |
c87d4863 | 181 | fprintf (stderr, "Entering new_itemsets, state = %d\n", |
32e1e0a4 | 182 | state->number); |
40675e7c DM |
183 | |
184 | for (i = 0; i < nsyms; i++) | |
125ecb56 | 185 | kernel_size[i] = 0; |
40675e7c | 186 | |
b2872512 | 187 | nshifts = 0; |
40675e7c | 188 | |
5123689b | 189 | for (i = 0; i < nritemset; ++i) |
5fbb0954 AD |
190 | if (ritem[itemset[i]] >= 0) |
191 | { | |
a49aecd5 AD |
192 | symbol_number_t symbol |
193 | = item_number_as_symbol_number (ritem[itemset[i]]); | |
5fbb0954 AD |
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 | } | |
40675e7c DM |
203 | } |
204 | ||
205 | ||
206 | ||
640748ee AD |
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 | `-----------------------------------------------------------------*/ | |
40675e7c | 212 | |
640748ee | 213 | static state_t * |
a49aecd5 | 214 | get_state (symbol_number_t symbol, size_t core_size, item_number_t *core) |
40675e7c | 215 | { |
f693ad14 | 216 | state_t *sp; |
40675e7c | 217 | |
9bfe901c | 218 | if (trace_flag) |
32e1e0a4 | 219 | fprintf (stderr, "Entering get_state, symbol = %d (%s)\n", |
97650f4e | 220 | symbol, symbols[symbol]->tag); |
40675e7c | 221 | |
c7ca99d4 AD |
222 | sp = state_hash_lookup (core_size, core); |
223 | if (!sp) | |
8b752b00 | 224 | sp = state_list_append (symbol, core_size, core); |
40675e7c | 225 | |
c87d4863 AD |
226 | if (trace_flag) |
227 | fprintf (stderr, "Exiting get_state => %d\n", sp->number); | |
228 | ||
640748ee | 229 | return sp; |
40675e7c DM |
230 | } |
231 | ||
640748ee AD |
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 | `---------------------------------------------------------------*/ | |
40675e7c | 238 | |
2fa6973e | 239 | static void |
32e1e0a4 | 240 | append_states (state_t *state) |
40675e7c | 241 | { |
2fa6973e AD |
242 | int i; |
243 | int j; | |
a49aecd5 | 244 | symbol_number_t symbol; |
40675e7c | 245 | |
9bfe901c | 246 | if (trace_flag) |
c87d4863 | 247 | fprintf (stderr, "Entering append_states, state = %d\n", |
32e1e0a4 | 248 | state->number); |
40675e7c | 249 | |
2fa6973e | 250 | /* first sort shift_symbol into increasing order */ |
40675e7c | 251 | |
2fa6973e AD |
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 | } | |
40675e7c | 263 | |
2fa6973e | 264 | for (i = 0; i < nshifts; i++) |
458be8e0 AD |
265 | { |
266 | symbol = shift_symbol[i]; | |
267 | shiftset[i] = get_state (symbol, | |
268 | kernel_size[symbol], kernel_base[symbol]); | |
269 | } | |
40675e7c DM |
270 | } |
271 | ||
272 | ||
2fa6973e AD |
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 | ||
4a120d45 | 279 | static void |
32e1e0a4 | 280 | save_reductions (state_t *state) |
40675e7c | 281 | { |
30171f79 | 282 | int count = 0; |
fb908786 | 283 | int i; |
40675e7c | 284 | |
30171f79 | 285 | /* If this is the final state, we want it to have no reductions at |
88bce5a2 | 286 | all, although it has one for `START_SYMBOL $end .'. */ |
32e1e0a4 | 287 | if (final_state && state->number == final_state->number) |
30171f79 | 288 | return; |
40675e7c | 289 | |
30171f79 | 290 | /* Find and count the active items that represent ends of rules. */ |
5123689b | 291 | for (i = 0; i < nritemset; ++i) |
2fa6973e | 292 | { |
fb908786 | 293 | int item = ritem[itemset[i]]; |
2fa6973e | 294 | if (item < 0) |
640748ee | 295 | redset[count++] = &rules[item_number_as_rule_number (item)]; |
2fa6973e | 296 | } |
40675e7c | 297 | |
2fa6973e | 298 | /* Make a reductions structure and copy the data into it. */ |
8a731ca8 | 299 | state_reductions_set (state, count, redset); |
2fa6973e AD |
300 | } |
301 | ||
302 | \f | |
82841af7 | 303 | /*---------------. |
29e88316 | 304 | | Build STATES. | |
82841af7 | 305 | `---------------*/ |
6a164e0c AD |
306 | |
307 | static void | |
29e88316 | 308 | set_states (void) |
6a164e0c | 309 | { |
29e88316 | 310 | states = XCALLOC (state_t *, nstates); |
6a164e0c | 311 | |
32e1e0a4 | 312 | while (first_state) |
2cec70b9 | 313 | { |
32e1e0a4 AD |
314 | state_list_t *this = first_state; |
315 | ||
2cec70b9 | 316 | /* Pessimization, but simplification of the code: make sure all |
8b752b00 AD |
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. */ | |
32e1e0a4 | 320 | state_t *state = this->state; |
8b752b00 | 321 | if (!state->transitions) |
ccaf65bc | 322 | state_transitions_set (state, 0, 0); |
32e1e0a4 | 323 | if (!state->reductions) |
8a731ca8 | 324 | state_reductions_set (state, 0, 0); |
32e1e0a4 AD |
325 | |
326 | states[state->number] = state; | |
327 | ||
328 | first_state = this->next; | |
329 | free (this); | |
2cec70b9 | 330 | } |
32e1e0a4 AD |
331 | first_state = NULL; |
332 | last_state = NULL; | |
6a164e0c AD |
333 | } |
334 | ||
c7ca99d4 | 335 | |
2fa6973e AD |
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 | { | |
32e1e0a4 | 344 | state_list_t *list = NULL; |
2fa6973e | 345 | allocate_storage (); |
9e7f6bbd | 346 | new_closure (nritems); |
8b752b00 AD |
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 | ||
32e1e0a4 | 354 | list = first_state; |
2fa6973e | 355 | |
32e1e0a4 | 356 | while (list) |
2fa6973e | 357 | { |
32e1e0a4 | 358 | state_t *state = list->state; |
23cbcc6c AD |
359 | if (trace_flag) |
360 | fprintf (stderr, "Processing state %d (reached by %s)\n", | |
32e1e0a4 | 361 | state->number, |
97650f4e | 362 | symbols[state->accessing_symbol]->tag); |
2fa6973e AD |
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. */ | |
32e1e0a4 AD |
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. */ | |
ccaf65bc | 377 | state_transitions_set (state, nshifts, shiftset); |
32e1e0a4 AD |
378 | |
379 | /* States are queued when they are created; process them all. | |
380 | */ | |
381 | list = list->next; | |
2fa6973e AD |
382 | } |
383 | ||
384 | /* discard various storage */ | |
385 | free_closure (); | |
386 | free_storage (); | |
387 | ||
29e88316 AD |
388 | /* Set up STATES. */ |
389 | set_states (); | |
40675e7c | 390 | } |