]>
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"
39 static bitset shiftset
;
40 static bitset lookaheadset
;
44 print_token (int extnum
, int token
)
46 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
52 /*---------------------------------------.
53 | *WIDTH := max (*WIDTH, strlen (STR)). |
54 `---------------------------------------*/
57 max_length (size_t *width
, const char *str
)
59 size_t len
= strlen (str
);
64 /*--------------------------------.
65 | Report information on a state. |
66 `--------------------------------*/
69 print_core (FILE *out
, state_t
*state
)
72 item_number_t
*sitems
= state
->items
;
73 int snritems
= state
->nitems
;
74 symbol_t
*previous_lhs
= NULL
;
76 /* Output all the items of a state, not only its kernel. */
77 if (report_flag
& report_itemsets
)
79 closure (sitems
, snritems
);
89 for (i
= 0; i
< snritems
; i
++)
95 sp1
= sp
= ritem
+ sitems
[i
];
100 rule
= item_number_as_rule_number (*sp
);
102 rule_lhs_print (&rules
[rule
], previous_lhs
, out
);
103 previous_lhs
= rules
[rule
].lhs
;
105 for (sp
= rules
[rule
].rhs
; sp
< sp1
; sp
++)
106 fprintf (out
, " %s", symbols
[*sp
]->tag
);
108 for (/* Nothing */; *sp
>= 0; ++sp
)
109 fprintf (out
, " %s", symbols
[*sp
]->tag
);
111 /* Display the lookaheads? */
112 if (report_flag
& report_lookaheads
)
113 state_rule_lookaheads_print (state
, &rules
[rule
], out
);
120 /*----------------------------------------------------------------.
121 | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of STATE on |
123 `----------------------------------------------------------------*/
126 print_transitions (state_t
*state
, FILE *out
, bool display_transitions_p
)
128 transitions_t
*transitions
= state
->transitions
;
132 /* Compute the width of the lookaheads column. */
133 for (i
= 0; i
< transitions
->num
; i
++)
134 if (!TRANSITION_IS_DISABLED (transitions
, i
)
135 && TRANSITION_IS_SHIFT (transitions
, i
) == display_transitions_p
)
137 symbol_t
*symbol
= symbols
[TRANSITION_SYMBOL (transitions
, i
)];
138 max_length (&width
, symbol
->tag
);
141 /* Nothing to report. */
148 /* Report lookaheads and shifts. */
149 for (i
= 0; i
< transitions
->num
; i
++)
150 if (!TRANSITION_IS_DISABLED (transitions
, i
)
151 && TRANSITION_IS_SHIFT (transitions
, i
) == display_transitions_p
)
153 symbol_t
*symbol
= symbols
[TRANSITION_SYMBOL (transitions
, i
)];
154 const char *tag
= symbol
->tag
;
155 state_t
*state1
= transitions
->states
[i
];
158 fprintf (out
, " %s", tag
);
159 for (j
= width
- strlen (tag
); j
> 0; --j
)
161 if (display_transitions_p
)
162 fprintf (out
, _("shift, and go to state %d\n"), state1
->number
);
164 fprintf (out
, _("go to state %d\n"), state1
->number
);
169 /*------------------------------------------------------------.
170 | Report the explicit errors of STATE raised from %nonassoc. |
171 `------------------------------------------------------------*/
174 print_errs (FILE *out
, state_t
*state
)
176 errs_t
*errp
= state
->errs
;
180 /* Compute the width of the lookaheads column. */
181 for (i
= 0; i
< errp
->num
; ++i
)
182 if (errp
->symbols
[i
])
183 max_length (&width
, errp
->symbols
[i
]->tag
);
185 /* Nothing to report. */
192 /* Report lookaheads and errors. */
193 for (i
= 0; i
< errp
->num
; ++i
)
194 if (errp
->symbols
[i
])
196 const char *tag
= errp
->symbols
[i
]->tag
;
198 fprintf (out
, " %s", tag
);
199 for (j
= width
- strlen (tag
); j
> 0; --j
)
201 fputs (_("error (nonassociative)\n"), out
);
206 /*----------------------------------------------------------.
207 | Return the default rule of this STATE if it has one, NULL |
209 `----------------------------------------------------------*/
212 state_default_rule (state_t
*state
)
214 reductions_t
*redp
= state
->reductions
;
215 rule_t
*default_rule
= NULL
;
219 /* No need for a lookahead. */
220 if (state
->consistent
)
221 return redp
->rules
[0];
223 /* 1. Each reduction is possibly masked by the lookaheads on which
224 we shift (S/R conflicts)... */
225 bitset_zero (shiftset
);
227 transitions_t
*transitions
= state
->transitions
;
228 FOR_EACH_SHIFT (transitions
, i
)
230 /* If this state has a shift for the error token, don't use a
232 if (TRANSITION_IS_ERROR (transitions
, i
))
234 bitset_set (shiftset
, TRANSITION_SYMBOL (transitions
, i
));
238 /* 2. Each reduction is possibly masked by the lookaheads on which
239 we raise an error (due to %nonassoc). */
241 errs_t
*errp
= state
->errs
;
242 for (i
= 0; i
< errp
->num
; i
++)
243 if (errp
->symbols
[i
])
244 bitset_set (shiftset
, errp
->symbols
[i
]->number
);
247 for (i
= 0; i
< redp
->num
; ++i
)
251 /* How many non-masked lookaheads are there for this reduction?
253 bitset_andn (lookaheadset
, redp
->lookaheads
[i
], shiftset
);
254 count
= bitset_count (lookaheadset
);
259 default_rule
= redp
->rules
[i
];
262 /* 3. And finally, each reduction is possibly masked by previous
263 reductions (in R/R conflicts, we keep the first reductions).
265 bitset_or (shiftset
, shiftset
, redp
->lookaheads
[i
]);
272 /*--------------------------------------------------------------------.
273 | Report a reduction of RULE on LOOKAHEADS (which can be `default'). |
274 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
276 `--------------------------------------------------------------------*/
279 print_reduction (FILE *out
, size_t width
,
280 const char *lookahead
,
281 rule_t
*rule
, bool enabled
)
284 fprintf (out
, " %s", lookahead
);
285 for (j
= width
- strlen (lookahead
); j
> 0; --j
)
290 fprintf (out
, _("reduce using rule %d (%s)"),
291 rule
->number
, rule
->lhs
->tag
);
293 fprintf (out
, _("accept"));
300 /*----------------------------------------------------.
301 | Report on OUT the reduction actions of this STATE. |
302 `----------------------------------------------------*/
305 print_reductions (FILE *out
, state_t
*state
)
307 transitions_t
*transitions
= state
->transitions
;
308 reductions_t
*redp
= state
->reductions
;
309 rule_t
*default_rule
= NULL
;
316 default_rule
= state_default_rule (state
);
318 bitset_zero (shiftset
);
319 FOR_EACH_SHIFT (transitions
, i
)
320 bitset_set (shiftset
, TRANSITION_SYMBOL (transitions
, i
));
322 /* Compute the width of the lookaheads column. */
324 width
= strlen (_("$default"));
326 if (redp
->lookaheads
)
327 for (i
= 0; i
< ntokens
; i
++)
329 int count
= bitset_test (shiftset
, i
);
331 for (j
= 0; j
< redp
->num
; ++j
)
332 if (bitset_test (redp
->lookaheads
[j
], i
))
336 if (redp
->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 (redp
->lookaheads
)
356 for (i
= 0; i
< ntokens
; i
++)
359 int count
= bitset_test (shiftset
, i
);
361 for (j
= 0; j
< redp
->num
; ++j
)
362 if (bitset_test (redp
->lookaheads
[j
], i
))
366 if (redp
->rules
[j
] != default_rule
)
367 print_reduction (out
, width
,
369 redp
->rules
[j
], TRUE
);
377 print_reduction (out
, width
,
381 print_reduction (out
, width
,
383 redp
->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 STATE. |
397 `--------------------------------------------------------------*/
400 print_actions (FILE *out
, state_t
*state
)
403 print_transitions (state
, out
, TRUE
);
404 print_errs (out
, state
);
405 print_reductions (out
, state
);
407 print_transitions (state
, out
, FALSE
);
411 /*--------------------------------------.
412 | Report all the data on STATE on OUT. |
413 `--------------------------------------*/
416 print_state (FILE *out
, state_t
*state
)
419 fprintf (out
, _("state %d"), state
->number
);
421 print_core (out
, state
);
422 print_actions (out
, state
);
423 if ((report_flag
& report_solved_conflicts
)
424 && state
->solved_conflicts
)
425 fputs (state
->solved_conflicts
, out
);
428 /*-----------------------------------------.
429 | Print information on the whole grammar. |
430 `-----------------------------------------*/
432 #define END_TEST(End) \
434 if (column + strlen(buffer) > (End)) \
436 fprintf (out, "%s\n ", buffer); \
444 print_grammar (FILE *out
)
450 grammar_rules_print (out
);
452 /* TERMINAL (type #) : rule #s terminal is on RHS */
453 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
454 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
455 if (token_translations
[i
] != undeftoken
->number
)
457 const char *tag
= symbols
[token_translations
[i
]]->tag
;
462 column
= strlen (tag
);
465 sprintf (buffer
, " (%d)", i
);
467 for (r
= 0; r
< nrules
; r
++)
468 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
469 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
472 sprintf (buffer
+ strlen (buffer
), " %d", r
);
475 fprintf (out
, "%s\n", buffer
);
480 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
481 for (i
= ntokens
; i
< nsyms
; i
++)
483 int left_count
= 0, right_count
= 0;
485 const char *tag
= symbols
[i
]->tag
;
487 for (r
= 0; r
< nrules
; r
++)
490 if (rules
[r
].lhs
->number
== i
)
492 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
493 if (item_number_as_symbol_number (*rhsp
) == i
)
502 column
= strlen (tag
);
503 sprintf (buffer
, " (%d)", i
);
509 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
511 for (r
= 0; r
< nrules
; r
++)
514 if (rules
[r
].lhs
->number
== i
)
515 sprintf (buffer
+ strlen (buffer
), " %d", r
);
522 sprintf (buffer
+ strlen (buffer
), ",");
524 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
525 for (r
= 0; r
< nrules
; r
++)
528 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
529 if (item_number_as_symbol_number (*rhsp
) == i
)
532 sprintf (buffer
+ strlen (buffer
), " %d", r
);
537 fprintf (out
, "%s\n", buffer
);
546 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
547 that conflicts with Posix. */
548 FILE *out
= xfopen (spec_verbose_file
, "w");
551 grammar_rules_partial_print (out
,
552 _("Rules never reduced"), rule_never_reduced_p
);
553 conflicts_output (out
);
557 /* If the whole state item sets, not only the kernels, are wanted,
558 `closure' will be run, which needs memory allocation/deallocation. */
559 if (report_flag
& report_itemsets
)
560 new_closure (nritems
);
561 /* Storage for print_reductions. */
562 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
563 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
564 for (i
= 0; i
< nstates
; i
++)
565 print_state (out
, states
[i
]);
566 bitset_free (shiftset
);
567 bitset_free (lookaheadset
);
568 if (report_flag
& report_itemsets
)