]>
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
]);
51 /*--------------------------------.
52 | Report information on a state. |
53 `--------------------------------*/
56 print_core (FILE *out
, state_t
*state
)
59 item_number_t
*sitems
= state
->items
;
60 int snritems
= state
->nitems
;
62 /* Output all the items of a state, not only its kernel. */
63 if (report_flag
& report_itemsets
)
65 closure (sitems
, snritems
);
72 for (i
= 0; i
< snritems
; i
++)
78 sp1
= sp
= ritem
+ sitems
[i
];
84 fprintf (out
, " %s -> ", symbol_tag_get (rules
[rule
].lhs
));
86 for (sp
= rules
[rule
].rhs
; sp
< sp1
; sp
++)
87 fprintf (out
, "%s ", symbol_tag_get (symbols
[*sp
]));
91 for (/* Nothing */; *sp
>= 0; ++sp
)
92 fprintf (out
, " %s", symbol_tag_get (symbols
[*sp
]));
94 /* Display the lookaheads? */
95 if (report_flag
& report_lookaheads
)
96 state_rule_lookaheads_print (state
, &rules
[rule
], out
);
98 fprintf (out
, _(" (rule %d)"), rule
- 1);
108 print_shifts (FILE *out
, state_t
*state
)
111 shifts_t
*shiftp
= state
->shifts
;
113 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
114 if (!SHIFT_IS_DISABLED (shiftp
, i
))
116 state_number_t state1
= shiftp
->shifts
[i
];
117 symbol_number_t symbol
= states
[state1
]->accessing_symbol
;
119 _(" %-4s\tshift, and go to state %d\n"),
120 symbol_tag_get (symbols
[symbol
]), state1
);
129 print_errs (FILE *out
, state_t
*state
)
131 errs_t
*errp
= state
->errs
;
134 for (i
= 0; i
< errp
->nerrs
; ++i
)
136 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
137 symbol_tag_get (symbols
[errp
->errs
[i
]]));
145 print_gotos (FILE *out
, state_t
*state
)
148 shifts_t
*shiftp
= state
->shifts
;
150 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
151 /* Skip token shifts. */;
153 if (i
< shiftp
->nshifts
)
155 for (; i
< shiftp
->nshifts
; i
++)
156 if (!SHIFT_IS_DISABLED (shiftp
, i
))
158 state_number_t state1
= shiftp
->shifts
[i
];
159 symbol_number_t symbol
= states
[state1
]->accessing_symbol
;
160 fprintf (out
, _(" %-4s\tgo to state %d\n"),
161 symbol_tag_get (symbols
[symbol
]), state1
);
169 /*----------------------------------------------------------.
170 | Return the default rule of this STATE if it has one, NULL |
172 `----------------------------------------------------------*/
175 state_default_rule_compute (state_t
*state
)
177 reductions_t
*redp
= state
->reductions
;
178 rule_t
*default_rule
= NULL
;
182 /* No need for a lookahead. */
183 if (state
->consistent
)
184 return &rules
[redp
->rules
[0]];
186 /* 1. Each reduction is possibly masked by the lookaheads on which
187 we shift (S/R conflicts)... */
188 bitset_zero (shiftset
);
190 shifts_t
*shiftp
= state
->shifts
;
191 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
192 if (!SHIFT_IS_DISABLED (shiftp
, i
))
194 /* If this state has a shift for the error token, don't use a
196 if (SHIFT_IS_ERROR (shiftp
, i
))
198 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
202 /* 2. Each reduction is possibly masked by the lookaheads on which
203 we raise an error (due to %nonassoc). */
205 errs_t
*errp
= state
->errs
;
206 for (i
= 0; i
< errp
->nerrs
; i
++)
208 bitset_set (shiftset
, errp
->errs
[i
]);
211 for (i
= 0; i
< state
->nlookaheads
; ++i
)
215 /* How many non-masked lookaheads are there for this reduction?
217 bitset_andn (lookaheadset
, state
->lookaheads
[i
], shiftset
);
218 count
= bitset_count (lookaheadset
);
223 default_rule
= state
->lookaheads_rule
[i
];
226 /* 3. And finally, each reduction is possibly masked by previous
227 reductions (in R/R conflicts, we keep the first reductions).
229 bitset_or (shiftset
, shiftset
, state
->lookaheads
[i
]);
236 /*----------------------------------------------------.
237 | Report on OUT the reduction actions of this STATE. |
238 `----------------------------------------------------*/
241 print_reductions (FILE *out
, state_t
*state
)
244 shifts_t
*shiftp
= state
->shifts
;
245 reductions_t
*redp
= state
->reductions
;
246 rule_t
*default_rule
= NULL
;
248 if (redp
->nreds
== 0)
251 default_rule
= state_default_rule_compute (state
);
253 bitset_zero (shiftset
);
254 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
255 if (!SHIFT_IS_DISABLED (shiftp
, i
))
256 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
258 for (i
= 0; i
< ntokens
; i
++)
262 int count
= bitset_test (shiftset
, i
);
264 for (j
= 0; j
< state
->nlookaheads
; ++j
)
265 if (bitset_test (state
->lookaheads
[j
], i
))
269 if (state
->lookaheads_rule
[j
] != default_rule
)
271 _(" %-4s\treduce using rule %d (%s)\n"),
272 symbol_tag_get (symbols
[i
]),
273 state
->lookaheads_rule
[j
]->number
- 1,
274 symbol_tag_get_n (state
->lookaheads_rule
[j
]->lhs
, 1));
283 _(" %-4s\treduce using rule %d (%s)\n"),
284 symbol_tag_get (symbols
[i
]),
285 default_rule
->number
- 1,
286 symbol_tag_get_n (default_rule
->lhs
, 1));
289 _(" %-4s\t[reduce using rule %d (%s)]\n"),
290 symbol_tag_get (symbols
[i
]),
291 state
->lookaheads_rule
[j
]->number
- 1,
292 symbol_tag_get_n (state
->lookaheads_rule
[j
]->lhs
, 1));
298 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
299 default_rule
->number
- 1,
300 symbol_tag_get (default_rule
->lhs
));
305 /*--------------------------------------------------------------.
306 | Report on OUT all the actions (shifts, gotos, reductions, and |
307 | explicit erros from %nonassoc) of STATE. |
308 `--------------------------------------------------------------*/
311 print_actions (FILE *out
, state_t
*state
)
313 reductions_t
*redp
= state
->reductions
;
314 shifts_t
*shiftp
= state
->shifts
;
316 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
318 if (state
->number
== final_state
->number
)
319 fprintf (out
, _(" $default\taccept\n"));
321 fprintf (out
, _(" NO ACTIONS\n"));
325 print_shifts (out
, state
);
326 print_errs (out
, state
);
327 print_reductions (out
, state
);
328 print_gotos (out
, state
);
333 print_state (FILE *out
, state_t
*state
)
335 fprintf (out
, _("state %d"), state
->number
);
337 print_core (out
, state
);
338 print_actions (out
, state
);
339 if ((report_flag
& report_solved_conflicts
)
340 && state
->solved_conflicts
)
341 fputs (state
->solved_conflicts
, out
);
345 /*-----------------------------------------.
346 | Print information on the whole grammar. |
347 `-----------------------------------------*/
349 #define END_TEST(End) \
351 if (column + strlen(buffer) > (End)) \
353 fprintf (out, "%s\n ", buffer); \
361 print_grammar (FILE *out
)
367 grammar_rules_print (out
);
369 /* TERMINAL (type #) : rule #s terminal is on RHS */
370 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
371 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
372 if (token_translations
[i
] != undeftoken
->number
)
374 const char *tag
= symbol_tag_get (symbols
[token_translations
[i
]]);
379 column
= strlen (tag
);
382 sprintf (buffer
, " (%d)", i
);
384 for (r
= 1; r
< nrules
+ 1; r
++)
385 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
386 if (item_number_as_symbol_number (*rhsp
) == token_translations
[i
])
389 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
392 fprintf (out
, "%s\n", buffer
);
397 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
398 for (i
= ntokens
; i
< nsyms
; i
++)
400 int left_count
= 0, right_count
= 0;
402 const char *tag
= symbol_tag_get (symbols
[i
]);
404 for (r
= 1; r
< nrules
+ 1; r
++)
407 if (rules
[r
].lhs
->number
== i
)
409 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
410 if (item_number_as_symbol_number (*rhsp
) == i
)
419 column
= strlen (tag
);
420 sprintf (buffer
, " (%d)", i
);
426 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
428 for (r
= 1; r
< nrules
+ 1; r
++)
431 if (rules
[r
].lhs
->number
== i
)
432 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
439 sprintf (buffer
+ strlen (buffer
), ",");
441 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
442 for (r
= 1; r
< nrules
+ 1; r
++)
445 for (rhsp
= rules
[r
].rhs
; *rhsp
>= 0; rhsp
++)
446 if (item_number_as_symbol_number (*rhsp
) == i
)
449 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
454 fprintf (out
, "%s\n", buffer
);
464 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
465 that conflicts with Posix. */
466 FILE *out
= xfopen (spec_verbose_file
, "w");
469 conflicts_output (out
);
473 /* If the whole state item sets, not only the kernels, are wanted,
474 `closure' will be run, which needs memory allocation/deallocation. */
475 if (report_flag
& report_itemsets
)
476 new_closure (nritems
);
477 /* Storage for print_reductions. */
478 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
479 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
480 for (i
= 0; i
< nstates
; i
++)
481 print_state (out
, states
[i
]);
482 bitset_free (shiftset
);
483 bitset_free (lookaheadset
);
484 if (report_flag
& report_itemsets
)