]>
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 state_rule_lookahead_tokens_print (s
, &rules
[r
], out
);
121 /*------------------------------------------------------------.
122 | Report the shifts iff DISPLAY_SHIFTS_P or the gotos of S on |
124 `------------------------------------------------------------*/
127 print_transitions (state
*s
, FILE *out
, bool display_transitions_p
)
129 transitions
*trans
= s
->transitions
;
133 /* Compute the width of the lookahead token column. */
134 for (i
= 0; i
< trans
->num
; i
++)
135 if (!TRANSITION_IS_DISABLED (trans
, i
)
136 && TRANSITION_IS_SHIFT (trans
, i
) == display_transitions_p
)
138 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
139 max_length (&width
, sym
->tag
);
142 /* Nothing to report. */
149 /* Report lookahead tokens and shifts. */
150 for (i
= 0; i
< trans
->num
; i
++)
151 if (!TRANSITION_IS_DISABLED (trans
, i
)
152 && TRANSITION_IS_SHIFT (trans
, i
) == display_transitions_p
)
154 symbol
*sym
= symbols
[TRANSITION_SYMBOL (trans
, i
)];
155 const char *tag
= sym
->tag
;
156 state
*s1
= trans
->states
[i
];
159 fprintf (out
, " %s", tag
);
160 for (j
= width
- strlen (tag
); j
> 0; --j
)
162 if (display_transitions_p
)
163 fprintf (out
, _("shift, and go to state %d\n"), s1
->number
);
165 fprintf (out
, _("go to state %d\n"), s1
->number
);
170 /*--------------------------------------------------------.
171 | Report the explicit errors of S raised from %nonassoc. |
172 `--------------------------------------------------------*/
175 print_errs (FILE *out
, state
*s
)
177 errs
*errp
= s
->errs
;
181 /* Compute the width of the lookahead token column. */
182 for (i
= 0; i
< errp
->num
; ++i
)
183 if (errp
->symbols
[i
])
184 max_length (&width
, errp
->symbols
[i
]->tag
);
186 /* Nothing to report. */
193 /* Report lookahead tokens and errors. */
194 for (i
= 0; i
< errp
->num
; ++i
)
195 if (errp
->symbols
[i
])
197 const char *tag
= errp
->symbols
[i
]->tag
;
199 fprintf (out
, " %s", tag
);
200 for (j
= width
- strlen (tag
); j
> 0; --j
)
202 fputs (_("error (nonassociative)\n"), out
);
207 /*-------------------------------------------------------------------------.
208 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
209 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
211 `-------------------------------------------------------------------------*/
214 print_reduction (FILE *out
, size_t width
,
215 const char *lookahead_token
,
216 rule
*r
, bool enabled
)
219 fprintf (out
, " %s", lookahead_token
);
220 for (j
= width
- strlen (lookahead_token
); j
> 0; --j
)
225 fprintf (out
, _("reduce using rule %d (%s)"), r
->number
, r
->lhs
->tag
);
227 fprintf (out
, _("accept"));
234 /*-------------------------------------------.
235 | Report on OUT the reduction actions of S. |
236 `-------------------------------------------*/
239 print_reductions (FILE *out
, state
*s
)
241 transitions
*trans
= s
->transitions
;
242 reductions
*reds
= s
->reductions
;
243 rule
*default_rule
= NULL
;
250 if (yydefact
[s
->number
] != 0)
251 default_rule
= &rules
[yydefact
[s
->number
] - 1];
253 bitset_zero (no_reduce_set
);
254 FOR_EACH_SHIFT (trans
, i
)
255 bitset_set (no_reduce_set
, TRANSITION_SYMBOL (trans
, i
));
256 for (i
= 0; i
< s
->errs
->num
; ++i
)
257 if (s
->errs
->symbols
[i
])
258 bitset_set (no_reduce_set
, s
->errs
->symbols
[i
]->number
);
260 /* Compute the width of the lookahead token column. */
262 width
= strlen (_("$default"));
264 if (reds
->lookahead_tokens
)
265 for (i
= 0; i
< ntokens
; i
++)
267 bool count
= bitset_test (no_reduce_set
, i
);
269 for (j
= 0; j
< reds
->num
; ++j
)
270 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
274 if (reds
->rules
[j
] != default_rule
)
275 max_length (&width
, symbols
[i
]->tag
);
280 max_length (&width
, symbols
[i
]->tag
);
285 /* Nothing to report. */
292 /* Report lookahead tokens (or $default) and reductions. */
293 if (reds
->lookahead_tokens
)
294 for (i
= 0; i
< ntokens
; i
++)
296 bool defaulted
= false;
297 bool count
= bitset_test (no_reduce_set
, i
);
299 for (j
= 0; j
< reds
->num
; ++j
)
300 if (bitset_test (reds
->lookahead_tokens
[j
], i
))
304 if (reds
->rules
[j
] != default_rule
)
305 print_reduction (out
, width
,
307 reds
->rules
[j
], true);
315 print_reduction (out
, width
,
319 print_reduction (out
, width
,
321 reds
->rules
[j
], false);
327 print_reduction (out
, width
,
328 _("$default"), default_rule
, true);
332 /*--------------------------------------------------------------.
333 | Report on OUT all the actions (shifts, gotos, reductions, and |
334 | explicit erros from %nonassoc) of S. |
335 `--------------------------------------------------------------*/
338 print_actions (FILE *out
, state
*s
)
341 print_transitions (s
, out
, true);
343 print_reductions (out
, s
);
345 print_transitions (s
, out
, false);
349 /*----------------------------------.
350 | Report all the data on S on OUT. |
351 `----------------------------------*/
354 print_state (FILE *out
, state
*s
)
357 fprintf (out
, _("state %d"), s
->number
);
360 print_actions (out
, s
);
361 if ((report_flag
& report_solved_conflicts
) && s
->solved_conflicts
)
364 fputs (s
->solved_conflicts
, out
);
368 /*-----------------------------------------.
369 | Print information on the whole grammar. |
370 `-----------------------------------------*/
372 #define END_TEST(End) \
374 if (column + strlen(buffer) > (End)) \
376 fprintf (out, "%s\n ", buffer); \
384 print_grammar (FILE *out
)
390 grammar_rules_print (out
);
392 /* TERMINAL (type #) : rule #s terminal is on RHS */
393 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
394 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
395 if (token_translations
[i
] != undeftoken
->number
)
397 const char *tag
= symbols
[token_translations
[i
]]->tag
;
402 column
= strlen (tag
);
405 sprintf (buffer
, " (%d)", i
);
407 for (r
= 0; r
< nrules
; r
++)
408 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
409 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
412 sprintf (buffer
+ strlen (buffer
), " %d", r
);
415 fprintf (out
, "%s\n", buffer
);
420 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
421 for (i
= ntokens
; i
< nsyms
; i
++)
423 int left_count
= 0, right_count
= 0;
425 const char *tag
= symbols
[i
]->tag
;
427 for (r
= 0; r
< nrules
; r
++)
430 if (rules
[r
].lhs
->number
== i
)
432 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
433 if (item_number_as_symbol_number (*rhsp
) == i
)
442 column
= strlen (tag
);
443 sprintf (buffer
, " (%d)", i
);
449 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
451 for (r
= 0; r
< nrules
; r
++)
454 if (rules
[r
].lhs
->number
== i
)
455 sprintf (buffer
+ strlen (buffer
), " %d", r
);
462 sprintf (buffer
+ strlen (buffer
), ",");
464 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
465 for (r
= 0; r
< nrules
; r
++)
468 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
469 if (item_number_as_symbol_number (*rhsp
) == i
)
472 sprintf (buffer
+ strlen (buffer
), " %d", r
);
477 fprintf (out
, "%s\n", buffer
);
486 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
487 that conflicts with Posix. */
488 FILE *out
= xfopen (spec_verbose_file
, "w");
491 grammar_rules_partial_print (out
,
492 _("Rules never reduced"), rule_never_reduced_p
);
493 conflicts_output (out
);
497 /* If the whole state item sets, not only the kernels, are wanted,
498 `closure' will be run, which needs memory allocation/deallocation. */
499 if (report_flag
& report_itemsets
)
500 new_closure (nritems
);
501 /* Storage for print_reductions. */
502 no_reduce_set
= bitset_create (ntokens
, BITSET_FIXED
);
503 for (i
= 0; i
< nstates
; i
++)
504 print_state (out
, states
[i
]);
505 bitset_free (no_reduce_set
);
506 if (report_flag
& report_itemsets
)