]>
git.saurik.com Git - bison.git/blob - src/print.c
280ac8d8417acbe5c4ffe8def58090df8968d31c
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
)
99 /* Look for lookaheads corresponding to this rule. */
100 for (j
= 0; j
< state
->nlookaheads
; ++j
)
101 for (k
= 0; k
< ntokens
; ++k
)
102 if (bitset_test (LA
[state
->lookaheadsp
+ j
], k
)
103 && LArule
[state
->lookaheadsp
+ j
]->number
== rule
)
108 for (j
= 0; j
< state
->nlookaheads
; ++j
)
109 for (k
= 0; k
< ntokens
; ++k
)
110 if (bitset_test (LA
[state
->lookaheadsp
+ j
], k
)
111 && LArule
[state
->lookaheadsp
+ j
]->number
== rule
)
112 fprintf (out
, "%s%s",
113 symbol_tag_get (symbols
[k
]),
114 --nlookaheads
? ", " : "");
119 fprintf (out
, _(" (rule %d)"), rule
- 1);
129 print_shifts (FILE *out
, state_t
*state
)
132 shifts
*shiftp
= state
->shifts
;
134 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
135 if (!SHIFT_IS_DISABLED (shiftp
, i
))
137 int state1
= shiftp
->shifts
[i
];
138 symbol_number_t symbol
= states
[state1
]->accessing_symbol
;
140 _(" %-4s\tshift, and go to state %d\n"),
141 symbol_tag_get (symbols
[symbol
]), state1
);
150 print_errs (FILE *out
, state_t
*state
)
152 errs
*errp
= state
->errs
;
155 for (i
= 0; i
< errp
->nerrs
; ++i
)
157 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
158 symbol_tag_get (symbols
[errp
->errs
[i
]]));
166 print_gotos (FILE *out
, state_t
*state
)
169 shifts
*shiftp
= state
->shifts
;
171 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
172 /* Skip token shifts. */;
174 if (i
< shiftp
->nshifts
)
176 for (; i
< shiftp
->nshifts
; i
++)
177 if (!SHIFT_IS_DISABLED (shiftp
, i
))
179 int state1
= shiftp
->shifts
[i
];
180 symbol_number_t symbol
= states
[state1
]->accessing_symbol
;
181 fprintf (out
, _(" %-4s\tgo to state %d\n"),
182 symbol_tag_get (symbols
[symbol
]), state1
);
190 print_reductions (FILE *out
, state_t
*state
)
193 shifts
*shiftp
= state
->shifts
;
194 reductions
*redp
= state
->reductions
;
195 errs
*errp
= state
->errs
;
198 if (redp
->nreds
== 0)
201 if (state
->consistent
)
203 int rule
= redp
->rules
[0];
204 symbol_number_t symbol
= rules
[rule
].lhs
->number
;
205 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
206 rule
- 1, symbol_tag_get (symbols
[symbol
]));
210 bitset_zero (shiftset
);
212 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
213 if (!SHIFT_IS_DISABLED (shiftp
, i
))
215 /* if this state has a shift for the error token, don't use a
217 if (SHIFT_IS_ERROR (shiftp
, i
))
219 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
222 for (i
= 0; i
< errp
->nerrs
; i
++)
224 bitset_set (shiftset
, errp
->errs
[i
]);
226 if (state
->nlookaheads
== 1 && !nodefault
)
228 rule_t
*default_rule
= LArule
[state
->lookaheadsp
];
230 bitset_and (lookaheadset
, LA
[state
->lookaheadsp
], shiftset
);
232 for (i
= 0; i
< ntokens
; i
++)
233 if (bitset_test (lookaheadset
, i
))
234 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
235 symbol_tag_get (symbols
[i
]),
236 default_rule
->number
- 1,
237 symbol_tag_get_n (default_rule
->lhs
, 1));
239 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
240 default_rule
->number
- 1,
241 symbol_tag_get (default_rule
->lhs
));
243 else if (state
->nlookaheads
>= 1)
247 rule_t
*default_rule
= NULL
;
250 for (i
= 0; i
< state
->nlookaheads
; ++i
)
255 bitset_andn (lookaheadset
, LA
[state
->lookaheadsp
+ i
], shiftset
);
257 for (j
= 0; j
< ntokens
; j
++)
258 if (bitset_test (lookaheadset
, j
))
264 default_LA
= state
->lookaheadsp
+ i
;
265 default_rule
= LArule
[state
->lookaheadsp
+ i
];
268 bitset_or (shiftset
, shiftset
, lookaheadset
);
271 bitset_zero (shiftset
);
273 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
274 if (!SHIFT_IS_DISABLED (shiftp
, i
))
275 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
277 for (i
= 0; i
< ntokens
; i
++)
281 int count
= bitset_test (shiftset
, i
);
283 for (j
= 0; j
< state
->nlookaheads
; ++j
)
284 if (bitset_test (LA
[state
->lookaheadsp
+ j
], i
))
288 if (state
->lookaheadsp
+ j
!= default_LA
)
290 _(" %-4s\treduce using rule %d (%s)\n"),
291 symbol_tag_get (symbols
[i
]),
292 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
293 symbol_tag_get_n (LArule
[state
->lookaheadsp
+ j
]->lhs
, 1));
303 _(" %-4s\treduce using rule %d (%s)\n"),
304 symbol_tag_get (symbols
[i
]),
305 LArule
[default_LA
]->number
- 1,
306 symbol_tag_get_n (LArule
[default_LA
]->lhs
, 1));
309 _(" %-4s\t[reduce using rule %d (%s)]\n"),
310 symbol_tag_get (symbols
[i
]),
311 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
312 symbol_tag_get_n (LArule
[state
->lookaheadsp
+ j
]->lhs
, 1));
318 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
319 default_rule
->number
- 1,
320 symbol_tag_get (default_rule
->lhs
));
326 print_actions (FILE *out
, state_t
*state
)
328 reductions
*redp
= state
->reductions
;
329 shifts
*shiftp
= state
->shifts
;
331 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
333 if (final_state
== state
->number
)
334 fprintf (out
, _(" $default\taccept\n"));
336 fprintf (out
, _(" NO ACTIONS\n"));
340 print_shifts (out
, state
);
341 print_errs (out
, state
);
342 print_reductions (out
, state
);
343 print_gotos (out
, state
);
347 print_state (FILE *out
, state_t
*state
)
349 fprintf (out
, _("state %d"), state
->number
);
351 print_core (out
, state
);
352 print_actions (out
, state
);
353 if ((report_flag
& report_solved_conflicts
)
354 && state
->solved_conflicts
)
355 fputs (state
->solved_conflicts
, out
);
359 /*-----------------------------------------.
360 | Print information on the whole grammar. |
361 `-----------------------------------------*/
363 #define END_TEST(End) \
365 if (column + strlen(buffer) > (End)) \
367 fprintf (out, "%s\n ", buffer); \
375 print_grammar (FILE *out
)
382 grammar_rules_print (out
);
384 /* TERMINAL (type #) : rule #s terminal is on RHS */
385 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
386 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
387 if (token_translations
[i
] != undeftoken
->number
)
389 const char *tag
= symbol_tag_get (symbols
[token_translations
[i
]]);
392 column
= strlen (tag
);
395 sprintf (buffer
, " (%d)", i
);
397 for (r
= 1; r
< nrules
+ 1; r
++)
398 for (rule
= rules
[r
].rhs
; *rule
>= 0; rule
++)
399 if (item_number_as_symbol_number (*rule
) == token_translations
[i
])
402 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
405 fprintf (out
, "%s\n", buffer
);
410 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
411 for (i
= ntokens
; i
< nsyms
; i
++)
413 int left_count
= 0, right_count
= 0;
415 const char *tag
= symbol_tag_get (symbols
[i
]);
417 for (r
= 1; r
< nrules
+ 1; r
++)
419 if (rules
[r
].lhs
->number
== i
)
421 for (rule
= rules
[r
].rhs
; *rule
>= 0; rule
++)
422 if (item_number_as_symbol_number (*rule
) == i
)
431 column
= strlen (tag
);
432 sprintf (buffer
, " (%d)", i
);
438 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
440 for (r
= 1; r
< nrules
+ 1; r
++)
443 if (rules
[r
].lhs
->number
== i
)
444 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
451 sprintf (buffer
+ strlen (buffer
), ",");
453 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
454 for (r
= 1; r
< nrules
+ 1; r
++)
456 for (rule
= rules
[r
].rhs
; *rule
>= 0; rule
++)
457 if (item_number_as_symbol_number (*rule
) == i
)
460 sprintf (buffer
+ strlen (buffer
), " %d", r
- 1);
465 fprintf (out
, "%s\n", buffer
);
475 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
476 that conflicts with Posix. */
477 FILE *out
= xfopen (spec_verbose_file
, "w");
480 conflicts_output (out
);
484 /* If the whole state item sets, not only the kernels, are wanted,
485 `closure' will be run, which needs memory allocation/deallocation. */
486 if (report_flag
& report_itemsets
)
487 new_closure (nritems
);
488 /* Storage for print_reductions. */
489 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
490 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
491 for (i
= 0; i
< nstates
; i
++)
492 print_state (out
, states
[i
]);
493 bitset_free (shiftset
);
494 bitset_free (lookaheadset
);
495 if (report_flag
& report_itemsets
)