]> git.saurik.com Git - bison.git/blob - src/gram.c
options: no longer document warnings when diagnosing an invalid -W
[bison.git] / src / gram.c
1 /* Allocate input grammar variables for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2013 Free Software
4 Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
8 This program 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 3 of the License, or
11 (at your option) any later version.
12
13 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include "complain.h"
25 #include "getargs.h"
26 #include "gram.h"
27 #include "print-xml.h"
28 #include "reader.h"
29 #include "reduce.h"
30 #include "symtab.h"
31
32 /* Comments for these variables are in gram.h. */
33
34 item_number *ritem = NULL;
35 unsigned int nritems = 0;
36
37 rule *rules = NULL;
38 rule_number nrules = 0;
39
40 symbol **symbols = NULL;
41 int nsyms = 0;
42 int ntokens = 1;
43 int nvars = 0;
44
45 symbol_number *token_translations = NULL;
46
47 int max_user_token_number = 256;
48
49 bool
50 rule_useful_in_grammar_p (rule const *r)
51 {
52 return r->number < nrules;
53 }
54
55 bool
56 rule_useless_in_grammar_p (rule const *r)
57 {
58 return !rule_useful_in_grammar_p (r);
59 }
60
61 bool
62 rule_useless_in_parser_p (rule const *r)
63 {
64 return !r->useful && rule_useful_in_grammar_p (r);
65 }
66
67 void
68 rule_lhs_print (rule const *r, symbol const *previous_lhs, FILE *out)
69 {
70 fprintf (out, " %3d ", r->number);
71 if (previous_lhs != r->lhs)
72 fprintf (out, "%s:", r->lhs->tag);
73 else
74 fprintf (out, "%*s|", (int) strlen (previous_lhs->tag), "");
75 }
76
77 void
78 rule_lhs_print_xml (rule const *r, FILE *out, int level)
79 {
80 xml_printf (out, level, "<lhs>%s</lhs>", r->lhs->tag);
81 }
82
83 size_t
84 rule_rhs_length (rule const *r)
85 {
86 size_t res = 0;
87 item_number *rhsp;
88 for (rhsp = r->rhs; *rhsp >= 0; ++rhsp)
89 ++res;
90 return res;
91 }
92
93 void
94 rule_rhs_print (rule const *r, FILE *out)
95 {
96 if (*r->rhs >= 0)
97 {
98 item_number *rp;
99 for (rp = r->rhs; *rp >= 0; rp++)
100 fprintf (out, " %s", symbols[*rp]->tag);
101 }
102 else
103 {
104 fprintf (out, " /* %s */", _("empty"));
105 }
106 }
107
108 static void
109 rule_rhs_print_xml (rule const *r, FILE *out, int level)
110 {
111 if (*r->rhs >= 0)
112 {
113 item_number *rp;
114 xml_puts (out, level, "<rhs>");
115 for (rp = r->rhs; *rp >= 0; rp++)
116 xml_printf (out, level + 1, "<symbol>%s</symbol>",
117 xml_escape (symbols[*rp]->tag));
118 xml_puts (out, level, "</rhs>");
119 }
120 else
121 {
122 xml_puts (out, level, "<rhs>");
123 xml_puts (out, level + 1, "<empty/>");
124 xml_puts (out, level, "</rhs>");
125 }
126 }
127
128 static void
129 rule_print (rule const *r, FILE *out)
130 {
131 fprintf (out, "%s:", r->lhs->tag);
132 rule_rhs_print (r, out);
133 }
134
135 void
136 ritem_print (FILE *out)
137 {
138 unsigned int i;
139 fputs ("RITEM\n", out);
140 for (i = 0; i < nritems; ++i)
141 if (ritem[i] >= 0)
142 fprintf (out, " %s", symbols[ritem[i]]->tag);
143 else
144 fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
145 fputs ("\n\n", out);
146 }
147
148 size_t
149 ritem_longest_rhs (void)
150 {
151 int max = 0;
152 rule_number r;
153
154 for (r = 0; r < nrules; ++r)
155 {
156 int length = rule_rhs_length (&rules[r]);
157 if (length > max)
158 max = length;
159 }
160
161 return max;
162 }
163
164 void
165 grammar_rules_partial_print (FILE *out, const char *title,
166 rule_filter filter)
167 {
168 rule_number r;
169 bool first = true;
170 symbol *previous_lhs = NULL;
171
172 /* rule # : LHS -> RHS */
173 for (r = 0; r < nrules + nuseless_productions; r++)
174 {
175 if (filter && !filter (&rules[r]))
176 continue;
177 if (first)
178 fprintf (out, "%s\n\n", title);
179 else if (previous_lhs && previous_lhs != rules[r].lhs)
180 fputc ('\n', out);
181 first = false;
182 rule_lhs_print (&rules[r], previous_lhs, out);
183 rule_rhs_print (&rules[r], out);
184 fprintf (out, "\n");
185 previous_lhs = rules[r].lhs;
186 }
187 if (!first)
188 fputs ("\n\n", out);
189 }
190
191 void
192 grammar_rules_print (FILE *out)
193 {
194 grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p);
195 }
196
197 void
198 grammar_rules_print_xml (FILE *out, int level)
199 {
200 rule_number r;
201 bool first = true;
202
203 for (r = 0; r < nrules + nuseless_productions; r++)
204 {
205 if (first)
206 xml_puts (out, level + 1, "<rules>");
207 first = false;
208 {
209 char const *usefulness;
210 if (rule_useless_in_grammar_p (&rules[r]))
211 usefulness = "useless-in-grammar";
212 else if (rule_useless_in_parser_p (&rules[r]))
213 usefulness = "useless-in-parser";
214 else
215 usefulness = "useful";
216 xml_indent (out, level + 2);
217 fprintf (out, "<rule number=\"%d\" usefulness=\"%s\"",
218 rules[r].number, usefulness);
219 if (rules[r].precsym)
220 fprintf (out, " percent_prec=\"%s\"",
221 xml_escape (rules[r].precsym->tag));
222 fputs (">\n", out);
223 }
224 rule_lhs_print_xml (&rules[r], out, level + 3);
225 rule_rhs_print_xml (&rules[r], out, level + 3);
226 xml_puts (out, level + 2, "</rule>");
227 }
228 if (!first)
229 xml_puts (out, level + 1, "</rules>");
230 else
231 xml_puts (out, level + 1, "<rules/>");
232 }
233
234 void
235 grammar_dump (FILE *out, const char *title)
236 {
237 fprintf (out, "%s\n\n", title);
238 fprintf (out,
239 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
240 ntokens, nvars, nsyms, nrules, nritems);
241
242
243 fprintf (out, "Variables\n---------\n\n");
244 {
245 symbol_number i;
246 fprintf (out, "Value Sprec Sassoc Tag\n");
247
248 for (i = ntokens; i < nsyms; i++)
249 fprintf (out, "%5d %5d %5d %s\n",
250 i,
251 symbols[i]->prec, symbols[i]->assoc,
252 symbols[i]->tag);
253 fprintf (out, "\n\n");
254 }
255
256 fprintf (out, "Rules\n-----\n\n");
257 {
258 rule_number i;
259 fprintf (out,
260 "Num (Prec, Assoc, Useful, Ritem Range) Lhs"
261 " -> Rhs (Ritem range) [Num]\n");
262 for (i = 0; i < nrules + nuseless_productions; i++)
263 {
264 rule const *rule_i = &rules[i];
265 item_number *rp = NULL;
266 unsigned int rhs_itemno = rule_i->rhs - ritem;
267 unsigned int rhs_count = 0;
268 /* Find the last RHS index in ritems. */
269 for (rp = rule_i->rhs; *rp >= 0; ++rp)
270 ++rhs_count;
271 fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
272 i,
273 rule_i->prec ? rule_i->prec->prec : 0,
274 rule_i->prec ? rule_i->prec->assoc : 0,
275 rule_i->useful,
276 rhs_itemno,
277 rhs_itemno + rhs_count - 1,
278 rule_i->lhs->number);
279 /* Dumped the RHS. */
280 for (rp = rule_i->rhs; *rp >= 0; rp++)
281 fprintf (out, " %3d", *rp);
282 fprintf (out, " [%d]\n", item_number_as_rule_number (*rp));
283 }
284 }
285 fprintf (out, "\n\n");
286
287 fprintf (out, "Rules interpreted\n-----------------\n\n");
288 {
289 rule_number r;
290 for (r = 0; r < nrules + nuseless_productions; r++)
291 {
292 fprintf (out, "%-5d ", r);
293 rule_print (&rules[r], out);
294 fprintf (out, "\n");
295 }
296 }
297 fprintf (out, "\n\n");
298 }
299
300 void
301 grammar_rules_useless_report (const char *message)
302 {
303 warnings w = Wother;
304 if (warnings_flag & w)
305 {
306 rule_number r;
307 for (r = 0; r < nrules ; ++r)
308 if (!rules[r].useful)
309 {
310 if (feature_flag & feature_caret)
311 complain (&rules[r].location, w, "%s", message);
312 else
313 {
314 complain (&rules[r].location, w | silent, "%s: ", message);
315 rule_print (&rules[r], stderr);
316 warnings_print_categories (w);
317 fprintf (stderr, "\n");
318 }
319 }
320 }
321 }
322
323 void
324 grammar_free (void)
325 {
326 if (ritem)
327 free (ritem - 1);
328 free (rules);
329 free (token_translations);
330 /* Free the symbol table data structure. */
331 symbols_free ();
332 free_merger_functions ();
333 }