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