]> git.saurik.com Git - bison.git/blame - src/lalr.c
* tests/regression.at (Unresolved SR Conflicts):
[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
d200e455 36/* All the decorated states, indexed by the state number. */
f693ad14 37state_t **state_table = NULL;
9703cc49 38
d0fb370f 39int tokensetsize;
d0fb370f
RS
40short *LAruleno;
41unsigned *LA;
d200e455 42size_t nLA;
9703cc49 43
aa2aab3c 44static int ngotos;
d0fb370f
RS
45short *goto_map;
46short *from_state;
47short *to_state;
48
fe961097 49/* And for the famous F variable, which name is so descriptive that a
ddcd5fdf
AD
50 comment is hardly needed. <grin>. */
51static unsigned *F = NULL;
52#define F(Rule) (F + (Rule) * tokensetsize)
53
d0fb370f
RS
54static short **includes;
55static shorts **lookback;
aa2aab3c
AD
56
57
58/*---------------------------------------------------------------.
59| digraph & traverse. |
60| |
61| The following variables are used as common storage between the |
62| two. |
63`---------------------------------------------------------------*/
64
d0fb370f
RS
65static short **R;
66static short *INDEX;
67static short *VERTICES;
68static int top;
aa2aab3c 69static int infinity;
d0fb370f 70
720d742f
AD
71static void
72traverse (int i)
d0fb370f 73{
720d742f 74 int j;
fe961097 75 size_t k;
720d742f 76 int height;
fe961097 77 size_t size = F (i + 1) - F(i);
720d742f
AD
78
79 VERTICES[++top] = i;
80 INDEX[i] = height = top;
81
fe961097
AD
82 if (R[i])
83 for (j = 0; R[i][j] >= 0; ++j)
84 {
85 if (INDEX[R[i][j]] == 0)
86 traverse (R[i][j]);
720d742f 87
fe961097
AD
88 if (INDEX[i] > INDEX[R[i][j]])
89 INDEX[i] = INDEX[R[i][j]];
720d742f 90
fe961097
AD
91 for (k = 0; k < size; ++k)
92 F (i)[k] |= F (R[i][j])[k];
93 }
720d742f
AD
94
95 if (INDEX[i] == height)
fe961097
AD
96 for (;;)
97 {
98 j = VERTICES[top--];
99 INDEX[j] = infinity;
720d742f 100
fe961097
AD
101 if (i == j)
102 break;
720d742f 103
fe961097 104 for (k = 0; k < size; ++k)
b9752825 105 F (j)[k] = F (i)[k];
fe961097 106 }
d0fb370f
RS
107}
108
109
720d742f
AD
110static void
111digraph (short **relation)
112{
113 int i;
114
115 infinity = ngotos + 2;
d7913476
AD
116 INDEX = XCALLOC (short, ngotos + 1);
117 VERTICES = XCALLOC (short, ngotos + 1);
720d742f
AD
118 top = 0;
119
120 R = relation;
121
122 for (i = 0; i < ngotos; i++)
123 INDEX[i] = 0;
124
125 for (i = 0; i < ngotos; i++)
ddcd5fdf
AD
126 if (INDEX[i] == 0 && R[i])
127 traverse (i);
720d742f 128
d7913476
AD
129 XFREE (INDEX);
130 XFREE (VERTICES);
720d742f
AD
131}
132
bb527fc2 133
4a120d45 134static void
d2729d44 135initialize_LA (void)
d0fb370f 136{
720d742f
AD
137 int i;
138 int j;
720d742f 139 short *np;
d0fb370f 140
d200e455 141 /* Avoid having to special case 0. */
a845a697
AD
142 if (!nLA)
143 nLA = 1;
d0fb370f 144
a845a697
AD
145 LA = XCALLOC (unsigned, nLA * tokensetsize);
146 LAruleno = XCALLOC (short, nLA);
147 lookback = XCALLOC (shorts *, nLA);
d0fb370f
RS
148
149 np = LAruleno;
150 for (i = 0; i < nstates; i++)
f693ad14 151 if (!state_table[i]->consistent)
80dac38c
AD
152 for (j = 0; j < state_table[i]->reductions->nreds; j++)
153 *np++ = state_table[i]->reductions->rules[j];
d0fb370f
RS
154}
155
156
4a120d45 157static void
d2729d44 158set_goto_map (void)
d0fb370f 159{
d0b0fefa 160 int state, i;
720d742f 161 short *temp_map;
d0fb370f 162
d7913476
AD
163 goto_map = XCALLOC (short, nvars + 1) - ntokens;
164 temp_map = XCALLOC (short, nvars + 1) - ntokens;
d0fb370f
RS
165
166 ngotos = 0;
7215de24
AD
167 for (state = 0; state < nstates; ++state)
168 {
169 shifts *sp = state_table[state]->shifts;
170 for (i = sp->nshifts - 1; i >= 0 && SHIFT_IS_GOTO (sp, i); --i)
171 {
7215de24
AD
172 if (ngotos == MAXSHORT)
173 fatal (_("too many gotos (max %d)"), MAXSHORT);
d0fb370f 174
7215de24 175 ngotos++;
13ca549a 176 goto_map[SHIFT_SYMBOL (sp, i)]++;
7215de24
AD
177 }
178 }
d0fb370f 179
d0b0fefa
AD
180 {
181 int k = 0;
182 for (i = ntokens; i < nsyms; i++)
183 {
184 temp_map[i] = k;
185 k += goto_map[i];
186 }
d0fb370f 187
d0b0fefa
AD
188 for (i = ntokens; i < nsyms; i++)
189 goto_map[i] = temp_map[i];
d0fb370f 190
d0b0fefa
AD
191 goto_map[nsyms] = ngotos;
192 temp_map[nsyms] = ngotos;
193 }
d0fb370f 194
d7913476
AD
195 from_state = XCALLOC (short, ngotos);
196 to_state = XCALLOC (short, ngotos);
d0fb370f 197
7215de24 198 for (state = 0; state < nstates; ++state)
d0fb370f 199 {
7215de24 200 shifts *sp = state_table[state]->shifts;
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 218static int
d2729d44 219map_goto (int state, int symbol)
d0fb370f 220{
720d742f
AD
221 int high;
222 int low;
223 int middle;
224 int 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 247static void
d2729d44 248initialize_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
4d4f699c 256 F = XCALLOC (unsigned, ngotos * tokensetsize);
d0fb370f 257
d0fb370f
RS
258 for (i = 0; i < ngotos; i++)
259 {
80a69750 260 int stateno = to_state[i];
f693ad14 261 shifts *sp = state_table[stateno]->shifts;
d0fb370f 262
d954473d
AD
263 int j;
264 for (j = 0; j < sp->nshifts && SHIFT_IS_SHIFT (sp, j); j++)
13ca549a 265 SETBIT (F (i), SHIFT_SYMBOL (sp, j));
d0fb370f 266
d954473d
AD
267 for (; j < sp->nshifts; j++)
268 {
13ca549a 269 int 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);
277 shortcpy (reads[i], edge, nedges);
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 293static void
dac3c910 294add_lookback_edge (state_t *state, int ruleno, int gotono)
d0fb370f 295{
720d742f 296 int i;
720d742f 297 shorts *sp;
d0fb370f 298
dac3c910
AD
299 for (i = 0; i < state->nlookaheads; ++i)
300 if (LAruleno[state->lookaheadsp + i] == ruleno)
065fbd27 301 break;
d0fb370f 302
dac3c910 303 assert (LAruleno[state->lookaheadsp + i] == ruleno);
d0fb370f 304
d7913476 305 sp = XCALLOC (shorts, 1);
dac3c910 306 sp->next = lookback[state->lookaheadsp + i];
d0fb370f 307 sp->value = gotono;
dac3c910 308 lookback[state->lookaheadsp + i] = sp;
d0fb370f
RS
309}
310
311
f67d13aa
AD
312static void
313matrix_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 338static short **
d2729d44 339transpose (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 398static void
720d742f 399build_relations (void)
d0fb370f 400{
9887c18a 401 short *edge = XCALLOC (short, ngotos + 1);
c2713865 402 short *states = XCALLOC (short, 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;
f693ad14 410 int symbol1 = state_table[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;
9887c18a 417 short *rp;
dac3c910
AD
418 state_t *state = state_table[from_state[i]];
419 states[0] = state->number;
d0fb370f 420
b2ed6e58 421 for (rp = ritem + rule_table[*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 {
dac3c910
AD
427 state = state_table[sp->shifts[j]];
428 if (state->accessing_symbol == *rp)
720d742f
AD
429 break;
430 }
d0fb370f 431
dac3c910 432 states[length++] = state->number;
720d742f
AD
433 }
434
dac3c910
AD
435 if (!state->consistent)
436 add_lookback_edge (state, *rulep, i);
720d742f
AD
437
438 length--;
439 done = 0;
440 while (!done)
441 {
442 done = 1;
443 rp--;
444 /* JF added rp>=ritem && I hope to god its right! */
445 if (rp >= ritem && ISVAR (*rp))
446 {
dac3c910 447 edge[nedges++] = map_goto (states[--length], *rp);
720d742f
AD
448 if (nullable[*rp])
449 done = 0;
450 }
451 }
d0fb370f
RS
452 }
453
720d742f
AD
454 if (nedges)
455 {
9887c18a 456 int j;
80a69750 457 includes[i] = XCALLOC (short, nedges + 1);
720d742f 458 for (j = 0; j < nedges; j++)
80a69750
AD
459 includes[i][j] = edge[j];
460 includes[i][nedges] = -1;
720d742f 461 }
d0fb370f
RS
462 }
463
d7913476
AD
464 XFREE (edge);
465 XFREE (states);
9887c18a
AD
466
467 includes = transpose (includes, ngotos);
d0fb370f
RS
468}
469
470
720d742f 471
4a120d45 472static void
720d742f 473compute_FOLLOWS (void)
d0fb370f 474{
720d742f 475 int i;
d0fb370f 476
720d742f 477 digraph (includes);
d0fb370f
RS
478
479 for (i = 0; i < ngotos; i++)
ddcd5fdf 480 XFREE (includes[i]);
d0fb370f 481
d7913476 482 XFREE (includes);
d0fb370f
RS
483}
484
485
4a120d45 486static void
720d742f 487compute_lookaheads (void)
d0fb370f 488{
d200e455 489 size_t i;
720d742f 490 shorts *sp;
d0fb370f 491
d200e455 492 for (i = 0; i < nLA; i++)
ddcd5fdf
AD
493 for (sp = lookback[i]; sp; sp = sp->next)
494 {
9887c18a
AD
495 int size = LA (i + 1) - LA (i);
496 int j;
497 for (j = 0; j < size; ++j)
498 LA (i)[j] |= F (sp->value)[j];
ddcd5fdf 499 }
d0fb370f 500
ddcd5fdf 501 /* Free LOOKBACK. */
d200e455 502 for (i = 0; i < nLA; i++)
300f275f 503 LIST_FREE (shorts, lookback[i]);
d0fb370f 504
d7913476
AD
505 XFREE (lookback);
506 XFREE (F);
720d742f 507}
d0fb370f 508
d0fb370f 509
5edafffd
AD
510/*--------------------------------------.
511| Initializing the lookaheads members. |
512`--------------------------------------*/
513
514static void
515initialize_lookaheads (void)
516{
517 int i;
d200e455 518 nLA = 0;
5edafffd
AD
519 for (i = 0; i < nstates; i++)
520 {
521 int k;
3877f72b 522 int nlookaheads = 0;
5edafffd
AD
523 reductions *rp = state_table[i]->reductions;
524 shifts *sp = state_table[i]->shifts;
525
80dac38c
AD
526 /* We need a lookahead either to distinguish different
527 reductions (i.e., there are two or more), or to distinguish a
528 reduction from a shift. Otherwise, it is straightforward,
529 and the state is `consistent'. */
530 if (rp->nreds > 1
531 || (rp->nreds == 1 && sp->nshifts && SHIFT_IS_SHIFT (sp, 0)))
3877f72b 532 nlookaheads += rp->nreds;
5edafffd
AD
533 else
534 state_table[i]->consistent = 1;
535
536 for (k = 0; k < sp->nshifts; k++)
537 if (SHIFT_IS_ERROR (sp, k))
538 {
539 state_table[i]->consistent = 0;
540 break;
541 }
3877f72b
AD
542
543 state_table[i]->nlookaheads = nlookaheads;
d200e455
AD
544 state_table[i]->lookaheadsp = nLA;
545 nLA += nlookaheads;
5edafffd 546 }
5edafffd
AD
547}
548
720d742f
AD
549void
550lalr (void)
551{
552 tokensetsize = WORDSIZE (ntokens);
553
5edafffd 554 initialize_lookaheads ();
720d742f
AD
555 initialize_LA ();
556 set_goto_map ();
557 initialize_F ();
558 build_relations ();
559 compute_FOLLOWS ();
560 compute_lookaheads ();
d0fb370f 561}