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