]>
Commit | Line | Data |
---|---|---|
f7d4d87a | 1 | /* Allocate input grammar variables for bison, |
8b3df748 | 2 | Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc. |
f7d4d87a | 3 | |
076ab033 | 4 | This file is part of Bison, the GNU Compiler Compiler. |
f7d4d87a | 5 | |
076ab033 AD |
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. | |
f7d4d87a | 10 | |
076ab033 AD |
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. | |
f7d4d87a | 15 | |
076ab033 AD |
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. */ | |
f7d4d87a DM |
20 | |
21 | ||
4a120d45 | 22 | #include "system.h" |
8b3df748 | 23 | #include "quotearg.h" |
0e78e603 | 24 | #include "symtab.h" |
78ab8f67 AD |
25 | #include "gram.h" |
26 | #include "reduce.h" | |
3067fbef | 27 | #include "reader.h" |
4a120d45 | 28 | |
6b98e4b5 | 29 | /* Comments for these variables are in gram.h. */ |
f7d4d87a | 30 | |
62a3e4f0 | 31 | item_number_t *ritem = NULL; |
0c2d3f4c | 32 | unsigned int nritems = 0; |
75142d45 | 33 | |
1a2b5d37 | 34 | rule_t *rules = NULL; |
9222837b | 35 | rule_number_t nrules = 0; |
0e78e603 | 36 | |
db8837cb | 37 | symbol_t **symbols = NULL; |
5123689b AD |
38 | int nsyms = 0; |
39 | int ntokens = 1; | |
40 | int nvars = 0; | |
41 | ||
a49aecd5 | 42 | symbol_number_t *token_translations = NULL; |
f7d4d87a | 43 | |
280a38c3 | 44 | int max_user_token_number = 256; |
f7d4d87a | 45 | |
676385e2 | 46 | int glr_parser = 0; |
280a38c3 | 47 | int pure_parser = 0; |
f7d4d87a | 48 | |
c2713865 | 49 | |
ce4ccb4b AD |
50 | /*----------------------------------------------------------------. |
51 | | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was | | |
52 | | already displayed (by a previous call for another rule), avoid | | |
53 | | useless repetitions. | | |
54 | `----------------------------------------------------------------*/ | |
55 | ||
56 | void | |
57 | rule_lhs_print (rule_t *rule, symbol_t *previous_lhs, FILE *out) | |
58 | { | |
4b3d3a8e | 59 | fprintf (out, " %3d ", rule->number); |
ce4ccb4b AD |
60 | if (previous_lhs != rule->lhs) |
61 | { | |
97650f4e | 62 | fprintf (out, "%s:", rule->lhs->tag); |
ce4ccb4b AD |
63 | } |
64 | else | |
65 | { | |
66 | int n; | |
97650f4e | 67 | for (n = strlen (previous_lhs->tag); n > 0; --n) |
ce4ccb4b AD |
68 | fputc (' ', out); |
69 | fputc ('|', out); | |
70 | } | |
71 | } | |
72 | ||
73 | ||
c3b407f4 AD |
74 | /*--------------------------------------. |
75 | | Return the number of symbols in RHS. | | |
76 | `--------------------------------------*/ | |
77 | ||
78 | int | |
79 | rule_rhs_length (rule_t *rule) | |
80 | { | |
81 | int res = 0; | |
62a3e4f0 | 82 | item_number_t *rhsp; |
c3b407f4 AD |
83 | for (rhsp = rule->rhs; *rhsp >= 0; ++rhsp) |
84 | ++res; | |
85 | return res; | |
86 | } | |
87 | ||
88 | ||
6b98e4b5 AD |
89 | /*-------------------------------. |
90 | | Print this RULE's RHS on OUT. | | |
91 | `-------------------------------*/ | |
92 | ||
93 | void | |
94 | rule_rhs_print (rule_t *rule, FILE *out) | |
95 | { | |
96 | if (*rule->rhs >= 0) | |
97 | { | |
98 | item_number_t *r; | |
99 | for (r = rule->rhs; *r >= 0; r++) | |
97650f4e | 100 | fprintf (out, " %s", symbols[*r]->tag); |
6b98e4b5 AD |
101 | fputc ('\n', out); |
102 | } | |
103 | else | |
104 | { | |
105 | fprintf (out, " /* %s */\n", _("empty")); | |
106 | } | |
107 | } | |
108 | ||
109 | ||
110 | /*-------------------------. | |
111 | | Print this RULE on OUT. | | |
112 | `-------------------------*/ | |
113 | ||
114 | void | |
115 | rule_print (rule_t *rule, FILE *out) | |
116 | { | |
97650f4e | 117 | fprintf (out, "%s:", rule->lhs->tag); |
6b98e4b5 AD |
118 | rule_rhs_print (rule, out); |
119 | } | |
120 | ||
121 | ||
c2713865 AD |
122 | /*------------------------. |
123 | | Dump RITEM for traces. | | |
124 | `------------------------*/ | |
125 | ||
cbbe7505 | 126 | void |
3067fbef | 127 | ritem_print (FILE *out) |
f7d4d87a | 128 | { |
0c2d3f4c | 129 | unsigned int i; |
3067fbef | 130 | fputs ("RITEM\n", out); |
75142d45 AD |
131 | for (i = 0; i < nritems; ++i) |
132 | if (ritem[i] >= 0) | |
97650f4e | 133 | fprintf (out, " %s", symbols[ritem[i]]->tag); |
3067fbef | 134 | else |
4b3d3a8e | 135 | fprintf (out, " (rule %d)\n", item_number_as_rule_number (ritem[i])); |
3067fbef | 136 | fputs ("\n\n", out); |
f7d4d87a | 137 | } |
c2713865 AD |
138 | |
139 | ||
140 | /*------------------------------------------. | |
141 | | Return the size of the longest rule RHS. | | |
142 | `------------------------------------------*/ | |
143 | ||
144 | size_t | |
145 | ritem_longest_rhs (void) | |
146 | { | |
c3b407f4 | 147 | int max = 0; |
9222837b | 148 | rule_number_t r; |
c2713865 | 149 | |
4b3d3a8e | 150 | for (r = 0; r < nrules; ++r) |
c3b407f4 | 151 | { |
9222837b | 152 | int length = rule_rhs_length (&rules[r]); |
c3b407f4 AD |
153 | if (length > max) |
154 | max = length; | |
155 | } | |
c2713865 AD |
156 | |
157 | return max; | |
158 | } | |
78ab8f67 AD |
159 | |
160 | ||
9757c359 AD |
161 | /*----------------------------------------------------------------. |
162 | | Print the grammar's rules numbers from BEGIN (inclusive) to END | | |
163 | | (exclusive) on OUT under TITLE. | | |
164 | `----------------------------------------------------------------*/ | |
6b98e4b5 | 165 | |
6b98e4b5 | 166 | void |
9757c359 | 167 | grammar_rules_partial_print (FILE *out, const char *title, |
9222837b | 168 | rule_number_t begin, rule_number_t end) |
6b98e4b5 AD |
169 | { |
170 | int r; | |
ce4ccb4b | 171 | symbol_t *previous_lhs = NULL; |
6b98e4b5 AD |
172 | |
173 | /* rule # : LHS -> RHS */ | |
9757c359 AD |
174 | fprintf (out, "%s\n\n", title); |
175 | for (r = begin; r < end; r++) | |
6b98e4b5 | 176 | { |
ce4ccb4b | 177 | if (previous_lhs && previous_lhs != rules[r].lhs) |
6b98e4b5 | 178 | fputc ('\n', out); |
ce4ccb4b | 179 | rule_lhs_print (&rules[r], previous_lhs, out); |
6b98e4b5 | 180 | rule_rhs_print (&rules[r], out); |
ce4ccb4b | 181 | previous_lhs = rules[r].lhs; |
6b98e4b5 AD |
182 | } |
183 | fputs ("\n\n", out); | |
184 | } | |
185 | ||
9757c359 AD |
186 | |
187 | /*------------------------------------------. | |
188 | | Print the grammar's useful rules on OUT. | | |
189 | `------------------------------------------*/ | |
190 | ||
191 | void | |
192 | grammar_rules_print (FILE *out) | |
193 | { | |
4b3d3a8e | 194 | grammar_rules_partial_print (out, _("Grammar"), 0, nrules); |
9757c359 AD |
195 | } |
196 | ||
197 | ||
78ab8f67 AD |
198 | /*-------------------. |
199 | | Dump the grammar. | | |
200 | `-------------------*/ | |
201 | ||
202 | void | |
203 | grammar_dump (FILE *out, const char *title) | |
204 | { | |
78ab8f67 AD |
205 | fprintf (out, "%s\n\n", title); |
206 | fprintf (out, | |
207 | "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n", | |
208 | ntokens, nvars, nsyms, nrules, nritems); | |
9222837b AD |
209 | |
210 | ||
78ab8f67 | 211 | fprintf (out, "Variables\n---------\n\n"); |
9222837b AD |
212 | { |
213 | symbol_number_t i; | |
214 | fprintf (out, "Value Sprec Sassoc Tag\n"); | |
215 | ||
216 | for (i = ntokens; i < nsyms; i++) | |
217 | fprintf (out, "%5d %5d %5d %s\n", | |
218 | i, | |
219 | symbols[i]->prec, symbols[i]->assoc, | |
97650f4e | 220 | symbols[i]->tag); |
9222837b AD |
221 | fprintf (out, "\n\n"); |
222 | } | |
223 | ||
78ab8f67 | 224 | fprintf (out, "Rules\n-----\n\n"); |
9222837b AD |
225 | { |
226 | rule_number_t i; | |
227 | fprintf (out, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n"); | |
4b3d3a8e | 228 | for (i = 0; i < nrules + nuseless_productions; i++) |
9222837b AD |
229 | { |
230 | rule_t *rule = &rules[i]; | |
231 | item_number_t *r = NULL; | |
232 | int rhs_count = 0; | |
233 | /* Find the last RHS index in ritems. */ | |
234 | for (r = rule->rhs; *r >= 0; ++r) | |
235 | ++rhs_count; | |
236 | fprintf (out, "%3d (%2d, %2d, %2d, %2d-%2d) %2d ->", | |
4b3d3a8e | 237 | i, |
9222837b AD |
238 | rule->prec ? rule->prec->prec : 0, |
239 | rule->prec ? rule->prec->assoc : 0, | |
240 | rule->useful, | |
241 | rule->rhs - ritem, | |
242 | rule->rhs - ritem + rhs_count - 1, | |
243 | rule->lhs->number); | |
244 | /* Dumped the RHS. */ | |
245 | for (r = rule->rhs; *r >= 0; r++) | |
246 | fprintf (out, " %3d", *r); | |
4b3d3a8e | 247 | fprintf (out, " [%d]\n", item_number_as_rule_number (*r)); |
9222837b AD |
248 | } |
249 | } | |
78ab8f67 | 250 | fprintf (out, "\n\n"); |
9222837b | 251 | |
78ab8f67 | 252 | fprintf (out, "Rules interpreted\n-----------------\n\n"); |
9222837b AD |
253 | { |
254 | rule_number_t r; | |
4b3d3a8e | 255 | for (r = 0; r < nrules + nuseless_productions; r++) |
9222837b AD |
256 | { |
257 | fprintf (out, "%-5d ", r); | |
258 | rule_print (&rules[r], out); | |
259 | } | |
260 | } | |
78ab8f67 AD |
261 | fprintf (out, "\n\n"); |
262 | } | |
5372019f AD |
263 | |
264 | ||
265 | void | |
266 | grammar_free (void) | |
267 | { | |
268 | XFREE (ritem); | |
4b3d3a8e | 269 | free (rules); |
5372019f AD |
270 | XFREE (token_translations); |
271 | /* Free the symbol table data structure. */ | |
272 | symbols_free (); | |
676385e2 | 273 | free_merger_functions (); |
5372019f | 274 | } |