]>
git.saurik.com Git - bison.git/blob - src/gram.c
86f75ec7b8f3d13fa208982e6f628ac46b471228
1 /* Allocate input grammar variables for Bison.
3 Copyright (C) 1984, 1986, 1989, 2001, 2002, 2003 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
33 /* Comments for these variables are in gram.h. */
35 item_number
*ritem
= NULL
;
36 unsigned int nritems
= 0;
39 rule_number nrules
= 0;
41 symbol
**symbols
= NULL
;
46 symbol_number
*token_translations
= NULL
;
48 int max_user_token_number
= 256;
54 /*--------------------------------------------------------------.
55 | Return true IFF the rule has a `number' smaller than NRULES. |
56 `--------------------------------------------------------------*/
59 rule_useful_p (rule
*r
)
61 return r
->number
< nrules
;
65 /*-------------------------------------------------------------.
66 | Return true IFF the rule has a `number' higher than NRULES. |
67 `-------------------------------------------------------------*/
70 rule_useless_p (rule
*r
)
72 return r
->number
>= nrules
;
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 `--------------------------------------------------------------------*/
82 rule_never_reduced_p (rule
*r
)
84 return !r
->useful
&& r
->number
< nrules
;
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 `----------------------------------------------------------------*/
95 rule_lhs_print (rule
*r
, symbol
*previous_lhs
, FILE *out
)
97 fprintf (out
, " %3d ", r
->number
);
98 if (previous_lhs
!= r
->lhs
)
100 fprintf (out
, "%s:", r
->lhs
->tag
);
105 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
112 /*--------------------------------------.
113 | Return the number of symbols in RHS. |
114 `--------------------------------------*/
117 rule_rhs_length (rule
*r
)
121 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
127 /*-------------------------------.
128 | Print this rule's RHS on OUT. |
129 `-------------------------------*/
132 rule_rhs_print (rule
*r
, FILE *out
)
137 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
138 fprintf (out
, " %s", symbols
[*rp
]->tag
);
143 fprintf (out
, " /* %s */\n", _("empty"));
148 /*-------------------------.
149 | Print this rule on OUT. |
150 `-------------------------*/
153 rule_print (rule
*r
, FILE *out
)
155 fprintf (out
, "%s:", r
->lhs
->tag
);
156 rule_rhs_print (r
, out
);
160 /*------------------------.
161 | Dump RITEM for traces. |
162 `------------------------*/
165 ritem_print (FILE *out
)
168 fputs ("RITEM\n", out
);
169 for (i
= 0; i
< nritems
; ++i
)
171 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
173 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
178 /*------------------------------------------.
179 | Return the size of the longest rule RHS. |
180 `------------------------------------------*/
183 ritem_longest_rhs (void)
188 for (r
= 0; r
< nrules
; ++r
)
190 int length
= rule_rhs_length (&rules
[r
]);
199 /*-----------------------------------------------------------------.
200 | Print the grammar's rules that match FILTER on OUT under TITLE. |
201 `-----------------------------------------------------------------*/
204 grammar_rules_partial_print (FILE *out
, const char *title
,
209 symbol
*previous_lhs
= NULL
;
211 /* rule # : LHS -> RHS */
212 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
214 if (filter
&& !filter (&rules
[r
]))
217 fprintf (out
, "%s\n\n", title
);
218 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
221 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
222 rule_rhs_print (&rules
[r
], out
);
223 previous_lhs
= rules
[r
].lhs
;
230 /*------------------------------------------.
231 | Print the grammar's useful rules on OUT. |
232 `------------------------------------------*/
235 grammar_rules_print (FILE *out
)
237 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_p
);
241 /*-------------------.
242 | Dump the grammar. |
243 `-------------------*/
246 grammar_dump (FILE *out
, const char *title
)
248 fprintf (out
, "%s\n\n", title
);
250 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
251 ntokens
, nvars
, nsyms
, nrules
, nritems
);
254 fprintf (out
, "Variables\n---------\n\n");
257 fprintf (out
, "Value Sprec Sassoc Tag\n");
259 for (i
= ntokens
; i
< nsyms
; i
++)
260 fprintf (out
, "%5d %5d %5d %s\n",
262 symbols
[i
]->prec
, symbols
[i
]->assoc
,
264 fprintf (out
, "\n\n");
267 fprintf (out
, "Rules\n-----\n\n");
270 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
271 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
273 rule
*rule_i
= &rules
[i
];
274 item_number
*rp
= NULL
;
275 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
276 unsigned int rhs_count
= 0;
277 /* Find the last RHS index in ritems. */
278 for (rp
= rule_i
->rhs
; *rp
>= 0; ++rp
)
280 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
282 rule_i
->prec
? rule_i
->prec
->prec
: 0,
283 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
286 rhs_itemno
+ rhs_count
- 1,
287 rule_i
->lhs
->number
);
288 /* Dumped the RHS. */
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
));
294 fprintf (out
, "\n\n");
296 fprintf (out
, "Rules interpreted\n-----------------\n\n");
299 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
301 fprintf (out
, "%-5d ", r
);
302 rule_print (&rules
[r
], out
);
305 fprintf (out
, "\n\n");
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 |
314 `------------------------------------------------------------------*/
317 grammar_rules_never_reduced_report (const char *message
)
320 for (r
= 0; r
< nrules
; ++r
)
321 if (!rules
[r
].useful
)
323 location_print (stderr
, rules
[r
].location
);
324 fprintf (stderr
, ": %s: %s: ",
325 _("warning"), message
);
326 rule_print (&rules
[r
], stderr
);
335 XFREE (token_translations
);
336 /* Free the symbol table data structure. */
338 free_merger_functions ();