]>
Commit | Line | Data |
---|---|---|
e06f0c34 | 1 | /* Print information on generated parser, for bison, |
09b503c8 | 2 | Copyright 1984, 1986, 1989, 2000, 2001 Free Software Foundation, Inc. |
e06f0c34 | 3 | |
c29240e7 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
e06f0c34 | 5 | |
c29240e7 AD |
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) | |
9 | any later version. | |
e06f0c34 | 10 | |
c29240e7 AD |
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. | |
e06f0c34 | 15 | |
c29240e7 AD |
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. */ | |
e06f0c34 RS |
20 | |
21 | ||
e06f0c34 | 22 | #include "system.h" |
e06f0c34 RS |
23 | #include "files.h" |
24 | #include "gram.h" | |
b2ca4022 | 25 | #include "LR0.h" |
720d742f | 26 | #include "lalr.h" |
0619caf0 | 27 | #include "conflicts.h" |
07a58c13 AD |
28 | #include "getargs.h" |
29 | #include "state.h" | |
b2ca4022 | 30 | #include "reader.h" |
d7913476 | 31 | #include "print.h" |
09b503c8 | 32 | #include "reduce.h" |
43168960 | 33 | #include "closure.h" |
e06f0c34 | 34 | |
07a58c13 | 35 | #if 0 |
4a120d45 | 36 | static void |
d2729d44 | 37 | print_token (int extnum, int token) |
e06f0c34 | 38 | { |
342b8b6e | 39 | fprintf (out, _(" type %d is %s\n"), extnum, tags[token]); |
e06f0c34 | 40 | } |
4a120d45 | 41 | #endif |
e06f0c34 | 42 | |
07a58c13 | 43 | \f |
342b8b6e | 44 | /*--------------------------------. |
07a58c13 | 45 | | Report information on a state. | |
342b8b6e | 46 | `--------------------------------*/ |
e06f0c34 | 47 | |
4a120d45 | 48 | static void |
065fbd27 | 49 | print_core (FILE *out, state_t *state) |
e06f0c34 | 50 | { |
c29240e7 | 51 | int i; |
065fbd27 AD |
52 | short *sitems = state->items; |
53 | int snitems = state->nitems; | |
e06f0c34 | 54 | |
43168960 AD |
55 | /* New experimental feature: if TRACE_FLAGS output all the items of |
56 | a state, not only its kernel. */ | |
57 | if (trace_flag) | |
58 | { | |
59 | closure (sitems, snitems); | |
60 | sitems = itemset; | |
61 | snitems = nitemset; | |
62 | } | |
e06f0c34 | 63 | |
43168960 | 64 | if (snitems) |
e06f0c34 | 65 | { |
43168960 AD |
66 | for (i = 0; i < snitems; i++) |
67 | { | |
68 | short *sp; | |
69 | short *sp1; | |
70 | int rule; | |
4bc30f78 | 71 | |
43168960 | 72 | sp1 = sp = ritem + sitems[i]; |
e06f0c34 | 73 | |
43168960 AD |
74 | while (*sp > 0) |
75 | sp++; | |
e06f0c34 | 76 | |
43168960 AD |
77 | rule = -(*sp); |
78 | fprintf (out, " %s -> ", tags[rule_table[rule].lhs]); | |
e06f0c34 | 79 | |
43168960 AD |
80 | for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++) |
81 | fprintf (out, "%s ", tags[*sp]); | |
e06f0c34 | 82 | |
43168960 | 83 | fputc ('.', out); |
e06f0c34 | 84 | |
43168960 AD |
85 | for (/* Nothing */; *sp > 0; ++sp) |
86 | fprintf (out, " %s", tags[*sp]); | |
87 | ||
88 | fprintf (out, _(" (rule %d)"), rule); | |
89 | fputc ('\n', out); | |
90 | } | |
e06f0c34 | 91 | |
342b8b6e | 92 | fputc ('\n', out); |
e06f0c34 | 93 | } |
e06f0c34 RS |
94 | } |
95 | ||
4a120d45 | 96 | static void |
065fbd27 | 97 | print_actions (FILE *out, state_t *state) |
e06f0c34 | 98 | { |
c29240e7 | 99 | int i; |
e06f0c34 | 100 | |
065fbd27 AD |
101 | shifts *shiftp = state->shifts; |
102 | reductions *redp = state->reductions; | |
103 | errs *errp = state->errs; | |
e06f0c34 | 104 | |
d954473d | 105 | if (!shiftp->nshifts && !redp) |
e06f0c34 | 106 | { |
065fbd27 | 107 | if (final_state == state->number) |
342b8b6e | 108 | fprintf (out, _(" $default\taccept\n")); |
e06f0c34 | 109 | else |
342b8b6e | 110 | fprintf (out, _(" NO ACTIONS\n")); |
e06f0c34 RS |
111 | return; |
112 | } | |
113 | ||
2e729273 | 114 | for (i = 0; i < shiftp->nshifts && SHIFT_IS_SHIFT (shiftp, i); i++) |
d954473d AD |
115 | if (!SHIFT_IS_DISABLED (shiftp, i)) |
116 | { | |
117 | int state1 = shiftp->shifts[i]; | |
f693ad14 | 118 | int symbol = state_table[state1]->accessing_symbol; |
2e729273 AD |
119 | fprintf (out, |
120 | _(" %-4s\tshift, and go to state %d\n"), | |
121 | tags[symbol], state1); | |
d954473d | 122 | } |
e06f0c34 | 123 | |
d954473d AD |
124 | if (i > 0) |
125 | fputc ('\n', out); | |
e06f0c34 RS |
126 | |
127 | if (errp) | |
128 | { | |
d9ec2d07 AD |
129 | int j; |
130 | for (j = 0; j < errp->nerrs; j++) | |
e06f0c34 | 131 | { |
d9ec2d07 AD |
132 | int symbol = errp->errs[j]; |
133 | if (!symbol) | |
c29240e7 | 134 | continue; |
342b8b6e | 135 | fprintf (out, _(" %-4s\terror (nonassociative)\n"), |
c29240e7 | 136 | tags[symbol]); |
e06f0c34 RS |
137 | } |
138 | ||
139 | if (j > 0) | |
342b8b6e | 140 | fputc ('\n', out); |
e06f0c34 RS |
141 | } |
142 | ||
065fbd27 | 143 | if (state->consistent && redp) |
e06f0c34 | 144 | { |
d9ec2d07 AD |
145 | int rule = redp->rules[0]; |
146 | int symbol = rule_table[rule].lhs; | |
342b8b6e AD |
147 | fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), |
148 | rule, tags[symbol]); | |
e06f0c34 RS |
149 | } |
150 | else if (redp) | |
151 | { | |
c73a41af | 152 | print_reductions (out, state); |
e06f0c34 RS |
153 | } |
154 | ||
d954473d | 155 | if (i < shiftp->nshifts) |
e06f0c34 | 156 | { |
d954473d AD |
157 | for (; i < shiftp->nshifts; i++) |
158 | if (!SHIFT_IS_DISABLED (shiftp, i)) | |
159 | { | |
160 | int state1 = shiftp->shifts[i]; | |
f693ad14 | 161 | int symbol = state_table[state1]->accessing_symbol; |
d954473d AD |
162 | fprintf (out, _(" %-4s\tgo to state %d\n"), |
163 | tags[symbol], state1); | |
164 | } | |
e06f0c34 | 165 | |
342b8b6e | 166 | fputc ('\n', out); |
e06f0c34 RS |
167 | } |
168 | } | |
169 | ||
07a58c13 | 170 | static void |
065fbd27 | 171 | print_state (FILE *out, state_t *state) |
07a58c13 | 172 | { |
065fbd27 | 173 | fprintf (out, _("state %d"), state->number); |
342b8b6e AD |
174 | fputs ("\n\n", out); |
175 | print_core (out, state); | |
176 | print_actions (out, state); | |
d2d1b42b | 177 | fputs ("\n\n", out); |
07a58c13 AD |
178 | } |
179 | \f | |
180 | /*-----------------------------------------. | |
181 | | Print information on the whole grammar. | | |
182 | `-----------------------------------------*/ | |
183 | ||
342b8b6e AD |
184 | #define END_TEST(End) \ |
185 | do { \ | |
186 | if (column + strlen(buffer) > (End)) \ | |
187 | { \ | |
188 | fprintf (out, "%s\n ", buffer); \ | |
189 | column = 3; \ | |
190 | buffer[0] = 0; \ | |
191 | } \ | |
ff4423cc | 192 | } while (0) |
e06f0c34 | 193 | |
07a58c13 | 194 | |
4a120d45 | 195 | static void |
342b8b6e | 196 | print_grammar (FILE *out) |
e06f0c34 RS |
197 | { |
198 | int i, j; | |
c29240e7 | 199 | short *rule; |
e06f0c34 RS |
200 | char buffer[90]; |
201 | int column = 0; | |
202 | ||
203 | /* rule # : LHS -> RHS */ | |
d2d1b42b | 204 | fprintf (out, "%s\n\n", _("Grammar")); |
b29b2ed5 | 205 | fprintf (out, " %s\n", _("Number, Line, Rule")); |
e06f0c34 RS |
206 | for (i = 1; i <= nrules; i++) |
207 | /* Don't print rules disabled in reduce_grammar_tables. */ | |
68f1e3ed | 208 | if (rule_table[i].useful) |
e06f0c34 | 209 | { |
b29b2ed5 AD |
210 | fprintf (out, _(" %3d %3d %s ->"), |
211 | i, rule_table[i].line, tags[rule_table[i].lhs]); | |
b2ed6e58 | 212 | rule = &ritem[rule_table[i].rhs]; |
e06f0c34 RS |
213 | if (*rule > 0) |
214 | while (*rule > 0) | |
342b8b6e | 215 | fprintf (out, " %s", tags[*rule++]); |
e06f0c34 | 216 | else |
b29b2ed5 | 217 | fprintf (out, " /* %s */", _("empty")); |
0df87bb6 | 218 | fputc ('\n', out); |
e06f0c34 | 219 | } |
d2d1b42b AD |
220 | fputs ("\n\n", out); |
221 | ||
e06f0c34 RS |
222 | |
223 | /* TERMINAL (type #) : rule #s terminal is on RHS */ | |
d2d1b42b | 224 | fprintf (out, "%s\n\n", _("Terminals, with rules where they appear")); |
342b8b6e | 225 | fprintf (out, "%s (-1)\n", tags[0]); |
e06f0c34 | 226 | |
342b8b6e AD |
227 | for (i = 0; i <= max_user_token_number; i++) |
228 | if (token_translations[i] != 2) | |
229 | { | |
230 | buffer[0] = 0; | |
231 | column = strlen (tags[token_translations[i]]); | |
232 | fputs (tags[token_translations[i]], out); | |
233 | END_TEST (50); | |
234 | sprintf (buffer, " (%d)", i); | |
e06f0c34 | 235 | |
342b8b6e | 236 | for (j = 1; j <= nrules; j++) |
b2ed6e58 | 237 | for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) |
342b8b6e AD |
238 | if (*rule == token_translations[i]) |
239 | { | |
240 | END_TEST (65); | |
241 | sprintf (buffer + strlen (buffer), " %d", j); | |
242 | break; | |
243 | } | |
244 | fprintf (out, "%s\n", buffer); | |
245 | } | |
d2d1b42b AD |
246 | fputs ("\n\n", out); |
247 | ||
342b8b6e | 248 | |
d2d1b42b | 249 | fprintf (out, "%s\n\n", _("Nonterminals, with rules where they appear")); |
e06f0c34 RS |
250 | for (i = ntokens; i <= nsyms - 1; i++) |
251 | { | |
252 | int left_count = 0, right_count = 0; | |
253 | ||
254 | for (j = 1; j <= nrules; j++) | |
255 | { | |
b2ed6e58 | 256 | if (rule_table[j].lhs == i) |
e06f0c34 | 257 | left_count++; |
b2ed6e58 | 258 | for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) |
e06f0c34 RS |
259 | if (*rule == i) |
260 | { | |
261 | right_count++; | |
262 | break; | |
263 | } | |
264 | } | |
265 | ||
266 | buffer[0] = 0; | |
342b8b6e | 267 | fputs (tags[i], out); |
e06f0c34 RS |
268 | column = strlen (tags[i]); |
269 | sprintf (buffer, " (%d)", i); | |
270 | END_TEST (0); | |
271 | ||
272 | if (left_count > 0) | |
273 | { | |
274 | END_TEST (50); | |
c29240e7 | 275 | sprintf (buffer + strlen (buffer), _(" on left:")); |
e06f0c34 RS |
276 | |
277 | for (j = 1; j <= nrules; j++) | |
278 | { | |
279 | END_TEST (65); | |
b2ed6e58 | 280 | if (rule_table[j].lhs == i) |
c29240e7 | 281 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
282 | } |
283 | } | |
284 | ||
285 | if (right_count > 0) | |
286 | { | |
287 | if (left_count > 0) | |
c29240e7 | 288 | sprintf (buffer + strlen (buffer), ","); |
e06f0c34 | 289 | END_TEST (50); |
c29240e7 | 290 | sprintf (buffer + strlen (buffer), _(" on right:")); |
e06f0c34 RS |
291 | for (j = 1; j <= nrules; j++) |
292 | { | |
b2ed6e58 | 293 | for (rule = &ritem[rule_table[j].rhs]; *rule > 0; rule++) |
e06f0c34 RS |
294 | if (*rule == i) |
295 | { | |
296 | END_TEST (65); | |
c29240e7 | 297 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
298 | break; |
299 | } | |
300 | } | |
301 | } | |
342b8b6e | 302 | fprintf (out, "%s\n", buffer); |
e06f0c34 | 303 | } |
d2d1b42b | 304 | fputs ("\n\n", out); |
e06f0c34 | 305 | } |
07a58c13 AD |
306 | \f |
307 | void | |
308 | print_results (void) | |
309 | { | |
64d15509 | 310 | int i; |
07a58c13 | 311 | |
64d15509 AD |
312 | /* We used to use just .out if SPEC_NAME_PREFIX (-p) was used, but |
313 | that conflicts with Posix. */ | |
314 | FILE *out = xfopen (spec_verbose_file, "w"); | |
07a58c13 | 315 | |
64d15509 AD |
316 | size_t size = obstack_object_size (&output_obstack); |
317 | fwrite (obstack_finish (&output_obstack), 1, size, out); | |
318 | obstack_free (&output_obstack, NULL); | |
07a58c13 | 319 | |
64d15509 AD |
320 | if (size) |
321 | fputs ("\n\n", out); | |
342b8b6e | 322 | |
64d15509 AD |
323 | reduce_output (out); |
324 | conflicts_output (out); | |
342b8b6e | 325 | |
64d15509 | 326 | print_grammar (out); |
342b8b6e | 327 | |
64d15509 AD |
328 | /* New experimental feature: output all the items of a state, |
329 | not only its kernel. Requires to run closure, which need | |
330 | memory allocation/deallocation. */ | |
331 | if (trace_flag) | |
332 | new_closure (nitems); | |
333 | for (i = 0; i < nstates; i++) | |
065fbd27 | 334 | print_state (out, state_table[i]); |
64d15509 AD |
335 | if (trace_flag) |
336 | free_closure (); | |
337 | ||
338 | xfclose (out); | |
07a58c13 | 339 | } |