]>
Commit | Line | Data |
---|---|---|
1 | /* Compute look-ahead criteria for bison, | |
2 | Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002 | |
3 | Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | Bison is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | Bison is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with Bison; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | ||
23 | /* Compute how to make the finite state machine deterministic; find | |
24 | which rules need lookahead in each state, and which lookahead | |
25 | tokens they accept. */ | |
26 | ||
27 | #include "system.h" | |
28 | #include "bitset.h" | |
29 | #include "bitsetv.h" | |
30 | #include "relation.h" | |
31 | #include "quotearg.h" | |
32 | #include "symtab.h" | |
33 | #include "gram.h" | |
34 | #include "reader.h" | |
35 | #include "LR0.h" | |
36 | #include "complain.h" | |
37 | #include "lalr.h" | |
38 | #include "nullable.h" | |
39 | #include "derives.h" | |
40 | #include "getargs.h" | |
41 | ||
42 | goto_number_t *goto_map = NULL; | |
43 | static goto_number_t ngotos = 0; | |
44 | state_number_t *from_state = NULL; | |
45 | state_number_t *to_state = NULL; | |
46 | ||
47 | /* Linked list of goto numbers. */ | |
48 | typedef struct goto_list_s | |
49 | { | |
50 | struct goto_list_s *next; | |
51 | goto_number_t value; | |
52 | } goto_list_t; | |
53 | ||
54 | ||
55 | /* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule | |
56 | LArule[l] is applicable in the appropriate state when the next | |
57 | token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j, | |
58 | it is a conflict. */ | |
59 | ||
60 | static bitsetv LA = NULL; | |
61 | size_t nLA; | |
62 | ||
63 | ||
64 | /* And for the famous F variable, which name is so descriptive that a | |
65 | comment is hardly needed. <grin>. */ | |
66 | static bitsetv F = NULL; | |
67 | ||
68 | static goto_number_t **includes; | |
69 | static goto_list_t **lookback; | |
70 | ||
71 | ||
72 | ||
73 | ||
74 | static void | |
75 | set_goto_map (void) | |
76 | { | |
77 | state_number_t state; | |
78 | goto_number_t *temp_map; | |
79 | ||
80 | goto_map = XCALLOC (goto_number_t, nvars + 1) - ntokens; | |
81 | temp_map = XCALLOC (goto_number_t, nvars + 1) - ntokens; | |
82 | ||
83 | ngotos = 0; | |
84 | for (state = 0; state < nstates; ++state) | |
85 | { | |
86 | transitions_t *sp = states[state]->transitions; | |
87 | int i; | |
88 | for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i) | |
89 | { | |
90 | if (ngotos >= GOTO_NUMBER_MAX) | |
91 | abort (); | |
92 | ngotos++; | |
93 | goto_map[TRANSITION_SYMBOL (sp, i)]++; | |
94 | } | |
95 | } | |
96 | ||
97 | { | |
98 | int k = 0; | |
99 | int i; | |
100 | for (i = ntokens; i < nsyms; i++) | |
101 | { | |
102 | temp_map[i] = k; | |
103 | k += goto_map[i]; | |
104 | } | |
105 | ||
106 | for (i = ntokens; i < nsyms; i++) | |
107 | goto_map[i] = temp_map[i]; | |
108 | ||
109 | goto_map[nsyms] = ngotos; | |
110 | temp_map[nsyms] = ngotos; | |
111 | } | |
112 | ||
113 | from_state = XCALLOC (state_number_t, ngotos); | |
114 | to_state = XCALLOC (state_number_t, ngotos); | |
115 | ||
116 | for (state = 0; state < nstates; ++state) | |
117 | { | |
118 | transitions_t *sp = states[state]->transitions; | |
119 | int i; | |
120 | for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i) | |
121 | { | |
122 | int k = temp_map[TRANSITION_SYMBOL (sp, i)]++; | |
123 | from_state[k] = state; | |
124 | to_state[k] = sp->states[i]->number; | |
125 | } | |
126 | } | |
127 | ||
128 | XFREE (temp_map + ntokens); | |
129 | } | |
130 | ||
131 | ||
132 | ||
133 | /*----------------------------------------------------------. | |
134 | | Map a state/symbol pair into its numeric representation. | | |
135 | `----------------------------------------------------------*/ | |
136 | ||
137 | static int | |
138 | map_goto (state_number_t state, symbol_number_t symbol) | |
139 | { | |
140 | int high; | |
141 | int low; | |
142 | int middle; | |
143 | state_number_t s; | |
144 | ||
145 | low = goto_map[symbol]; | |
146 | high = goto_map[symbol + 1] - 1; | |
147 | ||
148 | for (;;) | |
149 | { | |
150 | if (high < low) | |
151 | abort (); | |
152 | middle = (low + high) / 2; | |
153 | s = from_state[middle]; | |
154 | if (s == state) | |
155 | return middle; | |
156 | else if (s < state) | |
157 | low = middle + 1; | |
158 | else | |
159 | high = middle - 1; | |
160 | } | |
161 | } | |
162 | ||
163 | ||
164 | static void | |
165 | initialize_F (void) | |
166 | { | |
167 | goto_number_t **reads = XCALLOC (goto_number_t *, ngotos); | |
168 | goto_number_t *edge = XCALLOC (goto_number_t, ngotos + 1); | |
169 | int nedges = 0; | |
170 | ||
171 | int i; | |
172 | ||
173 | F = bitsetv_create (ngotos, ntokens, BITSET_FIXED); | |
174 | ||
175 | for (i = 0; i < ngotos; i++) | |
176 | { | |
177 | state_number_t stateno = to_state[i]; | |
178 | transitions_t *sp = states[stateno]->transitions; | |
179 | ||
180 | int j; | |
181 | FOR_EACH_SHIFT (sp, j) | |
182 | bitset_set (F[i], TRANSITION_SYMBOL (sp, j)); | |
183 | ||
184 | for (; j < sp->num; j++) | |
185 | { | |
186 | symbol_number_t symbol = TRANSITION_SYMBOL (sp, j); | |
187 | if (nullable[symbol]) | |
188 | edge[nedges++] = map_goto (stateno, symbol); | |
189 | } | |
190 | ||
191 | if (nedges) | |
192 | { | |
193 | reads[i] = XCALLOC (goto_number_t, nedges + 1); | |
194 | memcpy (reads[i], edge, nedges * sizeof (edge[0])); | |
195 | reads[i][nedges] = -1; | |
196 | nedges = 0; | |
197 | } | |
198 | } | |
199 | ||
200 | relation_digraph (reads, ngotos, &F); | |
201 | ||
202 | for (i = 0; i < ngotos; i++) | |
203 | XFREE (reads[i]); | |
204 | ||
205 | XFREE (reads); | |
206 | XFREE (edge); | |
207 | } | |
208 | ||
209 | ||
210 | static void | |
211 | add_lookback_edge (state_t *state, rule_t *rule, int gotono) | |
212 | { | |
213 | int r = state_reduction_find (state, rule); | |
214 | goto_list_t *sp = XCALLOC (goto_list_t, 1); | |
215 | sp->next = lookback[(state->reductions->lookaheads - LA) + r]; | |
216 | sp->value = gotono; | |
217 | lookback[(state->reductions->lookaheads - LA) + r] = sp; | |
218 | } | |
219 | ||
220 | ||
221 | ||
222 | static void | |
223 | build_relations (void) | |
224 | { | |
225 | goto_number_t *edge = XCALLOC (goto_number_t, ngotos + 1); | |
226 | state_number_t *states1 = XCALLOC (state_number_t, ritem_longest_rhs () + 1); | |
227 | int i; | |
228 | ||
229 | includes = XCALLOC (goto_number_t *, ngotos); | |
230 | ||
231 | for (i = 0; i < ngotos; i++) | |
232 | { | |
233 | int nedges = 0; | |
234 | symbol_number_t symbol1 = states[to_state[i]]->accessing_symbol; | |
235 | rule_t **rulep; | |
236 | ||
237 | for (rulep = derives[symbol1]; *rulep; rulep++) | |
238 | { | |
239 | int done; | |
240 | int length = 1; | |
241 | item_number_t *rp; | |
242 | state_t *state = states[from_state[i]]; | |
243 | states1[0] = state->number; | |
244 | ||
245 | for (rp = (*rulep)->rhs; *rp >= 0; rp++) | |
246 | { | |
247 | state = transitions_to (state->transitions, | |
248 | item_number_as_symbol_number (*rp)); | |
249 | states1[length++] = state->number; | |
250 | } | |
251 | ||
252 | if (!state->consistent) | |
253 | add_lookback_edge (state, *rulep, i); | |
254 | ||
255 | length--; | |
256 | done = 0; | |
257 | while (!done) | |
258 | { | |
259 | done = 1; | |
260 | rp--; | |
261 | /* JF added rp>=ritem && I hope to god its right! */ | |
262 | if (rp >= ritem && ISVAR (*rp)) | |
263 | { | |
264 | /* Downcasting from item_number_t to symbol_number_t. */ | |
265 | edge[nedges++] = map_goto (states1[--length], | |
266 | item_number_as_symbol_number (*rp)); | |
267 | if (nullable[*rp]) | |
268 | done = 0; | |
269 | } | |
270 | } | |
271 | } | |
272 | ||
273 | if (nedges) | |
274 | { | |
275 | int j; | |
276 | includes[i] = XCALLOC (goto_number_t, nedges + 1); | |
277 | for (j = 0; j < nedges; j++) | |
278 | includes[i][j] = edge[j]; | |
279 | includes[i][nedges] = -1; | |
280 | } | |
281 | } | |
282 | ||
283 | XFREE (edge); | |
284 | XFREE (states1); | |
285 | ||
286 | relation_transpose (&includes, ngotos); | |
287 | } | |
288 | ||
289 | ||
290 | ||
291 | static void | |
292 | compute_FOLLOWS (void) | |
293 | { | |
294 | int i; | |
295 | ||
296 | relation_digraph (includes, ngotos, &F); | |
297 | ||
298 | for (i = 0; i < ngotos; i++) | |
299 | XFREE (includes[i]); | |
300 | ||
301 | XFREE (includes); | |
302 | } | |
303 | ||
304 | ||
305 | static void | |
306 | compute_lookaheads (void) | |
307 | { | |
308 | size_t i; | |
309 | goto_list_t *sp; | |
310 | ||
311 | for (i = 0; i < nLA; i++) | |
312 | for (sp = lookback[i]; sp; sp = sp->next) | |
313 | bitset_or (LA[i], LA[i], F[sp->value]); | |
314 | ||
315 | /* Free LOOKBACK. */ | |
316 | for (i = 0; i < nLA; i++) | |
317 | LIST_FREE (goto_list_t, lookback[i]); | |
318 | ||
319 | XFREE (lookback); | |
320 | bitsetv_free (F); | |
321 | } | |
322 | ||
323 | ||
324 | /*---------------------------------------------------------------. | |
325 | | Count the number of lookaheads required for STATE (NLOOKAHEADS | | |
326 | | member). | | |
327 | `---------------------------------------------------------------*/ | |
328 | ||
329 | static int | |
330 | state_lookaheads_count (state_t *state) | |
331 | { | |
332 | int k; | |
333 | int nlookaheads = 0; | |
334 | reductions_t *rp = state->reductions; | |
335 | transitions_t *sp = state->transitions; | |
336 | ||
337 | /* We need a lookahead either to distinguish different | |
338 | reductions (i.e., there are two or more), or to distinguish a | |
339 | reduction from a shift. Otherwise, it is straightforward, | |
340 | and the state is `consistent'. */ | |
341 | if (rp->num > 1 | |
342 | || (rp->num == 1 && sp->num && | |
343 | !TRANSITION_IS_DISABLED (sp, 0) && TRANSITION_IS_SHIFT (sp, 0))) | |
344 | nlookaheads += rp->num; | |
345 | else | |
346 | state->consistent = 1; | |
347 | ||
348 | for (k = 0; k < sp->num; k++) | |
349 | if (!TRANSITION_IS_DISABLED (sp, k) && TRANSITION_IS_ERROR (sp, k)) | |
350 | { | |
351 | state->consistent = 0; | |
352 | break; | |
353 | } | |
354 | ||
355 | return nlookaheads; | |
356 | } | |
357 | ||
358 | ||
359 | /*----------------------------------------------. | |
360 | | Compute LA, NLA, and the lookaheads members. | | |
361 | `----------------------------------------------*/ | |
362 | ||
363 | static void | |
364 | initialize_LA (void) | |
365 | { | |
366 | state_number_t i; | |
367 | bitsetv pLA; | |
368 | ||
369 | /* Compute the total number of reductions requiring a lookahead. */ | |
370 | nLA = 0; | |
371 | for (i = 0; i < nstates; i++) | |
372 | nLA += state_lookaheads_count (states[i]); | |
373 | /* Avoid having to special case 0. */ | |
374 | if (!nLA) | |
375 | nLA = 1; | |
376 | ||
377 | pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED); | |
378 | lookback = XCALLOC (goto_list_t *, nLA); | |
379 | ||
380 | /* Initialize the members LOOKAHEADS for each state which reductions | |
381 | require lookaheads. */ | |
382 | for (i = 0; i < nstates; i++) | |
383 | { | |
384 | int count = state_lookaheads_count (states[i]); | |
385 | if (count) | |
386 | { | |
387 | states[i]->reductions->lookaheads = pLA; | |
388 | pLA += count; | |
389 | } | |
390 | } | |
391 | } | |
392 | ||
393 | ||
394 | /*---------------------------------------. | |
395 | | Output the lookaheads for each state. | | |
396 | `---------------------------------------*/ | |
397 | ||
398 | static void | |
399 | lookaheads_print (FILE *out) | |
400 | { | |
401 | state_number_t i; | |
402 | int j, k; | |
403 | fprintf (out, "Lookaheads: BEGIN\n"); | |
404 | for (i = 0; i < nstates; ++i) | |
405 | { | |
406 | reductions_t *reds = states[i]->reductions; | |
407 | bitset_iterator iter; | |
408 | int nlookaheads = 0; | |
409 | ||
410 | if (reds->lookaheads) | |
411 | for (k = 0; k < reds->num; ++k) | |
412 | if (reds->lookaheads[k]) | |
413 | ++nlookaheads; | |
414 | ||
415 | fprintf (out, "State %d: %d lookaheads\n", | |
416 | i, nlookaheads); | |
417 | ||
418 | if (reds->lookaheads) | |
419 | for (j = 0; j < reds->num; ++j) | |
420 | BITSET_FOR_EACH (iter, reds->lookaheads[j], k, 0) | |
421 | { | |
422 | fprintf (out, " on %d (%s) -> rule %d\n", | |
423 | k, symbols[k]->tag, | |
424 | reds->rules[j]->number); | |
425 | }; | |
426 | } | |
427 | fprintf (out, "Lookaheads: END\n"); | |
428 | } | |
429 | ||
430 | void | |
431 | lalr (void) | |
432 | { | |
433 | initialize_LA (); | |
434 | set_goto_map (); | |
435 | initialize_F (); | |
436 | build_relations (); | |
437 | compute_FOLLOWS (); | |
438 | compute_lookaheads (); | |
439 | ||
440 | if (trace_flag & trace_sets) | |
441 | lookaheads_print (stderr); | |
442 | } | |
443 | ||
444 | ||
445 | void | |
446 | lalr_free (void) | |
447 | { | |
448 | state_number_t s; | |
449 | for (s = 0; s < nstates; ++s) | |
450 | states[s]->reductions->lookaheads = NULL; | |
451 | bitsetv_free (LA); | |
452 | } |