]> git.saurik.com Git - bison.git/blob - src/gram.c
parser: use %empty
[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 void
129 ritem_print (FILE *out)
130 {
131 unsigned int i;
132 fputs ("RITEM\n", out);
133 for (i = 0; i < nritems; ++i)
134 if (ritem[i] >= 0)
135 fprintf (out, " %s", symbols[ritem[i]]->tag);
136 else
137 fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
138 fputs ("\n\n", out);
139 }
140
141 size_t
142 ritem_longest_rhs (void)
143 {
144 int max = 0;
145 rule_number r;
146
147 for (r = 0; r < nrules; ++r)
148 {
149 int length = rule_rhs_length (&rules[r]);
150 if (length > max)
151 max = length;
152 }
153
154 return max;
155 }
156
157 void
158 grammar_rules_partial_print (FILE *out, const char *title,
159 rule_filter filter)
160 {
161 rule_number r;
162 bool first = true;
163 symbol *previous_lhs = NULL;
164
165 /* rule # : LHS -> RHS */
166 for (r = 0; r < nrules + nuseless_productions; r++)
167 {
168 if (filter && !filter (&rules[r]))
169 continue;
170 if (first)
171 fprintf (out, "%s\n\n", title);
172 else if (previous_lhs && previous_lhs != rules[r].lhs)
173 fputc ('\n', out);
174 first = false;
175 rule_lhs_print (&rules[r], previous_lhs, out);
176 rule_rhs_print (&rules[r], out);
177 fprintf (out, "\n");
178 previous_lhs = rules[r].lhs;
179 }
180 if (!first)
181 fputs ("\n\n", out);
182 }
183
184 void
185 grammar_rules_print (FILE *out)
186 {
187 grammar_rules_partial_print (out, _("Grammar"), rule_useful_in_grammar_p);
188 }
189
190 void
191 grammar_rules_print_xml (FILE *out, int level)
192 {
193 rule_number r;
194 bool first = true;
195
196 for (r = 0; r < nrules + nuseless_productions; r++)
197 {
198 if (first)
199 xml_puts (out, level + 1, "<rules>");
200 first = false;
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";
209 xml_indent (out, level + 2);
210 fprintf (out, "<rule number=\"%d\" usefulness=\"%s\"",
211 rules[r].number, usefulness);
212 if (rules[r].precsym)
213 fprintf (out, " percent_prec=\"%s\"",
214 xml_escape (rules[r].precsym->tag));
215 fputs (">\n", out);
216 }
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 }
221 if (!first)
222 xml_puts (out, level + 1, "</rules>");
223 else
224 xml_puts (out, level + 1, "<rules/>");
225 }
226
227 void
228 grammar_dump (FILE *out, const char *title)
229 {
230 fprintf (out, "%s\n\n", title);
231 fprintf (out,
232 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
233 ntokens, nvars, nsyms, nrules, nritems);
234
235
236 fprintf (out, "Variables\n---------\n\n");
237 {
238 symbol_number i;
239 fprintf (out, "Value Sprec Sassoc Tag\n");
240
241 for (i = ntokens; i < nsyms; i++)
242 fprintf (out, "%5d %5d %5d %s\n",
243 i,
244 symbols[i]->prec, symbols[i]->assoc,
245 symbols[i]->tag);
246 fprintf (out, "\n\n");
247 }
248
249 fprintf (out, "Rules\n-----\n\n");
250 {
251 rule_number i;
252 fprintf (out,
253 "Num (Prec, Assoc, Useful, Ritem Range) Lhs"
254 " -> Rhs (Ritem range) [Num]\n");
255 for (i = 0; i < nrules + nuseless_productions; i++)
256 {
257 rule const *rule_i = &rules[i];
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));
276 }
277 }
278 fprintf (out, "\n\n");
279
280 fprintf (out, "Rules interpreted\n-----------------\n\n");
281 {
282 rule_number r;
283 for (r = 0; r < nrules + nuseless_productions; r++)
284 {
285 fprintf (out, "%-5d %s:", r, rules[r].lhs->tag);
286 rule_rhs_print (&rules[r], out);
287 fprintf (out, "\n");
288 }
289 }
290 fprintf (out, "\n\n");
291 }
292
293 void
294 grammar_rules_useless_report (const char *message)
295 {
296 rule_number r;
297 for (r = 0; r < nrules ; ++r)
298 if (!rules[r].useful)
299 complain (&rules[r].location, Wother, "%s", message);
300 }
301
302 void
303 grammar_free (void)
304 {
305 if (ritem)
306 free (ritem - 1);
307 free (rules);
308 free (token_translations);
309 /* Free the symbol table data structure. */
310 symbols_free ();
311 free_merger_functions ();
312 }