]>
git.saurik.com Git - bison.git/blob - src/print.c
1 /* Print information on generated parser, for bison,
2 Copyright (C) 1984, 1986, 1989, 2000, 2001, 2002
3 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
30 #include "conflicts.h"
41 static bitset shiftset
;
42 static bitset lookaheadset
;
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 int 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 lookaheads? */
114 if (report_flag
& report_lookaheads
)
115 state_rule_lookaheads_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 lookaheads 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 lookaheads 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 lookaheads 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 lookaheads 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 | Return the default rule of S if it has one, NULL otherwise. |
210 `-------------------------------------------------------------*/
213 state_default_rule (state
*s
)
215 reductions
*reds
= s
->reductions
;
216 rule
*default_rule
= NULL
;
220 /* No need for a lookahead. */
222 return reds
->rules
[0];
224 /* 1. Each reduction is possibly masked by the lookaheads on which
225 we shift (S/R conflicts)... */
226 bitset_zero (shiftset
);
228 transitions
*trans
= s
->transitions
;
229 FOR_EACH_SHIFT (trans
, i
)
231 /* If this state has a shift for the error token, don't use a
233 if (TRANSITION_IS_ERROR (trans
, i
))
235 bitset_set (shiftset
, TRANSITION_SYMBOL (trans
, i
));
239 /* 2. Each reduction is possibly masked by the lookaheads on which
240 we raise an error (due to %nonassoc). */
242 errs
*errp
= s
->errs
;
243 for (i
= 0; i
< errp
->num
; i
++)
244 if (errp
->symbols
[i
])
245 bitset_set (shiftset
, errp
->symbols
[i
]->number
);
248 for (i
= 0; i
< reds
->num
; ++i
)
252 /* How many non-masked lookaheads are there for this reduction?
254 bitset_andn (lookaheadset
, reds
->lookaheads
[i
], shiftset
);
255 count
= bitset_count (lookaheadset
);
260 default_rule
= reds
->rules
[i
];
263 /* 3. And finally, each reduction is possibly masked by previous
264 reductions (in R/R conflicts, we keep the first reductions).
266 bitset_or (shiftset
, shiftset
, reds
->lookaheads
[i
]);
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 |
277 `--------------------------------------------------------------------*/
280 print_reduction (FILE *out
, size_t width
,
281 const char *lookahead
,
282 rule
*r
, bool enabled
)
285 fprintf (out
, " %s", lookahead
);
286 for (j
= width
- strlen (lookahead
); j
> 0; --j
)
291 fprintf (out
, _("reduce using rule %d (%s)"), r
->number
, r
->lhs
->tag
);
293 fprintf (out
, _("accept"));
300 /*-------------------------------------------.
301 | Report on OUT the reduction actions of S. |
302 `-------------------------------------------*/
305 print_reductions (FILE *out
, state
*s
)
307 transitions
*trans
= s
->transitions
;
308 reductions
*reds
= s
->reductions
;
309 rule
*default_rule
= NULL
;
316 default_rule
= state_default_rule (s
);
318 bitset_zero (shiftset
);
319 FOR_EACH_SHIFT (trans
, i
)
320 bitset_set (shiftset
, TRANSITION_SYMBOL (trans
, i
));
322 /* Compute the width of the lookaheads column. */
324 width
= strlen (_("$default"));
326 if (reds
->lookaheads
)
327 for (i
= 0; i
< ntokens
; i
++)
329 int count
= bitset_test (shiftset
, i
);
331 for (j
= 0; j
< reds
->num
; ++j
)
332 if (bitset_test (reds
->lookaheads
[j
], i
))
336 if (reds
->rules
[j
] != default_rule
)
337 max_length (&width
, symbols
[i
]->tag
);
342 max_length (&width
, symbols
[i
]->tag
);
347 /* Nothing to report. */
354 /* Report lookaheads (or $default) and reductions. */
355 if (reds
->lookaheads
)
356 for (i
= 0; i
< ntokens
; i
++)
359 int count
= bitset_test (shiftset
, i
);
361 for (j
= 0; j
< reds
->num
; ++j
)
362 if (bitset_test (reds
->lookaheads
[j
], i
))
366 if (reds
->rules
[j
] != default_rule
)
367 print_reduction (out
, width
,
369 reds
->rules
[j
], true);
377 print_reduction (out
, width
,
381 print_reduction (out
, width
,
383 reds
->rules
[j
], false);
389 print_reduction (out
, width
,
390 _("$default"), default_rule
, true);
394 /*--------------------------------------------------------------.
395 | Report on OUT all the actions (shifts, gotos, reductions, and |
396 | explicit erros from %nonassoc) of S. |
397 `--------------------------------------------------------------*/
400 print_actions (FILE *out
, state
*s
)
403 print_transitions (s
, out
, true);
405 print_reductions (out
, s
);
407 print_transitions (s
, out
, false);
411 /*----------------------------------.
412 | Report all the data on S on OUT. |
413 `----------------------------------*/
416 print_state (FILE *out
, state
*s
)
419 fprintf (out
, _("state %d"), s
->number
);
422 print_actions (out
, s
);
423 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts
)
426 fputs (s
->solved_conflicts
, out
);
430 /*-----------------------------------------.
431 | Print information on the whole grammar. |
432 `-----------------------------------------*/
434 #define END_TEST(End) \
436 if (column + strlen(buffer) > (End)) \
438 fprintf (out, "%s\n ", buffer); \
446 print_grammar (FILE *out
)
452 grammar_rules_print (out
);
454 /* TERMINAL (type #) : rule #s terminal is on RHS */
455 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
456 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
457 if (token_translations
[i
] != undeftoken
->number
)
459 const char *tag
= symbols
[token_translations
[i
]]->tag
;
464 column
= strlen (tag
);
467 sprintf (buffer
, " (%d)", i
);
469 for (r
= 0; r
< nrules
; r
++)
470 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
471 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
474 sprintf (buffer
+ strlen (buffer
), " %d", r
);
477 fprintf (out
, "%s\n", buffer
);
482 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
483 for (i
= ntokens
; i
< nsyms
; i
++)
485 int left_count
= 0, right_count
= 0;
487 const char *tag
= symbols
[i
]->tag
;
489 for (r
= 0; r
< nrules
; r
++)
492 if (rules
[r
].lhs
->number
== i
)
494 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
495 if (item_number_as_symbol_number (*rhsp
) == i
)
504 column
= strlen (tag
);
505 sprintf (buffer
, " (%d)", i
);
511 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
513 for (r
= 0; r
< nrules
; r
++)
516 if (rules
[r
].lhs
->number
== i
)
517 sprintf (buffer
+ strlen (buffer
), " %d", r
);
524 sprintf (buffer
+ strlen (buffer
), ",");
526 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
527 for (r
= 0; r
< nrules
; r
++)
530 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
531 if (item_number_as_symbol_number (*rhsp
) == i
)
534 sprintf (buffer
+ strlen (buffer
), " %d", r
);
539 fprintf (out
, "%s\n", buffer
);
548 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
549 that conflicts with Posix. */
550 FILE *out
= xfopen (spec_verbose_file
, "w");
553 grammar_rules_partial_print (out
,
554 _("Rules never reduced"), rule_never_reduced_p
);
555 conflicts_output (out
);
559 /* If the whole state item sets, not only the kernels, are wanted,
560 `closure' will be run, which needs memory allocation/deallocation. */
561 if (report_flag
& report_itemsets
)
562 new_closure (nritems
);
563 /* Storage for print_reductions. */
564 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
565 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
566 for (i
= 0; i
< nstates
; i
++)
567 print_state (out
, states
[i
]);
568 bitset_free (shiftset
);
569 bitset_free (lookaheadset
);
570 if (report_flag
& report_itemsets
)