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