]>
Commit | Line | Data |
---|---|---|
d0fb370f | 1 | /* Compute look-ahead criteria for bison, |
b0299a2e AD |
2 | Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002 |
3 | Free Software Foundation, Inc. | |
d0fb370f | 4 | |
340ef489 | 5 | This file is part of Bison, the GNU Compiler Compiler. |
d0fb370f | 6 | |
340ef489 AD |
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. | |
d0fb370f | 11 | |
340ef489 AD |
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. | |
d0fb370f | 16 | |
340ef489 AD |
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. */ | |
d0fb370f RS |
21 | |
22 | ||
720d742f AD |
23 | /* Compute how to make the finite state machine deterministic; find |
24 | which rules need lookahead in each state, and which lookahead | |
340ef489 | 25 | tokens they accept. */ |
d0fb370f | 26 | |
d0fb370f | 27 | #include "system.h" |
602bbf31 | 28 | #include "bitset.h" |
914feea9 | 29 | #include "bitsetv.h" |
8b3df748 | 30 | #include "quotearg.h" |
62a3e4f0 AD |
31 | #include "symtab.h" |
32 | #include "gram.h" | |
7c6b64d0 | 33 | #include "reader.h" |
d0fb370f | 34 | #include "types.h" |
b2ca4022 | 35 | #include "LR0.h" |
a0f6b076 | 36 | #include "complain.h" |
720d742f | 37 | #include "lalr.h" |
3519ec76 | 38 | #include "nullable.h" |
340ef489 | 39 | #include "derives.h" |
f67d13aa | 40 | #include "getargs.h" |
d0fb370f | 41 | |
b0299a2e | 42 | rule_t **LArule = NULL; |
914feea9 | 43 | bitsetv LA = NULL; |
d200e455 | 44 | size_t nLA; |
9703cc49 | 45 | |
aa2aab3c | 46 | static int ngotos; |
602bbf31 | 47 | short *goto_map = NULL; |
d57650a5 AD |
48 | state_number_t *from_state = NULL; |
49 | state_number_t *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>. */ |
914feea9 | 53 | static bitsetv 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 | { |
d57650a5 | 134 | state_number_t i; |
720d742f | 135 | int j; |
b0299a2e | 136 | rule_t **np; |
d0fb370f | 137 | |
d200e455 | 138 | /* Avoid having to special case 0. */ |
a845a697 AD |
139 | if (!nLA) |
140 | nLA = 1; | |
d0fb370f | 141 | |
914feea9 | 142 | LA = bitsetv_create (nLA, ntokens, BITSET_FIXED); |
b0299a2e | 143 | LArule = XCALLOC (rule_t *, nLA); |
a845a697 | 144 | lookback = XCALLOC (shorts *, nLA); |
d0fb370f | 145 | |
b0299a2e | 146 | np = LArule; |
d0fb370f | 147 | for (i = 0; i < nstates; i++) |
29e88316 AD |
148 | if (!states[i]->consistent) |
149 | for (j = 0; j < states[i]->reductions->nreds; j++) | |
b0299a2e | 150 | *np++ = &rules[states[i]->reductions->rules[j]]; |
d0fb370f RS |
151 | } |
152 | ||
153 | ||
4a120d45 | 154 | static void |
d2729d44 | 155 | set_goto_map (void) |
d0fb370f | 156 | { |
d57650a5 | 157 | state_number_t state; |
720d742f | 158 | short *temp_map; |
d0fb370f | 159 | |
d7913476 AD |
160 | goto_map = XCALLOC (short, nvars + 1) - ntokens; |
161 | temp_map = XCALLOC (short, nvars + 1) - ntokens; | |
d0fb370f RS |
162 | |
163 | ngotos = 0; | |
7215de24 AD |
164 | for (state = 0; state < nstates; ++state) |
165 | { | |
29e88316 | 166 | shifts *sp = states[state]->shifts; |
d57650a5 | 167 | int i; |
7215de24 AD |
168 | for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
169 | { | |
62a3e4f0 AD |
170 | if (ngotos == SHRT_MAX) |
171 | fatal (_("too many gotos (max %d)"), SHRT_MAX); | |
d0fb370f | 172 | |
7215de24 | 173 | ngotos++; |
13ca549a | 174 | goto_map[SHIFT_SYMBOL (sp, i)]++; |
7215de24 AD |
175 | } |
176 | } | |
d0fb370f | 177 | |
d0b0fefa AD |
178 | { |
179 | int k = 0; | |
d57650a5 | 180 | int i; |
d0b0fefa AD |
181 | for (i = ntokens; i < nsyms; i++) |
182 | { | |
183 | temp_map[i] = k; | |
184 | k += goto_map[i]; | |
185 | } | |
d0fb370f | 186 | |
d0b0fefa AD |
187 | for (i = ntokens; i < nsyms; i++) |
188 | goto_map[i] = temp_map[i]; | |
d0fb370f | 189 | |
d0b0fefa AD |
190 | goto_map[nsyms] = ngotos; |
191 | temp_map[nsyms] = ngotos; | |
192 | } | |
d0fb370f | 193 | |
d57650a5 AD |
194 | from_state = XCALLOC (state_number_t, ngotos); |
195 | to_state = XCALLOC (state_number_t, ngotos); | |
d0fb370f | 196 | |
7215de24 | 197 | for (state = 0; state < nstates; ++state) |
d0fb370f | 198 | { |
29e88316 | 199 | shifts *sp = states[state]->shifts; |
d57650a5 | 200 | int i; |
aa2aab3c | 201 | for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i) |
d0fb370f | 202 | { |
13ca549a | 203 | int k = temp_map[SHIFT_SYMBOL (sp, i)]++; |
d0b0fefa | 204 | from_state[k] = state; |
13ca549a | 205 | to_state[k] = sp->shifts[i]; |
d0fb370f RS |
206 | } |
207 | } | |
208 | ||
d7913476 | 209 | XFREE (temp_map + ntokens); |
d0fb370f RS |
210 | } |
211 | ||
212 | ||
213 | ||
43591cec AD |
214 | /*----------------------------------------------------------. |
215 | | Map a state/symbol pair into its numeric representation. | | |
216 | `----------------------------------------------------------*/ | |
d0fb370f | 217 | |
4a120d45 | 218 | static int |
d57650a5 | 219 | map_goto (state_number_t state, symbol_number_t symbol) |
d0fb370f | 220 | { |
720d742f AD |
221 | int high; |
222 | int low; | |
223 | int middle; | |
d57650a5 | 224 | state_number_t s; |
d0fb370f RS |
225 | |
226 | low = goto_map[symbol]; | |
227 | high = goto_map[symbol + 1] - 1; | |
228 | ||
229 | while (low <= high) | |
230 | { | |
231 | middle = (low + high) / 2; | |
232 | s = from_state[middle]; | |
233 | if (s == state) | |
36281465 | 234 | return middle; |
d0fb370f RS |
235 | else if (s < state) |
236 | low = middle + 1; | |
237 | else | |
238 | high = middle - 1; | |
239 | } | |
240 | ||
43591cec AD |
241 | assert (0); |
242 | /* NOTREACHED */ | |
d0fb370f RS |
243 | return 0; |
244 | } | |
245 | ||
246 | ||
4a120d45 | 247 | static void |
d2729d44 | 248 | initialize_F (void) |
d0fb370f | 249 | { |
4d4f699c AD |
250 | short **reads = XCALLOC (short *, ngotos); |
251 | short *edge = XCALLOC (short, ngotos + 1); | |
252 | int nedges = 0; | |
d0fb370f | 253 | |
4d4f699c | 254 | int i; |
d0fb370f | 255 | |
914feea9 | 256 | F = bitsetv_create (ngotos, ntokens, BITSET_FIXED); |
d0fb370f | 257 | |
d0fb370f RS |
258 | for (i = 0; i < ngotos; i++) |
259 | { | |
d57650a5 | 260 | state_number_t stateno = to_state[i]; |
29e88316 | 261 | shifts *sp = states[stateno]->shifts; |
d0fb370f | 262 | |
d954473d AD |
263 | int j; |
264 | for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++) | |
0fb1ffb1 | 265 | bitset_set (F[i], SHIFT_SYMBOL (sp, j)); |
d0fb370f | 266 | |
d954473d AD |
267 | for (; j < sp->nshifts; j++) |
268 | { | |
a49aecd5 | 269 | symbol_number_t symbol = SHIFT_SYMBOL (sp, j); |
d954473d AD |
270 | if (nullable[symbol]) |
271 | edge[nedges++] = map_goto (stateno, symbol); | |
272 | } | |
a0f6b076 | 273 | |
d954473d AD |
274 | if (nedges) |
275 | { | |
276 | reads[i] = XCALLOC (short, nedges + 1); | |
62a3e4f0 | 277 | memcpy (reads[i], edge, nedges * sizeof (edge[0])); |
d954473d AD |
278 | reads[i][nedges] = -1; |
279 | nedges = 0; | |
d0fb370f | 280 | } |
d0fb370f RS |
281 | } |
282 | ||
720d742f | 283 | digraph (reads); |
d0fb370f RS |
284 | |
285 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf | 286 | XFREE (reads[i]); |
d0fb370f | 287 | |
d7913476 AD |
288 | XFREE (reads); |
289 | XFREE (edge); | |
d0fb370f RS |
290 | } |
291 | ||
292 | ||
4a120d45 | 293 | static void |
dac3c910 | 294 | add_lookback_edge (state_t *state, int ruleno, int gotono) |
d0fb370f | 295 | { |
720d742f | 296 | int i; |
720d742f | 297 | shorts *sp; |
d0fb370f | 298 | |
dac3c910 | 299 | for (i = 0; i < state->nlookaheads; ++i) |
c0263492 | 300 | if (state->lookaheads_rule[i]->number == ruleno) |
065fbd27 | 301 | break; |
d0fb370f | 302 | |
c0263492 | 303 | assert (state->lookaheads_rule[i]->number == ruleno); |
d0fb370f | 304 | |
d7913476 | 305 | sp = XCALLOC (shorts, 1); |
c0263492 | 306 | sp->next = lookback[(state->lookaheads - LA) + i]; |
d0fb370f | 307 | sp->value = gotono; |
c0263492 | 308 | lookback[(state->lookaheads - LA) + i] = sp; |
d0fb370f RS |
309 | } |
310 | ||
311 | ||
f67d13aa AD |
312 | static void |
313 | matrix_print (FILE *out, short **matrix, int n) | |
314 | { | |
315 | int i, j; | |
316 | ||
317 | for (i = 0; i < n; ++i) | |
318 | { | |
319 | fprintf (out, "%3d: ", i); | |
320 | if (matrix[i]) | |
321 | for (j = 0; matrix[i][j] != -1; ++j) | |
322 | fprintf (out, "%3d ", matrix[i][j]); | |
323 | fputc ('\n', out); | |
324 | } | |
325 | fputc ('\n', out); | |
326 | } | |
327 | ||
9887c18a AD |
328 | /*-------------------------------------------------------------------. |
329 | | Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is | | |
330 | | replaced with the result. | | |
f67d13aa AD |
331 | | | |
332 | | R_ARG[I] is NULL or a -1 terminated list of numbers. | | |
333 | | | | |
334 | | RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM | | |
335 | | is in R_ARG[I]. | | |
9887c18a AD |
336 | `-------------------------------------------------------------------*/ |
337 | ||
4a120d45 | 338 | static short ** |
d2729d44 | 339 | transpose (short **R_arg, int n) |
d0fb370f | 340 | { |
f67d13aa AD |
341 | /* The result. */ |
342 | short **new_R = XCALLOC (short *, n); | |
343 | /* END_R[I] -- next entry of NEW_R[I]. */ | |
344 | short **end_R = XCALLOC (short *, n); | |
345 | /* NEDGES[I] -- total size of NEW_R[I]. */ | |
346 | short *nedges = XCALLOC (short, n); | |
347 | int i, j; | |
348 | ||
349 | if (trace_flag) | |
d0fb370f | 350 | { |
f67d13aa AD |
351 | fputs ("transpose: input\n", stderr); |
352 | matrix_print (stderr, R_arg, n); | |
d0fb370f RS |
353 | } |
354 | ||
f67d13aa AD |
355 | /* Count. */ |
356 | for (i = 0; i < n; i++) | |
357 | if (R_arg[i]) | |
358 | for (j = 0; R_arg[i][j] >= 0; ++j) | |
359 | ++nedges[R_arg[i][j]]; | |
d0fb370f | 360 | |
f67d13aa | 361 | /* Allocate. */ |
d0fb370f | 362 | for (i = 0; i < n; i++) |
80a69750 AD |
363 | if (nedges[i] > 0) |
364 | { | |
365 | short *sp = XCALLOC (short, nedges[i] + 1); | |
80a69750 | 366 | sp[nedges[i]] = -1; |
f67d13aa AD |
367 | new_R[i] = sp; |
368 | end_R[i] = sp; | |
80a69750 | 369 | } |
d0fb370f | 370 | |
f67d13aa | 371 | /* Store. */ |
d0fb370f | 372 | for (i = 0; i < n; i++) |
f67d13aa AD |
373 | if (R_arg[i]) |
374 | for (j = 0; R_arg[i][j] >= 0; ++j) | |
375 | { | |
376 | *end_R[R_arg[i][j]] = i; | |
377 | ++end_R[R_arg[i][j]]; | |
378 | } | |
d0fb370f | 379 | |
f67d13aa AD |
380 | free (nedges); |
381 | free (end_R); | |
d0fb370f | 382 | |
9887c18a AD |
383 | /* Free the input: it is replaced with the result. */ |
384 | for (i = 0; i < n; i++) | |
385 | XFREE (R_arg[i]); | |
f67d13aa AD |
386 | free (R_arg); |
387 | ||
388 | if (trace_flag) | |
389 | { | |
390 | fputs ("transpose: output\n", stderr); | |
391 | matrix_print (stderr, new_R, n); | |
392 | } | |
9887c18a | 393 | |
36281465 | 394 | return new_R; |
d0fb370f RS |
395 | } |
396 | ||
397 | ||
4a120d45 | 398 | static void |
720d742f | 399 | build_relations (void) |
d0fb370f | 400 | { |
9887c18a | 401 | short *edge = XCALLOC (short, ngotos + 1); |
d57650a5 | 402 | state_number_t *states1 = XCALLOC (state_number_t, ritem_longest_rhs () + 1); |
720d742f | 403 | int i; |
720d742f | 404 | |
d7913476 | 405 | includes = XCALLOC (short *, ngotos); |
d0fb370f RS |
406 | |
407 | for (i = 0; i < ngotos; i++) | |
408 | { | |
9887c18a | 409 | int nedges = 0; |
a49aecd5 | 410 | symbol_number_t symbol1 = states[to_state[i]]->accessing_symbol; |
9887c18a | 411 | short *rulep; |
d0fb370f | 412 | |
720d742f AD |
413 | for (rulep = derives[symbol1]; *rulep > 0; rulep++) |
414 | { | |
9887c18a | 415 | int done; |
80a69750 | 416 | int length = 1; |
62a3e4f0 | 417 | item_number_t *rp; |
29e88316 | 418 | state_t *state = states[from_state[i]]; |
b9f71f19 | 419 | states1[0] = state->number; |
d0fb370f | 420 | |
99013900 | 421 | for (rp = rules[*rulep].rhs; *rp >= 0; rp++) |
720d742f | 422 | { |
dac3c910 | 423 | shifts *sp = state->shifts; |
9887c18a | 424 | int j; |
80a69750 | 425 | for (j = 0; j < sp->nshifts; j++) |
720d742f | 426 | { |
29e88316 | 427 | state = states[sp->shifts[j]]; |
5fbb0954 | 428 | if (state->accessing_symbol |
a49aecd5 | 429 | == item_number_as_symbol_number (*rp)) |
720d742f AD |
430 | break; |
431 | } | |
d0fb370f | 432 | |
b9f71f19 | 433 | states1[length++] = state->number; |
720d742f AD |
434 | } |
435 | ||
dac3c910 AD |
436 | if (!state->consistent) |
437 | add_lookback_edge (state, *rulep, i); | |
720d742f AD |
438 | |
439 | length--; | |
440 | done = 0; | |
441 | while (!done) | |
442 | { | |
443 | done = 1; | |
444 | rp--; | |
445 | /* JF added rp>=ritem && I hope to god its right! */ | |
446 | if (rp >= ritem && ISVAR (*rp)) | |
447 | { | |
a49aecd5 | 448 | /* Downcasting from item_number_t to symbol_number_t. */ |
5fbb0954 | 449 | edge[nedges++] = map_goto (states1[--length], |
a49aecd5 | 450 | item_number_as_symbol_number (*rp)); |
720d742f AD |
451 | if (nullable[*rp]) |
452 | done = 0; | |
453 | } | |
454 | } | |
d0fb370f RS |
455 | } |
456 | ||
720d742f AD |
457 | if (nedges) |
458 | { | |
9887c18a | 459 | int j; |
80a69750 | 460 | includes[i] = XCALLOC (short, nedges + 1); |
720d742f | 461 | for (j = 0; j < nedges; j++) |
80a69750 AD |
462 | includes[i][j] = edge[j]; |
463 | includes[i][nedges] = -1; | |
720d742f | 464 | } |
d0fb370f RS |
465 | } |
466 | ||
d7913476 | 467 | XFREE (edge); |
b9f71f19 | 468 | XFREE (states1); |
9887c18a AD |
469 | |
470 | includes = transpose (includes, ngotos); | |
d0fb370f RS |
471 | } |
472 | ||
473 | ||
720d742f | 474 | |
4a120d45 | 475 | static void |
720d742f | 476 | compute_FOLLOWS (void) |
d0fb370f | 477 | { |
720d742f | 478 | int i; |
d0fb370f | 479 | |
720d742f | 480 | digraph (includes); |
d0fb370f RS |
481 | |
482 | for (i = 0; i < ngotos; i++) | |
ddcd5fdf | 483 | XFREE (includes[i]); |
d0fb370f | 484 | |
d7913476 | 485 | XFREE (includes); |
d0fb370f RS |
486 | } |
487 | ||
488 | ||
4a120d45 | 489 | static void |
720d742f | 490 | compute_lookaheads (void) |
d0fb370f | 491 | { |
d200e455 | 492 | size_t i; |
720d742f | 493 | shorts *sp; |
d0fb370f | 494 | |
d200e455 | 495 | for (i = 0; i < nLA; i++) |
ddcd5fdf | 496 | for (sp = lookback[i]; sp; sp = sp->next) |
0fb1ffb1 | 497 | bitset_or (LA[i], LA[i], F[sp->value]); |
d0fb370f | 498 | |
ddcd5fdf | 499 | /* Free LOOKBACK. */ |
d200e455 | 500 | for (i = 0; i < nLA; i++) |
300f275f | 501 | LIST_FREE (shorts, lookback[i]); |
d0fb370f | 502 | |
d7913476 | 503 | XFREE (lookback); |
914feea9 | 504 | bitsetv_free (F); |
720d742f | 505 | } |
d0fb370f | 506 | |
d0fb370f | 507 | |
c0263492 AD |
508 | /*-------------------------------------------------------------. |
509 | | Count the number of lookaheads required for each state | | |
510 | | (NLOOKAHEADS member). Compute the total number of LA, NLA. | | |
511 | `-------------------------------------------------------------*/ | |
5edafffd AD |
512 | |
513 | static void | |
c0263492 | 514 | states_lookaheads_count (void) |
5edafffd | 515 | { |
d57650a5 | 516 | state_number_t i; |
d200e455 | 517 | nLA = 0; |
c0263492 AD |
518 | |
519 | /* Count */ | |
5edafffd AD |
520 | for (i = 0; i < nstates; i++) |
521 | { | |
522 | int k; | |
3877f72b | 523 | int nlookaheads = 0; |
29e88316 AD |
524 | reductions *rp = states[i]->reductions; |
525 | shifts *sp = states[i]->shifts; | |
5edafffd | 526 | |
80dac38c AD |
527 | /* We need a lookahead either to distinguish different |
528 | reductions (i.e., there are two or more), or to distinguish a | |
529 | reduction from a shift. Otherwise, it is straightforward, | |
530 | and the state is `consistent'. */ | |
531 | if (rp->nreds > 1 | |
532 | || (rp->nreds == 1 && sp->nshifts && SHIFT_IS_SHIFT (sp, 0))) | |
3877f72b | 533 | nlookaheads += rp->nreds; |
5edafffd | 534 | else |
29e88316 | 535 | states[i]->consistent = 1; |
5edafffd AD |
536 | |
537 | for (k = 0; k < sp->nshifts; k++) | |
538 | if (SHIFT_IS_ERROR (sp, k)) | |
539 | { | |
29e88316 | 540 | states[i]->consistent = 0; |
5edafffd AD |
541 | break; |
542 | } | |
3877f72b | 543 | |
29e88316 | 544 | states[i]->nlookaheads = nlookaheads; |
d200e455 | 545 | nLA += nlookaheads; |
5edafffd | 546 | } |
5edafffd AD |
547 | } |
548 | ||
7c6b64d0 | 549 | |
c0263492 AD |
550 | /*--------------------------------------. |
551 | | Initializing the lookaheads members. | | |
552 | `--------------------------------------*/ | |
553 | ||
554 | static void | |
555 | states_lookaheads_initialize (void) | |
556 | { | |
d57650a5 | 557 | state_number_t i; |
c0263492 AD |
558 | bitsetv pLA = LA; |
559 | rule_t **pLArule = LArule; | |
560 | ||
9801d40c AD |
561 | /* Initialize the members LOOKAHEADS and LOOKAHEADS_RULE for each |
562 | state. */ | |
c0263492 AD |
563 | for (i = 0; i < nstates; i++) |
564 | { | |
565 | states[i]->lookaheads = pLA; | |
566 | states[i]->lookaheads_rule = pLArule; | |
567 | pLA += states[i]->nlookaheads; | |
568 | pLArule += states[i]->nlookaheads; | |
569 | } | |
570 | } | |
571 | ||
572 | ||
7c6b64d0 AD |
573 | /*---------------------------------------. |
574 | | Output the lookaheads for each state. | | |
575 | `---------------------------------------*/ | |
576 | ||
577 | static void | |
578 | lookaheads_print (FILE *out) | |
579 | { | |
d57650a5 | 580 | state_number_t i; |
602bbf31 | 581 | int j, k; |
7c6b64d0 AD |
582 | fprintf (out, "Lookaheads: BEGIN\n"); |
583 | for (i = 0; i < nstates; ++i) | |
584 | { | |
585 | fprintf (out, "State %d: %d lookaheads\n", | |
29e88316 | 586 | i, states[i]->nlookaheads); |
7c6b64d0 | 587 | |
29e88316 | 588 | for (j = 0; j < states[i]->nlookaheads; ++j) |
7c6b64d0 | 589 | for (k = 0; k < ntokens; ++k) |
c0263492 | 590 | if (bitset_test (states[i]->lookaheads[j], k)) |
7c6b64d0 | 591 | fprintf (out, " on %d (%s) -> rule %d\n", |
6b98e4b5 | 592 | k, symbol_tag_get (symbols[k]), |
c0263492 | 593 | states[i]->lookaheads_rule[j]->number - 1); |
7c6b64d0 AD |
594 | } |
595 | fprintf (out, "Lookaheads: END\n"); | |
596 | } | |
597 | ||
720d742f AD |
598 | void |
599 | lalr (void) | |
600 | { | |
c0263492 | 601 | states_lookaheads_count (); |
720d742f | 602 | initialize_LA (); |
c0263492 | 603 | states_lookaheads_initialize (); |
720d742f AD |
604 | set_goto_map (); |
605 | initialize_F (); | |
606 | build_relations (); | |
607 | compute_FOLLOWS (); | |
608 | compute_lookaheads (); | |
7c6b64d0 AD |
609 | |
610 | if (trace_flag) | |
611 | lookaheads_print (stderr); | |
d0fb370f | 612 | } |