]> git.saurik.com Git - bison.git/blame - src/lalr.c
* src/Makefile.am (LDADD): Link $(LIBINTL) last to avoid the
[bison.git] / src / lalr.c
CommitLineData
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"
0e4d5753 30#include "relation.h"
8b3df748 31#include "quotearg.h"
62a3e4f0
AD
32#include "symtab.h"
33#include "gram.h"
7c6b64d0 34#include "reader.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
e68e0410
AD
42goto_number_t *goto_map = NULL;
43static goto_number_t ngotos = 0;
44state_number_t *from_state = NULL;
45state_number_t *to_state = NULL;
46
47/* Linked list of goto numbers. */
48typedef struct goto_list_s
49{
50 struct goto_list_s *next;
51 goto_number_t value;
52} goto_list_t;
53
54
3325ddc4
AD
55/* LA is a LR by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
56 LArule[l] is applicable in the appropriate state when the next
57 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
58 it is a conflict. */
59
60static bitsetv LA = NULL;
d200e455 61size_t nLA;
9703cc49 62
d0fb370f 63
fe961097 64/* And for the famous F variable, which name is so descriptive that a
ddcd5fdf 65 comment is hardly needed. <grin>. */
914feea9 66static bitsetv F = NULL;
ddcd5fdf 67
e68e0410
AD
68static goto_number_t **includes;
69static goto_list_t **lookback;
aa2aab3c
AD
70
71
720d742f 72
bb527fc2 73
4a120d45 74static void
d2729d44 75set_goto_map (void)
d0fb370f 76{
d57650a5 77 state_number_t state;
e68e0410 78 goto_number_t *temp_map;
d0fb370f 79
e68e0410
AD
80 goto_map = XCALLOC (goto_number_t, nvars + 1) - ntokens;
81 temp_map = XCALLOC (goto_number_t, nvars + 1) - ntokens;
d0fb370f
RS
82
83 ngotos = 0;
7215de24
AD
84 for (state = 0; state < nstates; ++state)
85 {
8b752b00 86 transitions_t *sp = states[state]->transitions;
d57650a5 87 int i;
ccaf65bc 88 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
7215de24 89 {
b1ae9233 90 assert (ngotos < GOTO_NUMBER_MAX);
7215de24 91 ngotos++;
ccaf65bc 92 goto_map[TRANSITION_SYMBOL (sp, i)]++;
7215de24
AD
93 }
94 }
d0fb370f 95
d0b0fefa
AD
96 {
97 int k = 0;
d57650a5 98 int i;
d0b0fefa
AD
99 for (i = ntokens; i < nsyms; i++)
100 {
101 temp_map[i] = k;
102 k += goto_map[i];
103 }
d0fb370f 104
d0b0fefa
AD
105 for (i = ntokens; i < nsyms; i++)
106 goto_map[i] = temp_map[i];
d0fb370f 107
d0b0fefa
AD
108 goto_map[nsyms] = ngotos;
109 temp_map[nsyms] = ngotos;
110 }
d0fb370f 111
d57650a5
AD
112 from_state = XCALLOC (state_number_t, ngotos);
113 to_state = XCALLOC (state_number_t, ngotos);
d0fb370f 114
7215de24 115 for (state = 0; state < nstates; ++state)
d0fb370f 116 {
8b752b00 117 transitions_t *sp = states[state]->transitions;
d57650a5 118 int i;
ccaf65bc 119 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
d0fb370f 120 {
ccaf65bc 121 int k = temp_map[TRANSITION_SYMBOL (sp, i)]++;
d0b0fefa 122 from_state[k] = state;
640748ee 123 to_state[k] = sp->states[i]->number;
d0fb370f
RS
124 }
125 }
126
d7913476 127 XFREE (temp_map + ntokens);
d0fb370f
RS
128}
129
130
131
43591cec
AD
132/*----------------------------------------------------------.
133| Map a state/symbol pair into its numeric representation. |
134`----------------------------------------------------------*/
d0fb370f 135
4a120d45 136static int
d57650a5 137map_goto (state_number_t state, symbol_number_t symbol)
d0fb370f 138{
720d742f
AD
139 int high;
140 int low;
141 int middle;
d57650a5 142 state_number_t s;
d0fb370f
RS
143
144 low = goto_map[symbol];
145 high = goto_map[symbol + 1] - 1;
146
147 while (low <= high)
148 {
149 middle = (low + high) / 2;
150 s = from_state[middle];
151 if (s == state)
36281465 152 return middle;
d0fb370f
RS
153 else if (s < state)
154 low = middle + 1;
155 else
156 high = middle - 1;
157 }
158
43591cec
AD
159 assert (0);
160 /* NOTREACHED */
d0fb370f
RS
161 return 0;
162}
163
164
4a120d45 165static void
d2729d44 166initialize_F (void)
d0fb370f 167{
e68e0410
AD
168 goto_number_t **reads = XCALLOC (goto_number_t *, ngotos);
169 goto_number_t *edge = XCALLOC (goto_number_t, ngotos + 1);
4d4f699c 170 int nedges = 0;
d0fb370f 171
4d4f699c 172 int i;
d0fb370f 173
914feea9 174 F = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
d0fb370f 175
d0fb370f
RS
176 for (i = 0; i < ngotos; i++)
177 {
d57650a5 178 state_number_t stateno = to_state[i];
8b752b00 179 transitions_t *sp = states[stateno]->transitions;
d0fb370f 180
d954473d 181 int j;
640748ee 182 FOR_EACH_SHIFT (sp, j)
ccaf65bc 183 bitset_set (F[i], TRANSITION_SYMBOL (sp, j));
d0fb370f 184
ccaf65bc 185 for (; j < sp->num; j++)
d954473d 186 {
ccaf65bc 187 symbol_number_t symbol = TRANSITION_SYMBOL (sp, j);
d954473d
AD
188 if (nullable[symbol])
189 edge[nedges++] = map_goto (stateno, symbol);
190 }
a0f6b076 191
d954473d
AD
192 if (nedges)
193 {
e68e0410 194 reads[i] = XCALLOC (goto_number_t, nedges + 1);
62a3e4f0 195 memcpy (reads[i], edge, nedges * sizeof (edge[0]));
d954473d
AD
196 reads[i][nedges] = -1;
197 nedges = 0;
d0fb370f 198 }
d0fb370f
RS
199 }
200
0e4d5753 201 relation_digraph (reads, ngotos, &F);
d0fb370f
RS
202
203 for (i = 0; i < ngotos; i++)
ddcd5fdf 204 XFREE (reads[i]);
d0fb370f 205
d7913476
AD
206 XFREE (reads);
207 XFREE (edge);
d0fb370f
RS
208}
209
210
4a120d45 211static void
bb0027a9 212add_lookback_edge (state_t *state, rule_t *rule, int gotono)
d0fb370f 213{
cd08e51e
AD
214 int r = state_reduction_find (state, rule);
215 goto_list_t *sp = XCALLOC (goto_list_t, 1);
216 sp->next = lookback[(state->reductions->lookaheads - LA) + r];
d0fb370f 217 sp->value = gotono;
cd08e51e 218 lookback[(state->reductions->lookaheads - LA) + r] = sp;
d0fb370f
RS
219}
220
221
d0fb370f 222
4a120d45 223static void
720d742f 224build_relations (void)
d0fb370f 225{
e68e0410 226 goto_number_t *edge = XCALLOC (goto_number_t, ngotos + 1);
d57650a5 227 state_number_t *states1 = XCALLOC (state_number_t, ritem_longest_rhs () + 1);
720d742f 228 int i;
720d742f 229
e68e0410 230 includes = XCALLOC (goto_number_t *, ngotos);
d0fb370f
RS
231
232 for (i = 0; i < ngotos; i++)
233 {
9887c18a 234 int nedges = 0;
a49aecd5 235 symbol_number_t symbol1 = states[to_state[i]]->accessing_symbol;
bb0027a9 236 rule_t **rulep;
d0fb370f 237
bb0027a9 238 for (rulep = derives[symbol1]; *rulep; rulep++)
720d742f 239 {
9887c18a 240 int done;
80a69750 241 int length = 1;
62a3e4f0 242 item_number_t *rp;
29e88316 243 state_t *state = states[from_state[i]];
b9f71f19 244 states1[0] = state->number;
d0fb370f 245
bb0027a9 246 for (rp = (*rulep)->rhs; *rp >= 0; rp++)
720d742f 247 {
8b752b00
AD
248 state = transitions_to (state->transitions,
249 item_number_as_symbol_number (*rp));
b9f71f19 250 states1[length++] = state->number;
720d742f
AD
251 }
252
dac3c910
AD
253 if (!state->consistent)
254 add_lookback_edge (state, *rulep, i);
720d742f
AD
255
256 length--;
257 done = 0;
258 while (!done)
259 {
260 done = 1;
261 rp--;
262 /* JF added rp>=ritem && I hope to god its right! */
263 if (rp >= ritem && ISVAR (*rp))
264 {
a49aecd5 265 /* Downcasting from item_number_t to symbol_number_t. */
5fbb0954 266 edge[nedges++] = map_goto (states1[--length],
a49aecd5 267 item_number_as_symbol_number (*rp));
720d742f
AD
268 if (nullable[*rp])
269 done = 0;
270 }
271 }
d0fb370f
RS
272 }
273
720d742f
AD
274 if (nedges)
275 {
9887c18a 276 int j;
e68e0410 277 includes[i] = XCALLOC (goto_number_t, nedges + 1);
720d742f 278 for (j = 0; j < nedges; j++)
80a69750
AD
279 includes[i][j] = edge[j];
280 includes[i][nedges] = -1;
720d742f 281 }
d0fb370f
RS
282 }
283
d7913476 284 XFREE (edge);
b9f71f19 285 XFREE (states1);
9887c18a 286
0e4d5753 287 relation_transpose (&includes, ngotos);
d0fb370f
RS
288}
289
290
720d742f 291
4a120d45 292static void
720d742f 293compute_FOLLOWS (void)
d0fb370f 294{
720d742f 295 int i;
d0fb370f 296
0e4d5753 297 relation_digraph (includes, ngotos, &F);
d0fb370f
RS
298
299 for (i = 0; i < ngotos; i++)
ddcd5fdf 300 XFREE (includes[i]);
d0fb370f 301
d7913476 302 XFREE (includes);
d0fb370f
RS
303}
304
305
4a120d45 306static void
720d742f 307compute_lookaheads (void)
d0fb370f 308{
d200e455 309 size_t i;
e68e0410 310 goto_list_t *sp;
d0fb370f 311
d200e455 312 for (i = 0; i < nLA; i++)
ddcd5fdf 313 for (sp = lookback[i]; sp; sp = sp->next)
0fb1ffb1 314 bitset_or (LA[i], LA[i], F[sp->value]);
d0fb370f 315
ddcd5fdf 316 /* Free LOOKBACK. */
d200e455 317 for (i = 0; i < nLA; i++)
e68e0410 318 LIST_FREE (goto_list_t, lookback[i]);
d0fb370f 319
d7913476 320 XFREE (lookback);
914feea9 321 bitsetv_free (F);
720d742f 322}
d0fb370f 323
d0fb370f 324
cd08e51e
AD
325/*---------------------------------------------------------------.
326| Count the number of lookaheads required for STATE (NLOOKAHEADS |
327| member). |
328`---------------------------------------------------------------*/
5edafffd 329
cd08e51e
AD
330static int
331state_lookaheads_count (state_t *state)
5edafffd 332{
cd08e51e
AD
333 int k;
334 int nlookaheads = 0;
335 reductions_t *rp = state->reductions;
336 transitions_t *sp = state->transitions;
337
338 /* We need a lookahead either to distinguish different
339 reductions (i.e., there are two or more), or to distinguish a
340 reduction from a shift. Otherwise, it is straightforward,
341 and the state is `consistent'. */
342 if (rp->num > 1
343 || (rp->num == 1 && sp->num &&
344 !TRANSITION_IS_DISABLED (sp, 0) && TRANSITION_IS_SHIFT (sp, 0)))
345 nlookaheads += rp->num;
346 else
347 state->consistent = 1;
348
349 for (k = 0; k < sp->num; k++)
350 if (!TRANSITION_IS_DISABLED (sp, k) && TRANSITION_IS_ERROR (sp, k))
351 {
352 state->consistent = 0;
353 break;
354 }
3877f72b 355
cd08e51e 356 return nlookaheads;
5edafffd
AD
357}
358
7c6b64d0 359
cd08e51e
AD
360/*----------------------------------------------.
361| Compute LA, NLA, and the lookaheads members. |
362`----------------------------------------------*/
c0263492
AD
363
364static void
cd08e51e 365initialize_LA (void)
c0263492 366{
d57650a5 367 state_number_t i;
cd08e51e 368 bitsetv pLA;
c0263492 369
cd08e51e
AD
370 /* Compute the total number of reductions requiring a lookahead. */
371 nLA = 0;
372 for (i = 0; i < nstates; i++)
373 nLA += state_lookaheads_count (states[i]);
374 /* Avoid having to special case 0. */
375 if (!nLA)
376 nLA = 1;
377
378 pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
379 lookback = XCALLOC (goto_list_t *, nLA);
380
381 /* Initialize the members LOOKAHEADS for each state which reductions
382 require lookaheads. */
c0263492
AD
383 for (i = 0; i < nstates; i++)
384 {
cd08e51e
AD
385 int count = state_lookaheads_count (states[i]);
386 if (count)
387 {
388 states[i]->reductions->lookaheads = pLA;
389 pLA += count;
390 }
c0263492
AD
391 }
392}
393
394
7c6b64d0
AD
395/*---------------------------------------.
396| Output the lookaheads for each state. |
397`---------------------------------------*/
398
399static void
400lookaheads_print (FILE *out)
401{
d57650a5 402 state_number_t i;
602bbf31 403 int j, k;
427c0dda 404 fprintf (out, "Lookaheads: BEGIN\n");
7c6b64d0
AD
405 for (i = 0; i < nstates; ++i)
406 {
cd08e51e 407 reductions_t *reds = states[i]->reductions;
613f5e1a 408 bitset_iterator iter;
cd08e51e
AD
409 int nlookaheads = 0;
410
411 if (reds->lookaheads)
412 for (k = 0; k < reds->num; ++k)
413 if (reds->lookaheads[k])
414 ++nlookaheads;
613f5e1a 415
427c0dda 416 fprintf (out, "State %d: %d lookaheads\n",
cd08e51e 417 i, nlookaheads);
7c6b64d0 418
cd08e51e
AD
419 if (reds->lookaheads)
420 for (j = 0; j < reds->num; ++j)
421 BITSET_FOR_EACH (iter, reds->lookaheads[j], k, 0)
422 {
427c0dda 423 fprintf (out, " on %d (%s) -> rule %d\n",
cd08e51e
AD
424 k, symbols[k]->tag,
425 reds->rules[j]->number);
426 };
7c6b64d0 427 }
427c0dda 428 fprintf (out, "Lookaheads: END\n");
7c6b64d0
AD
429}
430
720d742f
AD
431void
432lalr (void)
433{
720d742f
AD
434 initialize_LA ();
435 set_goto_map ();
436 initialize_F ();
437 build_relations ();
438 compute_FOLLOWS ();
439 compute_lookaheads ();
7c6b64d0 440
273a74fa 441 if (trace_flag & trace_sets)
7c6b64d0 442 lookaheads_print (stderr);
d0fb370f 443}
3325ddc4
AD
444
445
446void
447lalr_free (void)
448{
449 state_number_t s;
450 for (s = 0; s < nstates; ++s)
cd08e51e 451 states[s]->reductions->lookaheads = NULL;
3325ddc4 452 bitsetv_free (LA);
3325ddc4 453}