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