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