]> git.saurik.com Git - bison.git/blame_incremental - src/gram.c
style: use %code for local function declarations in our parser
[bison.git] / src / gram.c
... / ...
CommitLineData
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
34item_number *ritem = NULL;
35unsigned int nritems = 0;
36
37rule *rules = NULL;
38rule_number nrules = 0;
39
40symbol **symbols = NULL;
41int nsyms = 0;
42int ntokens = 1;
43int nvars = 0;
44
45symbol_number *token_translations = NULL;
46
47int max_user_token_number = 256;
48
49bool
50rule_useful_in_grammar_p (rule const *r)
51{
52 return r->number < nrules;
53}
54
55bool
56rule_useless_in_grammar_p (rule const *r)
57{
58 return !rule_useful_in_grammar_p (r);
59}
60
61bool
62rule_useless_in_parser_p (rule const *r)
63{
64 return !r->useful && rule_useful_in_grammar_p (r);
65}
66
67void
68rule_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
77void
78rule_lhs_print_xml (rule const *r, FILE *out, int level)
79{
80 xml_printf (out, level, "<lhs>%s</lhs>", r->lhs->tag);
81}
82
83size_t
84rule_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
93void
94rule_rhs_print (rule const *r, FILE *out)
95{
96 if (0 <= *r->rhs)
97 {
98 item_number *rp;
99 for (rp = r->rhs; *rp >= 0; rp++)
100 fprintf (out, " %s", symbols[*rp]->tag);
101 }
102 else
103 fputs (" %empty", out);
104}
105
106static void
107rule_rhs_print_xml (rule const *r, FILE *out, int level)
108{
109 if (*r->rhs >= 0)
110 {
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>");
117 }
118 else
119 {
120 xml_puts (out, level, "<rhs>");
121 xml_puts (out, level + 1, "<empty/>");
122 xml_puts (out, level, "</rhs>");
123 }
124}
125
126void
127ritem_print (FILE *out)
128{
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);
137}
138
139size_t
140ritem_longest_rhs (void)
141{
142 int max = 0;
143 rule_number r;
144
145 for (r = 0; r < nrules; ++r)
146 {
147 int length = rule_rhs_length (&rules[r]);
148 if (length > max)
149 max = length;
150 }
151
152 return max;
153}
154
155void
156grammar_rules_partial_print (FILE *out, const char *title,
157 rule_filter filter)
158{
159 rule_number r;
160 bool first = true;
161 symbol *previous_lhs = NULL;
162
163 /* rule # : LHS -> RHS */
164 for (r = 0; r < nrules + nuseless_productions; r++)
165 {
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;
177 }
178 if (!first)
179 fputs ("\n\n", out);
180}
181
182void
183grammar_rules_print (FILE *out)
184{
185 grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p);
186}
187
188void
189grammar_rules_print_xml (FILE *out, int level)
190{
191 rule_number r;
192 bool first = true;
193
194 for (r = 0; r < nrules + nuseless_productions; r++)
195 {
196 if (first)
197 xml_puts (out, level + 1, "<rules>");
198 first = false;
199 {
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->tag));
213 fputs (">\n", out);
214 }
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>");
218 }
219 if (!first)
220 xml_puts (out, level + 1, "</rules>");
221 else
222 xml_puts (out, level + 1, "<rules/>");
223}
224
225void
226grammar_dump (FILE *out, const char *title)
227{
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);
232
233
234 fprintf (out, "Variables\n---------\n\n");
235 {
236 symbol_number i;
237 fprintf (out, "Value Sprec Sassoc Tag\n");
238
239 for (i = ntokens; i < nsyms; i++)
240 fprintf (out, "%5d %5d %5d %s\n",
241 i,
242 symbols[i]->prec, symbols[i]->assoc,
243 symbols[i]->tag);
244 fprintf (out, "\n\n");
245 }
246
247 fprintf (out, "Rules\n-----\n\n");
248 {
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++)
254 {
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 ->",
263 i,
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));
274 }
275 }
276 fprintf (out, "\n\n");
277
278 fprintf (out, "Rules interpreted\n-----------------\n\n");
279 {
280 rule_number r;
281 for (r = 0; r < nrules + nuseless_productions; r++)
282 {
283 fprintf (out, "%-5d %s:", r, rules[r].lhs->tag);
284 rule_rhs_print (&rules[r], out);
285 fprintf (out, "\n");
286 }
287 }
288 fprintf (out, "\n\n");
289}
290
291void
292grammar_rules_useless_report (const char *message)
293{
294 rule_number r;
295 for (r = 0; r < nrules ; ++r)
296 if (!rules[r].useful)
297 complain (&rules[r].location, Wother, "%s", message);
298}
299
300void
301grammar_free (void)
302{
303 if (ritem)
304 free (ritem - 1);
305 free (rules);
306 free (token_translations);
307 /* Free the symbol table data structure. */
308 symbols_free ();
309 free_merger_functions ();
310}