]> git.saurik.com Git - bison.git/blob - src/gram.c
* src/gram.h (rule_number_t, RULE_NUMBER_MAX, int_of_rule_number)
[bison.git] / src / gram.c
1 /* Allocate input grammar variables for bison,
2 Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21
22 #include "system.h"
23 #include "quotearg.h"
24 #include "symtab.h"
25 #include "gram.h"
26 #include "reduce.h"
27 #include "reader.h"
28
29 /* Comments for these variables are in gram.h. */
30
31 item_number_t *ritem = NULL;
32 unsigned int nritems = 0;
33
34 rule_t *rules = NULL;
35 rule_number_t nrules = 0;
36
37 symbol_t **symbols = NULL;
38 int nsyms = 0;
39 int ntokens = 1;
40 int nvars = 0;
41
42 symbol_number_t *token_translations = NULL;
43
44 int max_user_token_number = 256;
45
46 int glr_parser = 0;
47 int pure_parser = 0;
48
49
50 /*--------------------------------------.
51 | Return the number of symbols in RHS. |
52 `--------------------------------------*/
53
54 int
55 rule_rhs_length (rule_t *rule)
56 {
57 int res = 0;
58 item_number_t *rhsp;
59 for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp)
60 ++res;
61 return res;
62 }
63
64
65 /*-------------------------------.
66 | Print this RULE's RHS on OUT. |
67 `-------------------------------*/
68
69 void
70 rule_rhs_print (rule_t *rule, FILE *out)
71 {
72 if (*rule->rhs >= 0)
73 {
74 item_number_t *r;
75 for (r = rule->rhs; *r >= 0; r++)
76 fprintf (out, " %s", symbol_tag_get (symbols[*r]));
77 fputc ('\n', out);
78 }
79 else
80 {
81 fprintf (out, " /* %s */\n", _("empty"));
82 }
83 }
84
85
86 /*-------------------------.
87 | Print this RULE on OUT. |
88 `-------------------------*/
89
90 void
91 rule_print (rule_t *rule, FILE *out)
92 {
93 fprintf (out, "%s:", symbol_tag_get (rule->lhs));
94 rule_rhs_print (rule, out);
95 }
96
97
98 /*------------------------.
99 | Dump RITEM for traces. |
100 `------------------------*/
101
102 void
103 ritem_print (FILE *out)
104 {
105 unsigned int i;
106 fputs ("RITEM\n", out);
107 for (i = 0; i < nritems; ++i)
108 if (ritem[i] >= 0)
109 fprintf (out, " %s", symbol_tag_get (symbols[ritem[i]]));
110 else
111 fprintf (out, " (rule %d)\n", -ritem[i] - 1);
112 fputs ("\n\n", out);
113 }
114
115
116 /*------------------------------------------.
117 | Return the size of the longest rule RHS. |
118 `------------------------------------------*/
119
120 size_t
121 ritem_longest_rhs (void)
122 {
123 int max = 0;
124 rule_number_t r;
125
126 for (r = 1; r < nrules + 1; ++r)
127 {
128 int length = rule_rhs_length (&rules[r]);
129 if (length > max)
130 max = length;
131 }
132
133 return max;
134 }
135
136
137 /*----------------------------------------------------------------.
138 | Print the grammar's rules numbers from BEGIN (inclusive) to END |
139 | (exclusive) on OUT under TITLE. |
140 `----------------------------------------------------------------*/
141
142 static inline void
143 blanks_print (unsigned n, FILE *out)
144 {
145 for (/* Nothing*/; n > 0; --n)
146 fputc (' ', out);
147 }
148
149 void
150 grammar_rules_partial_print (FILE *out, const char *title,
151 rule_number_t begin, rule_number_t end)
152 {
153 int r;
154 symbol_t *last_lhs = NULL;
155
156 /* rule # : LHS -> RHS */
157 fprintf (out, "%s\n\n", title);
158 for (r = begin; r < end; r++)
159 {
160 if (last_lhs && last_lhs != rules[r].lhs)
161 fputc ('\n', out);
162
163 fprintf (out, " %3d ", r - 1);
164 if (last_lhs != rules[r].lhs)
165 {
166 last_lhs = rules[r].lhs;
167 fprintf (out, "%s:", symbol_tag_get (last_lhs));
168 }
169 else
170 {
171 blanks_print (strlen (symbol_tag_get (last_lhs)), out);
172 fputc ('|', out);
173 }
174 rule_rhs_print (&rules[r], out);
175 }
176 fputs ("\n\n", out);
177 }
178
179
180 /*------------------------------------------.
181 | Print the grammar's useful rules on OUT. |
182 `------------------------------------------*/
183
184 void
185 grammar_rules_print (FILE *out)
186 {
187 grammar_rules_partial_print (out, _("Grammar"), 1, nrules + 1);
188 }
189
190
191 /*-------------------.
192 | Dump the grammar. |
193 `-------------------*/
194
195 void
196 grammar_dump (FILE *out, const char *title)
197 {
198 fprintf (out, "%s\n\n", title);
199 fprintf (out,
200 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
201 ntokens, nvars, nsyms, nrules, nritems);
202
203
204 fprintf (out, "Variables\n---------\n\n");
205 {
206 symbol_number_t i;
207 fprintf (out, "Value Sprec Sassoc Tag\n");
208
209 for (i = ntokens; i < nsyms; i++)
210 fprintf (out, "%5d %5d %5d %s\n",
211 i,
212 symbols[i]->prec, symbols[i]->assoc,
213 symbol_tag_get (symbols[i]));
214 fprintf (out, "\n\n");
215 }
216
217 fprintf (out, "Rules\n-----\n\n");
218 {
219 rule_number_t i;
220 fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
221 for (i = 1; i < nrules + nuseless_productions + 1; i++)
222 {
223 rule_t *rule = &rules[i];
224 item_number_t *r = NULL;
225 int rhs_count = 0;
226 /* Find the last RHS index in ritems. */
227 for (r = rule->rhs; *r >= 0; ++r)
228 ++rhs_count;
229 fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->",
230 i - 1,
231 rule->prec ? rule->prec->prec : 0,
232 rule->prec ? rule->prec->assoc : 0,
233 rule->useful,
234 rule->rhs - ritem,
235 rule->rhs - ritem + rhs_count - 1,
236 rule->lhs->number);
237 /* Dumped the RHS. */
238 for (r = rule->rhs; *r >= 0; r++)
239 fprintf (out, " %3d", *r);
240 fprintf (out, " [%d]\n", -(*r) - 1);
241 }
242 }
243 fprintf (out, "\n\n");
244
245 fprintf (out, "Rules interpreted\n-----------------\n\n");
246 {
247 rule_number_t r;
248 for (r = 1; r < nrules + nuseless_productions + 1; r++)
249 {
250 fprintf (out, "%-5d ", r);
251 rule_print (&rules[r], out);
252 }
253 }
254 fprintf (out, "\n\n");
255 }
256
257
258 void
259 grammar_free (void)
260 {
261 XFREE (ritem);
262 free (rules + 1);
263 XFREE (token_translations);
264 /* Free the symbol table data structure. */
265 symbols_free ();
266 free_merger_functions ();
267 }