]>
git.saurik.com Git - bison.git/blob - src/print.c
d66fc38d5713830c992087e2d5c322ffec0fe732
1 /* Print information on generated parser, for bison,
3 Copyright (C) 1984, 1986, 1989, 2000-2005, 2007, 2009-2010 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/>. */
29 #include "conflicts.h"
34 #include "muscle-tab.h"
42 static bitset no_reduce_set
;
46 print_token (int extnum
, int token
)
48 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
54 /*---------------------------------------.
55 | *WIDTH := max (*WIDTH, strlen (STR)). |
56 `---------------------------------------*/
59 max_length (size_t *width
, const char *str
)
61 size_t len
= strlen (str
);
66 /*--------------------------------.
67 | Report information on a state. |
68 `--------------------------------*/
71 print_core (FILE *out
, state
*s
)
74 item_number
*sitems
= s
->items
;
75 size_t snritems
= s
->nitems
;
76 symbol
*previous_lhs
= NULL
;
78 /* Output all the items of a state, not only its kernel. */
79 if (report_flag
& report_itemsets
)
81 closure (sitems
, snritems
);
91 for (i
= 0; i
< snritems
; i
++)
97 sp1
= sp
= ritem
+ sitems
[i
];
102 r
= item_number_as_rule_number (*sp
);
104 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
105 previous_lhs
= rules
[r
].lhs
;
107 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
108 fprintf (out
, " %s", symbols
[*sp
]->tag
);
110 for (/* Nothing */; *sp
>= 0; ++sp
)
111 fprintf (out
, " %s", symbols
[*sp
]->tag
);
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
);
123 /*------------------------------------------------------------.
124 | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of S on |
126 `------------------------------------------------------------*/
129 print_transitions (state
*s
, FILE *out
, bool display_transitions_p
)
131 transitions
*trans
= s
->transitions
;
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
)
140 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
141 max_length (&width
, sym
->tag
);
144 /* Nothing to report. */
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
)
156 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
157 const char *tag
= sym
->tag
;
158 state
*s1
= trans
->states
[i
];
161 fprintf (out
, " %s", tag
);
162 for (j
= width
- strlen (tag
); j
> 0; --j
)
164 if (display_transitions_p
)
165 fprintf (out
, _("shift, and go to state %d\n"), s1
->number
);
167 fprintf (out
, _("go to state %d\n"), s1
->number
);
172 /*--------------------------------------------------------.
173 | Report the explicit errors of S raised from %nonassoc. |
174 `--------------------------------------------------------*/
177 print_errs (FILE *out
, state
*s
)
179 errs
*errp
= s
->errs
;
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
);
188 /* Nothing to report. */
195 /* Report lookahead tokens and errors. */
196 for (i
= 0; i
< errp
->num
; ++i
)
197 if (errp
->symbols
[i
])
199 const char *tag
= errp
->symbols
[i
]->tag
;
201 fprintf (out
, " %s", tag
);
202 for (j
= width
- strlen (tag
); j
> 0; --j
)
204 fputs (_("error (nonassociative)\n"), out
);
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 |
213 `-------------------------------------------------------------------------*/
216 print_reduction (FILE *out
, size_t width
,
217 const char *lookahead_token
,
218 rule
*r
, bool enabled
)
221 fprintf (out
, " %s", lookahead_token
);
222 for (j
= width
- strlen (lookahead_token
); j
> 0; --j
)
227 fprintf (out
, _("reduce using rule %d (%s)"), r
->number
, r
->lhs
->tag
);
229 fprintf (out
, _("accept"));
236 /*-------------------------------------------.
237 | Report on OUT the reduction actions of S. |
238 `-------------------------------------------*/
241 print_reductions (FILE *out
, state
*s
)
243 transitions
*trans
= s
->transitions
;
244 reductions
*reds
= s
->reductions
;
245 rule
*default_reduction
= NULL
;
248 bool default_reduction_only
= true;
253 if (yydefact
[s
->number
] != 0)
254 default_reduction
= &rules
[yydefact
[s
->number
] - 1];
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
);
263 /* Compute the width of the lookahead token column. */
264 if (default_reduction
)
265 width
= strlen (_("$default"));
267 if (reds
->lookahead_tokens
)
268 for (i
= 0; i
< ntokens
; i
++)
270 bool count
= bitset_test (no_reduce_set
, i
);
272 for (j
= 0; j
< reds
->num
; ++j
)
273 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
277 if (reds
->rules
[j
] != default_reduction
)
278 max_length (&width
, symbols
[i
]->tag
);
283 max_length (&width
, symbols
[i
]->tag
);
288 /* Nothing to report. */
295 /* Report lookahead tokens (or $default) and reductions. */
296 if (reds
->lookahead_tokens
)
297 for (i
= 0; i
< ntokens
; i
++)
299 bool defaulted
= false;
300 bool count
= bitset_test (no_reduce_set
, i
);
302 default_reduction_only
= false;
304 for (j
= 0; j
< reds
->num
; ++j
)
305 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
309 if (reds
->rules
[j
] != default_reduction
)
311 default_reduction_only
= false;
312 print_reduction (out
, width
,
314 reds
->rules
[j
], true);
322 default_reduction_only
= false;
324 print_reduction (out
, width
,
326 default_reduction
, true);
328 print_reduction (out
, width
,
330 reds
->rules
[j
], false);
335 if (default_reduction
)
337 char *default_reductions
=
338 muscle_percent_define_get ("lr.default-reductions");
339 print_reduction (out
, width
, _("$default"), default_reduction
, true);
340 aver (0 == strcmp (default_reductions
, "all")
341 || (0 == strcmp (default_reductions
, "consistent")
342 && default_reduction_only
)
343 || (reds
->num
== 1 && reds
->rules
[0]->number
== 0));
344 free (default_reductions
);
349 /*--------------------------------------------------------------.
350 | Report on OUT all the actions (shifts, gotos, reductions, and |
351 | explicit erros from %nonassoc) of S. |
352 `--------------------------------------------------------------*/
355 print_actions (FILE *out
, state
*s
)
358 print_transitions (s
, out
, true);
360 print_reductions (out
, s
);
362 print_transitions (s
, out
, false);
366 /*----------------------------------.
367 | Report all the data on S on OUT. |
368 `----------------------------------*/
371 print_state (FILE *out
, state
*s
)
374 fprintf (out
, _("state %d"), s
->number
);
377 print_actions (out
, s
);
378 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts
)
381 fputs (s
->solved_conflicts
, out
);
385 /*-----------------------------------------.
386 | Print information on the whole grammar. |
387 `-----------------------------------------*/
389 #define END_TEST(End) \
391 if (column + strlen (buffer) > (End)) \
393 fprintf (out, "%s\n ", buffer); \
401 print_grammar (FILE *out
)
407 grammar_rules_print (out
);
409 /* TERMINAL (type #) : rule #s terminal is on RHS */
410 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
411 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
412 if (token_translations
[i
] != undeftoken
->number
)
414 const char *tag
= symbols
[token_translations
[i
]]->tag
;
419 column
= strlen (tag
);
422 sprintf (buffer
, " (%d)", i
);
424 for (r
= 0; r
< nrules
; r
++)
425 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
426 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
429 sprintf (buffer
+ strlen (buffer
), " %d", r
);
432 fprintf (out
, "%s\n", buffer
);
437 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
438 for (i
= ntokens
; i
< nsyms
; i
++)
440 int left_count
= 0, right_count
= 0;
442 const char *tag
= symbols
[i
]->tag
;
444 for (r
= 0; r
< nrules
; r
++)
447 if (rules
[r
].lhs
->number
== i
)
449 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
450 if (item_number_as_symbol_number (*rhsp
) == i
)
459 column
= strlen (tag
);
460 sprintf (buffer
, " (%d)", i
);
466 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
468 for (r
= 0; r
< nrules
; r
++)
470 if (rules
[r
].lhs
->number
== i
)
473 sprintf (buffer
+ strlen (buffer
), " %d", r
);
481 sprintf (buffer
+ strlen (buffer
), ",");
483 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
484 for (r
= 0; r
< nrules
; r
++)
487 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
488 if (item_number_as_symbol_number (*rhsp
) == i
)
491 sprintf (buffer
+ strlen (buffer
), " %d", r
);
496 fprintf (out
, "%s\n", buffer
);
505 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
506 that conflicts with Posix. */
507 FILE *out
= xfopen (spec_verbose_file
, "w");
510 grammar_rules_partial_print (out
,
511 _("Rules useless in parser due to conflicts"),
512 rule_useless_in_parser_p
);
513 conflicts_output (out
);
517 /* If the whole state item sets, not only the kernels, are wanted,
518 `closure' will be run, which needs memory allocation/deallocation. */
519 if (report_flag
& report_itemsets
)
520 new_closure (nritems
);
521 /* Storage for print_reductions. */
522 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
523 for (i
= 0; i
< nstates
; i
++)
524 print_state (out
, states
[i
]);
525 bitset_free (no_reduce_set
);
526 if (report_flag
& report_itemsets
)