]>
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, 2007
4 Free 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"
41 static bitset no_reduce_set
;
45 print_token (int extnum
, int token
)
47 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
53 /*---------------------------------------.
54 | *WIDTH := max (*WIDTH, strlen (STR)). |
55 `---------------------------------------*/
58 max_length (size_t *width
, const char *str
)
60 size_t len
= strlen (str
);
65 /*--------------------------------.
66 | Report information on a state. |
67 `--------------------------------*/
70 print_core (FILE *out
, state
*s
)
73 item_number
*sitems
= s
->items
;
74 size_t snritems
= s
->nitems
;
75 symbol
*previous_lhs
= NULL
;
77 /* Output all the items of a state, not only its kernel. */
78 if (report_flag
& report_itemsets
)
80 closure (sitems
, snritems
);
90 for (i
= 0; i
< snritems
; i
++)
96 sp1
= sp
= ritem
+ sitems
[i
];
101 r
= item_number_as_rule_number (*sp
);
103 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
104 previous_lhs
= rules
[r
].lhs
;
106 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
107 fprintf (out
, " %s", symbols
[*sp
]->tag
);
109 for (/* Nothing */; *sp
>= 0; ++sp
)
110 fprintf (out
, " %s", symbols
[*sp
]->tag
);
112 /* Display the lookahead tokens? */
113 if (report_flag
& report_lookahead_tokens
114 && item_number_is_rule_number (*sp1
))
115 state_rule_lookahead_tokens_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 lookahead token 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 lookahead tokens 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 lookahead token 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 lookahead tokens 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 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
210 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
212 `-------------------------------------------------------------------------*/
215 print_reduction (FILE *out
, size_t width
,
216 const char *lookahead_token
,
217 rule
*r
, bool enabled
)
220 fprintf (out
, " %s", lookahead_token
);
221 for (j
= width
- strlen (lookahead_token
); j
> 0; --j
)
226 fprintf (out
, _("reduce using rule %d (%s)"), r
->number
, r
->lhs
->tag
);
228 fprintf (out
, _("accept"));
235 /*-------------------------------------------.
236 | Report on OUT the reduction actions of S. |
237 `-------------------------------------------*/
240 print_reductions (FILE *out
, state
*s
)
242 transitions
*trans
= s
->transitions
;
243 reductions
*reds
= s
->reductions
;
244 rule
*default_rule
= NULL
;
251 if (yydefact
[s
->number
] != 0)
252 default_rule
= &rules
[yydefact
[s
->number
] - 1];
254 bitset_zero (no_reduce_set
);
255 FOR_EACH_SHIFT (trans
, i
)
256 bitset_set (no_reduce_set
, TRANSITION_SYMBOL (trans
, i
));
257 for (i
= 0; i
< s
->errs
->num
; ++i
)
258 if (s
->errs
->symbols
[i
])
259 bitset_set (no_reduce_set
, s
->errs
->symbols
[i
]->number
);
261 /* Compute the width of the lookahead token column. */
263 width
= strlen (_("$default"));
265 if (reds
->lookahead_tokens
)
266 for (i
= 0; i
< ntokens
; i
++)
268 bool count
= bitset_test (no_reduce_set
, i
);
270 for (j
= 0; j
< reds
->num
; ++j
)
271 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
275 if (reds
->rules
[j
] != default_rule
)
276 max_length (&width
, symbols
[i
]->tag
);
281 max_length (&width
, symbols
[i
]->tag
);
286 /* Nothing to report. */
293 /* Report lookahead tokens (or $default) and reductions. */
294 if (reds
->lookahead_tokens
)
295 for (i
= 0; i
< ntokens
; i
++)
297 bool defaulted
= false;
298 bool count
= bitset_test (no_reduce_set
, i
);
300 for (j
= 0; j
< reds
->num
; ++j
)
301 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
305 if (reds
->rules
[j
] != default_rule
)
306 print_reduction (out
, width
,
308 reds
->rules
[j
], true);
316 print_reduction (out
, width
,
320 print_reduction (out
, width
,
322 reds
->rules
[j
], false);
328 print_reduction (out
, width
,
329 _("$default"), default_rule
, true);
333 /*--------------------------------------------------------------.
334 | Report on OUT all the actions (shifts, gotos, reductions, and |
335 | explicit erros from %nonassoc) of S. |
336 `--------------------------------------------------------------*/
339 print_actions (FILE *out
, state
*s
)
342 print_transitions (s
, out
, true);
344 print_reductions (out
, s
);
346 print_transitions (s
, out
, false);
350 /*----------------------------------.
351 | Report all the data on S on OUT. |
352 `----------------------------------*/
355 print_state (FILE *out
, state
*s
)
358 fprintf (out
, _("state %d"), s
->number
);
361 print_actions (out
, s
);
362 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts
)
365 fputs (s
->solved_conflicts
, out
);
369 /*-----------------------------------------.
370 | Print information on the whole grammar. |
371 `-----------------------------------------*/
373 #define END_TEST(End) \
375 if (column + strlen(buffer) > (End)) \
377 fprintf (out, "%s\n ", buffer); \
385 print_grammar (FILE *out
)
391 grammar_rules_print (out
);
393 /* TERMINAL (type #) : rule #s terminal is on RHS */
394 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
395 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
396 if (token_translations
[i
] != undeftoken
->number
)
398 const char *tag
= symbols
[token_translations
[i
]]->tag
;
403 column
= strlen (tag
);
406 sprintf (buffer
, " (%d)", i
);
408 for (r
= 0; r
< nrules
; r
++)
409 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
410 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
413 sprintf (buffer
+ strlen (buffer
), " %d", r
);
416 fprintf (out
, "%s\n", buffer
);
421 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
422 for (i
= ntokens
; i
< nsyms
; i
++)
424 int left_count
= 0, right_count
= 0;
426 const char *tag
= symbols
[i
]->tag
;
428 for (r
= 0; r
< nrules
; r
++)
431 if (rules
[r
].lhs
->number
== i
)
433 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
434 if (item_number_as_symbol_number (*rhsp
) == i
)
443 column
= strlen (tag
);
444 sprintf (buffer
, " (%d)", i
);
450 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
452 for (r
= 0; r
< nrules
; r
++)
454 if (rules
[r
].lhs
->number
== i
)
457 sprintf (buffer
+ strlen (buffer
), " %d", r
);
465 sprintf (buffer
+ strlen (buffer
), ",");
467 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
468 for (r
= 0; r
< nrules
; r
++)
471 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
472 if (item_number_as_symbol_number (*rhsp
) == i
)
475 sprintf (buffer
+ strlen (buffer
), " %d", r
);
480 fprintf (out
, "%s\n", buffer
);
489 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
490 that conflicts with Posix. */
491 FILE *out
= xfopen (spec_verbose_file
, "w");
494 grammar_rules_partial_print (out
,
495 _("Rules never reduced"), rule_never_reduced_p
);
496 conflicts_output (out
);
500 /* If the whole state item sets, not only the kernels, are wanted,
501 `closure' will be run, which needs memory allocation/deallocation. */
502 if (report_flag
& report_itemsets
)
503 new_closure (nritems
);
504 /* Storage for print_reductions. */
505 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
506 for (i
= 0; i
< nstates
; i
++)
507 print_state (out
, states
[i
]);
508 bitset_free (no_reduce_set
);
509 if (report_flag
& report_itemsets
)