]>
git.saurik.com Git - bison.git/blob - src/print.c
d4ba0e15546aa5d0bfff7fdc95cd0545a58d986b
1 /* Print information on generated parser, for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
29 #include "conflicts.h"
38 static bitset shiftset
;
39 static bitset lookaheadset
;
43 print_token (int extnum
, int token
)
45 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
49 static inline const char *
50 escape (const char *s
)
52 return quotearg_n_style (1, escape_quoting_style
, s
);
55 /* Be cautious not to use twice the same slot in a single expression. */
56 static inline const char *
57 escape2 (const char *s
)
59 return quotearg_n_style (2, escape_quoting_style
, s
);
63 /*--------------------------------.
64 | Report information on a state. |
65 `--------------------------------*/
68 print_core (FILE *out
, state_t
*state
)
71 short *sitems
= state
->items
;
72 int snitems
= state
->nitems
;
74 /* New experimental feature: if TRACE_FLAGS output all the items of
75 a state, not only its kernel. */
78 closure (sitems
, snitems
);
85 for (i
= 0; i
< snitems
; i
++)
91 sp1
= sp
= ritem
+ sitems
[i
];
97 fprintf (out
, " %s -> ", escape (symbols
[rules
[rule
].lhs
]->tag
));
99 for (sp
= ritem
+ rules
[rule
].rhs
; sp
< sp1
; sp
++)
100 fprintf (out
, "%s ", escape (symbols
[*sp
]->tag
));
104 for (/* Nothing */; *sp
>= 0; ++sp
)
105 fprintf (out
, " %s", escape (symbols
[*sp
]->tag
));
107 fprintf (out
, _(" (rule %d)"), rule
- 1);
117 print_shifts (FILE *out
, state_t
*state
)
120 shifts
*shiftp
= state
->shifts
;
122 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
123 if (!SHIFT_IS_DISABLED (shiftp
, i
))
125 int state1
= shiftp
->shifts
[i
];
126 int symbol
= states
[state1
]->accessing_symbol
;
128 _(" %-4s\tshift, and go to state %d\n"),
129 escape (symbols
[symbol
]->tag
), state1
);
138 print_errs (FILE *out
, state_t
*state
)
140 errs
*errp
= state
->errs
;
143 for (i
= 0; i
< errp
->nerrs
; ++i
)
145 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
146 escape (symbols
[errp
->errs
[i
]]->tag
));
154 print_gotos (FILE *out
, state_t
*state
)
157 shifts
*shiftp
= state
->shifts
;
159 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
160 /* Skip token shifts. */;
162 if (i
< shiftp
->nshifts
)
164 for (; i
< shiftp
->nshifts
; i
++)
165 if (!SHIFT_IS_DISABLED (shiftp
, i
))
167 int state1
= shiftp
->shifts
[i
];
168 int symbol
= states
[state1
]->accessing_symbol
;
169 fprintf (out
, _(" %-4s\tgo to state %d\n"),
170 escape (symbols
[symbol
]->tag
), state1
);
178 print_reductions (FILE *out
, state_t
*state
)
181 shifts
*shiftp
= state
->shifts
;
182 reductions
*redp
= state
->reductions
;
183 errs
*errp
= state
->errs
;
186 if (redp
->nreds
== 0)
189 if (state
->consistent
)
191 int rule
= redp
->rules
[0];
192 int symbol
= rules
[rule
].lhs
;
193 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
194 rule
- 1, escape (symbols
[symbol
]->tag
));
198 bitset_zero (shiftset
);
200 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
201 if (!SHIFT_IS_DISABLED (shiftp
, i
))
203 /* if this state has a shift for the error token, don't use a
205 if (SHIFT_IS_ERROR (shiftp
, i
))
207 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
210 for (i
= 0; i
< errp
->nerrs
; i
++)
212 bitset_set (shiftset
, errp
->errs
[i
]);
214 if (state
->nlookaheads
== 1 && !nodefault
)
216 int default_rule
= LAruleno
[state
->lookaheadsp
];
218 for (i
= 0; i
< ntokens
; ++i
)
219 if (BITISSET (LA (state
->lookaheadsp
), i
)
220 && bitset_test (shiftset
, i
))
221 bitset_set (lookaheadset
, i
);
223 bitset_reset (lookaheadset
, i
);
225 for (i
= 0; i
< ntokens
; i
++)
226 if (bitset_test (lookaheadset
, i
))
227 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
228 escape (symbols
[i
]->tag
), default_rule
- 1,
229 escape2 (symbols
[rules
[default_rule
].lhs
]->tag
));
231 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
232 default_rule
- 1, escape (symbols
[rules
[default_rule
].lhs
]->tag
));
234 else if (state
->nlookaheads
>= 1)
238 int default_rule
= 0;
241 for (i
= 0; i
< state
->nlookaheads
; ++i
)
246 for (k
= 0; k
< ntokens
; ++k
)
247 if (BITISSET (LA (state
->lookaheadsp
+ i
), k
)
248 && ! bitset_test (shiftset
, k
))
249 bitset_set (lookaheadset
, k
);
251 bitset_reset (lookaheadset
, k
);
253 for (j
= 0; j
< ntokens
; j
++)
254 if (bitset_test (lookaheadset
, j
))
260 default_LA
= state
->lookaheadsp
+ i
;
261 default_rule
= LAruleno
[state
->lookaheadsp
+ i
];
264 bitset_or (shiftset
, shiftset
, lookaheadset
);
267 bitset_zero (shiftset
);
269 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
270 if (!SHIFT_IS_DISABLED (shiftp
, i
))
271 bitset_set (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
273 for (i
= 0; i
< ntokens
; i
++)
277 int count
= bitset_test (shiftset
, i
);
279 for (j
= 0; j
< state
->nlookaheads
; ++j
)
281 if (BITISSET (LA (state
->lookaheadsp
+ j
), i
))
285 if (state
->lookaheadsp
+ j
!= default_LA
)
287 _(" %-4s\treduce using rule %d (%s)\n"),
288 escape (symbols
[i
]->tag
),
289 LAruleno
[state
->lookaheadsp
+ j
] - 1,
290 escape2 (symbols
[rules
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]->tag
));
300 _(" %-4s\treduce using rule %d (%s)\n"),
301 escape (symbols
[i
]->tag
),
302 LAruleno
[default_LA
] - 1,
303 escape2 (symbols
[rules
[LAruleno
[default_LA
]].lhs
]->tag
));
306 _(" %-4s\t[reduce using rule %d (%s)]\n"),
307 escape (symbols
[i
]->tag
),
308 LAruleno
[state
->lookaheadsp
+ j
] - 1,
309 escape2 (symbols
[rules
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]->tag
));
316 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
318 escape (symbols
[rules
[default_rule
].lhs
]->tag
));
324 print_actions (FILE *out
, state_t
*state
)
326 reductions
*redp
= state
->reductions
;
327 shifts
*shiftp
= state
->shifts
;
329 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
331 if (final_state
== state
->number
)
332 fprintf (out
, _(" $default\taccept\n"));
334 fprintf (out
, _(" NO ACTIONS\n"));
338 print_shifts (out
, state
);
339 print_errs (out
, state
);
340 print_reductions (out
, state
);
341 print_gotos (out
, state
);
345 print_state (FILE *out
, state_t
*state
)
347 fprintf (out
, _("state %d"), state
->number
);
349 print_core (out
, state
);
350 print_actions (out
, state
);
354 /*-----------------------------------------.
355 | Print information on the whole grammar. |
356 `-----------------------------------------*/
358 #define END_TEST(End) \
360 if (column + strlen(buffer) > (End)) \
362 fprintf (out, "%s\n ", buffer); \
370 print_grammar (FILE *out
)
377 /* rule # : LHS -> RHS */
378 fprintf (out
, "%s\n\n", _("Grammar"));
379 fprintf (out
, " %s\n", _("Number, Line, Rule"));
380 for (i
= 1; i
<= nrules
; i
++)
381 /* Don't print rules disabled in reduce_grammar_tables. */
384 fprintf (out
, _(" %3d %3d %s ->"),
385 i
- 1, rules
[i
].line
, escape (symbols
[rules
[i
].lhs
]->tag
));
386 rule
= &ritem
[rules
[i
].rhs
];
389 fprintf (out
, " %s", escape (symbols
[*rule
++]->tag
));
391 fprintf (out
, " /* %s */", _("empty"));
397 /* TERMINAL (type #) : rule #s terminal is on RHS */
398 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
399 for (i
= 0; i
<= max_user_token_number
; i
++)
400 if (token_translations
[i
] != 2)
403 column
= strlen (escape (symbols
[token_translations
[i
]]->tag
));
404 fputs (escape (symbols
[token_translations
[i
]]->tag
), out
);
406 sprintf (buffer
, " (%d)", i
);
408 for (j
= 1; j
<= nrules
; j
++)
409 for (rule
= &ritem
[rules
[j
].rhs
]; *rule
>= 0; rule
++)
410 if (*rule
== token_translations
[i
])
413 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
416 fprintf (out
, "%s\n", buffer
);
421 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
422 for (i
= ntokens
; i
<= nsyms
- 1; i
++)
424 int left_count
= 0, right_count
= 0;
426 for (j
= 1; j
<= nrules
; j
++)
428 if (rules
[j
].lhs
== i
)
430 for (rule
= &ritem
[rules
[j
].rhs
]; *rule
>= 0; rule
++)
439 fputs (escape (symbols
[i
]->tag
), out
);
440 column
= strlen (escape (symbols
[i
]->tag
));
441 sprintf (buffer
, " (%d)", i
);
447 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
449 for (j
= 1; j
<= nrules
; j
++)
452 if (rules
[j
].lhs
== i
)
453 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
460 sprintf (buffer
+ strlen (buffer
), ",");
462 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
463 for (j
= 1; j
<= nrules
; j
++)
465 for (rule
= &ritem
[rules
[j
].rhs
]; *rule
>= 0; rule
++)
469 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
474 fprintf (out
, "%s\n", buffer
);
484 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
485 that conflicts with Posix. */
486 FILE *out
= xfopen (spec_verbose_file
, "w");
488 size_t size
= obstack_object_size (&output_obstack
);
489 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
490 obstack_free (&output_obstack
, NULL
);
496 conflicts_output (out
);
500 /* New experimental feature: output all the items of a state, not
501 only its kernel. Requires to run closure, which need memory
502 allocation/deallocation. */
504 new_closure (nritems
);
505 /* Storage for print_reductions. */
506 shiftset
= bitset_create (ntokens
, BITSET_FIXED
);
507 bitset_zero (shiftset
);
508 lookaheadset
= bitset_create (ntokens
, BITSET_FIXED
);
509 bitset_zero (lookaheadset
);
510 for (i
= 0; i
< nstates
; i
++)
511 print_state (out
, states
[i
]);
512 bitset_free (shiftset
);
513 bitset_free (lookaheadset
);