]>
Commit | Line | Data |
---|---|---|
1 | /* Allocate input grammar variables for Bison. | |
2 | ||
3 | Copyright (C) 1984, 1986, 1989, 2001, 2002, 2003 Free Software | |
4 | Foundation, Inc. | |
5 | ||
6 | This file is part of Bison, the GNU Compiler Compiler. | |
7 | ||
8 | Bison is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | Bison is distributed in the hope that it will be useful, | |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with Bison; see the file COPYING. If not, write to | |
20 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
21 | Boston, MA 02111-1307, USA. */ | |
22 | ||
23 | ||
24 | #include "system.h" | |
25 | ||
26 | #include <quotearg.h> | |
27 | ||
28 | #include "gram.h" | |
29 | #include "reader.h" | |
30 | #include "reduce.h" | |
31 | #include "symtab.h" | |
32 | ||
33 | /* Comments for these variables are in gram.h. */ | |
34 | ||
35 | item_number *ritem = NULL; | |
36 | unsigned int nritems = 0; | |
37 | ||
38 | rule *rules = NULL; | |
39 | rule_number nrules = 0; | |
40 | ||
41 | symbol **symbols = NULL; | |
42 | int nsyms = 0; | |
43 | int ntokens = 1; | |
44 | int nvars = 0; | |
45 | ||
46 | symbol_number *token_translations = NULL; | |
47 | ||
48 | int max_user_token_number = 256; | |
49 | ||
50 | int glr_parser = 0; | |
51 | int pure_parser = 0; | |
52 | ||
53 | ||
54 | /*--------------------------------------------------------------. | |
55 | | Return true IFF the rule has a `number' smaller than NRULES. | | |
56 | `--------------------------------------------------------------*/ | |
57 | ||
58 | bool | |
59 | rule_useful_p (rule *r) | |
60 | { | |
61 | return r->number < nrules; | |
62 | } | |
63 | ||
64 | ||
65 | /*-------------------------------------------------------------. | |
66 | | Return true IFF the rule has a `number' higher than NRULES. | | |
67 | `-------------------------------------------------------------*/ | |
68 | ||
69 | bool | |
70 | rule_useless_p (rule *r) | |
71 | { | |
72 | return r->number >= nrules; | |
73 | } | |
74 | ||
75 | ||
76 | /*--------------------------------------------------------------------. | |
77 | | Return true IFF the rule is not flagged as useful *and* is useful. | | |
78 | | In other words, it was discarded because of conflicts. | | |
79 | `--------------------------------------------------------------------*/ | |
80 | ||
81 | bool | |
82 | rule_never_reduced_p (rule *r) | |
83 | { | |
84 | return !r->useful && r->number < nrules; | |
85 | } | |
86 | ||
87 | ||
88 | /*----------------------------------------------------------------. | |
89 | | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was | | |
90 | | already displayed (by a previous call for another rule), avoid | | |
91 | | useless repetitions. | | |
92 | `----------------------------------------------------------------*/ | |
93 | ||
94 | void | |
95 | rule_lhs_print (rule *r, symbol *previous_lhs, FILE *out) | |
96 | { | |
97 | fprintf (out, " %3d ", r->number); | |
98 | if (previous_lhs != r->lhs) | |
99 | { | |
100 | fprintf (out, "%s:", r->lhs->tag); | |
101 | } | |
102 | else | |
103 | { | |
104 | int n; | |
105 | for (n = strlen (previous_lhs->tag); n > 0; --n) | |
106 | fputc (' ', out); | |
107 | fputc ('|', out); | |
108 | } | |
109 | } | |
110 | ||
111 | ||
112 | /*--------------------------------------. | |
113 | | Return the number of symbols in RHS. | | |
114 | `--------------------------------------*/ | |
115 | ||
116 | int | |
117 | rule_rhs_length (rule *r) | |
118 | { | |
119 | int res = 0; | |
120 | item_number *rhsp; | |
121 | for (rhsp = r->rhs; *rhsp >= 0; ++rhsp) | |
122 | ++res; | |
123 | return res; | |
124 | } | |
125 | ||
126 | ||
127 | /*-------------------------------. | |
128 | | Print this rule's RHS on OUT. | | |
129 | `-------------------------------*/ | |
130 | ||
131 | void | |
132 | rule_rhs_print (rule *r, FILE *out) | |
133 | { | |
134 | if (*r->rhs >= 0) | |
135 | { | |
136 | item_number *rp; | |
137 | for (rp = r->rhs; *rp >= 0; rp++) | |
138 | fprintf (out, " %s", symbols[*rp]->tag); | |
139 | fputc ('\n', out); | |
140 | } | |
141 | else | |
142 | { | |
143 | fprintf (out, " /* %s */\n", _("empty")); | |
144 | } | |
145 | } | |
146 | ||
147 | ||
148 | /*-------------------------. | |
149 | | Print this rule on OUT. | | |
150 | `-------------------------*/ | |
151 | ||
152 | void | |
153 | rule_print (rule *r, FILE *out) | |
154 | { | |
155 | fprintf (out, "%s:", r->lhs->tag); | |
156 | rule_rhs_print (r, out); | |
157 | } | |
158 | ||
159 | ||
160 | /*------------------------. | |
161 | | Dump RITEM for traces. | | |
162 | `------------------------*/ | |
163 | ||
164 | void | |
165 | ritem_print (FILE *out) | |
166 | { | |
167 | unsigned int i; | |
168 | fputs ("RITEM\n", out); | |
169 | for (i = 0; i < nritems; ++i) | |
170 | if (ritem[i] >= 0) | |
171 | fprintf (out, " %s", symbols[ritem[i]]->tag); | |
172 | else | |
173 | fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i])); | |
174 | fputs ("\n\n", out); | |
175 | } | |
176 | ||
177 | ||
178 | /*------------------------------------------. | |
179 | | Return the size of the longest rule RHS. | | |
180 | `------------------------------------------*/ | |
181 | ||
182 | size_t | |
183 | ritem_longest_rhs (void) | |
184 | { | |
185 | int max = 0; | |
186 | rule_number r; | |
187 | ||
188 | for (r = 0; r < nrules; ++r) | |
189 | { | |
190 | int length = rule_rhs_length (&rules[r]); | |
191 | if (length > max) | |
192 | max = length; | |
193 | } | |
194 | ||
195 | return max; | |
196 | } | |
197 | ||
198 | ||
199 | /*-----------------------------------------------------------------. | |
200 | | Print the grammar's rules that match FILTER on OUT under TITLE. | | |
201 | `-----------------------------------------------------------------*/ | |
202 | ||
203 | void | |
204 | grammar_rules_partial_print (FILE *out, const char *title, | |
205 | rule_filter filter) | |
206 | { | |
207 | rule_number r; | |
208 | bool first = true; | |
209 | symbol *previous_lhs = NULL; | |
210 | ||
211 | /* rule # : LHS -> RHS */ | |
212 | for (r = 0; r < nrules + nuseless_productions; r++) | |
213 | { | |
214 | if (filter && !filter (&rules[r])) | |
215 | continue; | |
216 | if (first) | |
217 | fprintf (out, "%s\n\n", title); | |
218 | else if (previous_lhs && previous_lhs != rules[r].lhs) | |
219 | fputc ('\n', out); | |
220 | first = false; | |
221 | rule_lhs_print (&rules[r], previous_lhs, out); | |
222 | rule_rhs_print (&rules[r], out); | |
223 | previous_lhs = rules[r].lhs; | |
224 | } | |
225 | if (!first) | |
226 | fputs ("\n\n", out); | |
227 | } | |
228 | ||
229 | ||
230 | /*------------------------------------------. | |
231 | | Print the grammar's useful rules on OUT. | | |
232 | `------------------------------------------*/ | |
233 | ||
234 | void | |
235 | grammar_rules_print (FILE *out) | |
236 | { | |
237 | grammar_rules_partial_print (out, _("Grammar"), rule_useful_p); | |
238 | } | |
239 | ||
240 | ||
241 | /*-------------------. | |
242 | | Dump the grammar. | | |
243 | `-------------------*/ | |
244 | ||
245 | void | |
246 | grammar_dump (FILE *out, const char *title) | |
247 | { | |
248 | fprintf (out, "%s\n\n", title); | |
249 | fprintf (out, | |
250 | "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n", | |
251 | ntokens, nvars, nsyms, nrules, nritems); | |
252 | ||
253 | ||
254 | fprintf (out, "Variables\n---------\n\n"); | |
255 | { | |
256 | symbol_number i; | |
257 | fprintf (out, "Value Sprec Sassoc Tag\n"); | |
258 | ||
259 | for (i = ntokens; i < nsyms; i++) | |
260 | fprintf (out, "%5d %5d %5d %s\n", | |
261 | i, | |
262 | symbols[i]->prec, symbols[i]->assoc, | |
263 | symbols[i]->tag); | |
264 | fprintf (out, "\n\n"); | |
265 | } | |
266 | ||
267 | fprintf (out, "Rules\n-----\n\n"); | |
268 | { | |
269 | rule_number i; | |
270 | fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n"); | |
271 | for (i = 0; i < nrules + nuseless_productions; i++) | |
272 | { | |
273 | rule *rule_i = &rules[i]; | |
274 | item_number *rp = NULL; | |
275 | unsigned int rhs_itemno = rule_i->rhs - ritem; | |
276 | unsigned int rhs_count = 0; | |
277 | /* Find the last RHS index in ritems. */ | |
278 | for (rp = rule_i->rhs; *rp >= 0; ++rp) | |
279 | ++rhs_count; | |
280 | fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->", | |
281 | i, | |
282 | rule_i->prec ? rule_i->prec->prec : 0, | |
283 | rule_i->prec ? rule_i->prec->assoc : 0, | |
284 | rule_i->useful, | |
285 | rhs_itemno, | |
286 | rhs_itemno + rhs_count - 1, | |
287 | rule_i->lhs->number); | |
288 | /* Dumped the RHS. */ | |
289 | for (rp = rule_i->rhs; *rp >= 0; rp++) | |
290 | fprintf (out, " %3d", *rp); | |
291 | fprintf (out, " [%d]\n", item_number_as_rule_number (*rp)); | |
292 | } | |
293 | } | |
294 | fprintf (out, "\n\n"); | |
295 | ||
296 | fprintf (out, "Rules interpreted\n-----------------\n\n"); | |
297 | { | |
298 | rule_number r; | |
299 | for (r = 0; r < nrules + nuseless_productions; r++) | |
300 | { | |
301 | fprintf (out, "%-5d ", r); | |
302 | rule_print (&rules[r], out); | |
303 | } | |
304 | } | |
305 | fprintf (out, "\n\n"); | |
306 | } | |
307 | ||
308 | ||
309 | /*------------------------------------------------------------------. | |
310 | | Report on STDERR the rules that are not flagged USEFUL, using the | | |
311 | | MESSAGE (which can be `useless rule' when invoked after grammar | | |
312 | | reduction, or `never reduced' after conflicts were taken into | | |
313 | | account). | | |
314 | `------------------------------------------------------------------*/ | |
315 | ||
316 | void | |
317 | grammar_rules_never_reduced_report (const char *message) | |
318 | { | |
319 | rule_number r; | |
320 | for (r = 0; r < nrules ; ++r) | |
321 | if (!rules[r].useful) | |
322 | { | |
323 | location_print (stderr, rules[r].location); | |
324 | fprintf (stderr, ": %s: %s: ", | |
325 | _("warning"), message); | |
326 | rule_print (&rules[r], stderr); | |
327 | } | |
328 | } | |
329 | ||
330 | void | |
331 | grammar_free (void) | |
332 | { | |
333 | XFREE (ritem); | |
334 | free (rules); | |
335 | XFREE (token_translations); | |
336 | /* Free the symbol table data structure. */ | |
337 | symbols_free (); | |
338 | free_merger_functions (); | |
339 | } |