]>
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, 2001, 2002, 2003, 2004, 2005
4 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison 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 2, or (at your option)
13 Bison 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 Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
31 #include "conflicts.h"
42 static bitset shift_set
;
43 static bitset lookahead_set
;
47 print_token (int extnum
, int token
)
49 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
55 /*---------------------------------------.
56 | *WIDTH := max (*WIDTH, strlen (STR)). |
57 `---------------------------------------*/
60 max_length (size_t *width
, const char *str
)
62 size_t len
= strlen (str
);
67 /*--------------------------------.
68 | Report information on a state. |
69 `--------------------------------*/
72 print_core (FILE *out
, state
*s
)
75 item_number
*sitems
= s
->items
;
76 size_t snritems
= s
->nitems
;
77 symbol
*previous_lhs
= NULL
;
79 /* Output all the items of a state, not only its kernel. */
80 if (report_flag
& report_itemsets
)
82 closure (sitems
, snritems
);
92 for (i
= 0; i
< snritems
; i
++)
98 sp1
= sp
= ritem
+ sitems
[i
];
103 r
= item_number_as_rule_number (*sp
);
105 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
106 previous_lhs
= rules
[r
].lhs
;
108 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
109 fprintf (out
, " %s", symbols
[*sp
]->tag
);
111 for (/* Nothing */; *sp
>= 0; ++sp
)
112 fprintf (out
, " %s", symbols
[*sp
]->tag
);
114 /* Display the lookahead tokens? */
115 if (report_flag
& report_lookahead_tokens
)
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 | Return the default rule of S if it has one, NULL otherwise. |
211 `-------------------------------------------------------------*/
214 state_default_rule (state
*s
)
216 reductions
*reds
= s
->reductions
;
217 rule
*default_rule
= NULL
;
221 /* No need for a lookahead. */
223 return reds
->rules
[0];
225 /* 1. Each reduction is possibly masked by the lookahead tokens on which
226 we shift (S/R conflicts)... */
227 bitset_zero (shift_set
);
229 transitions
*trans
= s
->transitions
;
230 FOR_EACH_SHIFT (trans
, i
)
232 /* If this state has a shift for the error token, don't use a
234 if (TRANSITION_IS_ERROR (trans
, i
))
236 bitset_set (shift_set
, TRANSITION_SYMBOL (trans
, i
));
240 /* 2. Each reduction is possibly masked by the lookahead tokens on which
241 we raise an error (due to %nonassoc). */
243 errs
*errp
= s
->errs
;
244 for (i
= 0; i
< errp
->num
; i
++)
245 if (errp
->symbols
[i
])
246 bitset_set (shift_set
, errp
->symbols
[i
]->number
);
249 for (i
= 0; i
< reds
->num
; ++i
)
253 /* How many non-masked lookahead tokens are there for this
255 bitset_andn (lookahead_set
, reds
->lookahead_tokens
[i
], shift_set
);
256 count
= bitset_count (lookahead_set
);
261 default_rule
= reds
->rules
[i
];
264 /* 3. And finally, each reduction is possibly masked by previous
265 reductions (in R/R conflicts, we keep the first reductions).
267 bitset_or (shift_set
, shift_set
, reds
->lookahead_tokens
[i
]);
274 /*-------------------------------------------------------------------------.
275 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
276 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
278 `-------------------------------------------------------------------------*/
281 print_reduction (FILE *out
, size_t width
,
282 const char *lookahead_token
,
283 rule
*r
, bool enabled
)
286 fprintf (out
, " %s", lookahead_token
);
287 for (j
= width
- strlen (lookahead_token
); j
> 0; --j
)
292 fprintf (out
, _("reduce using rule %d (%s)"), r
->number
, r
->lhs
->tag
);
294 fprintf (out
, _("accept"));
301 /*-------------------------------------------.
302 | Report on OUT the reduction actions of S. |
303 `-------------------------------------------*/
306 print_reductions (FILE *out
, state
*s
)
308 transitions
*trans
= s
->transitions
;
309 reductions
*reds
= s
->reductions
;
310 rule
*default_rule
= NULL
;
317 default_rule
= state_default_rule (s
);
319 bitset_zero (shift_set
);
320 FOR_EACH_SHIFT (trans
, i
)
321 bitset_set (shift_set
, TRANSITION_SYMBOL (trans
, i
));
323 /* Compute the width of the lookahead token column. */
325 width
= strlen (_("$default"));
327 if (reds
->lookahead_tokens
)
328 for (i
= 0; i
< ntokens
; i
++)
330 bool count
= bitset_test (shift_set
, i
);
332 for (j
= 0; j
< reds
->num
; ++j
)
333 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
337 if (reds
->rules
[j
] != default_rule
)
338 max_length (&width
, symbols
[i
]->tag
);
343 max_length (&width
, symbols
[i
]->tag
);
348 /* Nothing to report. */
355 /* Report lookahead tokens (or $default) and reductions. */
356 if (reds
->lookahead_tokens
)
357 for (i
= 0; i
< ntokens
; i
++)
359 bool defaulted
= false;
360 bool count
= bitset_test (shift_set
, i
);
362 for (j
= 0; j
< reds
->num
; ++j
)
363 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
367 if (reds
->rules
[j
] != default_rule
)
368 print_reduction (out
, width
,
370 reds
->rules
[j
], true);
378 print_reduction (out
, width
,
382 print_reduction (out
, width
,
384 reds
->rules
[j
], false);
390 print_reduction (out
, width
,
391 _("$default"), default_rule
, true);
395 /*--------------------------------------------------------------.
396 | Report on OUT all the actions (shifts, gotos, reductions, and |
397 | explicit erros from %nonassoc) of S. |
398 `--------------------------------------------------------------*/
401 print_actions (FILE *out
, state
*s
)
404 print_transitions (s
, out
, true);
406 print_reductions (out
, s
);
408 print_transitions (s
, out
, false);
412 /*----------------------------------.
413 | Report all the data on S on OUT. |
414 `----------------------------------*/
417 print_state (FILE *out
, state
*s
)
420 fprintf (out
, _("state %d"), s
->number
);
423 print_actions (out
, s
);
424 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts
)
427 fputs (s
->solved_conflicts
, out
);
431 /*-----------------------------------------.
432 | Print information on the whole grammar. |
433 `-----------------------------------------*/
435 #define END_TEST(End) \
437 if (column + strlen(buffer) > (End)) \
439 fprintf (out, "%s\n ", buffer); \
447 print_grammar (FILE *out
)
453 grammar_rules_print (out
);
455 /* TERMINAL (type #) : rule #s terminal is on RHS */
456 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
457 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
458 if (token_translations
[i
] != undeftoken
->number
)
460 const char *tag
= symbols
[token_translations
[i
]]->tag
;
465 column
= strlen (tag
);
468 sprintf (buffer
, " (%d)", i
);
470 for (r
= 0; r
< nrules
; r
++)
471 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
472 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
475 sprintf (buffer
+ strlen (buffer
), " %d", r
);
478 fprintf (out
, "%s\n", buffer
);
483 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
484 for (i
= ntokens
; i
< nsyms
; i
++)
486 int left_count
= 0, right_count
= 0;
488 const char *tag
= symbols
[i
]->tag
;
490 for (r
= 0; r
< nrules
; r
++)
493 if (rules
[r
].lhs
->number
== i
)
495 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
496 if (item_number_as_symbol_number (*rhsp
) == i
)
505 column
= strlen (tag
);
506 sprintf (buffer
, " (%d)", i
);
512 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
514 for (r
= 0; r
< nrules
; r
++)
517 if (rules
[r
].lhs
->number
== i
)
518 sprintf (buffer
+ strlen (buffer
), " %d", r
);
525 sprintf (buffer
+ strlen (buffer
), ",");
527 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
528 for (r
= 0; r
< nrules
; r
++)
531 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
532 if (item_number_as_symbol_number (*rhsp
) == i
)
535 sprintf (buffer
+ strlen (buffer
), " %d", r
);
540 fprintf (out
, "%s\n", buffer
);
549 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
550 that conflicts with Posix. */
551 FILE *out
= xfopen (spec_verbose_file
, "w");
554 grammar_rules_partial_print (out
,
555 _("Rules never reduced"), rule_never_reduced_p
);
556 conflicts_output (out
);
560 /* If the whole state item sets, not only the kernels, are wanted,
561 `closure' will be run, which needs memory allocation/deallocation. */
562 if (report_flag
& report_itemsets
)
563 new_closure (nritems
);
564 /* Storage for print_reductions. */
565 shift_set
= bitset_create (ntokens
, BITSET_FIXED
);
566 lookahead_set
= bitset_create (ntokens
, BITSET_FIXED
);
567 for (i
= 0; i
< nstates
; i
++)
568 print_state (out
, states
[i
]);
569 bitset_free (shift_set
);
570 bitset_free (lookahead_set
);
571 if (report_flag
& report_itemsets
)