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