]>
git.saurik.com Git - bison.git/blob - src/print.c
1 /* Print information on generated parser, for bison,
3 Copyright (C) 1984, 1986, 1989, 2000-2005, 2007, 2009-2015 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 if (0 <= *rules
[r
].rhs
)
110 for (/* Nothing */; 0 <= *sp
; ++sp
)
111 fprintf (out
, " %s", symbols
[*sp
]->tag
);
113 fprintf (out
, " %%empty");
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
);
125 /*------------------------------------------------------------.
126 | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of S on |
128 `------------------------------------------------------------*/
131 print_transitions (state
*s
, FILE *out
, bool display_transitions_p
)
133 transitions
*trans
= s
->transitions
;
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
)
142 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
143 max_length (&width
, sym
->tag
);
146 /* Nothing to report. */
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
)
158 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
159 const char *tag
= sym
->tag
;
160 state
*s1
= trans
->states
[i
];
163 fprintf (out
, " %s", tag
);
164 for (j
= width
- strlen (tag
); j
> 0; --j
)
166 if (display_transitions_p
)
167 fprintf (out
, _("shift, and go to state %d\n"), s1
->number
);
169 fprintf (out
, _("go to state %d\n"), s1
->number
);
174 /*--------------------------------------------------------.
175 | Report the explicit errors of S raised from %nonassoc. |
176 `--------------------------------------------------------*/
179 print_errs (FILE *out
, state
*s
)
181 errs
*errp
= s
->errs
;
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
);
190 /* Nothing to report. */
197 /* Report lookahead tokens and errors. */
198 for (i
= 0; i
< errp
->num
; ++i
)
199 if (errp
->symbols
[i
])
201 const char *tag
= errp
->symbols
[i
]->tag
;
203 fprintf (out
, " %s", tag
);
204 for (j
= width
- strlen (tag
); j
> 0; --j
)
206 fputs (_("error (nonassociative)\n"), out
);
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 |
215 `-------------------------------------------------------------------------*/
218 print_reduction (FILE *out
, size_t width
,
219 const char *lookahead_token
,
220 rule
*r
, bool enabled
)
223 fprintf (out
, " %s", lookahead_token
);
224 for (j
= width
- strlen (lookahead_token
); j
> 0; --j
)
229 fprintf (out
, _("reduce using rule %d (%s)"), r
->number
, r
->lhs
->tag
);
231 fprintf (out
, _("accept"));
238 /*-------------------------------------------.
239 | Report on OUT the reduction actions of S. |
240 `-------------------------------------------*/
243 print_reductions (FILE *out
, state
*s
)
245 transitions
*trans
= s
->transitions
;
246 reductions
*reds
= s
->reductions
;
247 rule
*default_reduction
= NULL
;
250 bool default_reduction_only
= true;
255 if (yydefact
[s
->number
] != 0)
256 default_reduction
= &rules
[yydefact
[s
->number
] - 1];
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
);
265 /* Compute the width of the lookahead token column. */
266 if (default_reduction
)
267 width
= strlen (_("$default"));
269 if (reds
->lookahead_tokens
)
270 for (i
= 0; i
< ntokens
; i
++)
272 bool count
= bitset_test (no_reduce_set
, i
);
274 for (j
= 0; j
< reds
->num
; ++j
)
275 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
279 if (reds
->rules
[j
] != default_reduction
)
280 max_length (&width
, symbols
[i
]->tag
);
285 max_length (&width
, symbols
[i
]->tag
);
290 /* Nothing to report. */
297 /* Report lookahead tokens (or $default) and reductions. */
298 if (reds
->lookahead_tokens
)
299 for (i
= 0; i
< ntokens
; i
++)
301 bool defaulted
= false;
302 bool count
= bitset_test (no_reduce_set
, i
);
304 default_reduction_only
= false;
306 for (j
= 0; j
< reds
->num
; ++j
)
307 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
311 if (reds
->rules
[j
] != default_reduction
)
313 default_reduction_only
= false;
314 print_reduction (out
, width
,
316 reds
->rules
[j
], true);
324 default_reduction_only
= false;
326 print_reduction (out
, width
,
328 default_reduction
, true);
330 print_reduction (out
, width
,
332 reds
->rules
[j
], false);
337 if (default_reduction
)
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 (void) default_reduction_only
;
347 free (default_reductions
);
352 /*--------------------------------------------------------------.
353 | Report on OUT all the actions (shifts, gotos, reductions, and |
354 | explicit erros from %nonassoc) of S. |
355 `--------------------------------------------------------------*/
358 print_actions (FILE *out
, state
*s
)
361 print_transitions (s
, out
, true);
363 print_reductions (out
, s
);
365 print_transitions (s
, out
, false);
369 /*----------------------------------.
370 | Report all the data on S on OUT. |
371 `----------------------------------*/
374 print_state (FILE *out
, state
*s
)
377 fprintf (out
, _("State %d"), s
->number
);
380 print_actions (out
, s
);
381 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts
)
384 fputs (s
->solved_conflicts
, out
);
388 /*-----------------------------------------.
389 | Print information on the whole grammar. |
390 `-----------------------------------------*/
392 #define END_TEST(End) \
394 if (column + strlen (buffer) > (End)) \
396 fprintf (out, "%s\n ", buffer); \
404 print_grammar (FILE *out
)
410 grammar_rules_print (out
);
412 /* TERMINAL (type #) : rule #s terminal is on RHS */
413 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
414 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
415 if (token_translations
[i
] != undeftoken
->number
)
417 const char *tag
= symbols
[token_translations
[i
]]->tag
;
422 column
= strlen (tag
);
425 sprintf (buffer
, " (%d)", i
);
427 for (r
= 0; r
< nrules
; r
++)
428 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
429 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
432 sprintf (buffer
+ strlen (buffer
), " %d", r
);
435 fprintf (out
, "%s\n", buffer
);
440 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
441 for (i
= ntokens
; i
< nsyms
; i
++)
443 int left_count
= 0, right_count
= 0;
445 const char *tag
= symbols
[i
]->tag
;
447 for (r
= 0; r
< nrules
; r
++)
450 if (rules
[r
].lhs
->number
== i
)
452 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
453 if (item_number_as_symbol_number (*rhsp
) == i
)
462 column
= strlen (tag
);
463 sprintf (buffer
, " (%d)", i
);
469 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
471 for (r
= 0; r
< nrules
; r
++)
473 if (rules
[r
].lhs
->number
== i
)
476 sprintf (buffer
+ strlen (buffer
), " %d", r
);
484 sprintf (buffer
+ strlen (buffer
), ",");
486 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
487 for (r
= 0; r
< nrules
; r
++)
490 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
491 if (item_number_as_symbol_number (*rhsp
) == i
)
494 sprintf (buffer
+ strlen (buffer
), " %d", r
);
499 fprintf (out
, "%s\n", buffer
);
508 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
509 that conflicts with Posix. */
510 FILE *out
= xfopen (spec_verbose_file
, "w");
513 grammar_rules_partial_print (out
,
514 _("Rules useless in parser due to conflicts"),
515 rule_useless_in_parser_p
);
516 conflicts_output (out
);
520 /* If the whole state item sets, not only the kernels, are wanted,
521 'closure' will be run, which needs memory allocation/deallocation. */
522 if (report_flag
& report_itemsets
)
523 new_closure (nritems
);
524 /* Storage for print_reductions. */
525 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
526 for (i
= 0; i
< nstates
; i
++)
527 print_state (out
, states
[i
]);
528 bitset_free (no_reduce_set
);
529 if (report_flag
& report_itemsets
)