1 /* Print information on generated parser, for bison,
3 Copyright (C) 1984, 1986, 1989, 2000-2005, 2007, 2009-2012 Free
4 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/>. */
28 #include "conflicts.h"
33 #include "muscle-tab.h"
41 static bitset no_reduce_set
;
45 print_token (int extnum
, int token
)
47 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
53 /*---------------------------------------.
54 | *WIDTH := max (*WIDTH, strlen (STR)). |
55 `---------------------------------------*/
58 max_length (size_t *width
, const char *str
)
60 size_t len
= strlen (str
);
65 /*--------------------------------.
66 | Report information on a state. |
67 `--------------------------------*/
70 print_core (FILE *out
, state
*s
)
73 item_number
*sitems
= s
->items
;
74 size_t snritems
= s
->nitems
;
75 symbol
*previous_lhs
= NULL
;
77 /* Output all the items of a state, not only its kernel. */
78 if (report_flag
& report_itemsets
)
80 closure (sitems
, snritems
);
90 for (i
= 0; i
< snritems
; i
++)
96 sp1
= sp
= ritem
+ sitems
[i
];
101 r
= item_number_as_rule_number (*sp
);
103 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
104 previous_lhs
= rules
[r
].lhs
;
106 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
107 fprintf (out
, " %s", symbols
[*sp
]->tag
);
109 for (/* Nothing */; *sp
>= 0; ++sp
)
110 fprintf (out
, " %s", symbols
[*sp
]->tag
);
112 /* Display the lookahead tokens? */
113 if (report_flag
& report_lookahead_tokens
114 && item_number_is_rule_number (*sp1
))
115 state_rule_lookahead_tokens_print (s
, &rules
[r
], out
);
122 /*------------------------------------------------------------.
123 | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of S on |
125 `------------------------------------------------------------*/
128 print_transitions (state
*s
, FILE *out
, bool display_transitions_p
)
130 transitions
*trans
= s
->transitions
;
134 /* Compute the width of the lookahead token column. */
135 for (i
= 0; i
< trans
->num
; i
++)
136 if (!TRANSITION_IS_DISABLED (trans
, i
)
137 && TRANSITION_IS_SHIFT (trans
, i
) == display_transitions_p
)
139 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
140 max_length (&width
, sym
->tag
);
143 /* Nothing to report. */
150 /* Report lookahead tokens and shifts. */
151 for (i
= 0; i
< trans
->num
; i
++)
152 if (!TRANSITION_IS_DISABLED (trans
, i
)
153 && TRANSITION_IS_SHIFT (trans
, i
) == display_transitions_p
)
155 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
156 const char *tag
= sym
->tag
;
157 state
*s1
= trans
->states
[i
];
160 fprintf (out
, " %s", tag
);
161 for (j
= width
- strlen (tag
); j
> 0; --j
)
163 if (display_transitions_p
)
164 fprintf (out
, _("shift, and go to state %d\n"), s1
->number
);
166 fprintf (out
, _("go to state %d\n"), s1
->number
);
171 /*--------------------------------------------------------.
172 | Report the explicit errors of S raised from %nonassoc. |
173 `--------------------------------------------------------*/
176 print_errs (FILE *out
, state
*s
)
178 errs
*errp
= s
->errs
;
182 /* Compute the width of the lookahead token column. */
183 for (i
= 0; i
< errp
->num
; ++i
)
184 if (errp
->symbols
[i
])
185 max_length (&width
, errp
->symbols
[i
]->tag
);
187 /* Nothing to report. */
194 /* Report lookahead tokens and errors. */
195 for (i
= 0; i
< errp
->num
; ++i
)
196 if (errp
->symbols
[i
])
198 const char *tag
= errp
->symbols
[i
]->tag
;
200 fprintf (out
, " %s", tag
);
201 for (j
= width
- strlen (tag
); j
> 0; --j
)
203 fputs (_("error (nonassociative)\n"), out
);
208 /*-------------------------------------------------------------------------.
209 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
210 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
212 `-------------------------------------------------------------------------*/
215 print_reduction (FILE *out
, size_t width
,
216 const char *lookahead_token
,
217 rule
*r
, bool enabled
)
220 fprintf (out
, " %s", lookahead_token
);
221 for (j
= width
- strlen (lookahead_token
); j
> 0; --j
)
226 fprintf (out
, _("reduce using rule %d (%s)"), r
->number
, r
->lhs
->tag
);
228 fprintf (out
, _("accept"));
235 /*-------------------------------------------.
236 | Report on OUT the reduction actions of S. |
237 `-------------------------------------------*/
240 print_reductions (FILE *out
, state
*s
)
242 transitions
*trans
= s
->transitions
;
243 reductions
*reds
= s
->reductions
;
244 rule
*default_reduction
= NULL
;
247 bool default_reduction_only
= true;
252 if (yydefact
[s
->number
] != 0)
253 default_reduction
= &rules
[yydefact
[s
->number
] - 1];
255 bitset_zero (no_reduce_set
);
256 FOR_EACH_SHIFT (trans
, i
)
257 bitset_set (no_reduce_set
, TRANSITION_SYMBOL (trans
, i
));
258 for (i
= 0; i
< s
->errs
->num
; ++i
)
259 if (s
->errs
->symbols
[i
])
260 bitset_set (no_reduce_set
, s
->errs
->symbols
[i
]->number
);
262 /* Compute the width of the lookahead token column. */
263 if (default_reduction
)
264 width
= strlen (_("$default"));
266 if (reds
->lookahead_tokens
)
267 for (i
= 0; i
< ntokens
; i
++)
269 bool count
= bitset_test (no_reduce_set
, i
);
271 for (j
= 0; j
< reds
->num
; ++j
)
272 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
276 if (reds
->rules
[j
] != default_reduction
)
277 max_length (&width
, symbols
[i
]->tag
);
282 max_length (&width
, symbols
[i
]->tag
);
287 /* Nothing to report. */
294 /* Report lookahead tokens (or $default) and reductions. */
295 if (reds
->lookahead_tokens
)
296 for (i
= 0; i
< ntokens
; i
++)
298 bool defaulted
= false;
299 bool count
= bitset_test (no_reduce_set
, i
);
301 default_reduction_only
= false;
303 for (j
= 0; j
< reds
->num
; ++j
)
304 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
308 if (reds
->rules
[j
] != default_reduction
)
310 default_reduction_only
= false;
311 print_reduction (out
, width
,
313 reds
->rules
[j
], true);
321 default_reduction_only
= false;
323 print_reduction (out
, width
,
325 default_reduction
, true);
327 print_reduction (out
, width
,
329 reds
->rules
[j
], false);
334 if (default_reduction
)
336 char *default_reductions
=
337 muscle_percent_define_get ("lr.default-reductions");
338 print_reduction (out
, width
, _("$default"), default_reduction
, true);
339 aver (STREQ (default_reductions
, "most")
340 || (STREQ (default_reductions
, "consistent")
341 && default_reduction_only
)
342 || (reds
->num
== 1 && reds
->rules
[0]->number
== 0));
343 free (default_reductions
);
348 /*--------------------------------------------------------------.
349 | Report on OUT all the actions (shifts, gotos, reductions, and |
350 | explicit erros from %nonassoc) of S. |
351 `--------------------------------------------------------------*/
354 print_actions (FILE *out
, state
*s
)
357 print_transitions (s
, out
, true);
359 print_reductions (out
, s
);
361 print_transitions (s
, out
, false);
365 /*----------------------------------.
366 | Report all the data on S on OUT. |
367 `----------------------------------*/
370 print_state (FILE *out
, state
*s
)
373 fprintf (out
, _("state %d"), s
->number
);
376 print_actions (out
, s
);
377 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts
)
380 fputs (s
->solved_conflicts
, out
);
384 /*-----------------------------------------.
385 | Print information on the whole grammar. |
386 `-----------------------------------------*/
388 #define END_TEST(End) \
390 if (column + strlen (buffer) > (End)) \
392 fprintf (out, "%s\n ", buffer); \
400 print_grammar (FILE *out
)
406 grammar_rules_print (out
);
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
)
413 const char *tag
= symbols
[token_translations
[i
]]->tag
;
418 column
= strlen (tag
);
421 sprintf (buffer
, " (%d)", i
);
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
])
428 sprintf (buffer
+ strlen (buffer
), " %d", r
);
431 fprintf (out
, "%s\n", buffer
);
436 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
437 for (i
= ntokens
; i
< nsyms
; i
++)
439 int left_count
= 0, right_count
= 0;
441 const char *tag
= symbols
[i
]->tag
;
443 for (r
= 0; r
< nrules
; r
++)
446 if (rules
[r
].lhs
->number
== i
)
448 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
449 if (item_number_as_symbol_number (*rhsp
) == i
)
458 column
= strlen (tag
);
459 sprintf (buffer
, " (%d)", i
);
465 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
467 for (r
= 0; r
< nrules
; r
++)
469 if (rules
[r
].lhs
->number
== i
)
472 sprintf (buffer
+ strlen (buffer
), " %d", r
);
480 sprintf (buffer
+ strlen (buffer
), ",");
482 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
483 for (r
= 0; r
< nrules
; r
++)
486 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
487 if (item_number_as_symbol_number (*rhsp
) == i
)
490 sprintf (buffer
+ strlen (buffer
), " %d", r
);
495 fprintf (out
, "%s\n", buffer
);
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");
509 grammar_rules_partial_print (out
,
510 _("Rules useless in parser due to conflicts"),
511 rule_useless_in_parser_p
);
512 conflicts_output (out
);
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
)