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