]> git.saurik.com Git - bison.git/blame - src/lalr.c
Use "Unresolved reference" error message when no symbols were found
[bison.git] / src / lalr.c
CommitLineData
742e4900 1/* Compute lookahead criteria for Bison.
5362cd5f 2
e9ad4aec 3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2003, 2004, 2005,
7254f6a8 4 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
d0fb370f 5
340ef489 6 This file is part of Bison, the GNU Compiler Compiler.
d0fb370f 7
f16b0819 8 This program is free software: you can redistribute it and/or modify
340ef489 9 it under the terms of the GNU General Public License as published by
f16b0819
PE
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
d0fb370f 12
f16b0819 13 This program is distributed in the hope that it will be useful,
340ef489
AD
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
d0fb370f 17
340ef489 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
d0fb370f
RS
20
21
720d742f 22/* Compute how to make the finite state machine deterministic; find
742e4900 23 which rules need lookahead in each state, and which lookahead
340ef489 24 tokens they accept. */
d0fb370f 25
2cec9080 26#include <config.h>
d0fb370f 27#include "system.h"
5dde5425
PE
28
29#include <bitset.h>
30#include <bitsetv.h>
31#include <quotearg.h>
32
b2ca4022 33#include "LR0.h"
a0f6b076 34#include "complain.h"
340ef489 35#include "derives.h"
f67d13aa 36#include "getargs.h"
5dde5425
PE
37#include "gram.h"
38#include "lalr.h"
7254f6a8 39#include "muscle-tab.h"
5dde5425
PE
40#include "nullable.h"
41#include "reader.h"
42#include "relation.h"
43#include "symtab.h"
d0fb370f 44
4cf44c00 45goto_number *goto_map;
db34f798 46goto_number ngotos;
4cf44c00
PE
47state_number *from_state;
48state_number *to_state;
db34f798 49bitsetv goto_follows = NULL;
e68e0410
AD
50
51/* Linked list of goto numbers. */
5dde5425 52typedef struct goto_list
e68e0410 53{
5dde5425
PE
54 struct goto_list *next;
55 goto_number value;
56} goto_list;
e68e0410
AD
57
58
b09f4f48 59/* LA is an NLA by NTOKENS matrix of bits. LA[l, i] is 1 if the rule
3325ddc4
AD
60 LArule[l] is applicable in the appropriate state when the next
61 token is symbol i. If LA[l, i] and LA[l, j] are both 1 for i != j,
62 it is a conflict. */
63
64static bitsetv LA = NULL;
d200e455 65size_t nLA;
9703cc49 66
d0fb370f 67
5dde5425
PE
68static goto_number **includes;
69static goto_list **lookback;
aa2aab3c
AD
70
71
720d742f 72
bb527fc2 73
db34f798 74void
d2729d44 75set_goto_map (void)
d0fb370f 76{
5dde5425
PE
77 state_number s;
78 goto_number *temp_map;
d0fb370f 79
da2a7671
PE
80 goto_map = xcalloc (nvars + 1, sizeof *goto_map);
81 temp_map = xnmalloc (nvars + 1, sizeof *temp_map);
d0fb370f
RS
82
83 ngotos = 0;
5dde5425 84 for (s = 0; s < nstates; ++s)
7215de24 85 {
5dde5425 86 transitions *sp = states[s]->transitions;
d57650a5 87 int i;
ccaf65bc 88 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
7215de24 89 {
7215de24 90 ngotos++;
60e3ecc7
PE
91
92 /* Abort if (ngotos + 1) would overflow. */
4f82b42a 93 aver (ngotos != GOTO_NUMBER_MAXIMUM);
60e3ecc7 94
5362cd5f 95 goto_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
7215de24
AD
96 }
97 }
d0fb370f 98
d0b0fefa 99 {
60e3ecc7 100 goto_number k = 0;
d57650a5 101 int i;
d0b0fefa
AD
102 for (i = ntokens; i < nsyms; i++)
103 {
5362cd5f
PE
104 temp_map[i - ntokens] = k;
105 k += goto_map[i - ntokens];
d0b0fefa 106 }
d0fb370f 107
d0b0fefa 108 for (i = ntokens; i < nsyms; i++)
5362cd5f 109 goto_map[i - ntokens] = temp_map[i - ntokens];
d0fb370f 110
5362cd5f
PE
111 goto_map[nsyms - ntokens] = ngotos;
112 temp_map[nsyms - ntokens] = ngotos;
d0b0fefa 113 }
d0fb370f 114
da2a7671
PE
115 from_state = xcalloc (ngotos, sizeof *from_state);
116 to_state = xcalloc (ngotos, sizeof *to_state);
d0fb370f 117
5dde5425 118 for (s = 0; s < nstates; ++s)
d0fb370f 119 {
5dde5425 120 transitions *sp = states[s]->transitions;
d57650a5 121 int i;
ccaf65bc 122 for (i = sp->num - 1; i >= 0 && TRANSITION_IS_GOTO (sp, i); --i)
d0fb370f 123 {
60e3ecc7 124 goto_number k = temp_map[TRANSITION_SYMBOL (sp, i) - ntokens]++;
5dde5425 125 from_state[k] = s;
640748ee 126 to_state[k] = sp->states[i]->number;
d0fb370f
RS
127 }
128 }
129
afbb696d 130 free (temp_map);
d0fb370f
RS
131}
132
133
db34f798 134goto_number
5dde5425 135map_goto (state_number s0, symbol_number sym)
d0fb370f 136{
60e3ecc7
PE
137 goto_number high;
138 goto_number low;
139 goto_number middle;
5dde5425 140 state_number s;
d0fb370f 141
5362cd5f
PE
142 low = goto_map[sym - ntokens];
143 high = goto_map[sym - ntokens + 1] - 1;
d0fb370f 144
58a84254 145 for (;;)
d0fb370f 146 {
4f82b42a 147 aver (low <= high);
d0fb370f
RS
148 middle = (low + high) / 2;
149 s = from_state[middle];
5dde5425 150 if (s == s0)
36281465 151 return middle;
5dde5425 152 else if (s < s0)
d0fb370f
RS
153 low = middle + 1;
154 else
155 high = middle - 1;
156 }
d0fb370f
RS
157}
158
159
4a120d45 160static void
d2729d44 161initialize_F (void)
d0fb370f 162{
da2a7671
PE
163 goto_number **reads = xnmalloc (ngotos, sizeof *reads);
164 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
60e3ecc7 165 goto_number nedges = 0;
d0fb370f 166
60e3ecc7 167 goto_number i;
d0fb370f 168
db34f798 169 goto_follows = bitsetv_create (ngotos, ntokens, BITSET_FIXED);
d0fb370f 170
d0fb370f
RS
171 for (i = 0; i < ngotos; i++)
172 {
5dde5425
PE
173 state_number stateno = to_state[i];
174 transitions *sp = states[stateno]->transitions;
d0fb370f 175
d954473d 176 int j;
640748ee 177 FOR_EACH_SHIFT (sp, j)
db34f798 178 bitset_set (goto_follows[i], TRANSITION_SYMBOL (sp, j));
d0fb370f 179
ccaf65bc 180 for (; j < sp->num; j++)
d954473d 181 {
5dde5425 182 symbol_number sym = TRANSITION_SYMBOL (sp, j);
5362cd5f 183 if (nullable[sym - ntokens])
5dde5425 184 edge[nedges++] = map_goto (stateno, sym);
d954473d 185 }
a0f6b076 186
da2a7671
PE
187 if (nedges == 0)
188 reads[i] = NULL;
189 else
d954473d 190 {
da2a7671
PE
191 reads[i] = xnmalloc (nedges + 1, sizeof reads[i][0]);
192 memcpy (reads[i], edge, nedges * sizeof edge[0]);
60e3ecc7 193 reads[i][nedges] = END_NODE;
d954473d 194 nedges = 0;
d0fb370f 195 }
d0fb370f
RS
196 }
197
db34f798 198 relation_digraph (reads, ngotos, &goto_follows);
d0fb370f
RS
199
200 for (i = 0; i < ngotos; i++)
afbb696d 201 free (reads[i]);
d0fb370f 202
afbb696d
PE
203 free (reads);
204 free (edge);
d0fb370f
RS
205}
206
207
4a120d45 208static void
60e3ecc7 209add_lookback_edge (state *s, rule *r, goto_number gotono)
d0fb370f 210{
5dde5425 211 int ri = state_reduction_find (s, r);
da2a7671 212 goto_list *sp = xmalloc (sizeof *sp);
742e4900 213 sp->next = lookback[(s->reductions->lookahead_tokens - LA) + ri];
d0fb370f 214 sp->value = gotono;
742e4900 215 lookback[(s->reductions->lookahead_tokens - LA) + ri] = sp;
d0fb370f
RS
216}
217
218
d0fb370f 219
4a120d45 220static void
720d742f 221build_relations (void)
d0fb370f 222{
da2a7671
PE
223 goto_number *edge = xnmalloc (ngotos + 1, sizeof *edge);
224 state_number *states1 = xnmalloc (ritem_longest_rhs () + 1, sizeof *states1);
60e3ecc7 225 goto_number i;
720d742f 226
da2a7671 227 includes = xnmalloc (ngotos, sizeof *includes);
d0fb370f
RS
228
229 for (i = 0; i < ngotos; i++)
230 {
9887c18a 231 int nedges = 0;
5dde5425
PE
232 symbol_number symbol1 = states[to_state[i]]->accessing_symbol;
233 rule **rulep;
d0fb370f 234
5362cd5f 235 for (rulep = derives[symbol1 - ntokens]; *rulep; rulep++)
720d742f 236 {
d0829076 237 bool done;
80a69750 238 int length = 1;
e9ad4aec 239 item_number const *rp;
5dde5425
PE
240 state *s = states[from_state[i]];
241 states1[0] = s->number;
d0fb370f 242
e9ad4aec 243 for (rp = (*rulep)->rhs; ! item_number_is_rule_number (*rp); rp++)
720d742f 244 {
5dde5425
PE
245 s = transitions_to (s->transitions,
246 item_number_as_symbol_number (*rp));
247 states1[length++] = s->number;
720d742f
AD
248 }
249
5dde5425
PE
250 if (!s->consistent)
251 add_lookback_edge (s, *rulep, i);
720d742f
AD
252
253 length--;
d0829076 254 done = false;
720d742f
AD
255 while (!done)
256 {
d0829076 257 done = true;
fa6aa7b3 258 /* Each rhs ends in a rule number, and there is a
db34f798 259 sentinel (ritem[-1]=0) before the first rhs, so it is safe to
e9ad4aec 260 decrement RP here. */
720d742f 261 rp--;
e9ad4aec 262 if (ISVAR (*rp))
720d742f 263 {
5dde5425 264 /* Downcasting from item_number to symbol_number. */
5fbb0954 265 edge[nedges++] = map_goto (states1[--length],
a49aecd5 266 item_number_as_symbol_number (*rp));
5362cd5f 267 if (nullable[*rp - ntokens])
d0829076 268 done = false;
720d742f
AD
269 }
270 }
d0fb370f
RS
271 }
272
da2a7671
PE
273 if (nedges == 0)
274 includes[i] = NULL;
275 else
720d742f 276 {
9887c18a 277 int j;
da2a7671 278 includes[i] = xnmalloc (nedges + 1, sizeof includes[i][0]);
720d742f 279 for (j = 0; j < nedges; j++)
80a69750 280 includes[i][j] = edge[j];
60e3ecc7 281 includes[i][nedges] = END_NODE;
720d742f 282 }
d0fb370f
RS
283 }
284
afbb696d
PE
285 free (edge);
286 free (states1);
9887c18a 287
0e4d5753 288 relation_transpose (&includes, ngotos);
d0fb370f
RS
289}
290
291
720d742f 292
4a120d45 293static void
720d742f 294compute_FOLLOWS (void)
d0fb370f 295{
60e3ecc7 296 goto_number i;
d0fb370f 297
db34f798 298 relation_digraph (includes, ngotos, &goto_follows);
d0fb370f
RS
299
300 for (i = 0; i < ngotos; i++)
afbb696d 301 free (includes[i]);
d0fb370f 302
afbb696d 303 free (includes);
d0fb370f
RS
304}
305
306
4a120d45 307static void
742e4900 308compute_lookahead_tokens (void)
d0fb370f 309{
d200e455 310 size_t i;
5dde5425 311 goto_list *sp;
d0fb370f 312
d200e455 313 for (i = 0; i < nLA; i++)
ddcd5fdf 314 for (sp = lookback[i]; sp; sp = sp->next)
db34f798 315 bitset_or (LA[i], LA[i], goto_follows[sp->value]);
d0fb370f 316
ddcd5fdf 317 /* Free LOOKBACK. */
d200e455 318 for (i = 0; i < nLA; i++)
5dde5425 319 LIST_FREE (goto_list, lookback[i]);
d0fb370f 320
afbb696d 321 free (lookback);
720d742f 322}
d0fb370f 323
d0fb370f 324
742e4900
JD
325/*----------------------------------------------------.
326| Count the number of lookahead tokens required for S |
327| (N_LOOKAHEAD_TOKENS member). |
328`----------------------------------------------------*/
5edafffd 329
cd08e51e 330static int
110ef36a 331state_lookahead_tokens_count (state *s, bool default_reduction_only_for_accept)
5edafffd 332{
742e4900 333 int n_lookahead_tokens = 0;
5dde5425
PE
334 reductions *rp = s->reductions;
335 transitions *sp = s->transitions;
cd08e51e 336
7254f6a8
JD
337 /* Transitions are only disabled during conflict resolution, and that
338 hasn't happened yet, so there should be no need to check that
339 transition 0 hasn't been disabled before checking if it is a shift.
340 However, this check was performed at one time, so we leave it as an
341 aver. */
2a6b783d 342 aver (sp->num == 0 || !TRANSITION_IS_DISABLED (sp, 0));
7254f6a8
JD
343
344 /* We need a lookahead either to distinguish different reductions
345 (i.e., there are two or more), or to distinguish a reduction from a
346 shift. Otherwise, it is straightforward, and the state is
e459fb0e
JD
347 `consistent'. However, do not treat a state with any reductions as
348 consistent unless it is the accepting state (because there is never
349 a lookahead token that makes sense there, and so no lookahead token
350 should be read) if the user has otherwise disabled default
351 reductions. */
cd08e51e 352 if (rp->num > 1
7254f6a8
JD
353 || (rp->num == 1 && sp->num && TRANSITION_IS_SHIFT (sp, 0))
354 || (rp->num == 1 && rp->rules[0]->number != 0
110ef36a 355 && default_reduction_only_for_accept))
742e4900 356 n_lookahead_tokens += rp->num;
cd08e51e 357 else
5dde5425 358 s->consistent = 1;
cd08e51e 359
742e4900 360 return n_lookahead_tokens;
5edafffd
AD
361}
362
7c6b64d0 363
742e4900
JD
364/*----------------------------------------------------.
365| Compute LA, NLA, and the lookahead_tokens members. |
366`----------------------------------------------------*/
c0263492 367
db34f798 368void
cd08e51e 369initialize_LA (void)
c0263492 370{
5dde5425 371 state_number i;
cd08e51e 372 bitsetv pLA;
110ef36a 373 bool default_reduction_only_for_accept;
7254f6a8 374 {
110ef36a 375 char *default_reductions =
5bab9d08 376 muscle_percent_define_get ("lr.default-reductions");
110ef36a
JD
377 default_reduction_only_for_accept =
378 0 == strcmp (default_reductions, "accepting");
379 free (default_reductions);
7254f6a8 380 }
c0263492 381
742e4900 382 /* Compute the total number of reductions requiring a lookahead. */
cd08e51e
AD
383 nLA = 0;
384 for (i = 0; i < nstates; i++)
7254f6a8 385 nLA +=
110ef36a
JD
386 state_lookahead_tokens_count (states[i],
387 default_reduction_only_for_accept);
cd08e51e
AD
388 /* Avoid having to special case 0. */
389 if (!nLA)
390 nLA = 1;
391
392 pLA = LA = bitsetv_create (nLA, ntokens, BITSET_FIXED);
cd08e51e 393
742e4900
JD
394 /* Initialize the members LOOKAHEAD_TOKENS for each state whose reductions
395 require lookahead tokens. */
c0263492
AD
396 for (i = 0; i < nstates; i++)
397 {
7254f6a8 398 int count =
110ef36a
JD
399 state_lookahead_tokens_count (states[i],
400 default_reduction_only_for_accept);
cd08e51e
AD
401 if (count)
402 {
742e4900 403 states[i]->reductions->lookahead_tokens = pLA;
cd08e51e
AD
404 pLA += count;
405 }
c0263492
AD
406 }
407}
408
409
742e4900
JD
410/*---------------------------------------------.
411| Output the lookahead tokens for each state. |
412`---------------------------------------------*/
7c6b64d0
AD
413
414static void
742e4900 415lookahead_tokens_print (FILE *out)
7c6b64d0 416{
5dde5425 417 state_number i;
602bbf31 418 int j, k;
742e4900 419 fprintf (out, "Lookahead tokens: BEGIN\n");
7c6b64d0
AD
420 for (i = 0; i < nstates; ++i)
421 {
5dde5425 422 reductions *reds = states[i]->reductions;
613f5e1a 423 bitset_iterator iter;
742e4900 424 int n_lookahead_tokens = 0;
cd08e51e 425
742e4900 426 if (reds->lookahead_tokens)
cd08e51e 427 for (k = 0; k < reds->num; ++k)
742e4900
JD
428 if (reds->lookahead_tokens[k])
429 ++n_lookahead_tokens;
613f5e1a 430
742e4900
JD
431 fprintf (out, "State %d: %d lookahead tokens\n",
432 i, n_lookahead_tokens);
7c6b64d0 433
742e4900 434 if (reds->lookahead_tokens)
cd08e51e 435 for (j = 0; j < reds->num; ++j)
742e4900 436 BITSET_FOR_EACH (iter, reds->lookahead_tokens[j], k, 0)
cd08e51e 437 {
427c0dda 438 fprintf (out, " on %d (%s) -> rule %d\n",
cd08e51e
AD
439 k, symbols[k]->tag,
440 reds->rules[j]->number);
441 };
7c6b64d0 442 }
742e4900 443 fprintf (out, "Lookahead tokens: END\n");
7c6b64d0
AD
444}
445
720d742f
AD
446void
447lalr (void)
448{
720d742f
AD
449 initialize_LA ();
450 set_goto_map ();
451 initialize_F ();
db34f798 452 lookback = xcalloc (nLA, sizeof *lookback);
720d742f
AD
453 build_relations ();
454 compute_FOLLOWS ();
742e4900 455 compute_lookahead_tokens ();
7c6b64d0 456
273a74fa 457 if (trace_flag & trace_sets)
742e4900 458 lookahead_tokens_print (stderr);
d0fb370f 459}
3325ddc4
AD
460
461
5967f0cf
JD
462void
463lalr_update_state_numbers (state_number old_to_new[], state_number nstates_old)
464{
465 goto_number ngotos_reachable = 0;
466 symbol_number nonterminal = 0;
467 aver (nsyms == nvars + ntokens);
14462c2b
JD
468 {
469 goto_number i;
470 for (i = 0; i < ngotos; ++i)
471 {
472 while (i == goto_map[nonterminal])
473 goto_map[nonterminal++] = ngotos_reachable;
474 /* If old_to_new[from_state[i]] = nstates_old, remove this goto
475 entry. */
476 if (old_to_new[from_state[i]] != nstates_old)
477 {
478 /* from_state[i] is not removed, so it and thus to_state[i] are
479 reachable, so to_state[i] != nstates_old. */
480 aver (old_to_new[to_state[i]] != nstates_old);
481 from_state[ngotos_reachable] = old_to_new[from_state[i]];
482 to_state[ngotos_reachable] = old_to_new[to_state[i]];
483 ++ngotos_reachable;
484 }
485 }
486 }
5967f0cf
JD
487 while (nonterminal <= nvars)
488 {
489 aver (ngotos == goto_map[nonterminal]);
490 goto_map[nonterminal++] = ngotos_reachable;
491 }
492 ngotos = ngotos_reachable;
493}
494
495
3325ddc4
AD
496void
497lalr_free (void)
498{
5dde5425 499 state_number s;
3325ddc4 500 for (s = 0; s < nstates; ++s)
742e4900 501 states[s]->reductions->lookahead_tokens = NULL;
3325ddc4 502 bitsetv_free (LA);
3325ddc4 503}