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