1 /* Compute lookahead criteria for Bison.
3 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
22 /* Find which rules need lookahead in each state, and which lookahead
23 tokens they accept. */
38 #include "muscle-tab.h"
44 goto_number
*goto_map
;
46 state_number
*from_state
;
47 state_number
*to_state
;
48 bitsetv goto_follows
= NULL
;
50 /* Linked list of goto numbers. */
51 typedef struct goto_list
53 struct goto_list
*next
;
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,
63 static bitsetv LA
= NULL
;
67 static goto_number
**includes
;
68 static goto_list
**lookback
;
77 goto_number
*temp_map
;
79 goto_map
= xcalloc (nvars
+ 1, sizeof *goto_map
);
80 temp_map
= xnmalloc (nvars
+ 1, sizeof *temp_map
);
83 for (s
= 0; s
< nstates
; ++s
)
85 transitions
*sp
= states
[s
]->transitions
;
87 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
91 /* Abort if (ngotos + 1) would overflow. */
92 aver (ngotos
!= GOTO_NUMBER_MAXIMUM
);
94 goto_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
101 for (i
= ntokens
; i
< nsyms
; i
++)
103 temp_map
[i
- ntokens
] = k
;
104 k
+= goto_map
[i
- ntokens
];
107 for (i
= ntokens
; i
< nsyms
; i
++)
108 goto_map
[i
- ntokens
] = temp_map
[i
- ntokens
];
110 goto_map
[nsyms
- ntokens
] = ngotos
;
111 temp_map
[nsyms
- ntokens
] = ngotos
;
114 from_state
= xcalloc (ngotos
, sizeof *from_state
);
115 to_state
= xcalloc (ngotos
, sizeof *to_state
);
117 for (s
= 0; s
< nstates
; ++s
)
119 transitions
*sp
= states
[s
]->transitions
;
121 for (i
= sp
->num
- 1; i
>= 0 && TRANSITION_IS_GOTO (sp
, i
); --i
)
123 goto_number k
= temp_map
[TRANSITION_SYMBOL (sp
, i
) - ntokens
]++;
125 to_state
[k
] = sp
->states
[i
]->number
;
134 map_goto (state_number s0
, symbol_number sym
)
141 low
= goto_map
[sym
- ntokens
];
142 high
= goto_map
[sym
- ntokens
+ 1] - 1;
147 middle
= (low
+ high
) / 2;
148 s
= from_state
[middle
];
162 goto_number
**reads
= xnmalloc (ngotos
, sizeof *reads
);
163 goto_number
*edge
= xnmalloc (ngotos
+ 1, sizeof *edge
);
164 goto_number nedges
= 0;
168 goto_follows
= bitsetv_create (ngotos
, ntokens
, BITSET_FIXED
);
170 for (i
= 0; i
< ngotos
; i
++)
172 state_number stateno
= to_state
[i
];
173 transitions
*sp
= states
[stateno
]->transitions
;
176 FOR_EACH_SHIFT (sp
, j
)
177 bitset_set (goto_follows
[i
], TRANSITION_SYMBOL (sp
, j
));
179 for (; j
< sp
->num
; j
++)
181 symbol_number sym
= TRANSITION_SYMBOL (sp
, j
);
182 if (nullable
[sym
- ntokens
])
183 edge
[nedges
++] = map_goto (stateno
, sym
);
190 reads
[i
] = xnmalloc (nedges
+ 1, sizeof reads
[i
][0]);
191 memcpy (reads
[i
], edge
, nedges
* sizeof edge
[0]);
192 reads
[i
][nedges
] = END_NODE
;
197 relation_digraph (reads
, ngotos
, &goto_follows
);
199 for (i
= 0; i
< ngotos
; i
++)
208 add_lookback_edge (state
*s
, rule
*r
, goto_number gotono
)
210 int ri
= state_reduction_find (s
, r
);
211 goto_list
*sp
= xmalloc (sizeof *sp
);
212 sp
->next
= lookback
[(s
->reductions
->lookahead_tokens
- LA
) + ri
];
214 lookback
[(s
->reductions
->lookahead_tokens
- LA
) + ri
] = sp
;
220 build_relations (void)
222 goto_number
*edge
= xnmalloc (ngotos
+ 1, sizeof *edge
);
223 state_number
*states1
= xnmalloc (ritem_longest_rhs () + 1, sizeof *states1
);
226 includes
= xnmalloc (ngotos
, sizeof *includes
);
228 for (i
= 0; i
< ngotos
; i
++)
231 symbol_number symbol1
= states
[to_state
[i
]]->accessing_symbol
;
234 for (rulep
= derives
[symbol1
- ntokens
]; *rulep
; rulep
++)
238 item_number
const *rp
;
239 state
*s
= states
[from_state
[i
]];
240 states1
[0] = s
->number
;
242 for (rp
= (*rulep
)->rhs
; ! item_number_is_rule_number (*rp
); rp
++)
244 s
= transitions_to (s
->transitions
,
245 item_number_as_symbol_number (*rp
));
246 states1
[length
++] = s
->number
;
250 add_lookback_edge (s
, *rulep
, i
);
257 /* Each rhs ends in a rule number, and there is a
258 sentinel (ritem[-1]=0) before the first rhs, so it is safe to
259 decrement RP here. */
263 /* Downcasting from item_number to symbol_number. */
264 edge
[nedges
++] = map_goto (states1
[--length
],
265 item_number_as_symbol_number (*rp
));
266 if (nullable
[*rp
- ntokens
])
277 includes
[i
] = xnmalloc (nedges
+ 1, sizeof includes
[i
][0]);
278 for (j
= 0; j
< nedges
; j
++)
279 includes
[i
][j
] = edge
[j
];
280 includes
[i
][nedges
] = END_NODE
;
287 relation_transpose (&includes
, ngotos
);
293 compute_FOLLOWS (void)
297 relation_digraph (includes
, ngotos
, &goto_follows
);
299 for (i
= 0; i
< ngotos
; i
++)
307 compute_lookahead_tokens (void)
312 for (i
= 0; i
< nLA
; i
++)
313 for (sp
= lookback
[i
]; sp
; sp
= sp
->next
)
314 bitset_or (LA
[i
], LA
[i
], goto_follows
[sp
->value
]);
317 for (i
= 0; i
< nLA
; i
++)
318 LIST_FREE (goto_list
, lookback
[i
]);
324 /*----------------------------------------------------.
325 | Count the number of lookahead tokens required for S |
326 | (N_LOOKAHEAD_TOKENS member). |
327 `----------------------------------------------------*/
330 state_lookahead_tokens_count (state
*s
, bool default_reduction_only_for_accept
)
332 int n_lookahead_tokens
= 0;
333 reductions
*rp
= s
->reductions
;
334 transitions
*sp
= s
->transitions
;
336 /* Transitions are only disabled during conflict resolution, and that
337 hasn't happened yet, so there should be no need to check that
338 transition 0 hasn't been disabled before checking if it is a shift.
339 However, this check was performed at one time, so we leave it as an
341 aver (sp
->num
== 0 || !TRANSITION_IS_DISABLED (sp
, 0));
343 /* We need a lookahead either to distinguish different reductions
344 (i.e., there are two or more), or to distinguish a reduction from a
345 shift. Otherwise, it is straightforward, and the state is
346 `consistent'. However, do not treat a state with any reductions as
347 consistent unless it is the accepting state (because there is never
348 a lookahead token that makes sense there, and so no lookahead token
349 should be read) if the user has otherwise disabled default
352 || (rp
->num
== 1 && sp
->num
&& TRANSITION_IS_SHIFT (sp
, 0))
353 || (rp
->num
== 1 && rp
->rules
[0]->number
!= 0
354 && default_reduction_only_for_accept
))
355 n_lookahead_tokens
+= rp
->num
;
359 return n_lookahead_tokens
;
363 /*----------------------------------------------------.
364 | Compute LA, NLA, and the lookahead_tokens members. |
365 `----------------------------------------------------*/
372 bool default_reduction_only_for_accept
;
374 char *default_reductions
=
375 muscle_percent_define_get ("lr.default-reductions");
376 default_reduction_only_for_accept
=
377 0 == strcmp (default_reductions
, "accepting");
378 free (default_reductions
);
381 /* Compute the total number of reductions requiring a lookahead. */
383 for (i
= 0; i
< nstates
; i
++)
385 state_lookahead_tokens_count (states
[i
],
386 default_reduction_only_for_accept
);
387 /* Avoid having to special case 0. */
391 pLA
= LA
= bitsetv_create (nLA
, ntokens
, BITSET_FIXED
);
393 /* Initialize the members LOOKAHEAD_TOKENS for each state whose reductions
394 require lookahead tokens. */
395 for (i
= 0; i
< nstates
; i
++)
398 state_lookahead_tokens_count (states
[i
],
399 default_reduction_only_for_accept
);
402 states
[i
]->reductions
->lookahead_tokens
= pLA
;
409 /*---------------------------------------------.
410 | Output the lookahead tokens for each state. |
411 `---------------------------------------------*/
414 lookahead_tokens_print (FILE *out
)
418 fprintf (out
, "Lookahead tokens: BEGIN\n");
419 for (i
= 0; i
< nstates
; ++i
)
421 reductions
*reds
= states
[i
]->reductions
;
422 bitset_iterator iter
;
423 int n_lookahead_tokens
= 0;
425 if (reds
->lookahead_tokens
)
426 for (k
= 0; k
< reds
->num
; ++k
)
427 if (reds
->lookahead_tokens
[k
])
428 ++n_lookahead_tokens
;
430 fprintf (out
, "State %d: %d lookahead tokens\n",
431 i
, n_lookahead_tokens
);
433 if (reds
->lookahead_tokens
)
434 for (j
= 0; j
< reds
->num
; ++j
)
435 BITSET_FOR_EACH (iter
, reds
->lookahead_tokens
[j
], k
, 0)
437 fprintf (out
, " on %d (%s) -> rule %d\n",
439 reds
->rules
[j
]->number
);
442 fprintf (out
, "Lookahead tokens: END\n");
451 lookback
= xcalloc (nLA
, sizeof *lookback
);
454 compute_lookahead_tokens ();
456 if (trace_flag
& trace_sets
)
457 lookahead_tokens_print (stderr
);
462 lalr_update_state_numbers (state_number old_to_new
[], state_number nstates_old
)
464 goto_number ngotos_reachable
= 0;
465 symbol_number nonterminal
= 0;
466 aver (nsyms
== nvars
+ ntokens
);
469 for (i
= 0; i
< ngotos
; ++i
)
471 while (i
== goto_map
[nonterminal
])
472 goto_map
[nonterminal
++] = ngotos_reachable
;
473 /* If old_to_new[from_state[i]] = nstates_old, remove this goto
475 if (old_to_new
[from_state
[i
]] != nstates_old
)
477 /* from_state[i] is not removed, so it and thus to_state[i] are
478 reachable, so to_state[i] != nstates_old. */
479 aver (old_to_new
[to_state
[i
]] != nstates_old
);
480 from_state
[ngotos_reachable
] = old_to_new
[from_state
[i
]];
481 to_state
[ngotos_reachable
] = old_to_new
[to_state
[i
]];
486 while (nonterminal
<= nvars
)
488 aver (ngotos
== goto_map
[nonterminal
]);
489 goto_map
[nonterminal
++] = ngotos_reachable
;
491 ngotos
= ngotos_reachable
;
499 for (s
= 0; s
< nstates
; ++s
)
500 states
[s
]->reductions
->lookahead_tokens
= NULL
;