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