]> git.saurik.com Git - bison.git/blame - src/gram.c
Don't include c.m4, as "include" doesn't do what we want.
[bison.git] / src / gram.c
CommitLineData
6f84e9ab
PE
1/* Allocate input grammar variables for Bison.
2
8b3df748 3 Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc.
f7d4d87a 4
076ab033 5 This file is part of Bison, the GNU Compiler Compiler.
f7d4d87a 6
076ab033
AD
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
f7d4d87a 11
076ab033
AD
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
f7d4d87a 16
076ab033
AD
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
f7d4d87a
DM
21
22
4a120d45 23#include "system.h"
81ebdef9
PE
24
25#include <quotearg.h>
26
78ab8f67 27#include "gram.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
676385e2 49int glr_parser = 0;
280a38c3 50int pure_parser = 0;
f7d4d87a 51
c2713865 52
c8f002c7
AD
53/*--------------------------------------------------------------.
54| Return true IFF the rule has a `number' smaller than NRULES. |
55`--------------------------------------------------------------*/
56
57bool
81ebdef9 58rule_useful_p (rule *r)
c8f002c7
AD
59{
60 return r->number < nrules;
61}
62
63
64/*-------------------------------------------------------------.
65| Return true IFF the rule has a `number' higher than NRULES. |
66`-------------------------------------------------------------*/
67
68bool
81ebdef9 69rule_useless_p (rule *r)
c8f002c7
AD
70{
71 return r->number >= nrules;
72}
73
74
75/*--------------------------------------------------------------------.
76| Return true IFF the rule is not flagged as useful *and* is useful. |
77| In other words, it was discarded because of conflicts. |
78`--------------------------------------------------------------------*/
79
80bool
81ebdef9 81rule_never_reduced_p (rule *r)
c8f002c7
AD
82{
83 return !r->useful && r->number < nrules;
84}
85
86
ce4ccb4b
AD
87/*----------------------------------------------------------------.
88| Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
89| already displayed (by a previous call for another rule), avoid |
90| useless repetitions. |
91`----------------------------------------------------------------*/
92
93void
81ebdef9 94rule_lhs_print (rule *r, symbol *previous_lhs, FILE *out)
ce4ccb4b 95{
81ebdef9
PE
96 fprintf (out, " %3d ", r->number);
97 if (previous_lhs != r->lhs)
ce4ccb4b 98 {
81ebdef9 99 fprintf (out, "%s:", r->lhs->tag);
ce4ccb4b
AD
100 }
101 else
102 {
103 int n;
97650f4e 104 for (n = strlen (previous_lhs->tag); n > 0; --n)
ce4ccb4b
AD
105 fputc (' ', out);
106 fputc ('|', out);
107 }
108}
109
110
c3b407f4
AD
111/*--------------------------------------.
112| Return the number of symbols in RHS. |
113`--------------------------------------*/
114
115int
81ebdef9 116rule_rhs_length (rule *r)
c3b407f4
AD
117{
118 int res = 0;
81ebdef9
PE
119 item_number *rhsp;
120 for (rhsp = r->rhs; *rhsp >= 0; ++rhsp)
c3b407f4
AD
121 ++res;
122 return res;
123}
124
125
6b98e4b5 126/*-------------------------------.
81ebdef9 127| Print this rule's RHS on OUT. |
6b98e4b5
AD
128`-------------------------------*/
129
130void
81ebdef9 131rule_rhs_print (rule *r, FILE *out)
6b98e4b5 132{
81ebdef9 133 if (*r->rhs >= 0)
6b98e4b5 134 {
81ebdef9
PE
135 item_number *rp;
136 for (rp = r->rhs; *rp >= 0; rp++)
137 fprintf (out, " %s", symbols[*rp]->tag);
6b98e4b5
AD
138 fputc ('\n', out);
139 }
140 else
141 {
142 fprintf (out, " /* %s */\n", _("empty"));
143 }
144}
145
146
147/*-------------------------.
81ebdef9 148| Print this rule on OUT. |
6b98e4b5
AD
149`-------------------------*/
150
151void
81ebdef9 152rule_print (rule *r, FILE *out)
6b98e4b5 153{
81ebdef9
PE
154 fprintf (out, "%s:", r->lhs->tag);
155 rule_rhs_print (r, out);
6b98e4b5
AD
156}
157
158
c2713865
AD
159/*------------------------.
160| Dump RITEM for traces. |
161`------------------------*/
162
cbbe7505 163void
3067fbef 164ritem_print (FILE *out)
f7d4d87a 165{
0c2d3f4c 166 unsigned int i;
3067fbef 167 fputs ("RITEM\n", out);
75142d45
AD
168 for (i = 0; i < nritems; ++i)
169 if (ritem[i] >= 0)
97650f4e 170 fprintf (out, " %s", symbols[ritem[i]]->tag);
3067fbef 171 else
4b3d3a8e 172 fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i]));
3067fbef 173 fputs ("\n\n", out);
f7d4d87a 174}
c2713865
AD
175
176
177/*------------------------------------------.
178| Return the size of the longest rule RHS. |
179`------------------------------------------*/
180
181size_t
182ritem_longest_rhs (void)
183{
c3b407f4 184 int max = 0;
81ebdef9 185 rule_number r;
c2713865 186
4b3d3a8e 187 for (r = 0; r < nrules; ++r)
c3b407f4 188 {
9222837b 189 int length = rule_rhs_length (&rules[r]);
c3b407f4
AD
190 if (length > max)
191 max = length;
192 }
c2713865
AD
193
194 return max;
195}
78ab8f67
AD
196
197
c8f002c7
AD
198/*-----------------------------------------------------------------.
199| Print the grammar's rules that match FILTER on OUT under TITLE. |
200`-----------------------------------------------------------------*/
6b98e4b5 201
6b98e4b5 202void
9757c359 203grammar_rules_partial_print (FILE *out, const char *title,
81ebdef9 204 rule_filter filter)
6b98e4b5
AD
205{
206 int r;
637c4b28 207 bool first = true;
81ebdef9 208 symbol *previous_lhs = NULL;
6b98e4b5
AD
209
210 /* rule # : LHS -> RHS */
c8f002c7 211 for (r = 0; r < nrules + nuseless_productions; r++)
6b98e4b5 212 {
c8f002c7
AD
213 if (filter && !filter (&rules[r]))
214 continue;
215 if (first)
216 fprintf (out, "%s\n\n", title);
217 else if (previous_lhs && previous_lhs != rules[r].lhs)
6b98e4b5 218 fputc ('\n', out);
637c4b28 219 first = false;
ce4ccb4b 220 rule_lhs_print (&rules[r], previous_lhs, out);
6b98e4b5 221 rule_rhs_print (&rules[r], out);
ce4ccb4b 222 previous_lhs = rules[r].lhs;
6b98e4b5 223 }
c8f002c7
AD
224 if (!first)
225 fputs ("\n\n", out);
6b98e4b5
AD
226}
227
9757c359
AD
228
229/*------------------------------------------.
230| Print the grammar's useful rules on OUT. |
231`------------------------------------------*/
232
233void
234grammar_rules_print (FILE *out)
235{
c8f002c7 236 grammar_rules_partial_print (out, _("Grammar"), rule_useful_p);
9757c359
AD
237}
238
239
78ab8f67
AD
240/*-------------------.
241| Dump the grammar. |
242`-------------------*/
243
244void
245grammar_dump (FILE *out, const char *title)
246{
78ab8f67
AD
247 fprintf (out, "%s\n\n", title);
248 fprintf (out,
249 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
250 ntokens, nvars, nsyms, nrules, nritems);
9222837b
AD
251
252
3f823769 253 fprintf (out, "Variables\n---------\n\n");
9222837b 254 {
81ebdef9 255 symbol_number i;
3f823769 256 fprintf (out, "Value Sprec Sassoc Tag\n");
9222837b
AD
257
258 for (i = ntokens; i < nsyms; i++)
259 fprintf (out, "%5d %5d %5d %s\n",
260 i,
261 symbols[i]->prec, symbols[i]->assoc,
97650f4e 262 symbols[i]->tag);
9222837b
AD
263 fprintf (out, "\n\n");
264 }
265
3f823769 266 fprintf (out, "Rules\n-----\n\n");
9222837b 267 {
81ebdef9 268 rule_number i;
3f823769 269 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
4b3d3a8e 270 for (i = 0; i < nrules + nuseless_productions; i++)
9222837b 271 {
81ebdef9
PE
272 rule *rule_i = &rules[i];
273 item_number *r = NULL;
274 unsigned int rhs_itemno = rule_i->rhs - ritem;
e3fbd37f 275 unsigned int rhs_count = 0;
9222837b 276 /* Find the last RHS index in ritems. */
81ebdef9 277 for (r = rule_i->rhs; *r >= 0; ++r)
9222837b 278 ++rhs_count;
e3fbd37f 279 fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
4b3d3a8e 280 i,
81ebdef9
PE
281 rule_i->prec ? rule_i->prec->prec : 0,
282 rule_i->prec ? rule_i->prec->assoc : 0,
283 rule_i->useful,
e3fbd37f
PE
284 rhs_itemno,
285 rhs_itemno + rhs_count - 1,
81ebdef9 286 rule_i->lhs->number);
9222837b 287 /* Dumped the RHS. */
81ebdef9 288 for (r = rule_i->rhs; *r >= 0; r++)
9222837b 289 fprintf (out, " %3d", *r);
4b3d3a8e 290 fprintf (out, " [%d]\n", item_number_as_rule_number (*r));
9222837b
AD
291 }
292 }
78ab8f67 293 fprintf (out, "\n\n");
9222837b 294
3f823769 295 fprintf (out, "Rules interpreted\n-----------------\n\n");
9222837b 296 {
81ebdef9 297 rule_number r;
4b3d3a8e 298 for (r = 0; r < nrules + nuseless_productions; r++)
9222837b
AD
299 {
300 fprintf (out, "%-5d ", r);
301 rule_print (&rules[r], out);
302 }
303 }
78ab8f67
AD
304 fprintf (out, "\n\n");
305}
5372019f
AD
306
307
c8f002c7
AD
308/*------------------------------------------------------------------.
309| Report on STDERR the rules that are not flagged USEFUL, using the |
310| MESSAGE (which can be `useless rule' when invoked after grammar |
311| reduction, or `never reduced' after conflicts were taken into |
312| account). |
313`------------------------------------------------------------------*/
314
315void
316grammar_rules_never_reduced_report (const char *message)
317{
81ebdef9 318 rule_number r;
c8f002c7
AD
319 for (r = 0; r < nrules ; ++r)
320 if (!rules[r].useful)
321 {
5fcdb07b 322 location_print (stderr, rules[r].location);
c8f002c7
AD
323 fprintf (stderr, ": %s: %s: ",
324 _("warning"), message);
325 rule_print (&rules[r], stderr);
326 }
327}
328
5372019f
AD
329void
330grammar_free (void)
331{
e59a6871 332 XFREE (ritem);
4b3d3a8e 333 free (rules);
e59a6871 334 XFREE (token_translations);
5372019f
AD
335 /* Free the symbol table data structure. */
336 symbols_free ();
676385e2 337 free_merger_functions ();
5372019f 338}