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