]>
git.saurik.com Git - bison.git/blob - src/LR0.c
d402d910b3c55c8eb0d01c07d2506e0bdeabe06f
1 /* Generate the nondeterministic finite state machine for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
22 /* See comments in state.h for the data structures that represent it.
23 The entry point is generate_states. */
40 typedef struct state_list_s
42 struct state_list_s
*next
;
46 static state_list_t
*first_state
= NULL
;
47 static state_list_t
*last_state
= NULL
;
50 state_list_append (state_t
*state
)
52 state_list_t
*node
= XMALLOC (state_list_t
, 1);
59 last_state
->next
= node
;
64 static symbol_number_t
*shift_symbol
= NULL
;
66 static short *redset
= NULL
;
67 static state_number_t
*shiftset
= NULL
;
69 static item_number_t
**kernel_base
= NULL
;
70 static int *kernel_size
= NULL
;
71 static item_number_t
*kernel_items
= NULL
;
75 allocate_itemsets (void)
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
85 short *symbol_count
= XCALLOC (short, nsyms
+ nuseless_nonterminals
);
87 for (r
= 1; r
< nrules
+ 1; ++r
)
88 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; ++rhsp
)
91 symbol_count
[*rhsp
]++;
94 /* See comments before new_itemsets. All the vectors of items
95 live inside KERNEL_ITEMS. The number of active items after
96 some symbol cannot be more than the number of times that symbol
97 appears as an item, which is SYMBOL_COUNT[SYMBOL].
98 We allocate that much space for each symbol. */
100 kernel_base
= XCALLOC (item_number_t
*, nsyms
);
102 kernel_items
= XCALLOC (item_number_t
, count
);
105 for (i
= 0; i
< nsyms
; i
++)
107 kernel_base
[i
] = kernel_items
+ count
;
108 count
+= symbol_count
[i
];
112 kernel_size
= XCALLOC (int, nsyms
);
117 allocate_storage (void)
119 allocate_itemsets ();
121 shiftset
= XCALLOC (state_number_t
, nsyms
);
122 redset
= XCALLOC (short, nrules
+ 1);
124 shift_symbol
= XCALLOC (symbol_number_t
, nsyms
);
136 XFREE (kernel_items
);
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. |
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 `---------------------------------------------------------------*/
155 new_itemsets (state_t
*state
)
160 fprintf (stderr
, "Entering new_itemsets, state = %d\n",
163 for (i
= 0; i
< nsyms
; i
++)
168 for (i
= 0; i
< nritemset
; ++i
)
169 if (ritem
[itemset
[i
]] >= 0)
171 symbol_number_t symbol
172 = item_number_as_symbol_number (ritem
[itemset
[i
]]);
173 if (!kernel_size
[symbol
])
175 shift_symbol
[nshifts
] = symbol
;
179 kernel_base
[symbol
][kernel_size
[symbol
]] = itemset
[i
] + 1;
180 kernel_size
[symbol
]++;
186 /*-----------------------------------------------------------------.
187 | Subroutine of get_state. Create a new state for those items, if |
189 `-----------------------------------------------------------------*/
192 new_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
197 fprintf (stderr
, "Entering new_state, state = %d, symbol = %d (%s)\n",
198 nstates
, symbol
, symbol_tag_get (symbols
[symbol
]));
200 res
= state_new (symbol
, core_size
, core
);
201 state_hash_insert (res
);
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
)
208 state_list_append (res
);
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 |
216 | equivalent one exists already. Used by append_states. |
217 `--------------------------------------------------------------*/
219 static state_number_t
220 get_state (symbol_number_t symbol
, size_t core_size
, item_number_t
*core
)
225 fprintf (stderr
, "Entering get_state, symbol = %d (%s)\n",
226 symbol
, symbol_tag_get (symbols
[symbol
]));
228 sp
= state_hash_lookup (core_size
, core
);
230 sp
= new_state (symbol
, core_size
, core
);
233 fprintf (stderr
, "Exiting get_state => %d\n", sp
->number
);
238 /*------------------------------------------------------------------.
239 | Use the information computed by new_itemsets to find the state |
240 | numbers reached by each shift transition from STATE. |
242 | SHIFTSET is set up as a vector of state numbers of those states. |
243 `------------------------------------------------------------------*/
246 append_states (state_t
*state
)
250 symbol_number_t symbol
;
253 fprintf (stderr
, "Entering append_states, state = %d\n",
256 /* first sort shift_symbol into increasing order */
258 for (i
= 1; i
< nshifts
; i
++)
260 symbol
= shift_symbol
[i
];
262 while (j
> 0 && shift_symbol
[j
- 1] > symbol
)
264 shift_symbol
[j
] = shift_symbol
[j
- 1];
267 shift_symbol
[j
] = symbol
;
270 for (i
= 0; i
< nshifts
; i
++)
272 symbol
= shift_symbol
[i
];
273 shiftset
[i
] = get_state (symbol
,
274 kernel_size
[symbol
], kernel_base
[symbol
]);
282 /* The 0 at the lhs is the index of the item of this initial rule. */
283 kernel_base
[0][0] = 0;
285 state_list_append (new_state (0, kernel_size
[0], kernel_base
[0]));
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 `----------------------------------------------------------------*/
297 save_reductions (state_t
*state
)
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 .'. */
304 if (final_state
&& state
->number
== final_state
->number
)
307 /* Find and count the active items that represent ends of rules. */
308 for (i
= 0; i
< nritemset
; ++i
)
310 int item
= ritem
[itemset
[i
]];
312 redset
[count
++] = -item
;
315 /* Make a reductions structure and copy the data into it. */
316 state_reductions_set (state
, count
, redset
);
327 states
= XCALLOC (state_t
*, nstates
);
331 state_list_t
*this = first_state
;
333 /* Pessimization, but simplification of the code: make sure all
334 the states have a shifts, errs, and reductions, even if
336 state_t
*state
= this->state
;
338 state_shifts_set (state
, 0, 0);
340 state
->errs
= errs_new (0);
341 if (!state
->reductions
)
342 state_reductions_set (state
, 0, 0);
344 states
[state
->number
] = state
;
346 first_state
= this->next
;
354 /*-------------------------------------------------------------------.
355 | Compute the nondeterministic finite state machine (see state.h for |
356 | details) from the grammar. |
357 `-------------------------------------------------------------------*/
360 generate_states (void)
362 state_list_t
*list
= NULL
;
364 new_closure (nritems
);
370 state_t
*state
= list
->state
;
372 fprintf (stderr
, "Processing state %d (reached by %s)\n",
374 symbol_tag_get (symbols
[state
->accessing_symbol
]));
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
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
);
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
);
391 /* States are queued when they are created; process them all.
396 /* discard various storage */