]>
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
]);
50 static inline const char *
51 escape (const char *s
)
53 return quotearg_n_style (1, escape_quoting_style
, s
);
56 /* Be cautious not to use twice the same slot in a single expression. */
57 static inline const char *
58 escape2 (const char *s
)
60 return quotearg_n_style (2, escape_quoting_style
, s
);
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
;
75 /* New experimental feature: if TRACE_FLAGS output all the items of
76 a state, not only its kernel. */
79 closure (sitems
, snritems
);
86 for (i
= 0; i
< snritems
; i
++)
92 sp1
= sp
= ritem
+ sitems
[i
];
98 fprintf (out
, " %s -> ", escape (rules
[rule
].lhs
->tag
));
100 for (sp
= rules
[rule
].rhs
; sp
< sp1
; sp
++)
101 fprintf (out
, "%s ", escape (symbols
[*sp
]->tag
));
105 for (/* Nothing */; *sp
>= 0; ++sp
)
106 fprintf (out
, " %s", escape (symbols
[*sp
]->tag
));
108 /* Experimental feature: display the lookaheads. */
109 if (trace_flag
&& state
->nlookaheads
)
113 /* Look for lookaheads corresponding to this rule. */
114 for (j
= 0; j
< state
->nlookaheads
; ++j
)
115 for (k
= 0; k
< ntokens
; ++k
)
116 if (bitset_test (LA
[state
->lookaheadsp
+ j
], k
)
117 && LArule
[state
->lookaheadsp
+ j
]->number
== rule
)
122 for (j
= 0; j
< state
->nlookaheads
; ++j
)
123 for (k
= 0; k
< ntokens
; ++k
)
124 if (bitset_test (LA
[state
->lookaheadsp
+ j
], k
)
125 && LArule
[state
->lookaheadsp
+ j
]->number
== rule
)
126 fprintf (out
, "%s%s",
127 quotearg_style (escape_quoting_style
,
129 --nlookaheads
? ", " : "");
134 fprintf (out
, _(" (rule %d)"), rule
- 1);
144 print_shifts (FILE *out
, state_t
*state
)
147 shifts
*shiftp
= state
->shifts
;
149 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
150 if (!SHIFT_IS_DISABLED (shiftp
, i
))
152 int state1
= shiftp
->shifts
[i
];
153 token_number_t symbol
= states
[state1
]->accessing_symbol
;
155 _(" %-4s\tshift, and go to state %d\n"),
156 escape (symbols
[symbol
]->tag
), state1
);
165 print_errs (FILE *out
, state_t
*state
)
167 errs
*errp
= state
->errs
;
170 for (i
= 0; i
< errp
->nerrs
; ++i
)
172 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
173 escape (symbols
[errp
->errs
[i
]]->tag
));
181 print_gotos (FILE *out
, state_t
*state
)
184 shifts
*shiftp
= state
->shifts
;
186 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
187 /* Skip token shifts. */;
189 if (i
< shiftp
->nshifts
)
191 for (; i
< shiftp
->nshifts
; i
++)
192 if (!SHIFT_IS_DISABLED (shiftp
, i
))
194 int state1
= shiftp
->shifts
[i
];
195 token_number_t symbol
= states
[state1
]->accessing_symbol
;
196 fprintf (out
, _(" %-4s\tgo to state %d\n"),
197 escape (symbols
[symbol
]->tag
), state1
);
205 print_reductions (FILE *out
, state_t
*state
)
208 shifts
*shiftp
= state
->shifts
;
209 reductions
*redp
= state
->reductions
;
210 errs
*errp
= state
->errs
;
213 if (redp
->nreds
== 0)
216 if (state
->consistent
)
218 int rule
= redp
->rules
[0];
219 token_number_t symbol
= rules
[rule
].lhs
->number
;
220 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
221 rule
- 1, escape (symbols
[symbol
]->tag
));
225 bitset_zero (shiftset
);
227 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
228 if (!SHIFT_IS_DISABLED (shiftp
, i
))
230 /* if this state has a shift for the error token, don't use a
232 if (SHIFT_IS_ERROR (shiftp
, i
))
234 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
237 for (i
= 0; i
< errp
->nerrs
; i
++)
239 bitset_set (shiftset
, errp
->errs
[i
]);
241 if (state
->nlookaheads
== 1 && !nodefault
)
243 rule_t
*default_rule
= LArule
[state
->lookaheadsp
];
245 bitset_and (lookaheadset
, LA
[state
->lookaheadsp
], shiftset
);
247 for (i
= 0; i
< ntokens
; i
++)
248 if (bitset_test (lookaheadset
, i
))
249 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
250 escape (symbols
[i
]->tag
),
251 default_rule
->number
- 1,
252 escape2 (default_rule
->lhs
->tag
));
254 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
255 default_rule
->number
- 1,
256 escape (default_rule
->lhs
->tag
));
258 else if (state
->nlookaheads
>= 1)
262 rule_t
*default_rule
= NULL
;
265 for (i
= 0; i
< state
->nlookaheads
; ++i
)
270 bitset_andn (lookaheadset
, LA
[state
->lookaheadsp
+ i
], shiftset
);
272 for (j
= 0; j
< ntokens
; j
++)
273 if (bitset_test (lookaheadset
, j
))
279 default_LA
= state
->lookaheadsp
+ i
;
280 default_rule
= LArule
[state
->lookaheadsp
+ i
];
283 bitset_or (shiftset
, shiftset
, lookaheadset
);
286 bitset_zero (shiftset
);
288 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
289 if (!SHIFT_IS_DISABLED (shiftp
, i
))
290 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
292 for (i
= 0; i
< ntokens
; i
++)
296 int count
= bitset_test (shiftset
, i
);
298 for (j
= 0; j
< state
->nlookaheads
; ++j
)
299 if (bitset_test (LA
[state
->lookaheadsp
+ j
], i
))
303 if (state
->lookaheadsp
+ j
!= default_LA
)
305 _(" %-4s\treduce using rule %d (%s)\n"),
306 escape (symbols
[i
]->tag
),
307 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
308 escape2 (LArule
[state
->lookaheadsp
+ j
]->lhs
->tag
));
318 _(" %-4s\treduce using rule %d (%s)\n"),
319 escape (symbols
[i
]->tag
),
320 LArule
[default_LA
]->number
- 1,
321 escape2 (LArule
[default_LA
]->lhs
->tag
));
324 _(" %-4s\t[reduce using rule %d (%s)]\n"),
325 escape (symbols
[i
]->tag
),
326 LArule
[state
->lookaheadsp
+ j
]->number
- 1,
327 escape2 (LArule
[state
->lookaheadsp
+ j
]->lhs
->tag
));
333 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
334 default_rule
->number
- 1,
335 escape (default_rule
->lhs
->tag
));
341 print_actions (FILE *out
, state_t
*state
)
343 reductions
*redp
= state
->reductions
;
344 shifts
*shiftp
= state
->shifts
;
346 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
348 if (final_state
== state
->number
)
349 fprintf (out
, _(" $default\taccept\n"));
351 fprintf (out
, _(" NO ACTIONS\n"));
355 print_shifts (out
, state
);
356 print_errs (out
, state
);
357 print_reductions (out
, state
);
358 print_gotos (out
, state
);
362 print_state (FILE *out
, state_t
*state
)
364 fprintf (out
, _("state %d"), state
->number
);
366 print_core (out
, state
);
367 print_actions (out
, state
);
371 /*-----------------------------------------.
372 | Print information on the whole grammar. |
373 `-----------------------------------------*/
375 #define END_TEST(End) \
377 if (column + strlen(buffer) > (End)) \
379 fprintf (out, "%s\n ", buffer); \
387 print_grammar (FILE *out
)
395 /* rule # : LHS -> RHS */
396 fprintf (out
, "%s\n\n", _("Grammar"));
397 fprintf (out
, " %s\n", _("Number, Line, Rule"));
398 for (j
= 1; j
< nrules
+ 1; j
++)
400 fprintf (out
, _(" %3d %3d %s ->"),
401 j
- 1, rules
[j
].line
, escape (rules
[j
].lhs
->tag
));
405 fprintf (out
, " %s", escape (symbols
[*rule
++]->tag
));
407 fprintf (out
, " /* %s */", _("empty"));
413 /* TERMINAL (type #) : rule #s terminal is on RHS */
414 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
415 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
416 if (token_translations
[i
] != undeftoken
->number
)
419 column
= strlen (escape (symbols
[token_translations
[i
]]->tag
));
420 fputs (escape (symbols
[token_translations
[i
]]->tag
), out
);
422 sprintf (buffer
, " (%d)", i
);
424 for (j
= 1; j
< nrules
+ 1; j
++)
425 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
426 if (item_number_as_token_number (*rule
) == token_translations
[i
])
429 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
432 fprintf (out
, "%s\n", buffer
);
437 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
438 for (i
= ntokens
; i
< nsyms
; i
++)
440 int left_count
= 0, right_count
= 0;
442 for (j
= 1; j
< nrules
+ 1; j
++)
444 if (rules
[j
].lhs
->number
== i
)
446 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
447 if (item_number_as_token_number (*rule
) == i
)
455 fputs (escape (symbols
[i
]->tag
), out
);
456 column
= strlen (escape (symbols
[i
]->tag
));
457 sprintf (buffer
, " (%d)", i
);
463 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
465 for (j
= 1; j
< nrules
+ 1; j
++)
468 if (rules
[j
].lhs
->number
== i
)
469 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
476 sprintf (buffer
+ strlen (buffer
), ",");
478 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
479 for (j
= 1; j
< nrules
+ 1; j
++)
481 for (rule
= rules
[j
].rhs
; *rule
>= 0; rule
++)
482 if (item_number_as_token_number (*rule
) == i
)
485 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
490 fprintf (out
, "%s\n", buffer
);
500 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
501 that conflicts with Posix. */
502 FILE *out
= xfopen (spec_verbose_file
, "w");
504 size_t size
= obstack_object_size (&output_obstack
);
505 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
506 obstack_free (&output_obstack
, NULL
);
512 conflicts_output (out
);
516 /* New experimental feature: output all the items of a state, not
517 only its kernel. Requires to run closure, which need memory
518 allocation/deallocation. */
520 new_closure (nritems
);
521 /* Storage for print_reductions. */
522 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
523 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
524 for (i
= 0; i
< nstates
; i
++)
525 print_state (out
, states
[i
]);
526 bitset_free (shiftset
);
527 bitset_free (lookaheadset
);