style: reduce scopes
[bison.git] / src / gram.c
blobfb2593e2a244d4a081b8c34f344a37e34de573d8
1 /* Allocate input grammar variables for Bison.
3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2015, 2018 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
21 #include <config.h>
22 #include "system.h"
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"
32 /* Comments for these variables are in gram.h. */
34 item_number *ritem = NULL;
35 unsigned int nritems = 0;
37 rule *rules = NULL;
38 rule_number nrules = 0;
40 symbol **symbols = NULL;
41 int nsyms = 0;
42 int ntokens = 1;
43 int nvars = 0;
45 symbol_number *token_translations = NULL;
47 int max_user_token_number = 256;
49 bool
50 rule_useful_in_grammar_p (rule const *r)
52 return r->number < nrules;
55 bool
56 rule_useless_in_grammar_p (rule const *r)
58 return !rule_useful_in_grammar_p (r);
61 bool
62 rule_useless_in_parser_p (rule const *r)
64 return !r->useful && rule_useful_in_grammar_p (r);
67 void
68 rule_lhs_print (rule const *r, sym_content const *previous_lhs, FILE *out)
70 fprintf (out, " %3d ", r->number);
71 if (previous_lhs != r->lhs)
72 fprintf (out, "%s:", r->lhs->symbol->tag);
73 else
74 fprintf (out, "%*s|", (int) strlen (previous_lhs->symbol->tag), "");
77 void
78 rule_lhs_print_xml (rule const *r, FILE *out, int level)
80 xml_printf (out, level, "<lhs>%s</lhs>", r->lhs->symbol->tag);
83 size_t
84 rule_rhs_length (rule const *r)
86 size_t res = 0;
87 item_number *rhsp;
88 for (rhsp = r->rhs; *rhsp >= 0; ++rhsp)
89 ++res;
90 return res;
93 void
94 rule_rhs_print (rule const *r, FILE *out)
96 if (0 <= *r->rhs)
98 item_number *rp;
99 for (rp = r->rhs; *rp >= 0; rp++)
100 fprintf (out, " %s", symbols[*rp]->tag);
102 else
103 fputs (" %empty", out);
106 static void
107 rule_rhs_print_xml (rule const *r, FILE *out, int level)
109 if (*r->rhs >= 0)
111 item_number *rp;
112 xml_puts (out, level, "<rhs>");
113 for (rp = r->rhs; *rp >= 0; rp++)
114 xml_printf (out, level + 1, "<symbol>%s</symbol>",
115 xml_escape (symbols[*rp]->tag));
116 xml_puts (out, level, "</rhs>");
118 else
120 xml_puts (out, level, "<rhs>");
121 xml_puts (out, level + 1, "<empty/>");
122 xml_puts (out, level, "</rhs>");
126 void
127 ritem_print (FILE *out)
129 unsigned int i;
130 fputs ("RITEM\n", out);
131 for (i = 0; i < nritems; ++i)
132 if (ritem[i] >= 0)
133 fprintf (out, " %s", symbols[ritem[i]]->tag);
134 else
135 fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
136 fputs ("\n\n", out);
139 size_t
140 ritem_longest_rhs (void)
142 int max = 0;
143 rule_number r;
145 for (r = 0; r < nrules; ++r)
147 int length = rule_rhs_length (&rules[r]);
148 if (length > max)
149 max = length;
152 return max;
155 void
156 grammar_rules_partial_print (FILE *out, const char *title,
157 rule_filter filter)
159 rule_number r;
160 bool first = true;
161 sym_content *previous_lhs = NULL;
163 /* rule # : LHS -> RHS */
164 for (r = 0; r < nrules + nuseless_productions; r++)
166 if (filter && !filter (&rules[r]))
167 continue;
168 if (first)
169 fprintf (out, "%s\n\n", title);
170 else if (previous_lhs && previous_lhs != rules[r].lhs)
171 fputc ('\n', out);
172 first = false;
173 rule_lhs_print (&rules[r], previous_lhs, out);
174 rule_rhs_print (&rules[r], out);
175 fprintf (out, "\n");
176 previous_lhs = rules[r].lhs;
178 if (!first)
179 fputs ("\n\n", out);
182 void
183 grammar_rules_print (FILE *out)
185 grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p);
188 void
189 grammar_rules_print_xml (FILE *out, int level)
191 rule_number r;
192 bool first = true;
194 for (r = 0; r < nrules + nuseless_productions; r++)
196 if (first)
197 xml_puts (out, level + 1, "<rules>");
198 first = false;
200 char const *usefulness;
201 if (rule_useless_in_grammar_p (&rules[r]))
202 usefulness = "useless-in-grammar";
203 else if (rule_useless_in_parser_p (&rules[r]))
204 usefulness = "useless-in-parser";
205 else
206 usefulness = "useful";
207 xml_indent (out, level + 2);
208 fprintf (out, "<rule number=\"%d\" usefulness=\"%s\"",
209 rules[r].number, usefulness);
210 if (rules[r].precsym)
211 fprintf (out, " percent_prec=\"%s\"",
212 xml_escape (rules[r].precsym->symbol->tag));
213 fputs (">\n", out);
215 rule_lhs_print_xml (&rules[r], out, level + 3);
216 rule_rhs_print_xml (&rules[r], out, level + 3);
217 xml_puts (out, level + 2, "</rule>");
219 if (!first)
220 xml_puts (out, level + 1, "</rules>");
221 else
222 xml_puts (out, level + 1, "<rules/>");
225 void
226 grammar_dump (FILE *out, const char *title)
228 fprintf (out, "%s\n\n", title);
229 fprintf (out,
230 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
231 ntokens, nvars, nsyms, nrules, nritems);
234 fprintf (out, "Variables\n---------\n\n");
236 symbol_number i;
237 fprintf (out, "Value Sprec Sassoc Tag\n");
239 for (i = ntokens; i < nsyms; i++)
240 fprintf (out, "%5d %5d %5d %s\n",
242 symbols[i]->content->prec, symbols[i]->content->assoc,
243 symbols[i]->tag);
244 fprintf (out, "\n\n");
247 fprintf (out, "Rules\n-----\n\n");
249 rule_number i;
250 fprintf (out,
251 "Num (Prec, Assoc, Useful, Ritem Range) Lhs"
252 " -> Rhs (Ritem range) [Num]\n");
253 for (i = 0; i < nrules + nuseless_productions; i++)
255 rule const *rule_i = &rules[i];
256 item_number *rp = NULL;
257 unsigned int rhs_itemno = rule_i->rhs - ritem;
258 unsigned int rhs_count = 0;
259 /* Find the last RHS index in ritems. */
260 for (rp = rule_i->rhs; *rp >= 0; ++rp)
261 ++rhs_count;
262 fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
264 rule_i->prec ? rule_i->prec->prec : 0,
265 rule_i->prec ? rule_i->prec->assoc : 0,
266 rule_i->useful,
267 rhs_itemno,
268 rhs_itemno + rhs_count - 1,
269 rule_i->lhs->number);
270 /* Dumped the RHS. */
271 for (rp = rule_i->rhs; *rp >= 0; rp++)
272 fprintf (out, " %3d", *rp);
273 fprintf (out, " [%d]\n", item_number_as_rule_number (*rp));
276 fprintf (out, "\n\n");
278 fprintf (out, "Rules interpreted\n-----------------\n\n");
280 rule_number r;
281 for (r = 0; r < nrules + nuseless_productions; r++)
283 fprintf (out, "%-5d %s:", r, rules[r].lhs->symbol->tag);
284 rule_rhs_print (&rules[r], out);
285 fprintf (out, "\n");
288 fprintf (out, "\n\n");
291 void
292 grammar_rules_useless_report (const char *message)
294 rule_number r;
295 for (r = 0; r < nrules ; ++r)
296 /* Don't complain about rules whose LHS is useless, we already
297 complained about it. */
298 if (!reduce_nonterminal_useless_in_grammar (rules[r].lhs)
299 && !rules[r].useful)
300 complain (&rules[r].location, Wother, "%s", message);
303 void
304 grammar_free (void)
306 if (ritem)
307 free (ritem - 1);
308 free (rules);
309 free (token_translations);
310 /* Free the symbol table data structure. */
311 symbols_free ();
312 free_merger_functions ();