]>
git.saurik.com Git - bison.git/blob - src/gram.c
dd77a766078be0b54cc795ff5c736cdd824de0b9
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., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, 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;
50 /*--------------------------------------------------------------.
51 | Return true IFF the rule has a `number' smaller than NRULES. |
52 `--------------------------------------------------------------*/
55 rule_useful_p (rule
*r
)
57 return r
->number
< nrules
;
61 /*-------------------------------------------------------------.
62 | Return true IFF the rule has a `number' higher than NRULES. |
63 `-------------------------------------------------------------*/
66 rule_useless_p (rule
*r
)
68 return r
->number
>= nrules
;
72 /*--------------------------------------------------------------------.
73 | Return true IFF the rule is not flagged as useful *and* is useful. |
74 | In other words, it was discarded because of conflicts. |
75 `--------------------------------------------------------------------*/
78 rule_never_reduced_p (rule
*r
)
80 return !r
->useful
&& r
->number
< nrules
;
84 /*----------------------------------------------------------------.
85 | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
86 | already displayed (by a previous call for another rule), avoid |
87 | useless repetitions. |
88 `----------------------------------------------------------------*/
91 rule_lhs_print (rule
*r
, symbol
*previous_lhs
, FILE *out
)
93 fprintf (out
, " %3d ", r
->number
);
94 if (previous_lhs
!= r
->lhs
)
96 fprintf (out
, "%s:", r
->lhs
->tag
);
101 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
108 /*--------------------------------------.
109 | Return the number of symbols in RHS. |
110 `--------------------------------------*/
113 rule_rhs_length (rule
*r
)
117 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
123 /*-------------------------------.
124 | Print this rule's RHS on OUT. |
125 `-------------------------------*/
128 rule_rhs_print (rule
*r
, FILE *out
)
133 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
134 fprintf (out
, " %s", symbols
[*rp
]->tag
);
139 fprintf (out
, " /* %s */\n", _("empty"));
144 /*-------------------------.
145 | Print this rule on OUT. |
146 `-------------------------*/
149 rule_print (rule
*r
, FILE *out
)
151 fprintf (out
, "%s:", r
->lhs
->tag
);
152 rule_rhs_print (r
, out
);
156 /*------------------------.
157 | Dump RITEM for traces. |
158 `------------------------*/
161 ritem_print (FILE *out
)
164 fputs ("RITEM\n", out
);
165 for (i
= 0; i
< nritems
; ++i
)
167 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
169 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
174 /*------------------------------------------.
175 | Return the size of the longest rule RHS. |
176 `------------------------------------------*/
179 ritem_longest_rhs (void)
184 for (r
= 0; r
< nrules
; ++r
)
186 int length
= rule_rhs_length (&rules
[r
]);
195 /*-----------------------------------------------------------------.
196 | Print the grammar's rules that match FILTER on OUT under TITLE. |
197 `-----------------------------------------------------------------*/
200 grammar_rules_partial_print (FILE *out
, const char *title
,
205 symbol
*previous_lhs
= NULL
;
207 /* rule # : LHS -> RHS */
208 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
210 if (filter
&& !filter (&rules
[r
]))
213 fprintf (out
, "%s\n\n", title
);
214 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
217 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
218 rule_rhs_print (&rules
[r
], out
);
219 previous_lhs
= rules
[r
].lhs
;
226 /*------------------------------------------.
227 | Print the grammar's useful rules on OUT. |
228 `------------------------------------------*/
231 grammar_rules_print (FILE *out
)
233 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_p
);
237 /*-------------------.
238 | Dump the grammar. |
239 `-------------------*/
242 grammar_dump (FILE *out
, const char *title
)
244 fprintf (out
, "%s\n\n", title
);
246 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
247 ntokens
, nvars
, nsyms
, nrules
, nritems
);
250 fputs (("Variables\n---------\n\n"
251 "Value Sprec Sassoc Tag\n"),
255 for (i
= ntokens
; i
< nsyms
; i
++)
256 fprintf (out
, "%5d %5d %5d %s\n",
258 symbols
[i
]->prec
, symbols
[i
]->assoc
,
263 fputs (("Rules\n-----\n\n"
264 "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n"),
268 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
270 rule
*rule_i
= &rules
[i
];
271 item_number
*rp
= NULL
;
272 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
273 unsigned int rhs_count
= 0;
274 /* Find the last RHS index in ritems. */
275 for (rp
= rule_i
->rhs
; *rp
>= 0; ++rp
)
277 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
279 rule_i
->prec
? rule_i
->prec
->prec
: 0,
280 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
283 rhs_itemno
+ rhs_count
- 1,
284 rule_i
->lhs
->number
);
285 /* Dumped the RHS. */
286 for (rp
= rule_i
->rhs
; *rp
>= 0; rp
++)
287 fprintf (out
, " %3d", *rp
);
288 fprintf (out
, " [%d]\n", item_number_as_rule_number (*rp
));
293 fputs ("Rules interpreted\n-----------------\n\n", out
);
296 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
298 fprintf (out
, "%-5d ", r
);
299 rule_print (&rules
[r
], out
);
306 /*------------------------------------------------------------------.
307 | Report on STDERR the rules that are not flagged USEFUL, using the |
308 | MESSAGE (which can be `useless rule' when invoked after grammar |
309 | reduction, or `never reduced' after conflicts were taken into |
311 `------------------------------------------------------------------*/
314 grammar_rules_never_reduced_report (const char *message
)
317 for (r
= 0; r
< nrules
; ++r
)
318 if (!rules
[r
].useful
)
320 location_print (stderr
, rules
[r
].location
);
321 fprintf (stderr
, ": %s: %s: ",
322 _("warning"), message
);
323 rule_print (&rules
[r
], stderr
);
332 free (token_translations
);
333 /* Free the symbol table data structure. */
335 free_merger_functions ();