1/* Allocate input grammar variables for Bison.
2
3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2015, 2018-2019 Free
4 Software 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 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
49int required_version = 0;
50
51rule const *
52item_rule (item_number const *item)
53{
54 item_number const *sp = item;
55 while (0 <= *sp)
56 ++sp;
57 rule_number r = item_number_as_rule_number (*sp);
58 return &rules[r];
59}
60
61
62void
63item_print (item_number *item, rule const *previous_rule, FILE *out)
64{
65 rule const *r = item_rule (item);
66 rule_lhs_print (r, previous_rule ? previous_rule->lhs : NULL, out);
67
68 for (item_number *sp = r->rhs; sp < item; sp++)
69 fprintf (out, " %s", symbols[*sp]->tag);
70 fputs (" .", out);
71 if (0 <= *r->rhs)
72 for (item_number *sp = item; 0 <= *sp; ++sp)
73 fprintf (out, " %s", symbols[*sp]->tag);
74 else
75 fprintf (out, " %%empty");
76}
77
78
79bool
80rule_useful_in_grammar_p (rule const *r)
81{
82 return r->number < nrules;
83}
84
85bool
86rule_useless_in_grammar_p (rule const *r)
87{
88 return !rule_useful_in_grammar_p (r);
89}
90
91bool
92rule_useless_in_parser_p (rule const *r)
93{
94 return !r->useful && rule_useful_in_grammar_p (r);
95}
96
97bool
98rule_useless_chain_p (rule const *r)
99{
100 return rule_rhs_length (r) == 1 && !r->action;
101}
102
103void
104rule_lhs_print (rule const *r, sym_content const *previous_lhs, FILE *out)
105{
106 fprintf (out, " %3d ", r->number);
107 if (previous_lhs != r->lhs)
108 fprintf (out, "%s:", r->lhs->symbol->tag);
109 else
110 fprintf (out, "%*s|", (int) strlen (previous_lhs->symbol->tag), "");
111}
112
113void
114rule_lhs_print_xml (rule const *r, FILE *out, int level)
115{
116 xml_printf (out, level, "<lhs>%s</lhs>", r->lhs->symbol->tag);
117}
118
119size_t
120rule_rhs_length (rule const *r)
121{
122 size_t res = 0;
123 for (item_number *rhsp = r->rhs; 0 <= *rhsp; ++rhsp)
124 ++res;
125 return res;
126}
127
128void
129rule_rhs_print (rule const *r, FILE *out)
130{
131 if (0 <= *r->rhs)
132 for (item_number *rhsp = r->rhs; 0 <= *rhsp; ++rhsp)
133 fprintf (out, " %s", symbols[*rhsp]->tag);
134 else
135 fputs (" %empty", out);
136}
137
138static void
139rule_rhs_print_xml (rule const *r, FILE *out, int level)
140{
141 if (*r->rhs >= 0)
142 {
143 xml_puts (out, level, "<rhs>");
144 for (item_number *rhsp = r->rhs; 0 <= *rhsp; ++rhsp)
145 xml_printf (out, level + 1, "<symbol>%s</symbol>",
146 xml_escape (symbols[*rhsp]->tag));
147 xml_puts (out, level, "</rhs>");
148 }
149 else
150 {
151 xml_puts (out, level, "<rhs>");
152 xml_puts (out, level + 1, "<empty/>");
153 xml_puts (out, level, "</rhs>");
154 }
155}
156
157void
158rule_print (rule const *r, rule const *prev_rule, FILE *out)
159{
160 rule_lhs_print (r, prev_rule ? prev_rule->lhs : NULL, out);
161 rule_rhs_print (r, out);
162}
163
164void
165ritem_print (FILE *out)
166{
167 fputs ("RITEM\n", out);
168 for (unsigned i = 0; i < nritems; ++i)
169 if (ritem[i] >= 0)
170 fprintf (out, " %s", symbols[ritem[i]]->tag);
171 else
172 fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
173 fputs ("\n\n", out);
174}
175
176size_t
177ritem_longest_rhs (void)
178{
179 int max = 0;
180 for (rule_number r = 0; r < nrules; ++r)
181 {
182 size_t length = rule_rhs_length (&rules[r]);
183 if (length > max)
184 max = length;
185 }
186
187 return max;
188}
189
190void
191grammar_rules_partial_print (FILE *out, const char *title,
192 rule_filter filter)
193{
194 bool first = true;
195 rule *previous_rule = NULL;
196
197 /* rule # : LHS -> RHS */
198 for (rule_number r = 0; r < nrules + nuseless_productions; r++)
199 {
200 if (filter && !filter (&rules[r]))
201 continue;
202 if (first)
203 fprintf (out, "%s\n\n", title);
204 else if (previous_rule && previous_rule->lhs != rules[r].lhs)
205 fputc ('\n', out);
206 first = false;
207 rule_print (&rules[r], previous_rule, out);
208 fputc ('\n', out);
209 previous_rule = &rules[r];
210 }
211 if (!first)
212 fputs ("\n\n", out);
213}
214
215void
216grammar_rules_print (FILE *out)
217{
218 grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p);
219}
220
221void
222grammar_rules_print_xml (FILE *out, int level)
223{
224 bool first = true;
225
226 for (rule_number r = 0; r < nrules + nuseless_productions; r++)
227 {
228 if (first)
229 xml_puts (out, level + 1, "<rules>");
230 first = false;
231 {
232 char const *usefulness
233 = rule_useless_in_grammar_p (&rules[r]) ? "useless-in-grammar"
234 : rule_useless_in_parser_p (&rules[r]) ? "useless-in-parser"
235 : "useful";
236 xml_indent (out, level + 2);
237 fprintf (out, "<rule number=\"%d\" usefulness=\"%s\"",
238 rules[r].number, usefulness);
239 if (rules[r].precsym)
240 fprintf (out, " percent_prec=\"%s\"",
241 xml_escape (rules[r].precsym->symbol->tag));
242 fputs (">\n", out);
243 }
244 rule_lhs_print_xml (&rules[r], out, level + 3);
245 rule_rhs_print_xml (&rules[r], out, level + 3);
246 xml_puts (out, level + 2, "</rule>");
247 }
248 if (!first)
249 xml_puts (out, level + 1, "</rules>");
250 else
251 xml_puts (out, level + 1, "<rules/>");
252}
253
254void
255grammar_dump (FILE *out, const char *title)
256{
257 fprintf (out, "%s\n\n", title);
258 fprintf (out,
259 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
260 ntokens, nvars, nsyms, nrules, nritems);
261
262
263 fprintf (out, "Variables\n---------\n\n");
264 {
265 fprintf (out, "Value Sprec Sassoc Tag\n");
266
267 for (symbol_number i = ntokens; i < nsyms; i++)
268 fprintf (out, "%5d %5d %5d %s\n",
269 i,
270 symbols[i]->content->prec, symbols[i]->content->assoc,
271 symbols[i]->tag);
272 fprintf (out, "\n\n");
273 }
274
275 fprintf (out, "Rules\n-----\n\n");
276 {
277 fprintf (out,
278 "Num (Prec, Assoc, Useful, UselessChain) Lhs"
279 " -> (Ritem Range) Rhs\n");
280 for (rule_number i = 0; i < nrules + nuseless_productions; ++i)
281 {
282 rule const *rule_i = &rules[i];
283 unsigned const rhs_itemno = rule_i->rhs - ritem;
284 unsigned length = rule_rhs_length (rule_i);
285 aver (item_number_as_rule_number (rule_i->rhs[length] == i));
286 fprintf (out, "%3d (%2d, %2d, %2s, %2s) %2d -> (%2u-%2u)",
287 i,
288 rule_i->prec ? rule_i->prec->prec : 0,
289 rule_i->prec ? rule_i->prec->assoc : 0,
290 rule_i->useful ? "t" : "f",
291 rule_useless_chain_p (rule_i) ? "t" : "f",
292 rule_i->lhs->number,
293 rhs_itemno, rhs_itemno + length - 1);
294 /* Dumped the RHS. */
295 for (item_number *rhsp = rule_i->rhs; 0 <= *rhsp; ++rhsp)
296 fprintf (out, " %3d", *rhsp);
297 fputc ('\n', out);
298 }
299 }
300 fprintf (out, "\n\n");
301
302 fprintf (out, "Rules interpreted\n-----------------\n\n");
303 for (rule_number r = 0; r < nrules + nuseless_productions; ++r)
304 {
305 fprintf (out, "%-5d %s:", r, rules[r].lhs->symbol->tag);
306 rule_rhs_print (&rules[r], out);
307 fputc ('\n', out);
308 }
309 fprintf (out, "\n\n");
310}
311
312void
313grammar_rules_useless_report (const char *message)
314{
315 for (rule_number r = 0; r < nrules; ++r)
316 /* Don't complain about rules whose LHS is useless, we already
317 complained about it. */
318 if (!reduce_nonterminal_useless_in_grammar (rules[r].lhs)
319 && !rules[r].useful)
320 complain (&rules[r].location, Wother, "%s", message);
321}
322
323void
324grammar_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}
334