]> git.saurik.com Git - bison.git/blame - src/lalr.c
New experimental feature: if --verbose --trace output all the
[bison.git] / src / lalr.c
CommitLineData
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 */
39state_t *state_table = NULL;
40
d0fb370f 41int tokensetsize;
d0fb370f
RS
42short *LAruleno;
43unsigned *LA;
9703cc49 44
aa2aab3c 45static int ngotos;
d0fb370f
RS
46short *goto_map;
47short *from_state;
48short *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>. */
52static unsigned *F = NULL;
53#define F(Rule) (F + (Rule) * tokensetsize)
54
d0fb370f
RS
55static short **includes;
56static 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
66static short **R;
67static short *INDEX;
68static short *VERTICES;
69static int top;
aa2aab3c 70static int infinity;
d0fb370f 71
720d742f
AD
72static void
73traverse (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
111static void
112digraph (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
AD
134
135/*--------------------.
136| Build STATE_TABLE. |
137`--------------------*/
138
4a120d45 139static void
d2729d44 140set_state_table (void)
d0fb370f 141{
f004bf6a
AD
142 /* NSTATES + 1 because lookahead for the pseudo state number NSTATES
143 might be used (see conflicts.c). It is too opaque for me to
144 provide a probably less hacky implementation. --akim */
145 state_table = XCALLOC (state_t, nstates + 1);
d0fb370f 146
90b4416b
AD
147 {
148 core *sp;
149 for (sp = first_state; sp; sp = sp->next)
150 {
151 state_table[sp->number].state = sp;
152 state_table[sp->number].accessing_symbol = sp->accessing_symbol;
153 }
154 }
155
156 {
157 shifts *sp;
158 for (sp = first_shift; sp; sp = sp->next)
159 state_table[sp->number].shift_table = sp;
160 }
161
162 {
163 reductions *rp;
164 for (rp = first_reduction; rp; rp = rp->next)
165 state_table[rp->number].reduction_table = rp;
166 }
a845a697 167
d954473d
AD
168 /* Pessimization, but simplification of the code: make sense all the
169 states have a shift_table, even if reduced to 0 shifts. */
170 {
171 int i;
172 for (i = 0; i < nstates; i++)
173 if (!state_table[i].shift_table)
174 state_table[i].shift_table = shifts_new (0);
175 }
176
a845a697
AD
177 /* Initializing the lookaheads members. Please note that it must be
178 performed after having set some of the other members which are
179 used below. Change with extreme caution. */
180 {
181 int i;
182 int count = 0;
183 for (i = 0; i < nstates; i++)
184 {
185 int k;
186 reductions *rp = state_table[i].reduction_table;
187 shifts *sp = state_table[i].shift_table;
188
189 state_table[i].lookaheads = count;
190
191 if (rp
d954473d 192 && (rp->nreds > 1 || (sp->nshifts && SHIFT_IS_SHIFT (sp, 0))))
a845a697
AD
193 count += rp->nreds;
194 else
195 state_table[i].consistent = 1;
196
d954473d
AD
197 for (k = 0; k < sp->nshifts; k++)
198 if (SHIFT_IS_ERROR (sp, k))
199 {
200 state_table[i].consistent = 0;
201 break;
202 }
a845a697
AD
203 }
204 state_table[nstates].lookaheads = count;
205 }
d0fb370f
RS
206}
207
208
4a120d45 209static void
d2729d44 210initialize_LA (void)
d0fb370f 211{
720d742f
AD
212 int i;
213 int j;
720d742f 214 short *np;
a845a697 215 reductions *rp;
d0fb370f 216
a845a697
AD
217 size_t nLA = state_table[nstates].lookaheads;
218 if (!nLA)
219 nLA = 1;
d0fb370f 220
a845a697
AD
221 LA = XCALLOC (unsigned, nLA * tokensetsize);
222 LAruleno = XCALLOC (short, nLA);
223 lookback = XCALLOC (shorts *, nLA);
d0fb370f
RS
224
225 np = LAruleno;
226 for (i = 0; i < nstates; i++)
a845a697
AD
227 if (!state_table[i].consistent)
228 if ((rp = state_table[i].reduction_table))
229 for (j = 0; j < rp->nreds; j++)
230 *np++ = rp->rules[j];
d0fb370f
RS
231}
232
233
4a120d45 234static void
d2729d44 235set_goto_map (void)
d0fb370f 236{
720d742f
AD
237 shifts *sp;
238 int i;
239 int symbol;
240 int k;
241 short *temp_map;
242 int state2;
243 int state1;
d0fb370f 244
d7913476
AD
245 goto_map = XCALLOC (short, nvars + 1) - ntokens;
246 temp_map = XCALLOC (short, nvars + 1) - ntokens;
d0fb370f
RS
247
248 ngotos = 0;
249 for (sp = first_shift; sp; sp = sp->next)
d954473d
AD
250 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
251 {
252 symbol = state_table[sp->shifts[i]].accessing_symbol;
d0fb370f 253
d954473d
AD
254 if (ngotos == MAXSHORT)
255 fatal (_("too many gotos (max %d)"), MAXSHORT);
d0fb370f 256
d954473d
AD
257 ngotos++;
258 goto_map[symbol]++;
259 }
d0fb370f
RS
260
261 k = 0;
262 for (i = ntokens; i < nsyms; i++)
263 {
264 temp_map[i] = k;
265 k += goto_map[i];
266 }
267
268 for (i = ntokens; i < nsyms; i++)
269 goto_map[i] = temp_map[i];
270
271 goto_map[nsyms] = ngotos;
272 temp_map[nsyms] = ngotos;
273
d7913476
AD
274 from_state = XCALLOC (short, ngotos);
275 to_state = XCALLOC (short, ngotos);
d0fb370f
RS
276
277 for (sp = first_shift; sp; sp = sp->next)
278 {
279 state1 = sp->number;
aa2aab3c 280 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
d0fb370f
RS
281 {
282 state2 = sp->shifts[i];
9703cc49 283 symbol = state_table[state2].accessing_symbol;
d0fb370f 284
d0fb370f
RS
285 k = temp_map[symbol]++;
286 from_state[k] = state1;
287 to_state[k] = state2;
288 }
289 }
290
d7913476 291 XFREE (temp_map + ntokens);
d0fb370f
RS
292}
293
294
295
43591cec
AD
296/*----------------------------------------------------------.
297| Map a state/symbol pair into its numeric representation. |
298`----------------------------------------------------------*/
d0fb370f 299
4a120d45 300static int
d2729d44 301map_goto (int state, int symbol)
d0fb370f 302{
720d742f
AD
303 int high;
304 int low;
305 int middle;
306 int s;
d0fb370f
RS
307
308 low = goto_map[symbol];
309 high = goto_map[symbol + 1] - 1;
310
311 while (low <= high)
312 {
313 middle = (low + high) / 2;
314 s = from_state[middle];
315 if (s == state)
36281465 316 return middle;
d0fb370f
RS
317 else if (s < state)
318 low = middle + 1;
319 else
320 high = middle - 1;
321 }
322
43591cec
AD
323 assert (0);
324 /* NOTREACHED */
d0fb370f
RS
325 return 0;
326}
327
328
4a120d45 329static void
d2729d44 330initialize_F (void)
d0fb370f 331{
4d4f699c
AD
332 short **reads = XCALLOC (short *, ngotos);
333 short *edge = XCALLOC (short, ngotos + 1);
334 int nedges = 0;
d0fb370f 335
4d4f699c 336 int i;
d0fb370f 337
4d4f699c 338 F = XCALLOC (unsigned, ngotos * tokensetsize);
d0fb370f 339
d0fb370f
RS
340 for (i = 0; i < ngotos; i++)
341 {
80a69750
AD
342 int stateno = to_state[i];
343 shifts *sp = state_table[stateno].shift_table;
d0fb370f 344
d954473d
AD
345 int j;
346 for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++)
d0fb370f 347 {
d954473d
AD
348 int symbol = state_table[sp->shifts[j]].accessing_symbol;
349 SETBIT (F + i * tokensetsize, symbol);
350 }
d0fb370f 351
d954473d
AD
352 for (; j < sp->nshifts; j++)
353 {
354 int symbol = state_table[sp->shifts[j]].accessing_symbol;
355 if (nullable[symbol])
356 edge[nedges++] = map_goto (stateno, symbol);
357 }
a0f6b076 358
d954473d
AD
359 if (nedges)
360 {
361 reads[i] = XCALLOC (short, nedges + 1);
362 shortcpy (reads[i], edge, nedges);
363 reads[i][nedges] = -1;
364 nedges = 0;
d0fb370f 365 }
d0fb370f
RS
366 }
367
720d742f 368 digraph (reads);
d0fb370f
RS
369
370 for (i = 0; i < ngotos; i++)
ddcd5fdf 371 XFREE (reads[i]);
d0fb370f 372
d7913476
AD
373 XFREE (reads);
374 XFREE (edge);
d0fb370f
RS
375}
376
377
4a120d45 378static void
d2729d44 379add_lookback_edge (int stateno, int ruleno, int gotono)
d0fb370f 380{
720d742f
AD
381 int i;
382 int k;
383 int found;
384 shorts *sp;
d0fb370f 385
f004bf6a
AD
386 i = state_table[stateno].lookaheads;
387 k = state_table[stateno + 1].lookaheads;
d0fb370f
RS
388 found = 0;
389 while (!found && i < k)
390 {
391 if (LAruleno[i] == ruleno)
392 found = 1;
393 else
394 i++;
395 }
396
340ef489 397 assert (found);
d0fb370f 398
d7913476 399 sp = XCALLOC (shorts, 1);
d0fb370f
RS
400 sp->next = lookback[i];
401 sp->value = gotono;
402 lookback[i] = sp;
403}
404
405
f67d13aa
AD
406static void
407matrix_print (FILE *out, short **matrix, int n)
408{
409 int i, j;
410
411 for (i = 0; i < n; ++i)
412 {
413 fprintf (out, "%3d: ", i);
414 if (matrix[i])
415 for (j = 0; matrix[i][j] != -1; ++j)
416 fprintf (out, "%3d ", matrix[i][j]);
417 fputc ('\n', out);
418 }
419 fputc ('\n', out);
420}
421
9887c18a
AD
422/*-------------------------------------------------------------------.
423| Return the transpose of R_ARG, of size N. Destroy R_ARG, as it is |
424| replaced with the result. |
f67d13aa
AD
425| |
426| R_ARG[I] is NULL or a -1 terminated list of numbers. |
427| |
428| RESULT[NUM] is NULL or the -1 terminated list of the I such as NUM |
429| is in R_ARG[I]. |
9887c18a
AD
430`-------------------------------------------------------------------*/
431
4a120d45 432static short **
d2729d44 433transpose (short **R_arg, int n)
d0fb370f 434{
f67d13aa
AD
435 /* The result. */
436 short **new_R = XCALLOC (short *, n);
437 /* END_R[I] -- next entry of NEW_R[I]. */
438 short **end_R = XCALLOC (short *, n);
439 /* NEDGES[I] -- total size of NEW_R[I]. */
440 short *nedges = XCALLOC (short, n);
441 int i, j;
442
443 if (trace_flag)
d0fb370f 444 {
f67d13aa
AD
445 fputs ("transpose: input\n", stderr);
446 matrix_print (stderr, R_arg, n);
d0fb370f
RS
447 }
448
f67d13aa
AD
449 /* Count. */
450 for (i = 0; i < n; i++)
451 if (R_arg[i])
452 for (j = 0; R_arg[i][j] >= 0; ++j)
453 ++nedges[R_arg[i][j]];
d0fb370f 454
f67d13aa 455 /* Allocate. */
d0fb370f 456 for (i = 0; i < n; i++)
80a69750
AD
457 if (nedges[i] > 0)
458 {
459 short *sp = XCALLOC (short, nedges[i] + 1);
80a69750 460 sp[nedges[i]] = -1;
f67d13aa
AD
461 new_R[i] = sp;
462 end_R[i] = sp;
80a69750 463 }
d0fb370f 464
f67d13aa 465 /* Store. */
d0fb370f 466 for (i = 0; i < n; i++)
f67d13aa
AD
467 if (R_arg[i])
468 for (j = 0; R_arg[i][j] >= 0; ++j)
469 {
470 *end_R[R_arg[i][j]] = i;
471 ++end_R[R_arg[i][j]];
472 }
d0fb370f 473
f67d13aa
AD
474 free (nedges);
475 free (end_R);
d0fb370f 476
9887c18a
AD
477 /* Free the input: it is replaced with the result. */
478 for (i = 0; i < n; i++)
479 XFREE (R_arg[i]);
f67d13aa
AD
480 free (R_arg);
481
482 if (trace_flag)
483 {
484 fputs ("transpose: output\n", stderr);
485 matrix_print (stderr, new_R, n);
486 }
9887c18a 487
36281465 488 return new_R;
d0fb370f
RS
489}
490
491
4a120d45 492static void
720d742f 493build_relations (void)
d0fb370f 494{
9887c18a 495 short *edge = XCALLOC (short, ngotos + 1);
c2713865 496 short *states = XCALLOC (short, ritem_longest_rhs () + 1);
720d742f 497 int i;
720d742f 498
d7913476 499 includes = XCALLOC (short *, ngotos);
d0fb370f
RS
500
501 for (i = 0; i < ngotos; i++)
502 {
9887c18a
AD
503 int nedges = 0;
504 int state1 = from_state[i];
505 int symbol1 = state_table[to_state[i]].accessing_symbol;
506 short *rulep;
d0fb370f 507
720d742f
AD
508 for (rulep = derives[symbol1]; *rulep > 0; rulep++)
509 {
9887c18a 510 int done;
80a69750 511 int length = 1;
9887c18a
AD
512 int stateno = state1;
513 short *rp;
720d742f 514 states[0] = state1;
d0fb370f 515
b2ed6e58 516 for (rp = ritem + rule_table[*rulep].rhs; *rp > 0; rp++)
720d742f 517 {
80a69750 518 shifts *sp = state_table[stateno].shift_table;
9887c18a 519 int j;
80a69750 520 for (j = 0; j < sp->nshifts; j++)
720d742f
AD
521 {
522 stateno = sp->shifts[j];
9887c18a 523 if (state_table[stateno].accessing_symbol == *rp)
720d742f
AD
524 break;
525 }
d0fb370f 526
720d742f
AD
527 states[length++] = stateno;
528 }
529
de326cc0 530 if (!state_table[stateno].consistent)
720d742f
AD
531 add_lookback_edge (stateno, *rulep, i);
532
533 length--;
534 done = 0;
535 while (!done)
536 {
537 done = 1;
538 rp--;
539 /* JF added rp>=ritem && I hope to god its right! */
540 if (rp >= ritem && ISVAR (*rp))
541 {
542 stateno = states[--length];
543 edge[nedges++] = map_goto (stateno, *rp);
544 if (nullable[*rp])
545 done = 0;
546 }
547 }
d0fb370f
RS
548 }
549
720d742f
AD
550 if (nedges)
551 {
9887c18a 552 int j;
80a69750 553 includes[i] = XCALLOC (short, nedges + 1);
720d742f 554 for (j = 0; j < nedges; j++)
80a69750
AD
555 includes[i][j] = edge[j];
556 includes[i][nedges] = -1;
720d742f 557 }
d0fb370f
RS
558 }
559
d7913476
AD
560 XFREE (edge);
561 XFREE (states);
9887c18a
AD
562
563 includes = transpose (includes, ngotos);
d0fb370f
RS
564}
565
566
720d742f 567
4a120d45 568static void
720d742f 569compute_FOLLOWS (void)
d0fb370f 570{
720d742f 571 int i;
d0fb370f 572
720d742f 573 digraph (includes);
d0fb370f
RS
574
575 for (i = 0; i < ngotos; i++)
ddcd5fdf 576 XFREE (includes[i]);
d0fb370f 577
d7913476 578 XFREE (includes);
d0fb370f
RS
579}
580
581
4a120d45 582static void
720d742f 583compute_lookaheads (void)
d0fb370f 584{
720d742f 585 int i;
720d742f 586 shorts *sp;
d0fb370f 587
f004bf6a 588 for (i = 0; i < state_table[nstates].lookaheads; i++)
ddcd5fdf
AD
589 for (sp = lookback[i]; sp; sp = sp->next)
590 {
9887c18a
AD
591 int size = LA (i + 1) - LA (i);
592 int j;
593 for (j = 0; j < size; ++j)
594 LA (i)[j] |= F (sp->value)[j];
ddcd5fdf 595 }
d0fb370f 596
ddcd5fdf 597 /* Free LOOKBACK. */
f004bf6a 598 for (i = 0; i < state_table[nstates].lookaheads; i++)
300f275f 599 LIST_FREE (shorts, lookback[i]);
d0fb370f 600
d7913476
AD
601 XFREE (lookback);
602 XFREE (F);
720d742f 603}
d0fb370f 604
d0fb370f 605
720d742f
AD
606void
607lalr (void)
608{
609 tokensetsize = WORDSIZE (ntokens);
610
611 set_state_table ();
720d742f
AD
612 initialize_LA ();
613 set_goto_map ();
614 initialize_F ();
615 build_relations ();
616 compute_FOLLOWS ();
617 compute_lookaheads ();
d0fb370f 618}