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