]>
git.saurik.com Git - bison.git/blob - src/print.c
e1f53a1e12dce3521eeb04cb98eb67994449a785
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. */
27 #include "conflicts.h"
35 static unsigned *shiftset
= NULL
;
36 static unsigned *lookaheadset
= NULL
;
40 print_token (int extnum
, int token
)
42 fprintf (out
, _(" type %d is %s\n"), extnum
, tags
[token
]);
47 /*--------------------------------.
48 | Report information on a state. |
49 `--------------------------------*/
52 print_core (FILE *out
, state_t
*state
)
55 short *sitems
= state
->items
;
56 int snitems
= state
->nitems
;
58 /* New experimental feature: if TRACE_FLAGS output all the items of
59 a state, not only its kernel. */
62 closure (sitems
, snitems
);
69 for (i
= 0; i
< snitems
; i
++)
75 sp1
= sp
= ritem
+ sitems
[i
];
81 fprintf (out
, " %s -> ", tags
[rule_table
[rule
].lhs
]);
83 for (sp
= ritem
+ rule_table
[rule
].rhs
; sp
< sp1
; sp
++)
84 fprintf (out
, "%s ", tags
[*sp
]);
88 for (/* Nothing */; *sp
> 0; ++sp
)
89 fprintf (out
, " %s", tags
[*sp
]);
91 fprintf (out
, _(" (rule %d)"), rule
);
101 print_shifts (FILE *out
, state_t
*state
)
104 shifts
*shiftp
= state
->shifts
;
106 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
107 if (!SHIFT_IS_DISABLED (shiftp
, i
))
109 int state1
= shiftp
->shifts
[i
];
110 int symbol
= state_table
[state1
]->accessing_symbol
;
112 _(" %-4s\tshift, and go to state %d\n"),
113 tags
[symbol
], state1
);
122 print_errs (FILE *out
, state_t
*state
)
124 errs
*errp
= state
->errs
;
130 for (i
= 0; i
< errp
->nerrs
; ++i
)
132 fprintf (out
, _(" %-4s\terror (nonassociative)\n"),
133 tags
[errp
->errs
[i
]]);
141 print_gotos (FILE *out
, state_t
*state
)
144 shifts
*shiftp
= state
->shifts
;
146 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
147 /* Skip token shifts. */;
149 if (i
< shiftp
->nshifts
)
151 for (; i
< shiftp
->nshifts
; i
++)
152 if (!SHIFT_IS_DISABLED (shiftp
, i
))
154 int state1
= shiftp
->shifts
[i
];
155 int symbol
= state_table
[state1
]->accessing_symbol
;
156 fprintf (out
, _(" %-4s\tgo to state %d\n"),
157 tags
[symbol
], state1
);
165 print_reductions (FILE *out
, state_t
*state
)
168 shifts
*shiftp
= state
->shifts
;
169 reductions
*redp
= state
->reductions
;
170 errs
*errp
= state
->errs
;
173 if (state
->consistent
)
175 int rule
= redp
->rules
[0];
176 int symbol
= rule_table
[rule
].lhs
;
177 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
182 for (i
= 0; i
< tokensetsize
; i
++)
185 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
186 if (!SHIFT_IS_DISABLED (shiftp
, i
))
188 /* if this state has a shift for the error token, don't use a
190 if (SHIFT_IS_ERROR (shiftp
, i
))
192 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
196 for (i
= 0; i
< errp
->nerrs
; i
++)
198 SETBIT (shiftset
, errp
->errs
[i
]);
200 if (state
->nlookaheads
== 1 && !nodefault
)
203 int default_rule
= LAruleno
[state
->lookaheadsp
];
205 for (k
= 0; k
< tokensetsize
; ++k
)
206 lookaheadset
[k
] = LA (state
->lookaheadsp
)[k
] & shiftset
[k
];
208 for (i
= 0; i
< ntokens
; i
++)
209 if (BITISSET (lookaheadset
, i
))
210 fprintf (out
, _(" %-4s\t[reduce using rule %d (%s)]\n"),
211 tags
[i
], default_rule
,
212 tags
[rule_table
[default_rule
].lhs
]);
214 fprintf (out
, _(" $default\treduce using rule %d (%s)\n\n"),
215 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
217 else if (state
->nlookaheads
>= 1)
221 int default_rule
= 0;
224 for (i
= 0; i
< state
->nlookaheads
; ++i
)
229 for (k
= 0; k
< tokensetsize
; ++k
)
230 lookaheadset
[k
] = LA (state
->lookaheadsp
+ i
)[k
] & ~shiftset
[k
];
232 for (j
= 0; j
< ntokens
; j
++)
233 if (BITISSET (lookaheadset
, j
))
239 default_LA
= state
->lookaheadsp
+ i
;
240 default_rule
= LAruleno
[state
->lookaheadsp
+ i
];
243 for (k
= 0; k
< tokensetsize
; ++k
)
244 shiftset
[k
] |= lookaheadset
[k
];
247 for (i
= 0; i
< tokensetsize
; i
++)
250 for (i
= 0; i
< shiftp
->nshifts
&& SHIFT_IS_SHIFT (shiftp
, i
); i
++)
251 if (!SHIFT_IS_DISABLED (shiftp
, i
))
252 SETBIT (shiftset
, SHIFT_SYMBOL (shiftp
, i
));
254 for (i
= 0; i
< ntokens
; i
++)
258 int count
= BITISSET (shiftset
, i
);
260 for (j
= 0; j
< state
->nlookaheads
; ++j
)
262 if (BITISSET (LA (state
->lookaheadsp
+ j
), i
))
266 if (state
->lookaheadsp
+ j
!= default_LA
)
268 _(" %-4s\treduce using rule %d (%s)\n"),
270 LAruleno
[state
->lookaheadsp
+ j
],
271 tags
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]);
281 _(" %-4s\treduce using rule %d (%s)\n"),
283 LAruleno
[default_LA
],
284 tags
[rule_table
[LAruleno
[default_LA
]].lhs
]);
287 _(" %-4s\t[reduce using rule %d (%s)]\n"),
289 LAruleno
[state
->lookaheadsp
+ j
],
290 tags
[rule_table
[LAruleno
[state
->lookaheadsp
+ j
]].lhs
]);
297 fprintf (out
, _(" $default\treduce using rule %d (%s)\n"),
298 default_rule
, tags
[rule_table
[default_rule
].lhs
]);
304 print_actions (FILE *out
, state_t
*state
)
306 reductions
*redp
= state
->reductions
;
307 shifts
*shiftp
= state
->shifts
;
309 if (!shiftp
->nshifts
&& !redp
)
311 if (final_state
== state
->number
)
312 fprintf (out
, _(" $default\taccept\n"));
314 fprintf (out
, _(" NO ACTIONS\n"));
318 print_shifts (out
, state
);
319 print_errs (out
, state
);
321 print_reductions (out
, state
);
322 print_gotos (out
, state
);
326 print_state (FILE *out
, state_t
*state
)
328 fprintf (out
, _("state %d"), state
->number
);
330 print_core (out
, state
);
331 print_actions (out
, state
);
335 /*-----------------------------------------.
336 | Print information on the whole grammar. |
337 `-----------------------------------------*/
339 #define END_TEST(End) \
341 if (column + strlen(buffer) > (End)) \
343 fprintf (out, "%s\n ", buffer); \
351 print_grammar (FILE *out
)
358 /* rule # : LHS -> RHS */
359 fprintf (out
, "%s\n\n", _("Grammar"));
360 fprintf (out
, " %s\n", _("Number, Line, Rule"));
361 for (i
= 1; i
<= nrules
; i
++)
362 /* Don't print rules disabled in reduce_grammar_tables. */
363 if (rule_table
[i
].useful
)
365 fprintf (out
, _(" %3d %3d %s ->"),
366 i
, rule_table
[i
].line
, tags
[rule_table
[i
].lhs
]);
367 rule
= &ritem
[rule_table
[i
].rhs
];
370 fprintf (out
, " %s", tags
[*rule
++]);
372 fprintf (out
, " /* %s */", _("empty"));
378 /* TERMINAL (type #) : rule #s terminal is on RHS */
379 fprintf (out
, "%s\n\n", _("Terminals, with rules where they appear"));
380 fprintf (out
, "%s (-1)\n", tags
[0]);
382 for (i
= 0; i
<= max_user_token_number
; i
++)
383 if (token_translations
[i
] != 2)
386 column
= strlen (tags
[token_translations
[i
]]);
387 fputs (tags
[token_translations
[i
]], out
);
389 sprintf (buffer
, " (%d)", i
);
391 for (j
= 1; j
<= nrules
; j
++)
392 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
> 0; rule
++)
393 if (*rule
== token_translations
[i
])
396 sprintf (buffer
+ strlen (buffer
), " %d", j
);
399 fprintf (out
, "%s\n", buffer
);
404 fprintf (out
, "%s\n\n", _("Nonterminals, with rules where they appear"));
405 for (i
= ntokens
; i
<= nsyms
- 1; i
++)
407 int left_count
= 0, right_count
= 0;
409 for (j
= 1; j
<= nrules
; j
++)
411 if (rule_table
[j
].lhs
== i
)
413 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
> 0; rule
++)
422 fputs (tags
[i
], out
);
423 column
= strlen (tags
[i
]);
424 sprintf (buffer
, " (%d)", i
);
430 sprintf (buffer
+ strlen (buffer
), _(" on left:"));
432 for (j
= 1; j
<= nrules
; j
++)
435 if (rule_table
[j
].lhs
== i
)
436 sprintf (buffer
+ strlen (buffer
), " %d", j
);
443 sprintf (buffer
+ strlen (buffer
), ",");
445 sprintf (buffer
+ strlen (buffer
), _(" on right:"));
446 for (j
= 1; j
<= nrules
; j
++)
448 for (rule
= &ritem
[rule_table
[j
].rhs
]; *rule
> 0; rule
++)
452 sprintf (buffer
+ strlen (buffer
), " %d", j
);
457 fprintf (out
, "%s\n", buffer
);
467 /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but
468 that conflicts with Posix. */
469 FILE *out
= xfopen (spec_verbose_file
, "w");
471 size_t size
= obstack_object_size (&output_obstack
);
472 fwrite (obstack_finish (&output_obstack
), 1, size
, out
);
473 obstack_free (&output_obstack
, NULL
);
479 conflicts_output (out
);
483 /* New experimental feature: output all the items of a state, not
484 only its kernel. Requires to run closure, which need memory
485 allocation/deallocation. */
487 new_closure (nitems
);
488 /* Storage for print_reductions. */
489 shiftset
= XCALLOC (unsigned, tokensetsize
);
490 lookaheadset
= XCALLOC (unsigned, tokensetsize
);
491 for (i
= 0; i
< nstates
; i
++)
492 print_state (out
, state_table
[i
]);