]>
git.saurik.com Git - bison.git/blob - src/print.c
1 /* Print information on generated parser, for bison,
2 Copyright 1984, 1986, 1989, 2000, 2001 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"
37 static unsigned *shiftset
= NULL
;
38 static unsigned *lookaheadset
= NULL
;
42 print_token (int extnum
, int token
)
44 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
48 static inline const char *
49 escape (const char *s
)
51 return quotearg_n_style (1, escape_quoting_style
, s
);
54 /* Be cautious not to use twice the same slot in a single expression. */
55 static inline const char *
56 escape2 (const char *s
)
58 return quotearg_n_style (2, escape_quoting_style
, s
);
62 /*--------------------------------.
63 | Report information on a state. |
64 `--------------------------------*/
67 print_core (FILE *out
, state_t
*state
)
70 short *sitems
= state
->items
;
71 int snitems
= state
->nitems
;
73 /* New experimental feature: if TRACE_FLAGS output all the items of
74 a state, not only its kernel. */
77 closure (sitems
, snitems
);
84 for (i
= 0; i
< snitems
; i
++)
90 sp1
= sp
= ritem
+ sitems
[i
];
96 fprintf (out
, " %s -> ", escape (symbols
[rule_table
[rule
].lhs
]->tag
));
98 for (sp
= ritem
+ rule_table
[rule
].rhs
; sp
< sp1
; sp
++)
99 fprintf (out
, "%s ", escape (symbols
[*sp
]->tag
));
103 for (/* Nothing */; *sp
>= 0; ++sp
)
104 fprintf (out
, " %s", escape (symbols
[*sp
]->tag
));
106 fprintf (out
, _(" (rule %d)"), rule
- 1);
116 print_shifts (FILE *out
, state_t
*state
)
119 shifts
*shiftp
= state
->shifts
;
121 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
122 if (!SHIFT_IS_DISABLED (shiftp
, i
))
124 int state1
= shiftp
->shifts
[i
];
125 int symbol
= state_table
[state1
]->accessing_symbol
;
127 _(" %-4s\tshift, and go to state %d\n"),
128 escape (symbols
[symbol
]->tag
), state1
);
137 print_errs (FILE *out
, state_t
*state
)
139 errs
*errp
= state
->errs
;
142 for (i
= 0; i
< errp
->nerrs
; ++i
)
144 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
145 escape (symbols
[errp
->errs
[i
]]->tag
));
153 print_gotos (FILE *out
, state_t
*state
)
156 shifts
*shiftp
= state
->shifts
;
158 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
159 /* Skip token shifts. */;
161 if (i
< shiftp
->nshifts
)
163 for (; i
< shiftp
->nshifts
; i
++)
164 if (!SHIFT_IS_DISABLED (shiftp
, i
))
166 int state1
= shiftp
->shifts
[i
];
167 int symbol
= state_table
[state1
]->accessing_symbol
;
168 fprintf (out
, _(" %-4s\tgo to state %d\n"),
169 escape (symbols
[symbol
]->tag
), state1
);
177 print_reductions (FILE *out
, state_t
*state
)
180 shifts
*shiftp
= state
->shifts
;
181 reductions
*redp
= state
->reductions
;
182 errs
*errp
= state
->errs
;
185 if (redp
->nreds
== 0)
188 if (state
->consistent
)
190 int rule
= redp
->rules
[0];
191 int symbol
= rule_table
[rule
].lhs
;
192 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
193 rule
- 1, escape (symbols
[symbol
]->tag
));
197 for (i
= 0; i
< tokensetsize
; i
++)
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 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
210 for (i
= 0; i
< errp
->nerrs
; i
++)
212 SETBIT (shiftset
, errp
->errs
[i
]);
214 if (state
->nlookaheads
== 1 && !nodefault
)
217 int default_rule
= LAruleno
[state
->lookaheadsp
];
219 for (k
= 0; k
< tokensetsize
; ++k
)
220 lookaheadset
[k
] = LA (state
->lookaheadsp
)[k
] & shiftset
[k
];
222 for (i
= 0; i
< ntokens
; i
++)
223 if (BITISSET (lookaheadset
, i
))
224 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
225 escape (symbols
[i
]->tag
), default_rule
- 1,
226 escape2 (symbols
[rule_table
[default_rule
].lhs
]->tag
));
228 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
229 default_rule
- 1, escape (symbols
[rule_table
[default_rule
].lhs
]->tag
));
231 else if (state
->nlookaheads
>= 1)
235 int default_rule
= 0;
238 for (i
= 0; i
< state
->nlookaheads
; ++i
)
243 for (k
= 0; k
< tokensetsize
; ++k
)
244 lookaheadset
[k
] = LA (state
->lookaheadsp
+ i
)[k
] & ~shiftset
[k
];
246 for (j
= 0; j
< ntokens
; j
++)
247 if (BITISSET (lookaheadset
, j
))
253 default_LA
= state
->lookaheadsp
+ i
;
254 default_rule
= LAruleno
[state
->lookaheadsp
+ i
];
257 for (k
= 0; k
< tokensetsize
; ++k
)
258 shiftset
[k
] |= lookaheadset
[k
];
261 for (i
= 0; i
< tokensetsize
; i
++)
264 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
265 if (!SHIFT_IS_DISABLED (shiftp
, i
))
266 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
268 for (i
= 0; i
< ntokens
; i
++)
272 int count
= BITISSET (shiftset
, i
);
274 for (j
= 0; j
< state
->nlookaheads
; ++j
)
276 if (BITISSET (LA (state
->lookaheadsp
+ j
), i
))
280 if (state
->lookaheadsp
+ j
!= default_LA
)
282 _(" %-4s\treduce using rule %d (%s)\n"),
283 escape (symbols
[i
]->tag
),
284 LAruleno
[state
->lookaheadsp
+ j
] - 1,
285 escape2 (symbols
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]->tag
));
295 _(" %-4s\treduce using rule %d (%s)\n"),
296 escape (symbols
[i
]->tag
),
297 LAruleno
[default_LA
] - 1,
298 escape2 (symbols
[rule_table
[LAruleno
[default_LA
]].lhs
]->tag
));
301 _(" %-4s\t[reduce using rule %d (%s)]\n"),
302 escape (symbols
[i
]->tag
),
303 LAruleno
[state
->lookaheadsp
+ j
] - 1,
304 escape2 (symbols
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]->tag
));
311 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
313 escape (symbols
[rule_table
[default_rule
].lhs
]->tag
));
319 print_actions (FILE *out
, state_t
*state
)
321 reductions
*redp
= state
->reductions
;
322 shifts
*shiftp
= state
->shifts
;
324 if (shiftp
->nshifts
== 0 && redp
->nreds
== 0)
326 if (final_state
== state
->number
)
327 fprintf (out
, _(" $default\taccept\n"));
329 fprintf (out
, _(" NO ACTIONS\n"));
333 print_shifts (out
, state
);
334 print_errs (out
, state
);
335 print_reductions (out
, state
);
336 print_gotos (out
, state
);
340 print_state (FILE *out
, state_t
*state
)
342 fprintf (out
, _("state %d"), state
->number
);
344 print_core (out
, state
);
345 print_actions (out
, state
);
349 /*-----------------------------------------.
350 | Print information on the whole grammar. |
351 `-----------------------------------------*/
353 #define END_TEST(End) \
355 if (column + strlen(buffer) > (End)) \
357 fprintf (out, "%s\n ", buffer); \
365 print_grammar (FILE *out
)
372 /* rule # : LHS -> RHS */
373 fprintf (out
, "%s\n\n", _("Grammar"));
374 fprintf (out
, " %s\n", _("Number, Line, Rule"));
375 for (i
= 1; i
<= nrules
; i
++)
376 /* Don't print rules disabled in reduce_grammar_tables. */
377 if (rule_table
[i
].useful
)
379 fprintf (out
, _(" %3d %3d %s ->"),
380 i
- 1, rule_table
[i
].line
, escape (symbols
[rule_table
[i
].lhs
]->tag
));
381 rule
= &ritem
[rule_table
[i
].rhs
];
384 fprintf (out
, " %s", escape (symbols
[*rule
++]->tag
));
386 fprintf (out
, " /* %s */", _("empty"));
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
; i
++)
395 if (token_translations
[i
] != 2)
398 column
= strlen (escape (symbols
[token_translations
[i
]]->tag
));
399 fputs (escape (symbols
[token_translations
[i
]]->tag
), out
);
401 sprintf (buffer
, " (%d)", i
);
403 for (j
= 1; j
<= nrules
; j
++)
404 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
>= 0; rule
++)
405 if (*rule
== token_translations
[i
])
408 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
411 fprintf (out
, "%s\n", buffer
);
416 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
417 for (i
= ntokens
; i
<= nsyms
- 1; i
++)
419 int left_count
= 0, right_count
= 0;
421 for (j
= 1; j
<= nrules
; j
++)
423 if (rule_table
[j
].lhs
== i
)
425 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
>= 0; rule
++)
434 fputs (escape (symbols
[i
]->tag
), out
);
435 column
= strlen (escape (symbols
[i
]->tag
));
436 sprintf (buffer
, " (%d)", i
);
442 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
444 for (j
= 1; j
<= nrules
; j
++)
447 if (rule_table
[j
].lhs
== i
)
448 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
455 sprintf (buffer
+ strlen (buffer
), ",");
457 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
458 for (j
= 1; j
<= nrules
; j
++)
460 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
>= 0; rule
++)
464 sprintf (buffer
+ strlen (buffer
), " %d", j
- 1);
469 fprintf (out
, "%s\n", buffer
);
479 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
480 that conflicts with Posix. */
481 FILE *out
= xfopen (spec_verbose_file
, "w");
483 size_t size
= obstack_object_size (&output_obstack
);
484 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
485 obstack_free (&output_obstack
, NULL
);
491 conflicts_output (out
);
495 /* New experimental feature: output all the items of a state, not
496 only its kernel. Requires to run closure, which need memory
497 allocation/deallocation. */
499 new_closure (nritems
);
500 /* Storage for print_reductions. */
501 shiftset
= XCALLOC (unsigned, tokensetsize
);
502 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
503 for (i
= 0; i
< nstates
; i
++)
504 print_state (out
, state_table
[i
]);