]>
Commit | Line | Data |
---|---|---|
e06f0c34 | 1 | /* Print information on generated parser, for bison, |
aa7815f5 | 2 | Copyright 1984, 1986, 1989, 2000 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" |
e06f0c34 | 32 | |
07a58c13 | 33 | #if 0 |
4a120d45 | 34 | static void |
d2729d44 | 35 | print_token (int extnum, int token) |
e06f0c34 | 36 | { |
342b8b6e | 37 | fprintf (out, _(" type %d is %s\n"), extnum, tags[token]); |
e06f0c34 | 38 | } |
4a120d45 | 39 | #endif |
e06f0c34 | 40 | |
07a58c13 | 41 | \f |
342b8b6e | 42 | /*--------------------------------. |
07a58c13 | 43 | | Report information on a state. | |
342b8b6e | 44 | `--------------------------------*/ |
e06f0c34 | 45 | |
4a120d45 | 46 | static void |
342b8b6e | 47 | print_core (FILE *out, int state) |
e06f0c34 | 48 | { |
c29240e7 AD |
49 | int i; |
50 | int k; | |
51 | int rule; | |
52 | core *statep; | |
53 | short *sp; | |
54 | short *sp1; | |
e06f0c34 RS |
55 | |
56 | statep = state_table[state]; | |
57 | k = statep->nitems; | |
58 | ||
c29240e7 AD |
59 | if (k == 0) |
60 | return; | |
e06f0c34 RS |
61 | |
62 | for (i = 0; i < k; i++) | |
63 | { | |
64 | sp1 = sp = ritem + statep->items[i]; | |
65 | ||
66 | while (*sp > 0) | |
67 | sp++; | |
68 | ||
69 | rule = -(*sp); | |
342b8b6e | 70 | fprintf (out, " %s -> ", tags[rlhs[rule]]); |
e06f0c34 RS |
71 | |
72 | for (sp = ritem + rrhs[rule]; sp < sp1; sp++) | |
73 | { | |
342b8b6e | 74 | fprintf (out, "%s ", tags[*sp]); |
e06f0c34 RS |
75 | } |
76 | ||
342b8b6e | 77 | fputc ('.', out); |
e06f0c34 RS |
78 | |
79 | while (*sp > 0) | |
80 | { | |
342b8b6e | 81 | fprintf (out, " %s", tags[*sp]); |
e06f0c34 RS |
82 | sp++; |
83 | } | |
84 | ||
342b8b6e AD |
85 | fprintf (out, _(" (rule %d)"), rule); |
86 | fputc ('\n', out); | |
e06f0c34 RS |
87 | } |
88 | ||
342b8b6e | 89 | fputc ('\n', out); |
e06f0c34 RS |
90 | } |
91 | ||
4a120d45 | 92 | static void |
342b8b6e | 93 | print_actions (FILE *out, int state) |
e06f0c34 | 94 | { |
c29240e7 AD |
95 | int i; |
96 | int k; | |
97 | int state1; | |
98 | int symbol; | |
99 | shifts *shiftp; | |
100 | errs *errp; | |
101 | reductions *redp; | |
102 | int rule; | |
e06f0c34 RS |
103 | |
104 | shiftp = shift_table[state]; | |
105 | redp = reduction_table[state]; | |
106 | errp = err_table[state]; | |
107 | ||
108 | if (!shiftp && !redp) | |
109 | { | |
110 | if (final_state == state) | |
342b8b6e | 111 | fprintf (out, _(" $default\taccept\n")); |
e06f0c34 | 112 | else |
342b8b6e | 113 | fprintf (out, _(" NO ACTIONS\n")); |
e06f0c34 RS |
114 | return; |
115 | } | |
116 | ||
117 | if (shiftp) | |
118 | { | |
119 | k = shiftp->nshifts; | |
120 | ||
121 | for (i = 0; i < k; i++) | |
122 | { | |
c29240e7 AD |
123 | if (!shiftp->shifts[i]) |
124 | continue; | |
e06f0c34 RS |
125 | state1 = shiftp->shifts[i]; |
126 | symbol = accessing_symbol[state1]; | |
127 | /* The following line used to be turned off. */ | |
c29240e7 AD |
128 | if (ISVAR (symbol)) |
129 | break; | |
130 | if (symbol == 0) /* I.e. strcmp(tags[symbol],"$")==0 */ | |
342b8b6e AD |
131 | fprintf (out, |
132 | _(" $ \tgo to state %d\n"), state1); | |
c29240e7 | 133 | else |
342b8b6e AD |
134 | fprintf (out, |
135 | _(" %-4s\tshift, and go to state %d\n"), | |
136 | tags[symbol], state1); | |
e06f0c34 RS |
137 | } |
138 | ||
139 | if (i > 0) | |
342b8b6e | 140 | fputc ('\n', out); |
e06f0c34 RS |
141 | } |
142 | else | |
143 | { | |
144 | i = 0; | |
145 | k = 0; | |
146 | } | |
147 | ||
148 | if (errp) | |
149 | { | |
150 | int j, nerrs; | |
151 | ||
152 | nerrs = errp->nerrs; | |
153 | ||
154 | for (j = 0; j < nerrs; j++) | |
155 | { | |
c29240e7 AD |
156 | if (!errp->errs[j]) |
157 | continue; | |
e06f0c34 | 158 | symbol = errp->errs[j]; |
342b8b6e | 159 | fprintf (out, _(" %-4s\terror (nonassociative)\n"), |
c29240e7 | 160 | tags[symbol]); |
e06f0c34 RS |
161 | } |
162 | ||
163 | if (j > 0) | |
342b8b6e | 164 | fputc ('\n', out); |
e06f0c34 RS |
165 | } |
166 | ||
167 | if (consistent[state] && redp) | |
168 | { | |
169 | rule = redp->rules[0]; | |
170 | symbol = rlhs[rule]; | |
342b8b6e AD |
171 | fprintf (out, _(" $default\treduce using rule %d (%s)\n\n"), |
172 | rule, tags[symbol]); | |
e06f0c34 RS |
173 | } |
174 | else if (redp) | |
175 | { | |
c73a41af | 176 | print_reductions (out, state); |
e06f0c34 RS |
177 | } |
178 | ||
179 | if (i < k) | |
180 | { | |
181 | for (; i < k; i++) | |
182 | { | |
c29240e7 AD |
183 | if (!shiftp->shifts[i]) |
184 | continue; | |
e06f0c34 RS |
185 | state1 = shiftp->shifts[i]; |
186 | symbol = accessing_symbol[state1]; | |
342b8b6e AD |
187 | fprintf (out, _(" %-4s\tgo to state %d\n"), |
188 | tags[symbol], state1); | |
e06f0c34 RS |
189 | } |
190 | ||
342b8b6e | 191 | fputc ('\n', out); |
e06f0c34 RS |
192 | } |
193 | } | |
194 | ||
07a58c13 | 195 | static void |
342b8b6e | 196 | print_state (FILE *out, int state) |
07a58c13 | 197 | { |
342b8b6e AD |
198 | fputs ("\n\n", out); |
199 | fprintf (out, _("state %d"), state); | |
200 | fputs ("\n\n", out); | |
201 | print_core (out, state); | |
202 | print_actions (out, state); | |
07a58c13 AD |
203 | } |
204 | \f | |
205 | /*-----------------------------------------. | |
206 | | Print information on the whole grammar. | | |
207 | `-----------------------------------------*/ | |
208 | ||
342b8b6e AD |
209 | #define END_TEST(End) \ |
210 | do { \ | |
211 | if (column + strlen(buffer) > (End)) \ | |
212 | { \ | |
213 | fprintf (out, "%s\n ", buffer); \ | |
214 | column = 3; \ | |
215 | buffer[0] = 0; \ | |
216 | } \ | |
ff4423cc | 217 | } while (0) |
e06f0c34 | 218 | |
07a58c13 | 219 | |
4a120d45 | 220 | static void |
342b8b6e | 221 | print_grammar (FILE *out) |
e06f0c34 RS |
222 | { |
223 | int i, j; | |
c29240e7 | 224 | short *rule; |
e06f0c34 RS |
225 | char buffer[90]; |
226 | int column = 0; | |
227 | ||
228 | /* rule # : LHS -> RHS */ | |
0df87bb6 | 229 | fprintf (out, "\n%s\n\n", _("Grammar")); |
e06f0c34 RS |
230 | for (i = 1; i <= nrules; i++) |
231 | /* Don't print rules disabled in reduce_grammar_tables. */ | |
232 | if (rlhs[i] >= 0) | |
233 | { | |
342b8b6e | 234 | fprintf (out, _("rule %-4d %s ->"), i, tags[rlhs[i]]); |
e06f0c34 RS |
235 | rule = &ritem[rrhs[i]]; |
236 | if (*rule > 0) | |
237 | while (*rule > 0) | |
342b8b6e | 238 | fprintf (out, " %s", tags[*rule++]); |
e06f0c34 | 239 | else |
0df87bb6 AD |
240 | fprintf (out, " /* %s */", _("empty")); |
241 | fputc ('\n', out); | |
e06f0c34 RS |
242 | } |
243 | ||
244 | /* TERMINAL (type #) : rule #s terminal is on RHS */ | |
342b8b6e AD |
245 | fprintf (out, "\n%s\n\n", _("Terminals, with rules where they appear")); |
246 | fprintf (out, "%s (-1)\n", tags[0]); | |
e06f0c34 | 247 | |
342b8b6e AD |
248 | for (i = 0; i <= max_user_token_number; i++) |
249 | if (token_translations[i] != 2) | |
250 | { | |
251 | buffer[0] = 0; | |
252 | column = strlen (tags[token_translations[i]]); | |
253 | fputs (tags[token_translations[i]], out); | |
254 | END_TEST (50); | |
255 | sprintf (buffer, " (%d)", i); | |
e06f0c34 | 256 | |
342b8b6e AD |
257 | for (j = 1; j <= nrules; j++) |
258 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
259 | if (*rule == token_translations[i]) | |
260 | { | |
261 | END_TEST (65); | |
262 | sprintf (buffer + strlen (buffer), " %d", j); | |
263 | break; | |
264 | } | |
265 | fprintf (out, "%s\n", buffer); | |
266 | } | |
267 | ||
268 | fprintf (out, "\n%s\n\n", | |
269 | _("Nonterminals, with rules where they appear")); | |
e06f0c34 RS |
270 | for (i = ntokens; i <= nsyms - 1; i++) |
271 | { | |
272 | int left_count = 0, right_count = 0; | |
273 | ||
274 | for (j = 1; j <= nrules; j++) | |
275 | { | |
276 | if (rlhs[j] == i) | |
277 | left_count++; | |
278 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
279 | if (*rule == i) | |
280 | { | |
281 | right_count++; | |
282 | break; | |
283 | } | |
284 | } | |
285 | ||
286 | buffer[0] = 0; | |
342b8b6e | 287 | fputs (tags[i], out); |
e06f0c34 RS |
288 | column = strlen (tags[i]); |
289 | sprintf (buffer, " (%d)", i); | |
290 | END_TEST (0); | |
291 | ||
292 | if (left_count > 0) | |
293 | { | |
294 | END_TEST (50); | |
c29240e7 | 295 | sprintf (buffer + strlen (buffer), _(" on left:")); |
e06f0c34 RS |
296 | |
297 | for (j = 1; j <= nrules; j++) | |
298 | { | |
299 | END_TEST (65); | |
300 | if (rlhs[j] == i) | |
c29240e7 | 301 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
302 | } |
303 | } | |
304 | ||
305 | if (right_count > 0) | |
306 | { | |
307 | if (left_count > 0) | |
c29240e7 | 308 | sprintf (buffer + strlen (buffer), ","); |
e06f0c34 | 309 | END_TEST (50); |
c29240e7 | 310 | sprintf (buffer + strlen (buffer), _(" on right:")); |
e06f0c34 RS |
311 | for (j = 1; j <= nrules; j++) |
312 | { | |
313 | for (rule = &ritem[rrhs[j]]; *rule > 0; rule++) | |
314 | if (*rule == i) | |
315 | { | |
316 | END_TEST (65); | |
c29240e7 | 317 | sprintf (buffer + strlen (buffer), " %d", j); |
e06f0c34 RS |
318 | break; |
319 | } | |
320 | } | |
321 | } | |
342b8b6e | 322 | fprintf (out, "%s\n", buffer); |
e06f0c34 RS |
323 | } |
324 | } | |
07a58c13 AD |
325 | \f |
326 | void | |
327 | print_results (void) | |
328 | { | |
342b8b6e AD |
329 | if (verbose_flag) |
330 | { | |
331 | int i; | |
07a58c13 | 332 | |
342b8b6e AD |
333 | /* We used to use just .out if spec_name_prefix (-p) was used, but |
334 | that conflicts with Posix. */ | |
335 | FILE *out = xfopen (spec_verbose_file, "w"); | |
07a58c13 | 336 | |
342b8b6e AD |
337 | size_t size = obstack_object_size (&output_obstack); |
338 | fwrite (obstack_finish (&output_obstack), 1, size, out); | |
07a58c13 | 339 | |
337c5bd1 | 340 | reduce_output (out); |
0df87bb6 | 341 | conflicts_output (out); |
342b8b6e AD |
342 | |
343 | print_grammar (out); | |
344 | ||
345 | for (i = 0; i < nstates; i++) | |
346 | print_state (out, i); | |
347 | ||
348 | xfclose (out); | |
349 | } | |
350 | obstack_free (&output_obstack, NULL); | |
07a58c13 | 351 | } |