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