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