]>
git.saurik.com Git - bison.git/blob - src/print.c
7adb51b06016495469461e2b4b49e3a9cf1d894f
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
];
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_number_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
);
164 fprintf (out
, _("go to state %d\n"), state1
);
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
, symbols
[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
= symbols
[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 &rules
[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 (i
= 0; i
< transitions
->num
&& TRANSITION_IS_SHIFT (transitions
, i
); i
++)
229 if (!TRANSITION_IS_DISABLED (transitions
, i
))
231 /* If this state has a shift for the error token, don't use a
233 if (TRANSITION_IS_ERROR (transitions
, i
))
235 bitset_set (shiftset
, TRANSITION_SYMBOL (transitions
, i
));
239 /* 2. Each reduction is possibly masked by the lookaheads on which
240 we raise an error (due to %nonassoc). */
242 errs_t
*errp
= state
->errs
;
243 for (i
= 0; i
< errp
->num
; i
++)
244 if (errp
->symbols
[i
])
245 bitset_set (shiftset
, errp
->symbols
[i
]);
248 for (i
= 0; i
< state
->nlookaheads
; ++i
)
252 /* How many non-masked lookaheads are there for this reduction?
254 bitset_andn (lookaheadset
, state
->lookaheads
[i
], shiftset
);
255 count
= bitset_count (lookaheadset
);
260 default_rule
= state
->lookaheads_rule
[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
, state
->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_t
*rule
, bool enabled
)
285 fprintf (out
, " %s", lookahead
);
286 for (j
= width
- strlen (lookahead
); j
> 0; --j
)
290 fprintf (out
, _("reduce using rule %d (%s)"),
291 rule
->number
- 1, rule
->lhs
->tag
);
298 /*----------------------------------------------------.
299 | Report on OUT the reduction actions of this STATE. |
300 `----------------------------------------------------*/
303 print_reductions (FILE *out
, state_t
*state
)
305 transitions_t
*transitions
= state
->transitions
;
306 reductions_t
*redp
= state
->reductions
;
307 rule_t
*default_rule
= NULL
;
314 default_rule
= state_default_rule (state
);
316 bitset_zero (shiftset
);
317 for (i
= 0; i
< transitions
->num
&& TRANSITION_IS_SHIFT (transitions
, i
); i
++)
318 if (!TRANSITION_IS_DISABLED (transitions
, i
))
319 bitset_set (shiftset
, TRANSITION_SYMBOL (transitions
, i
));
321 /* Compute the width of the lookaheads column. */
323 width
= strlen (_("$default"));
324 for (i
= 0; i
< ntokens
; i
++)
326 int count
= bitset_test (shiftset
, i
);
328 for (j
= 0; j
< state
->nlookaheads
; ++j
)
329 if (bitset_test (state
->lookaheads
[j
], i
))
333 if (state
->lookaheads_rule
[j
] != default_rule
)
334 max_length (&width
, symbols
[i
]->tag
);
339 max_length (&width
, symbols
[i
]->tag
);
344 /* Nothing to report. */
351 /* Report lookaheads (or $default) and reductions. */
352 for (i
= 0; i
< ntokens
; i
++)
355 int count
= bitset_test (shiftset
, i
);
357 for (j
= 0; j
< state
->nlookaheads
; ++j
)
358 if (bitset_test (state
->lookaheads
[j
], i
))
362 if (state
->lookaheads_rule
[j
] != default_rule
)
363 print_reduction (out
, width
,
365 state
->lookaheads_rule
[j
], TRUE
);
373 print_reduction (out
, width
,
377 print_reduction (out
, width
,
379 state
->lookaheads_rule
[j
], FALSE
);
385 print_reduction (out
, width
,
386 _("$default"), default_rule
, TRUE
);
390 /*--------------------------------------------------------------.
391 | Report on OUT all the actions (shifts, gotos, reductions, and |
392 | explicit erros from %nonassoc) of STATE. |
393 `--------------------------------------------------------------*/
396 print_actions (FILE *out
, state_t
*state
)
398 reductions_t
*redp
= state
->reductions
;
399 transitions_t
*transitions
= state
->transitions
;
401 if (transitions
->num
== 0 && redp
->num
== 0)
404 if (state
->number
== final_state
->number
)
405 fprintf (out
, _(" $default\taccept\n"));
407 fprintf (out
, _(" NO ACTIONS\n"));
412 print_transitions (state
, out
, TRUE
);
413 print_errs (out
, state
);
414 print_reductions (out
, state
);
416 print_transitions (state
, out
, FALSE
);
420 /*--------------------------------------.
421 | Report all the data on STATE on OUT. |
422 `--------------------------------------*/
425 print_state (FILE *out
, state_t
*state
)
428 fprintf (out
, _("state %d"), state
->number
);
430 print_core (out
, state
);
431 print_actions (out
, state
);
432 if ((report_flag
& report_solved_conflicts
)
433 && state
->solved_conflicts
)
434 fputs (state
->solved_conflicts
, out
);
437 /*-----------------------------------------.
438 | Print information on the whole grammar. |
439 `-----------------------------------------*/
441 #define END_TEST(End) \
443 if (column + strlen(buffer) > (End)) \
445 fprintf (out, "%s\n ", buffer); \
453 print_grammar (FILE *out
)
459 grammar_rules_print (out
);
461 /* TERMINAL (type #) : rule #s terminal is on RHS */
462 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
463 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
464 if (token_translations
[i
] != undeftoken
->number
)
466 const char *tag
= symbols
[token_translations
[i
]]->tag
;
471 column
= strlen (tag
);
474 sprintf (buffer
, " (%d)", i
);
476 for (r
= 1; r
< nrules
+ 1; r
++)
477 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
478 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
481 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
484 fprintf (out
, "%s\n", buffer
);
489 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
490 for (i
= ntokens
; i
< nsyms
; i
++)
492 int left_count
= 0, right_count
= 0;
494 const char *tag
= symbols
[i
]->tag
;
496 for (r
= 1; r
< nrules
+ 1; r
++)
499 if (rules
[r
].lhs
->number
== i
)
501 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
502 if (item_number_as_symbol_number (*rhsp
) == i
)
511 column
= strlen (tag
);
512 sprintf (buffer
, " (%d)", i
);
518 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
520 for (r
= 1; r
< nrules
+ 1; r
++)
523 if (rules
[r
].lhs
->number
== i
)
524 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
531 sprintf (buffer
+ strlen (buffer
), ",");
533 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
534 for (r
= 1; r
< nrules
+ 1; r
++)
537 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
538 if (item_number_as_symbol_number (*rhsp
) == i
)
541 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
546 fprintf (out
, "%s\n", buffer
);
555 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
556 that conflicts with Posix. */
557 FILE *out
= xfopen (spec_verbose_file
, "w");
560 conflicts_output (out
);
564 /* If the whole state item sets, not only the kernels, are wanted,
565 `closure' will be run, which needs memory allocation/deallocation. */
566 if (report_flag
& report_itemsets
)
567 new_closure (nritems
);
568 /* Storage for print_reductions. */
569 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
570 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
571 for (i
= 0; i
< nstates
; i
++)
572 print_state (out
, states
[i
]);
573 bitset_free (shiftset
);
574 bitset_free (lookaheadset
);
575 if (report_flag
& report_itemsets
)