]>
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" |
602bbf31 | 27 | #include "bitset.h" |
7c6b64d0 | 28 | #include "reader.h" |
d0fb370f | 29 | #include "types.h" |
b2ca4022 | 30 | #include "LR0.h" |
ad949da9 | 31 | #include "symtab.h" |
d0fb370f | 32 | #include "gram.h" |
a0f6b076 | 33 | #include "complain.h" |
720d742f | 34 | #include "lalr.h" |
3519ec76 | 35 | #include "nullable.h" |
340ef489 | 36 | #include "derives.h" |
f67d13aa | 37 | #include "getargs.h" |
d0fb370f | 38 | |
d200e455 | 39 | /* All the decorated states, indexed by the state number. */ |
29e88316 | 40 | state_t **states = NULL; |
9703cc49 | 41 | |
602bbf31 AD |
42 | short *LAruleno = NULL; |
43 | bitset *LA = NULL; | |
d200e455 | 44 | size_t nLA; |
9703cc49 | 45 | |
aa2aab3c | 46 | static int ngotos; |
602bbf31 AD |
47 | short *goto_map = NULL; |
48 | short *from_state = NULL; | |
49 | short *to_state = NULL; | |
d0fb370f | 50 | |
fe961097 | 51 | /* And for the famous F variable, which name is so descriptive that a |
ddcd5fdf | 52 | comment is hardly needed. <grin>. */ |
0fb1ffb1 | 53 | static bitset *F = NULL; |
ddcd5fdf | 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; |
720d742f | 76 | int height; |
720d742f AD |
77 | |
78 | VERTICES[++top] = i; | |
79 | INDEX[i] = height = top; | |
80 | ||
fe961097 AD |
81 | if (R[i]) |
82 | for (j = 0; R[i][j] >= 0; ++j) | |
83 | { | |
84 | if (INDEX[R[i][j]] == 0) | |
85 | traverse (R[i][j]); | |
720d742f | 86 | |
fe961097 AD |
87 | if (INDEX[i] > INDEX[R[i][j]]) |
88 | INDEX[i] = INDEX[R[i][j]]; | |
720d742f | 89 | |
0fb1ffb1 | 90 | bitset_or (F[i], F[i], F[R[i][j]]); |
fe961097 | 91 | } |
720d742f AD |
92 | |
93 | if (INDEX[i] == height) | |
fe961097 AD |
94 | for (;;) |
95 | { | |
96 | j = VERTICES[top--]; | |
97 | INDEX[j] = infinity; | |
720d742f | 98 | |
fe961097 AD |
99 | if (i == j) |
100 | break; | |
720d742f | 101 | |
0fb1ffb1 | 102 | bitset_copy (F[j], F[i]); |
fe961097 | 103 | } |
d0fb370f RS |
104 | } |
105 | ||
106 | ||
720d742f AD |
107 | static void |
108 | digraph (short **relation) | |
109 | { | |
110 | int i; | |
111 | ||
112 | infinity = ngotos + 2; | |
d7913476 AD |
113 | INDEX = XCALLOC (short, ngotos + 1); |
114 | VERTICES = XCALLOC (short, ngotos + 1); | |
720d742f AD |
115 | top = 0; |
116 | ||
117 | R = relation; | |
118 | ||
119 | for (i = 0; i < ngotos; i++) | |
120 | INDEX[i] = 0; | |
121 | ||
122 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf AD |
123 | if (INDEX[i] == 0 && R[i]) |
124 | traverse (i); | |
720d742f | 125 | |
d7913476 AD |
126 | XFREE (INDEX); |
127 | XFREE (VERTICES); | |
720d742f AD |
128 | } |
129 | ||
bb527fc2 | 130 | |
4a120d45 | 131 | static void |
d2729d44 | 132 | initialize_LA (void) |
d0fb370f | 133 | { |
602bbf31 | 134 | size_t i; |
720d742f | 135 | int j; |
720d742f | 136 | short *np; |
d0fb370f | 137 | |
d200e455 | 138 | /* Avoid having to special case 0. */ |
a845a697 AD |
139 | if (!nLA) |
140 | nLA = 1; | |
d0fb370f | 141 | |
602bbf31 AD |
142 | LA = XCALLOC (bitset, nLA); |
143 | for (i = 0; i < nLA; ++i) | |
144 | { | |
145 | LA[i] = bitset_create (ntokens, BITSET_FIXED); | |
146 | bitset_zero (LA[i]); | |
147 | } | |
a845a697 AD |
148 | LAruleno = XCALLOC (short, nLA); |
149 | lookback = XCALLOC (shorts *, nLA); | |
d0fb370f RS |
150 | |
151 | np = LAruleno; | |
152 | for (i = 0; i < nstates; i++) | |
29e88316 AD |
153 | if (!states[i]->consistent) |
154 | for (j = 0; j < states[i]->reductions->nreds; j++) | |
155 | *np++ = states[i]->reductions->rules[j]; | |
d0fb370f RS |
156 | } |
157 | ||
158 | ||
4a120d45 | 159 | static void |
d2729d44 | 160 | set_goto_map (void) |
d0fb370f | 161 | { |
602bbf31 AD |
162 | size_t state; |
163 | int i; | |
720d742f | 164 | short *temp_map; |
d0fb370f | 165 | |
d7913476 AD |
166 | goto_map = XCALLOC (short, nvars + 1) - ntokens; |
167 | temp_map = XCALLOC (short, nvars + 1) - ntokens; | |
d0fb370f RS |
168 | |
169 | ngotos = 0; | |
7215de24 AD |
170 | for (state = 0; state < nstates; ++state) |
171 | { | |
29e88316 | 172 | shifts *sp = states[state]->shifts; |
7215de24 AD |
173 | for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
174 | { | |
7215de24 AD |
175 | if (ngotos == MAXSHORT) |
176 | fatal (_("too many gotos (max %d)"), MAXSHORT); | |
d0fb370f | 177 | |
7215de24 | 178 | ngotos++; |
13ca549a | 179 | goto_map[SHIFT_SYMBOL (sp, i)]++; |
7215de24 AD |
180 | } |
181 | } | |
d0fb370f | 182 | |
d0b0fefa AD |
183 | { |
184 | int k = 0; | |
185 | for (i = ntokens; i < nsyms; i++) | |
186 | { | |
187 | temp_map[i] = k; | |
188 | k += goto_map[i]; | |
189 | } | |
d0fb370f | 190 | |
d0b0fefa AD |
191 | for (i = ntokens; i < nsyms; i++) |
192 | goto_map[i] = temp_map[i]; | |
d0fb370f | 193 | |
d0b0fefa AD |
194 | goto_map[nsyms] = ngotos; |
195 | temp_map[nsyms] = ngotos; | |
196 | } | |
d0fb370f | 197 | |
d7913476 AD |
198 | from_state = XCALLOC (short, ngotos); |
199 | to_state = XCALLOC (short, ngotos); | |
d0fb370f | 200 | |
7215de24 | 201 | for (state = 0; state < nstates; ++state) |
d0fb370f | 202 | { |
29e88316 | 203 | shifts *sp = states[state]->shifts; |
aa2aab3c | 204 | for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
d0fb370f | 205 | { |
13ca549a | 206 | int k = temp_map[SHIFT_SYMBOL (sp, i)]++; |
d0b0fefa | 207 | from_state[k] = state; |
13ca549a | 208 | to_state[k] = sp->shifts[i]; |
d0fb370f RS |
209 | } |
210 | } | |
211 | ||
d7913476 | 212 | XFREE (temp_map + ntokens); |
d0fb370f RS |
213 | } |
214 | ||
215 | ||
216 | ||
43591cec AD |
217 | /*----------------------------------------------------------. |
218 | | Map a state/symbol pair into its numeric representation. | | |
219 | `----------------------------------------------------------*/ | |
d0fb370f | 220 | |
4a120d45 | 221 | static int |
d2729d44 | 222 | map_goto (int state, int symbol) |
d0fb370f | 223 | { |
720d742f AD |
224 | int high; |
225 | int low; | |
226 | int middle; | |
227 | int s; | |
d0fb370f RS |
228 | |
229 | low = goto_map[symbol]; | |
230 | high = goto_map[symbol + 1] - 1; | |
231 | ||
232 | while (low <= high) | |
233 | { | |
234 | middle = (low + high) / 2; | |
235 | s = from_state[middle]; | |
236 | if (s == state) | |
36281465 | 237 | return middle; |
d0fb370f RS |
238 | else if (s < state) |
239 | low = middle + 1; | |
240 | else | |
241 | high = middle - 1; | |
242 | } | |
243 | ||
43591cec AD |
244 | assert (0); |
245 | /* NOTREACHED */ | |
d0fb370f RS |
246 | return 0; |
247 | } | |
248 | ||
249 | ||
4a120d45 | 250 | static void |
d2729d44 | 251 | initialize_F (void) |
d0fb370f | 252 | { |
4d4f699c AD |
253 | short **reads = XCALLOC (short *, ngotos); |
254 | short *edge = XCALLOC (short, ngotos + 1); | |
255 | int nedges = 0; | |
d0fb370f | 256 | |
4d4f699c | 257 | int i; |
d0fb370f | 258 | |
0fb1ffb1 AD |
259 | F = XCALLOC (bitset, ngotos); |
260 | for (i = 0; i < ngotos; ++i) | |
261 | { | |
262 | F[i] = bitset_create (ntokens, BITSET_FIXED); | |
263 | bitset_zero (F[i]); | |
264 | } | |
d0fb370f | 265 | |
d0fb370f RS |
266 | for (i = 0; i < ngotos; i++) |
267 | { | |
80a69750 | 268 | int stateno = to_state[i]; |
29e88316 | 269 | shifts *sp = states[stateno]->shifts; |
d0fb370f | 270 | |
d954473d AD |
271 | int j; |
272 | for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++) | |
0fb1ffb1 | 273 | bitset_set (F[i], SHIFT_SYMBOL (sp, j)); |
d0fb370f | 274 | |
d954473d AD |
275 | for (; j < sp->nshifts; j++) |
276 | { | |
13ca549a | 277 | int symbol = SHIFT_SYMBOL (sp, j); |
d954473d AD |
278 | if (nullable[symbol]) |
279 | edge[nedges++] = map_goto (stateno, symbol); | |
280 | } | |
a0f6b076 | 281 | |
d954473d AD |
282 | if (nedges) |
283 | { | |
284 | reads[i] = XCALLOC (short, nedges + 1); | |
285 | shortcpy (reads[i], edge, nedges); | |
286 | reads[i][nedges] = -1; | |
287 | nedges = 0; | |
d0fb370f | 288 | } |
d0fb370f RS |
289 | } |
290 | ||
720d742f | 291 | digraph (reads); |
d0fb370f RS |
292 | |
293 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf | 294 | XFREE (reads[i]); |
d0fb370f | 295 | |
d7913476 AD |
296 | XFREE (reads); |
297 | XFREE (edge); | |
d0fb370f RS |
298 | } |
299 | ||
300 | ||
4a120d45 | 301 | static void |
dac3c910 | 302 | add_lookback_edge (state_t *state, int ruleno, int gotono) |
d0fb370f | 303 | { |
720d742f | 304 | int i; |
720d742f | 305 | shorts *sp; |
d0fb370f | 306 | |
dac3c910 AD |
307 | for (i = 0; i < state->nlookaheads; ++i) |
308 | if (LAruleno[state->lookaheadsp + i] == ruleno) | |
065fbd27 | 309 | break; |
d0fb370f | 310 | |
dac3c910 | 311 | assert (LAruleno[state->lookaheadsp + i] == ruleno); |
d0fb370f | 312 | |
d7913476 | 313 | sp = XCALLOC (shorts, 1); |
dac3c910 | 314 | sp->next = lookback[state->lookaheadsp + i]; |
d0fb370f | 315 | sp->value = gotono; |
dac3c910 | 316 | lookback[state->lookaheadsp + i] = sp; |
d0fb370f RS |
317 | } |
318 | ||
319 | ||
f67d13aa AD |
320 | static void |
321 | matrix_print (FILE *out, short **matrix, int n) | |
322 | { | |
323 | int i, j; | |
324 | ||
325 | for (i = 0; i < n; ++i) | |
326 | { | |
327 | fprintf (out, "%3d: ", i); | |
328 | if (matrix[i]) | |
329 | for (j = 0; matrix[i][j] != -1; ++j) | |
330 | fprintf (out, "%3d ", matrix[i][j]); | |
331 | fputc ('\n', out); | |
332 | } | |
333 | fputc ('\n', out); | |
334 | } | |
335 | ||
9887c18a AD |
336 | /*-------------------------------------------------------------------. |
337 | | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is | | |
338 | | replaced with the result. | | |
f67d13aa AD |
339 | | | |
340 | | R_ARG[I] is NULL or a -1 terminated list of numbers. | | |
341 | | | | |
342 | | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM | | |
343 | | is in R_ARG[I]. | | |
9887c18a AD |
344 | `-------------------------------------------------------------------*/ |
345 | ||
4a120d45 | 346 | static short ** |
d2729d44 | 347 | transpose (short **R_arg, int n) |
d0fb370f | 348 | { |
f67d13aa AD |
349 | /* The result. */ |
350 | short **new_R = XCALLOC (short *, n); | |
351 | /* END_R[I] -- next entry of NEW_R[I]. */ | |
352 | short **end_R = XCALLOC (short *, n); | |
353 | /* NEDGES[I] -- total size of NEW_R[I]. */ | |
354 | short *nedges = XCALLOC (short, n); | |
355 | int i, j; | |
356 | ||
357 | if (trace_flag) | |
d0fb370f | 358 | { |
f67d13aa AD |
359 | fputs ("transpose: input\n", stderr); |
360 | matrix_print (stderr, R_arg, n); | |
d0fb370f RS |
361 | } |
362 | ||
f67d13aa AD |
363 | /* Count. */ |
364 | for (i = 0; i < n; i++) | |
365 | if (R_arg[i]) | |
366 | for (j = 0; R_arg[i][j] >= 0; ++j) | |
367 | ++nedges[R_arg[i][j]]; | |
d0fb370f | 368 | |
f67d13aa | 369 | /* Allocate. */ |
d0fb370f | 370 | for (i = 0; i < n; i++) |
80a69750 AD |
371 | if (nedges[i] > 0) |
372 | { | |
373 | short *sp = XCALLOC (short, nedges[i] + 1); | |
80a69750 | 374 | sp[nedges[i]] = -1; |
f67d13aa AD |
375 | new_R[i] = sp; |
376 | end_R[i] = sp; | |
80a69750 | 377 | } |
d0fb370f | 378 | |
f67d13aa | 379 | /* Store. */ |
d0fb370f | 380 | for (i = 0; i < n; i++) |
f67d13aa AD |
381 | if (R_arg[i]) |
382 | for (j = 0; R_arg[i][j] >= 0; ++j) | |
383 | { | |
384 | *end_R[R_arg[i][j]] = i; | |
385 | ++end_R[R_arg[i][j]]; | |
386 | } | |
d0fb370f | 387 | |
f67d13aa AD |
388 | free (nedges); |
389 | free (end_R); | |
d0fb370f | 390 | |
9887c18a AD |
391 | /* Free the input: it is replaced with the result. */ |
392 | for (i = 0; i < n; i++) | |
393 | XFREE (R_arg[i]); | |
f67d13aa AD |
394 | free (R_arg); |
395 | ||
396 | if (trace_flag) | |
397 | { | |
398 | fputs ("transpose: output\n", stderr); | |
399 | matrix_print (stderr, new_R, n); | |
400 | } | |
9887c18a | 401 | |
36281465 | 402 | return new_R; |
d0fb370f RS |
403 | } |
404 | ||
405 | ||
4a120d45 | 406 | static void |
720d742f | 407 | build_relations (void) |
d0fb370f | 408 | { |
9887c18a | 409 | short *edge = XCALLOC (short, ngotos + 1); |
b9f71f19 | 410 | short *states1 = XCALLOC (short, ritem_longest_rhs () + 1); |
720d742f | 411 | int i; |
720d742f | 412 | |
d7913476 | 413 | includes = XCALLOC (short *, ngotos); |
d0fb370f RS |
414 | |
415 | for (i = 0; i < ngotos; i++) | |
416 | { | |
9887c18a | 417 | int nedges = 0; |
29e88316 | 418 | int symbol1 = states[to_state[i]]->accessing_symbol; |
9887c18a | 419 | short *rulep; |
d0fb370f | 420 | |
720d742f AD |
421 | for (rulep = derives[symbol1]; *rulep > 0; rulep++) |
422 | { | |
9887c18a | 423 | int done; |
80a69750 | 424 | int length = 1; |
9887c18a | 425 | short *rp; |
29e88316 | 426 | state_t *state = states[from_state[i]]; |
b9f71f19 | 427 | states1[0] = state->number; |
d0fb370f | 428 | |
1a2b5d37 | 429 | for (rp = &ritem[rules[*rulep].rhs]; *rp >= 0; rp++) |
720d742f | 430 | { |
dac3c910 | 431 | shifts *sp = state->shifts; |
9887c18a | 432 | int j; |
80a69750 | 433 | for (j = 0; j < sp->nshifts; j++) |
720d742f | 434 | { |
29e88316 | 435 | state = states[sp->shifts[j]]; |
dac3c910 | 436 | if (state->accessing_symbol == *rp) |
720d742f AD |
437 | break; |
438 | } | |
d0fb370f | 439 | |
b9f71f19 | 440 | states1[length++] = state->number; |
720d742f AD |
441 | } |
442 | ||
dac3c910 AD |
443 | if (!state->consistent) |
444 | add_lookback_edge (state, *rulep, i); | |
720d742f AD |
445 | |
446 | length--; | |
447 | done = 0; | |
448 | while (!done) | |
449 | { | |
450 | done = 1; | |
451 | rp--; | |
452 | /* JF added rp>=ritem && I hope to god its right! */ | |
453 | if (rp >= ritem && ISVAR (*rp)) | |
454 | { | |
b9f71f19 | 455 | edge[nedges++] = map_goto (states1[--length], *rp); |
720d742f AD |
456 | if (nullable[*rp]) |
457 | done = 0; | |
458 | } | |
459 | } | |
d0fb370f RS |
460 | } |
461 | ||
720d742f AD |
462 | if (nedges) |
463 | { | |
9887c18a | 464 | int j; |
80a69750 | 465 | includes[i] = XCALLOC (short, nedges + 1); |
720d742f | 466 | for (j = 0; j < nedges; j++) |
80a69750 AD |
467 | includes[i][j] = edge[j]; |
468 | includes[i][nedges] = -1; | |
720d742f | 469 | } |
d0fb370f RS |
470 | } |
471 | ||
d7913476 | 472 | XFREE (edge); |
b9f71f19 | 473 | XFREE (states1); |
9887c18a AD |
474 | |
475 | includes = transpose (includes, ngotos); | |
d0fb370f RS |
476 | } |
477 | ||
478 | ||
720d742f | 479 | |
4a120d45 | 480 | static void |
720d742f | 481 | compute_FOLLOWS (void) |
d0fb370f | 482 | { |
720d742f | 483 | int i; |
d0fb370f | 484 | |
720d742f | 485 | digraph (includes); |
d0fb370f RS |
486 | |
487 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf | 488 | XFREE (includes[i]); |
d0fb370f | 489 | |
d7913476 | 490 | XFREE (includes); |
d0fb370f RS |
491 | } |
492 | ||
493 | ||
4a120d45 | 494 | static void |
720d742f | 495 | compute_lookaheads (void) |
d0fb370f | 496 | { |
d200e455 | 497 | size_t i; |
720d742f | 498 | shorts *sp; |
d0fb370f | 499 | |
d200e455 | 500 | for (i = 0; i < nLA; i++) |
ddcd5fdf | 501 | for (sp = lookback[i]; sp; sp = sp->next) |
0fb1ffb1 | 502 | bitset_or (LA[i], LA[i], F[sp->value]); |
d0fb370f | 503 | |
ddcd5fdf | 504 | /* Free LOOKBACK. */ |
d200e455 | 505 | for (i = 0; i < nLA; i++) |
300f275f | 506 | LIST_FREE (shorts, lookback[i]); |
d0fb370f | 507 | |
d7913476 | 508 | XFREE (lookback); |
0fb1ffb1 AD |
509 | for (i = 0; i < (unsigned) ngotos; ++i) |
510 | bitset_free (F[i]); | |
d7913476 | 511 | XFREE (F); |
720d742f | 512 | } |
d0fb370f | 513 | |
d0fb370f | 514 | |
5edafffd AD |
515 | /*--------------------------------------. |
516 | | Initializing the lookaheads members. | | |
517 | `--------------------------------------*/ | |
518 | ||
519 | static void | |
520 | initialize_lookaheads (void) | |
521 | { | |
602bbf31 | 522 | size_t i; |
d200e455 | 523 | nLA = 0; |
5edafffd AD |
524 | for (i = 0; i < nstates; i++) |
525 | { | |
526 | int k; | |
3877f72b | 527 | int nlookaheads = 0; |
29e88316 AD |
528 | reductions *rp = states[i]->reductions; |
529 | shifts *sp = states[i]->shifts; | |
5edafffd | 530 | |
80dac38c AD |
531 | /* We need a lookahead either to distinguish different |
532 | reductions (i.e., there are two or more), or to distinguish a | |
533 | reduction from a shift. Otherwise, it is straightforward, | |
534 | and the state is `consistent'. */ | |
535 | if (rp->nreds > 1 | |
536 | || (rp->nreds == 1 && sp->nshifts && SHIFT_IS_SHIFT (sp, 0))) | |
3877f72b | 537 | nlookaheads += rp->nreds; |
5edafffd | 538 | else |
29e88316 | 539 | states[i]->consistent = 1; |
5edafffd AD |
540 | |
541 | for (k = 0; k < sp->nshifts; k++) | |
542 | if (SHIFT_IS_ERROR (sp, k)) | |
543 | { | |
29e88316 | 544 | states[i]->consistent = 0; |
5edafffd AD |
545 | break; |
546 | } | |
3877f72b | 547 | |
29e88316 AD |
548 | states[i]->nlookaheads = nlookaheads; |
549 | states[i]->lookaheadsp = nLA; | |
d200e455 | 550 | nLA += nlookaheads; |
5edafffd | 551 | } |
5edafffd AD |
552 | } |
553 | ||
7c6b64d0 AD |
554 | |
555 | /*---------------------------------------. | |
556 | | Output the lookaheads for each state. | | |
557 | `---------------------------------------*/ | |
558 | ||
559 | static void | |
560 | lookaheads_print (FILE *out) | |
561 | { | |
602bbf31 AD |
562 | size_t i; |
563 | int j, k; | |
7c6b64d0 AD |
564 | fprintf (out, "Lookaheads: BEGIN\n"); |
565 | for (i = 0; i < nstates; ++i) | |
566 | { | |
567 | fprintf (out, "State %d: %d lookaheads\n", | |
29e88316 | 568 | i, states[i]->nlookaheads); |
7c6b64d0 | 569 | |
29e88316 | 570 | for (j = 0; j < states[i]->nlookaheads; ++j) |
7c6b64d0 | 571 | for (k = 0; k < ntokens; ++k) |
602bbf31 | 572 | if (bitset_test (LA[states[i]->lookaheadsp + j], j)) |
7c6b64d0 | 573 | fprintf (out, " on %d (%s) -> rule %d\n", |
ad949da9 | 574 | k, symbols[k]->tag, |
29e88316 | 575 | -LAruleno[states[i]->lookaheadsp + j] - 1); |
7c6b64d0 AD |
576 | } |
577 | fprintf (out, "Lookaheads: END\n"); | |
578 | } | |
579 | ||
720d742f AD |
580 | void |
581 | lalr (void) | |
582 | { | |
5edafffd | 583 | initialize_lookaheads (); |
720d742f AD |
584 | initialize_LA (); |
585 | set_goto_map (); | |
586 | initialize_F (); | |
587 | build_relations (); | |
588 | compute_FOLLOWS (); | |
589 | compute_lookaheads (); | |
7c6b64d0 AD |
590 | |
591 | if (trace_flag) | |
592 | lookaheads_print (stderr); | |
d0fb370f | 593 | } |