]> git.saurik.com Git - bison.git/blame - src/gram.c
style: move argument handling of -W into the diagnostics module
[bison.git] / src / gram.c
CommitLineData
6f84e9ab
PE
1/* Allocate input grammar variables for Bison.
2
7d6bad19 3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2013 Free Software
575619af 4 Foundation, Inc.
f7d4d87a 5
076ab033 6 This file is part of Bison, the GNU Compiler Compiler.
f7d4d87a 7
f16b0819 8 This program is free software: you can redistribute it and/or modify
076ab033 9 it under the terms of the GNU General Public License as published by
f16b0819
PE
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
f7d4d87a 12
f16b0819 13 This program is distributed in the hope that it will be useful,
076ab033
AD
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.
f7d4d87a 17
076ab033 18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
f7d4d87a 20
2cec9080 21#include <config.h>
4a120d45 22#include "system.h"
81ebdef9 23
2bfcac9a 24#include "complain.h"
c39014ae 25#include "getargs.h"
78ab8f67 26#include "gram.h"
2bfcac9a 27#include "print-xml.h"
3067fbef 28#include "reader.h"
81ebdef9
PE
29#include "reduce.h"
30#include "symtab.h"
4a120d45 31
6b98e4b5 32/* Comments for these variables are in gram.h. */
f7d4d87a 33
81ebdef9 34item_number *ritem = NULL;
0c2d3f4c 35unsigned int nritems = 0;
75142d45 36
81ebdef9
PE
37rule *rules = NULL;
38rule_number nrules = 0;
0e78e603 39
81ebdef9 40symbol **symbols = NULL;
5123689b
AD
41int nsyms = 0;
42int ntokens = 1;
43int nvars = 0;
44
81ebdef9 45symbol_number *token_translations = NULL;
f7d4d87a 46
280a38c3 47int max_user_token_number = 256;
f7d4d87a 48
c8f002c7 49bool
510c8497 50rule_useful_in_grammar_p (rule const *r)
c8f002c7
AD
51{
52 return r->number < nrules;
53}
54
c8f002c7 55bool
510c8497 56rule_useless_in_grammar_p (rule const *r)
c8f002c7 57{
cff03fb2 58 return !rule_useful_in_grammar_p (r);
c8f002c7
AD
59}
60
c8f002c7 61bool
510c8497 62rule_useless_in_parser_p (rule const *r)
c8f002c7 63{
cff03fb2 64 return !r->useful && rule_useful_in_grammar_p (r);
c8f002c7
AD
65}
66
ce4ccb4b 67void
510c8497 68rule_lhs_print (rule const *r, symbol const *previous_lhs, FILE *out)
ce4ccb4b 69{
81ebdef9
PE
70 fprintf (out, " %3d ", r->number);
71 if (previous_lhs != r->lhs)
fd7f0289 72 fprintf (out, "%s:", r->lhs->tag);
ce4ccb4b 73 else
fd7f0289 74 fprintf (out, "%*s|", (int) strlen (previous_lhs->tag), "");
ce4ccb4b
AD
75}
76
41d7a5f2 77void
510c8497 78rule_lhs_print_xml (rule const *r, FILE *out, int level)
41d7a5f2
PE
79{
80 xml_printf (out, level, "<lhs>%s</lhs>", r->lhs->tag);
81}
82
08c81469 83size_t
510c8497 84rule_rhs_length (rule const *r)
c3b407f4 85{
08c81469 86 size_t res = 0;
81ebdef9
PE
87 item_number *rhsp;
88 for (rhsp = r->rhs; *rhsp >= 0; ++rhsp)
c3b407f4
AD
89 ++res;
90 return res;
91}
92
6b98e4b5 93void
510c8497 94rule_rhs_print (rule const *r, FILE *out)
6b98e4b5 95{
81ebdef9 96 if (*r->rhs >= 0)
6b98e4b5 97 {
81ebdef9
PE
98 item_number *rp;
99 for (rp = r->rhs; *rp >= 0; rp++)
e9690142 100 fprintf (out, " %s", symbols[*rp]->tag);
6b98e4b5
AD
101 }
102 else
103 {
3e153163 104 fprintf (out, " /* %s */", _("empty"));
6b98e4b5
AD
105 }
106}
107
41d7a5f2 108static void
510c8497 109rule_rhs_print_xml (rule const *r, FILE *out, int level)
41d7a5f2
PE
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++)
e9690142
JD
116 xml_printf (out, level + 1, "<symbol>%s</symbol>",
117 xml_escape (symbols[*rp]->tag));
41d7a5f2
PE
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}
6b98e4b5 127
cbbe7505 128void
3067fbef 129ritem_print (FILE *out)
f7d4d87a 130{
0c2d3f4c 131 unsigned int i;
3067fbef 132 fputs ("RITEM\n", out);
75142d45
AD
133 for (i = 0; i < nritems; ++i)
134 if (ritem[i] >= 0)
97650f4e 135 fprintf (out, " %s", symbols[ritem[i]]->tag);
3067fbef 136 else
4b3d3a8e 137 fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
3067fbef 138 fputs ("\n\n", out);
f7d4d87a 139}
c2713865 140
c2713865
AD
141size_t
142ritem_longest_rhs (void)
143{
c3b407f4 144 int max = 0;
81ebdef9 145 rule_number r;
c2713865 146
4b3d3a8e 147 for (r = 0; r < nrules; ++r)
c3b407f4 148 {
9222837b 149 int length = rule_rhs_length (&rules[r]);
c3b407f4 150 if (length > max)
e9690142 151 max = length;
c3b407f4 152 }
c2713865
AD
153
154 return max;
155}
78ab8f67 156
6b98e4b5 157void
9757c359 158grammar_rules_partial_print (FILE *out, const char *title,
e9690142 159 rule_filter filter)
6b98e4b5 160{
a737b216 161 rule_number r;
637c4b28 162 bool first = true;
81ebdef9 163 symbol *previous_lhs = NULL;
6b98e4b5
AD
164
165 /* rule # : LHS -> RHS */
c8f002c7 166 for (r = 0; r < nrules + nuseless_productions; r++)
6b98e4b5 167 {
c8f002c7 168 if (filter && !filter (&rules[r]))
e9690142 169 continue;
c8f002c7 170 if (first)
e9690142 171 fprintf (out, "%s\n\n", title);
c8f002c7 172 else if (previous_lhs && previous_lhs != rules[r].lhs)
e9690142 173 fputc ('\n', out);
637c4b28 174 first = false;
ce4ccb4b 175 rule_lhs_print (&rules[r], previous_lhs, out);
6b98e4b5 176 rule_rhs_print (&rules[r], out);
3e153163 177 fprintf (out, "\n");
ce4ccb4b 178 previous_lhs = rules[r].lhs;
6b98e4b5 179 }
c8f002c7
AD
180 if (!first)
181 fputs ("\n\n", out);
6b98e4b5
AD
182}
183
41d7a5f2 184void
d80fb37a
JD
185grammar_rules_print (FILE *out)
186{
187 grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p);
188}
189
190void
191grammar_rules_print_xml (FILE *out, int level)
41d7a5f2
PE
192{
193 rule_number r;
194 bool first = true;
195
196 for (r = 0; r < nrules + nuseless_productions; r++)
197 {
d80fb37a 198 if (first)
e9690142 199 xml_puts (out, level + 1, "<rules>");
41d7a5f2 200 first = false;
d80fb37a
JD
201 {
202 char const *usefulness;
203 if (rule_useless_in_grammar_p (&rules[r]))
204 usefulness = "useless-in-grammar";
205 else if (rule_useless_in_parser_p (&rules[r]))
206 usefulness = "useless-in-parser";
207 else
208 usefulness = "useful";
408476bc
JD
209 xml_indent (out, level + 2);
210 fprintf (out, "<rule number=\"%d\" usefulness=\"%s\"",
211 rules[r].number, usefulness);
212 if (rules[r].precsym)
44bb9084
AD
213 fprintf (out, " percent_prec=\"%s\"",
214 xml_escape (rules[r].precsym->tag));
408476bc 215 fputs (">\n", out);
d80fb37a 216 }
41d7a5f2
PE
217 rule_lhs_print_xml (&rules[r], out, level + 3);
218 rule_rhs_print_xml (&rules[r], out, level + 3);
219 xml_puts (out, level + 2, "</rule>");
220 }
d80fb37a
JD
221 if (!first)
222 xml_puts (out, level + 1, "</rules>");
223 else
224 xml_puts (out, level + 1, "<rules/>");
41d7a5f2
PE
225}
226
78ab8f67
AD
227void
228grammar_dump (FILE *out, const char *title)
229{
78ab8f67
AD
230 fprintf (out, "%s\n\n", title);
231 fprintf (out,
e9690142
JD
232 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
233 ntokens, nvars, nsyms, nrules, nritems);
9222837b
AD
234
235
2f4f028d 236 fprintf (out, "Variables\n---------\n\n");
9222837b 237 {
81ebdef9 238 symbol_number i;
2f4f028d
PE
239 fprintf (out, "Value Sprec Sassoc Tag\n");
240
9222837b
AD
241 for (i = ntokens; i < nsyms; i++)
242 fprintf (out, "%5d %5d %5d %s\n",
e9690142
JD
243 i,
244 symbols[i]->prec, symbols[i]->assoc,
245 symbols[i]->tag);
2f4f028d 246 fprintf (out, "\n\n");
9222837b
AD
247 }
248
2f4f028d 249 fprintf (out, "Rules\n-----\n\n");
9222837b 250 {
81ebdef9 251 rule_number i;
510c8497
AD
252 fprintf (out,
253 "Num (Prec, Assoc, Useful, Ritem Range) Lhs"
254 " -> Rhs (Ritem range) [Num]\n");
4b3d3a8e 255 for (i = 0; i < nrules + nuseless_productions; i++)
9222837b 256 {
510c8497 257 rule const *rule_i = &rules[i];
e9690142
JD
258 item_number *rp = NULL;
259 unsigned int rhs_itemno = rule_i->rhs - ritem;
260 unsigned int rhs_count = 0;
261 /* Find the last RHS index in ritems. */
262 for (rp = rule_i->rhs; *rp >= 0; ++rp)
263 ++rhs_count;
264 fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
265 i,
266 rule_i->prec ? rule_i->prec->prec : 0,
267 rule_i->prec ? rule_i->prec->assoc : 0,
268 rule_i->useful,
269 rhs_itemno,
270 rhs_itemno + rhs_count - 1,
271 rule_i->lhs->number);
272 /* Dumped the RHS. */
273 for (rp = rule_i->rhs; *rp >= 0; rp++)
274 fprintf (out, " %3d", *rp);
275 fprintf (out, " [%d]\n", item_number_as_rule_number (*rp));
9222837b
AD
276 }
277 }
2f4f028d 278 fprintf (out, "\n\n");
9222837b 279
2f4f028d 280 fprintf (out, "Rules interpreted\n-----------------\n\n");
9222837b 281 {
81ebdef9 282 rule_number r;
4b3d3a8e 283 for (r = 0; r < nrules + nuseless_productions; r++)
9222837b 284 {
fec5f3c0
AD
285 fprintf (out, "%-5d %s:", r, rules[r].lhs->tag);
286 rule_rhs_print (&rules[r], out);
3e153163 287 fprintf (out, "\n");
9222837b
AD
288 }
289 }
2f4f028d 290 fprintf (out, "\n\n");
78ab8f67 291}
5372019f 292
c8f002c7 293void
cff03fb2 294grammar_rules_useless_report (const char *message)
c8f002c7 295{
fec5f3c0
AD
296 rule_number r;
297 for (r = 0; r < nrules ; ++r)
298 if (!rules[r].useful)
299 complain (&rules[r].location, Wother, "%s", message);
c8f002c7
AD
300}
301
5372019f
AD
302void
303grammar_free (void)
304{
e9ad4aec
PE
305 if (ritem)
306 free (ritem - 1);
4b3d3a8e 307 free (rules);
afbb696d 308 free (token_translations);
5372019f
AD
309 /* Free the symbol table data structure. */
310 symbols_free ();
676385e2 311 free_merger_functions ();
5372019f 312}