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