]>
git.saurik.com Git - bison.git/blob - src/gram.c
1 /* Allocate input grammar variables for Bison.
3 Copyright (C) 1984, 1986, 1989, 2001, 2002, 2003, 2005, 2006 Free
4 Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
31 /* Comments for these variables are in gram.h. */
33 item_number
*ritem
= NULL
;
34 unsigned int nritems
= 0;
37 rule_number nrules
= 0;
39 symbol
**symbols
= NULL
;
44 symbol_number
*token_translations
= NULL
;
46 int max_user_token_number
= 256;
48 /*--------------------------------------------------------------.
49 | Return true IFF the rule has a `number' smaller than NRULES. |
50 `--------------------------------------------------------------*/
53 rule_useful_p (rule
*r
)
55 return r
->number
< nrules
;
59 /*-------------------------------------------------------------.
60 | Return true IFF the rule has a `number' higher than NRULES. |
61 `-------------------------------------------------------------*/
64 rule_useless_p (rule
*r
)
66 return !rule_useful_p (r
);
70 /*--------------------------------------------------------------------.
71 | Return true IFF the rule is not flagged as useful *and* is useful. |
72 | In other words, it was discarded because of conflicts. |
73 `--------------------------------------------------------------------*/
76 rule_never_reduced_p (rule
*r
)
78 return !r
->useful
&& rule_useful_p (r
);
82 /*----------------------------------------------------------------.
83 | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
84 | already displayed (by a previous call for another rule), avoid |
85 | useless repetitions. |
86 `----------------------------------------------------------------*/
89 rule_lhs_print (rule
*r
, symbol
*previous_lhs
, FILE *out
)
91 fprintf (out
, " %3d ", r
->number
);
92 if (previous_lhs
!= r
->lhs
)
94 fprintf (out
, "%s:", r
->lhs
->tag
);
99 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
106 /*--------------------------------------.
107 | Return the number of symbols in RHS. |
108 `--------------------------------------*/
111 rule_rhs_length (rule
*r
)
115 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
121 /*-------------------------------.
122 | Print this rule's RHS on OUT. |
123 `-------------------------------*/
126 rule_rhs_print (rule
*r
, FILE *out
)
131 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
132 fprintf (out
, " %s", symbols
[*rp
]->tag
);
137 fprintf (out
, " /* %s */\n", _("empty"));
142 /*-------------------------.
143 | Print this rule on OUT. |
144 `-------------------------*/
147 rule_print (rule
*r
, FILE *out
)
149 fprintf (out
, "%s:", r
->lhs
->tag
);
150 rule_rhs_print (r
, out
);
154 /*------------------------.
155 | Dump RITEM for traces. |
156 `------------------------*/
159 ritem_print (FILE *out
)
162 fputs ("RITEM\n", out
);
163 for (i
= 0; i
< nritems
; ++i
)
165 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
167 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
172 /*------------------------------------------.
173 | Return the size of the longest rule RHS. |
174 `------------------------------------------*/
177 ritem_longest_rhs (void)
182 for (r
= 0; r
< nrules
; ++r
)
184 int length
= rule_rhs_length (&rules
[r
]);
193 /*-----------------------------------------------------------------.
194 | Print the grammar's rules that match FILTER on OUT under TITLE. |
195 `-----------------------------------------------------------------*/
198 grammar_rules_partial_print (FILE *out
, const char *title
,
203 symbol
*previous_lhs
= NULL
;
205 /* rule # : LHS -> RHS */
206 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
208 if (filter
&& !filter (&rules
[r
]))
211 fprintf (out
, "%s\n\n", title
);
212 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
215 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
216 rule_rhs_print (&rules
[r
], out
);
217 previous_lhs
= rules
[r
].lhs
;
224 /*------------------------------------------.
225 | Print the grammar's useful rules on OUT. |
226 `------------------------------------------*/
229 grammar_rules_print (FILE *out
)
231 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_p
);
235 /*-------------------.
236 | Dump the grammar. |
237 `-------------------*/
240 grammar_dump (FILE *out
, const char *title
)
242 fprintf (out
, "%s\n\n", title
);
244 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
245 ntokens
, nvars
, nsyms
, nrules
, nritems
);
248 fprintf (out
, "Variables\n---------\n\n");
251 fprintf (out
, "Value Sprec Sassoc Tag\n");
253 for (i
= ntokens
; i
< nsyms
; i
++)
254 fprintf (out
, "%5d %5d %5d %s\n",
256 symbols
[i
]->prec
, symbols
[i
]->assoc
,
258 fprintf (out
, "\n\n");
261 fprintf (out
, "Rules\n-----\n\n");
264 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
265 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
267 rule
*rule_i
= &rules
[i
];
268 item_number
*rp
= NULL
;
269 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
270 unsigned int rhs_count
= 0;
271 /* Find the last RHS index in ritems. */
272 for (rp
= rule_i
->rhs
; *rp
>= 0; ++rp
)
274 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
276 rule_i
->prec
? rule_i
->prec
->prec
: 0,
277 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
280 rhs_itemno
+ rhs_count
- 1,
281 rule_i
->lhs
->number
);
282 /* Dumped the RHS. */
283 for (rp
= rule_i
->rhs
; *rp
>= 0; rp
++)
284 fprintf (out
, " %3d", *rp
);
285 fprintf (out
, " [%d]\n", item_number_as_rule_number (*rp
));
288 fprintf (out
, "\n\n");
290 fprintf (out
, "Rules interpreted\n-----------------\n\n");
293 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
295 fprintf (out
, "%-5d ", r
);
296 rule_print (&rules
[r
], out
);
299 fprintf (out
, "\n\n");
303 /*------------------------------------------------------------------.
304 | Report on STDERR the rules that are not flagged USEFUL, using the |
305 | MESSAGE (which can be `useless rule' when invoked after grammar |
306 | reduction, or `never reduced' after conflicts were taken into |
308 `------------------------------------------------------------------*/
311 grammar_rules_never_reduced_report (const char *message
)
314 for (r
= 0; r
< nrules
; ++r
)
315 if (!rules
[r
].useful
)
317 location_print (stderr
, rules
[r
].location
);
318 fprintf (stderr
, ": %s: %s: ", _("warning"), message
);
319 rule_print (&rules
[r
], stderr
);
329 free (token_translations
);
330 /* Free the symbol table data structure. */
332 free_merger_functions ();