]>
Commit | Line | Data |
---|---|---|
1 | /* Print information on generated parser, for bison, | |
2 | ||
3 | Copyright (C) 1984, 1986, 1989, 2000-2005, 2007, 2009-2015 Free | |
4 | 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 | #include <config.h> | |
22 | #include "system.h" | |
23 | ||
24 | #include <bitset.h> | |
25 | ||
26 | #include "LR0.h" | |
27 | #include "closure.h" | |
28 | #include "conflicts.h" | |
29 | #include "files.h" | |
30 | #include "getargs.h" | |
31 | #include "gram.h" | |
32 | #include "lalr.h" | |
33 | #include "muscle-tab.h" | |
34 | #include "print.h" | |
35 | #include "reader.h" | |
36 | #include "reduce.h" | |
37 | #include "state.h" | |
38 | #include "symtab.h" | |
39 | #include "tables.h" | |
40 | ||
41 | static bitset no_reduce_set; | |
42 | ||
43 | #if 0 | |
44 | static void | |
45 | print_token (int extnum, int token) | |
46 | { | |
47 | fprintf (out, _(" type %d is %s\n"), extnum, tags[token]); | |
48 | } | |
49 | #endif | |
50 | ||
51 | \f | |
52 | ||
53 | /*---------------------------------------. | |
54 | | *WIDTH := max (*WIDTH, strlen (STR)). | | |
55 | `---------------------------------------*/ | |
56 | ||
57 | static void | |
58 | max_length (size_t *width, const char *str) | |
59 | { | |
60 | size_t len = strlen (str); | |
61 | if (len > *width) | |
62 | *width = len; | |
63 | } | |
64 | ||
65 | /*--------------------------------. | |
66 | | Report information on a state. | | |
67 | `--------------------------------*/ | |
68 | ||
69 | static void | |
70 | print_core (FILE *out, state *s) | |
71 | { | |
72 | size_t i; | |
73 | item_number *sitems = s->items; | |
74 | size_t snritems = s->nitems; | |
75 | symbol *previous_lhs = NULL; | |
76 | ||
77 | /* Output all the items of a state, not only its kernel. */ | |
78 | if (report_flag & report_itemsets) | |
79 | { | |
80 | closure (sitems, snritems); | |
81 | sitems = itemset; | |
82 | snritems = nitemset; | |
83 | } | |
84 | ||
85 | if (!snritems) | |
86 | return; | |
87 | ||
88 | fputc ('\n', out); | |
89 | ||
90 | for (i = 0; i < snritems; i++) | |
91 | { | |
92 | item_number *sp; | |
93 | item_number *sp1; | |
94 | rule_number r; | |
95 | ||
96 | sp1 = sp = ritem + sitems[i]; | |
97 | ||
98 | while (*sp >= 0) | |
99 | sp++; | |
100 | ||
101 | r = item_number_as_rule_number (*sp); | |
102 | ||
103 | rule_lhs_print (&rules[r], previous_lhs, out); | |
104 | previous_lhs = rules[r].lhs; | |
105 | ||
106 | for (sp = rules[r].rhs; sp < sp1; sp++) | |
107 | fprintf (out, " %s", symbols[*sp]->tag); | |
108 | fputs (" .", out); | |
109 | if (0 <= *rules[r].rhs) | |
110 | for (/* Nothing */; 0 <= *sp; ++sp) | |
111 | fprintf (out, " %s", symbols[*sp]->tag); | |
112 | else | |
113 | fprintf (out, " %%empty"); | |
114 | ||
115 | /* Display the lookahead tokens? */ | |
116 | if (report_flag & report_lookahead_tokens | |
117 | && item_number_is_rule_number (*sp1)) | |
118 | state_rule_lookahead_tokens_print (s, &rules[r], out); | |
119 | ||
120 | fputc ('\n', out); | |
121 | } | |
122 | } | |
123 | ||
124 | ||
125 | /*------------------------------------------------------------. | |
126 | | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of S on | | |
127 | | OUT. | | |
128 | `------------------------------------------------------------*/ | |
129 | ||
130 | static void | |
131 | print_transitions (state *s, FILE *out, bool display_transitions_p) | |
132 | { | |
133 | transitions *trans = s->transitions; | |
134 | size_t width = 0; | |
135 | int i; | |
136 | ||
137 | /* Compute the width of the lookahead token column. */ | |
138 | for (i = 0; i < trans->num; i++) | |
139 | if (!TRANSITION_IS_DISABLED (trans, i) | |
140 | && TRANSITION_IS_SHIFT (trans, i) == display_transitions_p) | |
141 | { | |
142 | symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)]; | |
143 | max_length (&width, sym->tag); | |
144 | } | |
145 | ||
146 | /* Nothing to report. */ | |
147 | if (!width) | |
148 | return; | |
149 | ||
150 | fputc ('\n', out); | |
151 | width += 2; | |
152 | ||
153 | /* Report lookahead tokens and shifts. */ | |
154 | for (i = 0; i < trans->num; i++) | |
155 | if (!TRANSITION_IS_DISABLED (trans, i) | |
156 | && TRANSITION_IS_SHIFT (trans, i) == display_transitions_p) | |
157 | { | |
158 | symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)]; | |
159 | const char *tag = sym->tag; | |
160 | state *s1 = trans->states[i]; | |
161 | int j; | |
162 | ||
163 | fprintf (out, " %s", tag); | |
164 | for (j = width - strlen (tag); j > 0; --j) | |
165 | fputc (' ', out); | |
166 | if (display_transitions_p) | |
167 | fprintf (out, _("shift, and go to state %d\n"), s1->number); | |
168 | else | |
169 | fprintf (out, _("go to state %d\n"), s1->number); | |
170 | } | |
171 | } | |
172 | ||
173 | ||
174 | /*--------------------------------------------------------. | |
175 | | Report the explicit errors of S raised from %nonassoc. | | |
176 | `--------------------------------------------------------*/ | |
177 | ||
178 | static void | |
179 | print_errs (FILE *out, state *s) | |
180 | { | |
181 | errs *errp = s->errs; | |
182 | size_t width = 0; | |
183 | int i; | |
184 | ||
185 | /* Compute the width of the lookahead token column. */ | |
186 | for (i = 0; i < errp->num; ++i) | |
187 | if (errp->symbols[i]) | |
188 | max_length (&width, errp->symbols[i]->tag); | |
189 | ||
190 | /* Nothing to report. */ | |
191 | if (!width) | |
192 | return; | |
193 | ||
194 | fputc ('\n', out); | |
195 | width += 2; | |
196 | ||
197 | /* Report lookahead tokens and errors. */ | |
198 | for (i = 0; i < errp->num; ++i) | |
199 | if (errp->symbols[i]) | |
200 | { | |
201 | const char *tag = errp->symbols[i]->tag; | |
202 | int j; | |
203 | fprintf (out, " %s", tag); | |
204 | for (j = width - strlen (tag); j > 0; --j) | |
205 | fputc (' ', out); | |
206 | fputs (_("error (nonassociative)\n"), out); | |
207 | } | |
208 | } | |
209 | ||
210 | ||
211 | /*-------------------------------------------------------------------------. | |
212 | | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be 'default'). | | |
213 | | If not ENABLED, the rule is masked by a shift or a reduce (S/R and | | |
214 | | R/R conflicts). | | |
215 | `-------------------------------------------------------------------------*/ | |
216 | ||
217 | static void | |
218 | print_reduction (FILE *out, size_t width, | |
219 | const char *lookahead_token, | |
220 | rule *r, bool enabled) | |
221 | { | |
222 | int j; | |
223 | fprintf (out, " %s", lookahead_token); | |
224 | for (j = width - strlen (lookahead_token); j > 0; --j) | |
225 | fputc (' ', out); | |
226 | if (!enabled) | |
227 | fputc ('[', out); | |
228 | if (r->number) | |
229 | fprintf (out, _("reduce using rule %d (%s)"), r->number, r->lhs->tag); | |
230 | else | |
231 | fprintf (out, _("accept")); | |
232 | if (!enabled) | |
233 | fputc (']', out); | |
234 | fputc ('\n', out); | |
235 | } | |
236 | ||
237 | ||
238 | /*-------------------------------------------. | |
239 | | Report on OUT the reduction actions of S. | | |
240 | `-------------------------------------------*/ | |
241 | ||
242 | static void | |
243 | print_reductions (FILE *out, state *s) | |
244 | { | |
245 | transitions *trans = s->transitions; | |
246 | reductions *reds = s->reductions; | |
247 | rule *default_reduction = NULL; | |
248 | size_t width = 0; | |
249 | int i, j; | |
250 | bool default_reduction_only = true; | |
251 | ||
252 | if (reds->num == 0) | |
253 | return; | |
254 | ||
255 | if (yydefact[s->number] != 0) | |
256 | default_reduction = &rules[yydefact[s->number] - 1]; | |
257 | ||
258 | bitset_zero (no_reduce_set); | |
259 | FOR_EACH_SHIFT (trans, i) | |
260 | bitset_set (no_reduce_set, TRANSITION_SYMBOL (trans, i)); | |
261 | for (i = 0; i < s->errs->num; ++i) | |
262 | if (s->errs->symbols[i]) | |
263 | bitset_set (no_reduce_set, s->errs->symbols[i]->number); | |
264 | ||
265 | /* Compute the width of the lookahead token column. */ | |
266 | if (default_reduction) | |
267 | width = strlen (_("$default")); | |
268 | ||
269 | if (reds->lookahead_tokens) | |
270 | for (i = 0; i < ntokens; i++) | |
271 | { | |
272 | bool count = bitset_test (no_reduce_set, i); | |
273 | ||
274 | for (j = 0; j < reds->num; ++j) | |
275 | if (bitset_test (reds->lookahead_tokens[j], i)) | |
276 | { | |
277 | if (! count) | |
278 | { | |
279 | if (reds->rules[j] != default_reduction) | |
280 | max_length (&width, symbols[i]->tag); | |
281 | count = true; | |
282 | } | |
283 | else | |
284 | { | |
285 | max_length (&width, symbols[i]->tag); | |
286 | } | |
287 | } | |
288 | } | |
289 | ||
290 | /* Nothing to report. */ | |
291 | if (!width) | |
292 | return; | |
293 | ||
294 | fputc ('\n', out); | |
295 | width += 2; | |
296 | ||
297 | /* Report lookahead tokens (or $default) and reductions. */ | |
298 | if (reds->lookahead_tokens) | |
299 | for (i = 0; i < ntokens; i++) | |
300 | { | |
301 | bool defaulted = false; | |
302 | bool count = bitset_test (no_reduce_set, i); | |
303 | if (count) | |
304 | default_reduction_only = false; | |
305 | ||
306 | for (j = 0; j < reds->num; ++j) | |
307 | if (bitset_test (reds->lookahead_tokens[j], i)) | |
308 | { | |
309 | if (! count) | |
310 | { | |
311 | if (reds->rules[j] != default_reduction) | |
312 | { | |
313 | default_reduction_only = false; | |
314 | print_reduction (out, width, | |
315 | symbols[i]->tag, | |
316 | reds->rules[j], true); | |
317 | } | |
318 | else | |
319 | defaulted = true; | |
320 | count = true; | |
321 | } | |
322 | else | |
323 | { | |
324 | default_reduction_only = false; | |
325 | if (defaulted) | |
326 | print_reduction (out, width, | |
327 | symbols[i]->tag, | |
328 | default_reduction, true); | |
329 | defaulted = false; | |
330 | print_reduction (out, width, | |
331 | symbols[i]->tag, | |
332 | reds->rules[j], false); | |
333 | } | |
334 | } | |
335 | } | |
336 | ||
337 | if (default_reduction) | |
338 | { | |
339 | char *default_reductions = | |
340 | muscle_percent_define_get ("lr.default-reduction"); | |
341 | print_reduction (out, width, _("$default"), default_reduction, true); | |
342 | aver (STREQ (default_reductions, "most") | |
343 | || (STREQ (default_reductions, "consistent") | |
344 | && default_reduction_only) | |
345 | || (reds->num == 1 && reds->rules[0]->number == 0)); | |
346 | free (default_reductions); | |
347 | } | |
348 | } | |
349 | ||
350 | ||
351 | /*--------------------------------------------------------------. | |
352 | | Report on OUT all the actions (shifts, gotos, reductions, and | | |
353 | | explicit erros from %nonassoc) of S. | | |
354 | `--------------------------------------------------------------*/ | |
355 | ||
356 | static void | |
357 | print_actions (FILE *out, state *s) | |
358 | { | |
359 | /* Print shifts. */ | |
360 | print_transitions (s, out, true); | |
361 | print_errs (out, s); | |
362 | print_reductions (out, s); | |
363 | /* Print gotos. */ | |
364 | print_transitions (s, out, false); | |
365 | } | |
366 | ||
367 | ||
368 | /*----------------------------------. | |
369 | | Report all the data on S on OUT. | | |
370 | `----------------------------------*/ | |
371 | ||
372 | static void | |
373 | print_state (FILE *out, state *s) | |
374 | { | |
375 | fputs ("\n\n", out); | |
376 | fprintf (out, _("State %d"), s->number); | |
377 | fputc ('\n', out); | |
378 | print_core (out, s); | |
379 | print_actions (out, s); | |
380 | if ((report_flag & report_solved_conflicts) && s->solved_conflicts) | |
381 | { | |
382 | fputc ('\n', out); | |
383 | fputs (s->solved_conflicts, out); | |
384 | } | |
385 | } | |
386 | \f | |
387 | /*-----------------------------------------. | |
388 | | Print information on the whole grammar. | | |
389 | `-----------------------------------------*/ | |
390 | ||
391 | #define END_TEST(End) \ | |
392 | do { \ | |
393 | if (column + strlen (buffer) > (End)) \ | |
394 | { \ | |
395 | fprintf (out, "%s\n ", buffer); \ | |
396 | column = 3; \ | |
397 | buffer[0] = 0; \ | |
398 | } \ | |
399 | } while (0) | |
400 | ||
401 | ||
402 | static void | |
403 | print_grammar (FILE *out) | |
404 | { | |
405 | symbol_number i; | |
406 | char buffer[90]; | |
407 | int column = 0; | |
408 | ||
409 | grammar_rules_print (out); | |
410 | ||
411 | /* TERMINAL (type #) : rule #s terminal is on RHS */ | |
412 | fprintf (out, "%s\n\n", _("Terminals, with rules where they appear")); | |
413 | for (i = 0; i < max_user_token_number + 1; i++) | |
414 | if (token_translations[i] != undeftoken->number) | |
415 | { | |
416 | const char *tag = symbols[token_translations[i]]->tag; | |
417 | rule_number r; | |
418 | item_number *rhsp; | |
419 | ||
420 | buffer[0] = 0; | |
421 | column = strlen (tag); | |
422 | fputs (tag, out); | |
423 | END_TEST (65); | |
424 | sprintf (buffer, " (%d)", i); | |
425 | ||
426 | for (r = 0; r < nrules; r++) | |
427 | for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++) | |
428 | if (item_number_as_symbol_number (*rhsp) == token_translations[i]) | |
429 | { | |
430 | END_TEST (65); | |
431 | sprintf (buffer + strlen (buffer), " %d", r); | |
432 | break; | |
433 | } | |
434 | fprintf (out, "%s\n", buffer); | |
435 | } | |
436 | fputs ("\n\n", out); | |
437 | ||
438 | ||
439 | fprintf (out, "%s\n\n", _("Nonterminals, with rules where they appear")); | |
440 | for (i = ntokens; i < nsyms; i++) | |
441 | { | |
442 | int left_count = 0, right_count = 0; | |
443 | rule_number r; | |
444 | const char *tag = symbols[i]->tag; | |
445 | ||
446 | for (r = 0; r < nrules; r++) | |
447 | { | |
448 | item_number *rhsp; | |
449 | if (rules[r].lhs->number == i) | |
450 | left_count++; | |
451 | for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++) | |
452 | if (item_number_as_symbol_number (*rhsp) == i) | |
453 | { | |
454 | right_count++; | |
455 | break; | |
456 | } | |
457 | } | |
458 | ||
459 | buffer[0] = 0; | |
460 | fputs (tag, out); | |
461 | column = strlen (tag); | |
462 | sprintf (buffer, " (%d)", i); | |
463 | END_TEST (0); | |
464 | ||
465 | if (left_count > 0) | |
466 | { | |
467 | END_TEST (65); | |
468 | sprintf (buffer + strlen (buffer), _(" on left:")); | |
469 | ||
470 | for (r = 0; r < nrules; r++) | |
471 | { | |
472 | if (rules[r].lhs->number == i) | |
473 | { | |
474 | END_TEST (65); | |
475 | sprintf (buffer + strlen (buffer), " %d", r); | |
476 | } | |
477 | } | |
478 | } | |
479 | ||
480 | if (right_count > 0) | |
481 | { | |
482 | if (left_count > 0) | |
483 | sprintf (buffer + strlen (buffer), ","); | |
484 | END_TEST (65); | |
485 | sprintf (buffer + strlen (buffer), _(" on right:")); | |
486 | for (r = 0; r < nrules; r++) | |
487 | { | |
488 | item_number *rhsp; | |
489 | for (rhsp = rules[r].rhs; *rhsp >= 0; rhsp++) | |
490 | if (item_number_as_symbol_number (*rhsp) == i) | |
491 | { | |
492 | END_TEST (65); | |
493 | sprintf (buffer + strlen (buffer), " %d", r); | |
494 | break; | |
495 | } | |
496 | } | |
497 | } | |
498 | fprintf (out, "%s\n", buffer); | |
499 | } | |
500 | } | |
501 | \f | |
502 | void | |
503 | print_results (void) | |
504 | { | |
505 | state_number i; | |
506 | ||
507 | /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but | |
508 | that conflicts with Posix. */ | |
509 | FILE *out = xfopen (spec_verbose_file, "w"); | |
510 | ||
511 | reduce_output (out); | |
512 | grammar_rules_partial_print (out, | |
513 | _("Rules useless in parser due to conflicts"), | |
514 | rule_useless_in_parser_p); | |
515 | conflicts_output (out); | |
516 | ||
517 | print_grammar (out); | |
518 | ||
519 | /* If the whole state item sets, not only the kernels, are wanted, | |
520 | 'closure' will be run, which needs memory allocation/deallocation. */ | |
521 | if (report_flag & report_itemsets) | |
522 | new_closure (nritems); | |
523 | /* Storage for print_reductions. */ | |
524 | no_reduce_set = bitset_create (ntokens, BITSET_FIXED); | |
525 | for (i = 0; i < nstates; i++) | |
526 | print_state (out, states[i]); | |
527 | bitset_free (no_reduce_set); | |
528 | if (report_flag & report_itemsets) | |
529 | free_closure (); | |
530 | ||
531 | xfclose (out); | |
532 | } |