]>
Commit | Line | Data |
---|---|---|
d0fb370f | 1 | /* Compute look-ahead criteria for bison, |
3feec034 | 2 | Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc. |
d0fb370f | 3 | |
340ef489 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
d0fb370f | 5 | |
340ef489 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. | |
d0fb370f | 10 | |
340ef489 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. | |
d0fb370f | 15 | |
340ef489 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. */ | |
d0fb370f RS |
20 | |
21 | ||
720d742f AD |
22 | /* Compute how to make the finite state machine deterministic; find |
23 | which rules need lookahead in each state, and which lookahead | |
340ef489 | 24 | tokens they accept. */ |
d0fb370f | 25 | |
d0fb370f | 26 | #include "system.h" |
d0fb370f | 27 | #include "types.h" |
b2ca4022 | 28 | #include "LR0.h" |
d0fb370f | 29 | #include "gram.h" |
a0f6b076 | 30 | #include "complain.h" |
720d742f | 31 | #include "lalr.h" |
3519ec76 | 32 | #include "nullable.h" |
340ef489 | 33 | #include "derives.h" |
f67d13aa | 34 | #include "getargs.h" |
d0fb370f | 35 | |
9703cc49 AD |
36 | /* All the decorated states, indexed by the state number. Warning: |
37 | there is a state_TABLE in LR0.c, but it is different and static. | |
38 | */ | |
f693ad14 | 39 | state_t **state_table = NULL; |
9703cc49 | 40 | |
d0fb370f | 41 | int tokensetsize; |
d0fb370f RS |
42 | short *LAruleno; |
43 | unsigned *LA; | |
9703cc49 | 44 | |
aa2aab3c | 45 | static int ngotos; |
d0fb370f RS |
46 | short *goto_map; |
47 | short *from_state; | |
48 | short *to_state; | |
49 | ||
fe961097 | 50 | /* And for the famous F variable, which name is so descriptive that a |
ddcd5fdf AD |
51 | comment is hardly needed. <grin>. */ |
52 | static unsigned *F = NULL; | |
53 | #define F(Rule) (F + (Rule) * tokensetsize) | |
54 | ||
d0fb370f RS |
55 | static short **includes; |
56 | static shorts **lookback; | |
aa2aab3c AD |
57 | |
58 | ||
59 | /*---------------------------------------------------------------. | |
60 | | digraph & traverse. | | |
61 | | | | |
62 | | The following variables are used as common storage between the | | |
63 | | two. | | |
64 | `---------------------------------------------------------------*/ | |
65 | ||
d0fb370f RS |
66 | static short **R; |
67 | static short *INDEX; | |
68 | static short *VERTICES; | |
69 | static int top; | |
aa2aab3c | 70 | static int infinity; |
d0fb370f | 71 | |
720d742f AD |
72 | static void |
73 | traverse (int i) | |
d0fb370f | 74 | { |
720d742f | 75 | int j; |
fe961097 | 76 | size_t k; |
720d742f | 77 | int height; |
fe961097 | 78 | size_t size = F (i + 1) - F(i); |
720d742f AD |
79 | |
80 | VERTICES[++top] = i; | |
81 | INDEX[i] = height = top; | |
82 | ||
fe961097 AD |
83 | if (R[i]) |
84 | for (j = 0; R[i][j] >= 0; ++j) | |
85 | { | |
86 | if (INDEX[R[i][j]] == 0) | |
87 | traverse (R[i][j]); | |
720d742f | 88 | |
fe961097 AD |
89 | if (INDEX[i] > INDEX[R[i][j]]) |
90 | INDEX[i] = INDEX[R[i][j]]; | |
720d742f | 91 | |
fe961097 AD |
92 | for (k = 0; k < size; ++k) |
93 | F (i)[k] |= F (R[i][j])[k]; | |
94 | } | |
720d742f AD |
95 | |
96 | if (INDEX[i] == height) | |
fe961097 AD |
97 | for (;;) |
98 | { | |
99 | j = VERTICES[top--]; | |
100 | INDEX[j] = infinity; | |
720d742f | 101 | |
fe961097 AD |
102 | if (i == j) |
103 | break; | |
720d742f | 104 | |
fe961097 AD |
105 | for (k = 0; k < size; ++k) |
106 | F (i)[k] = F (j)[k]; | |
107 | } | |
d0fb370f RS |
108 | } |
109 | ||
110 | ||
720d742f AD |
111 | static void |
112 | digraph (short **relation) | |
113 | { | |
114 | int i; | |
115 | ||
116 | infinity = ngotos + 2; | |
d7913476 AD |
117 | INDEX = XCALLOC (short, ngotos + 1); |
118 | VERTICES = XCALLOC (short, ngotos + 1); | |
720d742f AD |
119 | top = 0; |
120 | ||
121 | R = relation; | |
122 | ||
123 | for (i = 0; i < ngotos; i++) | |
124 | INDEX[i] = 0; | |
125 | ||
126 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf AD |
127 | if (INDEX[i] == 0 && R[i]) |
128 | traverse (i); | |
720d742f | 129 | |
d7913476 AD |
130 | XFREE (INDEX); |
131 | XFREE (VERTICES); | |
720d742f AD |
132 | } |
133 | ||
bb527fc2 | 134 | |
4a120d45 | 135 | static void |
d2729d44 | 136 | initialize_LA (void) |
d0fb370f | 137 | { |
720d742f AD |
138 | int i; |
139 | int j; | |
720d742f | 140 | short *np; |
a845a697 | 141 | reductions *rp; |
d0fb370f | 142 | |
f693ad14 | 143 | size_t nLA = state_table[nstates]->lookaheads; |
a845a697 AD |
144 | if (!nLA) |
145 | nLA = 1; | |
d0fb370f | 146 | |
a845a697 AD |
147 | LA = XCALLOC (unsigned, nLA * tokensetsize); |
148 | LAruleno = XCALLOC (short, nLA); | |
149 | lookback = XCALLOC (shorts *, nLA); | |
d0fb370f RS |
150 | |
151 | np = LAruleno; | |
152 | for (i = 0; i < nstates; i++) | |
f693ad14 AD |
153 | if (!state_table[i]->consistent) |
154 | if ((rp = state_table[i]->reductions)) | |
a845a697 AD |
155 | for (j = 0; j < rp->nreds; j++) |
156 | *np++ = rp->rules[j]; | |
d0fb370f RS |
157 | } |
158 | ||
159 | ||
4a120d45 | 160 | static void |
d2729d44 | 161 | set_goto_map (void) |
d0fb370f | 162 | { |
7215de24 | 163 | int state; |
720d742f AD |
164 | int i; |
165 | int symbol; | |
166 | int k; | |
167 | short *temp_map; | |
168 | int state2; | |
d0fb370f | 169 | |
d7913476 AD |
170 | goto_map = XCALLOC (short, nvars + 1) - ntokens; |
171 | temp_map = XCALLOC (short, nvars + 1) - ntokens; | |
d0fb370f RS |
172 | |
173 | ngotos = 0; | |
7215de24 AD |
174 | for (state = 0; state < nstates; ++state) |
175 | { | |
176 | shifts *sp = state_table[state]->shifts; | |
177 | for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) | |
178 | { | |
179 | symbol = state_table[sp->shifts[i]]->accessing_symbol; | |
d0fb370f | 180 | |
7215de24 AD |
181 | if (ngotos == MAXSHORT) |
182 | fatal (_("too many gotos (max %d)"), MAXSHORT); | |
d0fb370f | 183 | |
7215de24 AD |
184 | ngotos++; |
185 | goto_map[symbol]++; | |
186 | } | |
187 | } | |
d0fb370f RS |
188 | |
189 | k = 0; | |
190 | for (i = ntokens; i < nsyms; i++) | |
191 | { | |
192 | temp_map[i] = k; | |
193 | k += goto_map[i]; | |
194 | } | |
195 | ||
196 | for (i = ntokens; i < nsyms; i++) | |
197 | goto_map[i] = temp_map[i]; | |
198 | ||
199 | goto_map[nsyms] = ngotos; | |
200 | temp_map[nsyms] = ngotos; | |
201 | ||
d7913476 AD |
202 | from_state = XCALLOC (short, ngotos); |
203 | to_state = XCALLOC (short, ngotos); | |
d0fb370f | 204 | |
7215de24 | 205 | for (state = 0; state < nstates; ++state) |
d0fb370f | 206 | { |
7215de24 | 207 | shifts *sp = state_table[state]->shifts; |
aa2aab3c | 208 | for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
d0fb370f | 209 | { |
7215de24 AD |
210 | for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
211 | { | |
212 | state2 = sp->shifts[i]; | |
213 | symbol = state_table[state2]->accessing_symbol; | |
d0fb370f | 214 | |
7215de24 AD |
215 | k = temp_map[symbol]++; |
216 | from_state[k] = state; | |
217 | to_state[k] = state2; | |
218 | } | |
d0fb370f RS |
219 | } |
220 | } | |
221 | ||
d7913476 | 222 | XFREE (temp_map + ntokens); |
d0fb370f RS |
223 | } |
224 | ||
225 | ||
226 | ||
43591cec AD |
227 | /*----------------------------------------------------------. |
228 | | Map a state/symbol pair into its numeric representation. | | |
229 | `----------------------------------------------------------*/ | |
d0fb370f | 230 | |
4a120d45 | 231 | static int |
d2729d44 | 232 | map_goto (int state, int symbol) |
d0fb370f | 233 | { |
720d742f AD |
234 | int high; |
235 | int low; | |
236 | int middle; | |
237 | int s; | |
d0fb370f RS |
238 | |
239 | low = goto_map[symbol]; | |
240 | high = goto_map[symbol + 1] - 1; | |
241 | ||
242 | while (low <= high) | |
243 | { | |
244 | middle = (low + high) / 2; | |
245 | s = from_state[middle]; | |
246 | if (s == state) | |
36281465 | 247 | return middle; |
d0fb370f RS |
248 | else if (s < state) |
249 | low = middle + 1; | |
250 | else | |
251 | high = middle - 1; | |
252 | } | |
253 | ||
43591cec AD |
254 | assert (0); |
255 | /* NOTREACHED */ | |
d0fb370f RS |
256 | return 0; |
257 | } | |
258 | ||
259 | ||
4a120d45 | 260 | static void |
d2729d44 | 261 | initialize_F (void) |
d0fb370f | 262 | { |
4d4f699c AD |
263 | short **reads = XCALLOC (short *, ngotos); |
264 | short *edge = XCALLOC (short, ngotos + 1); | |
265 | int nedges = 0; | |
d0fb370f | 266 | |
4d4f699c | 267 | int i; |
d0fb370f | 268 | |
4d4f699c | 269 | F = XCALLOC (unsigned, ngotos * tokensetsize); |
d0fb370f | 270 | |
d0fb370f RS |
271 | for (i = 0; i < ngotos; i++) |
272 | { | |
80a69750 | 273 | int stateno = to_state[i]; |
f693ad14 | 274 | shifts *sp = state_table[stateno]->shifts; |
d0fb370f | 275 | |
d954473d AD |
276 | int j; |
277 | for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++) | |
d0fb370f | 278 | { |
f693ad14 | 279 | int symbol = state_table[sp->shifts[j]]->accessing_symbol; |
92b16366 | 280 | SETBIT (F (i), symbol); |
d954473d | 281 | } |
d0fb370f | 282 | |
d954473d AD |
283 | for (; j < sp->nshifts; j++) |
284 | { | |
f693ad14 | 285 | int symbol = state_table[sp->shifts[j]]->accessing_symbol; |
d954473d AD |
286 | if (nullable[symbol]) |
287 | edge[nedges++] = map_goto (stateno, symbol); | |
288 | } | |
a0f6b076 | 289 | |
d954473d AD |
290 | if (nedges) |
291 | { | |
292 | reads[i] = XCALLOC (short, nedges + 1); | |
293 | shortcpy (reads[i], edge, nedges); | |
294 | reads[i][nedges] = -1; | |
295 | nedges = 0; | |
d0fb370f | 296 | } |
d0fb370f RS |
297 | } |
298 | ||
720d742f | 299 | digraph (reads); |
d0fb370f RS |
300 | |
301 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf | 302 | XFREE (reads[i]); |
d0fb370f | 303 | |
d7913476 AD |
304 | XFREE (reads); |
305 | XFREE (edge); | |
d0fb370f RS |
306 | } |
307 | ||
308 | ||
4a120d45 | 309 | static void |
d2729d44 | 310 | add_lookback_edge (int stateno, int ruleno, int gotono) |
d0fb370f | 311 | { |
720d742f AD |
312 | int i; |
313 | int k; | |
314 | int found; | |
315 | shorts *sp; | |
d0fb370f | 316 | |
f693ad14 AD |
317 | i = state_table[stateno]->lookaheads; |
318 | k = state_table[stateno + 1]->lookaheads; | |
d0fb370f RS |
319 | found = 0; |
320 | while (!found && i < k) | |
321 | { | |
322 | if (LAruleno[i] == ruleno) | |
323 | found = 1; | |
324 | else | |
325 | i++; | |
326 | } | |
327 | ||
340ef489 | 328 | assert (found); |
d0fb370f | 329 | |
d7913476 | 330 | sp = XCALLOC (shorts, 1); |
d0fb370f RS |
331 | sp->next = lookback[i]; |
332 | sp->value = gotono; | |
333 | lookback[i] = sp; | |
334 | } | |
335 | ||
336 | ||
f67d13aa AD |
337 | static void |
338 | matrix_print (FILE *out, short **matrix, int n) | |
339 | { | |
340 | int i, j; | |
341 | ||
342 | for (i = 0; i < n; ++i) | |
343 | { | |
344 | fprintf (out, "%3d: ", i); | |
345 | if (matrix[i]) | |
346 | for (j = 0; matrix[i][j] != -1; ++j) | |
347 | fprintf (out, "%3d ", matrix[i][j]); | |
348 | fputc ('\n', out); | |
349 | } | |
350 | fputc ('\n', out); | |
351 | } | |
352 | ||
9887c18a AD |
353 | /*-------------------------------------------------------------------. |
354 | | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is | | |
355 | | replaced with the result. | | |
f67d13aa AD |
356 | | | |
357 | | R_ARG[I] is NULL or a -1 terminated list of numbers. | | |
358 | | | | |
359 | | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM | | |
360 | | is in R_ARG[I]. | | |
9887c18a AD |
361 | `-------------------------------------------------------------------*/ |
362 | ||
4a120d45 | 363 | static short ** |
d2729d44 | 364 | transpose (short **R_arg, int n) |
d0fb370f | 365 | { |
f67d13aa AD |
366 | /* The result. */ |
367 | short **new_R = XCALLOC (short *, n); | |
368 | /* END_R[I] -- next entry of NEW_R[I]. */ | |
369 | short **end_R = XCALLOC (short *, n); | |
370 | /* NEDGES[I] -- total size of NEW_R[I]. */ | |
371 | short *nedges = XCALLOC (short, n); | |
372 | int i, j; | |
373 | ||
374 | if (trace_flag) | |
d0fb370f | 375 | { |
f67d13aa AD |
376 | fputs ("transpose: input\n", stderr); |
377 | matrix_print (stderr, R_arg, n); | |
d0fb370f RS |
378 | } |
379 | ||
f67d13aa AD |
380 | /* Count. */ |
381 | for (i = 0; i < n; i++) | |
382 | if (R_arg[i]) | |
383 | for (j = 0; R_arg[i][j] >= 0; ++j) | |
384 | ++nedges[R_arg[i][j]]; | |
d0fb370f | 385 | |
f67d13aa | 386 | /* Allocate. */ |
d0fb370f | 387 | for (i = 0; i < n; i++) |
80a69750 AD |
388 | if (nedges[i] > 0) |
389 | { | |
390 | short *sp = XCALLOC (short, nedges[i] + 1); | |
80a69750 | 391 | sp[nedges[i]] = -1; |
f67d13aa AD |
392 | new_R[i] = sp; |
393 | end_R[i] = sp; | |
80a69750 | 394 | } |
d0fb370f | 395 | |
f67d13aa | 396 | /* Store. */ |
d0fb370f | 397 | for (i = 0; i < n; i++) |
f67d13aa AD |
398 | if (R_arg[i]) |
399 | for (j = 0; R_arg[i][j] >= 0; ++j) | |
400 | { | |
401 | *end_R[R_arg[i][j]] = i; | |
402 | ++end_R[R_arg[i][j]]; | |
403 | } | |
d0fb370f | 404 | |
f67d13aa AD |
405 | free (nedges); |
406 | free (end_R); | |
d0fb370f | 407 | |
9887c18a AD |
408 | /* Free the input: it is replaced with the result. */ |
409 | for (i = 0; i < n; i++) | |
410 | XFREE (R_arg[i]); | |
f67d13aa AD |
411 | free (R_arg); |
412 | ||
413 | if (trace_flag) | |
414 | { | |
415 | fputs ("transpose: output\n", stderr); | |
416 | matrix_print (stderr, new_R, n); | |
417 | } | |
9887c18a | 418 | |
36281465 | 419 | return new_R; |
d0fb370f RS |
420 | } |
421 | ||
422 | ||
4a120d45 | 423 | static void |
720d742f | 424 | build_relations (void) |
d0fb370f | 425 | { |
9887c18a | 426 | short *edge = XCALLOC (short, ngotos + 1); |
c2713865 | 427 | short *states = XCALLOC (short, ritem_longest_rhs () + 1); |
720d742f | 428 | int i; |
720d742f | 429 | |
d7913476 | 430 | includes = XCALLOC (short *, ngotos); |
d0fb370f RS |
431 | |
432 | for (i = 0; i < ngotos; i++) | |
433 | { | |
9887c18a AD |
434 | int nedges = 0; |
435 | int state1 = from_state[i]; | |
f693ad14 | 436 | int symbol1 = state_table[to_state[i]]->accessing_symbol; |
9887c18a | 437 | short *rulep; |
d0fb370f | 438 | |
720d742f AD |
439 | for (rulep = derives[symbol1]; *rulep > 0; rulep++) |
440 | { | |
9887c18a | 441 | int done; |
80a69750 | 442 | int length = 1; |
9887c18a AD |
443 | int stateno = state1; |
444 | short *rp; | |
720d742f | 445 | states[0] = state1; |
d0fb370f | 446 | |
b2ed6e58 | 447 | for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++) |
720d742f | 448 | { |
f693ad14 | 449 | shifts *sp = state_table[stateno]->shifts; |
9887c18a | 450 | int j; |
80a69750 | 451 | for (j = 0; j < sp->nshifts; j++) |
720d742f AD |
452 | { |
453 | stateno = sp->shifts[j]; | |
f693ad14 | 454 | if (state_table[stateno]->accessing_symbol == *rp) |
720d742f AD |
455 | break; |
456 | } | |
d0fb370f | 457 | |
720d742f AD |
458 | states[length++] = stateno; |
459 | } | |
460 | ||
f693ad14 | 461 | if (!state_table[stateno]->consistent) |
720d742f AD |
462 | add_lookback_edge (stateno, *rulep, i); |
463 | ||
464 | length--; | |
465 | done = 0; | |
466 | while (!done) | |
467 | { | |
468 | done = 1; | |
469 | rp--; | |
470 | /* JF added rp>=ritem && I hope to god its right! */ | |
471 | if (rp >= ritem && ISVAR (*rp)) | |
472 | { | |
473 | stateno = states[--length]; | |
474 | edge[nedges++] = map_goto (stateno, *rp); | |
475 | if (nullable[*rp]) | |
476 | done = 0; | |
477 | } | |
478 | } | |
d0fb370f RS |
479 | } |
480 | ||
720d742f AD |
481 | if (nedges) |
482 | { | |
9887c18a | 483 | int j; |
80a69750 | 484 | includes[i] = XCALLOC (short, nedges + 1); |
720d742f | 485 | for (j = 0; j < nedges; j++) |
80a69750 AD |
486 | includes[i][j] = edge[j]; |
487 | includes[i][nedges] = -1; | |
720d742f | 488 | } |
d0fb370f RS |
489 | } |
490 | ||
d7913476 AD |
491 | XFREE (edge); |
492 | XFREE (states); | |
9887c18a AD |
493 | |
494 | includes = transpose (includes, ngotos); | |
d0fb370f RS |
495 | } |
496 | ||
497 | ||
720d742f | 498 | |
4a120d45 | 499 | static void |
720d742f | 500 | compute_FOLLOWS (void) |
d0fb370f | 501 | { |
720d742f | 502 | int i; |
d0fb370f | 503 | |
720d742f | 504 | digraph (includes); |
d0fb370f RS |
505 | |
506 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf | 507 | XFREE (includes[i]); |
d0fb370f | 508 | |
d7913476 | 509 | XFREE (includes); |
d0fb370f RS |
510 | } |
511 | ||
512 | ||
4a120d45 | 513 | static void |
720d742f | 514 | compute_lookaheads (void) |
d0fb370f | 515 | { |
720d742f | 516 | int i; |
720d742f | 517 | shorts *sp; |
d0fb370f | 518 | |
f693ad14 | 519 | for (i = 0; i < state_table[nstates]->lookaheads; i++) |
ddcd5fdf AD |
520 | for (sp = lookback[i]; sp; sp = sp->next) |
521 | { | |
9887c18a AD |
522 | int size = LA (i + 1) - LA (i); |
523 | int j; | |
524 | for (j = 0; j < size; ++j) | |
525 | LA (i)[j] |= F (sp->value)[j]; | |
ddcd5fdf | 526 | } |
d0fb370f | 527 | |
ddcd5fdf | 528 | /* Free LOOKBACK. */ |
f693ad14 | 529 | for (i = 0; i < state_table[nstates]->lookaheads; i++) |
300f275f | 530 | LIST_FREE (shorts, lookback[i]); |
d0fb370f | 531 | |
d7913476 AD |
532 | XFREE (lookback); |
533 | XFREE (F); | |
720d742f | 534 | } |
d0fb370f | 535 | |
d0fb370f | 536 | |
720d742f AD |
537 | void |
538 | lalr (void) | |
539 | { | |
540 | tokensetsize = WORDSIZE (ntokens); | |
541 | ||
720d742f AD |
542 | initialize_LA (); |
543 | set_goto_map (); | |
544 | initialize_F (); | |
545 | build_relations (); | |
546 | compute_FOLLOWS (); | |
547 | compute_lookaheads (); | |
d0fb370f | 548 | } |