]>
git.saurik.com Git - bison.git/blob - src/gram.c
1 /* Allocate input grammar variables for bison,
2 Copyright (C) 1984, 1986, 1989, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
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;
52 /*--------------------------------------------------------------.
53 | Return true IFF the rule has a `number' smaller than NRULES. |
54 `--------------------------------------------------------------*/
57 rule_useful_p (rule
*r
)
59 return r
->number
< nrules
;
63 /*-------------------------------------------------------------.
64 | Return true IFF the rule has a `number' higher than NRULES. |
65 `-------------------------------------------------------------*/
68 rule_useless_p (rule
*r
)
70 return r
->number
>= nrules
;
74 /*--------------------------------------------------------------------.
75 | Return true IFF the rule is not flagged as useful *and* is useful. |
76 | In other words, it was discarded because of conflicts. |
77 `--------------------------------------------------------------------*/
80 rule_never_reduced_p (rule
*r
)
82 return !r
->useful
&& r
->number
< nrules
;
86 /*----------------------------------------------------------------.
87 | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
88 | already displayed (by a previous call for another rule), avoid |
89 | useless repetitions. |
90 `----------------------------------------------------------------*/
93 rule_lhs_print (rule
*r
, symbol
*previous_lhs
, FILE *out
)
95 fprintf (out
, " %3d ", r
->number
);
96 if (previous_lhs
!= r
->lhs
)
98 fprintf (out
, "%s:", r
->lhs
->tag
);
103 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
110 /*--------------------------------------.
111 | Return the number of symbols in RHS. |
112 `--------------------------------------*/
115 rule_rhs_length (rule
*r
)
119 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
125 /*-------------------------------.
126 | Print this rule's RHS on OUT. |
127 `-------------------------------*/
130 rule_rhs_print (rule
*r
, FILE *out
)
135 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
136 fprintf (out
, " %s", symbols
[*rp
]->tag
);
141 fprintf (out
, " /* %s */\n", _("empty"));
146 /*-------------------------.
147 | Print this rule on OUT. |
148 `-------------------------*/
151 rule_print (rule
*r
, FILE *out
)
153 fprintf (out
, "%s:", r
->lhs
->tag
);
154 rule_rhs_print (r
, out
);
158 /*------------------------.
159 | Dump RITEM for traces. |
160 `------------------------*/
163 ritem_print (FILE *out
)
166 fputs ("RITEM\n", out
);
167 for (i
= 0; i
< nritems
; ++i
)
169 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
171 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
176 /*------------------------------------------.
177 | Return the size of the longest rule RHS. |
178 `------------------------------------------*/
181 ritem_longest_rhs (void)
186 for (r
= 0; r
< nrules
; ++r
)
188 int length
= rule_rhs_length (&rules
[r
]);
197 /*-----------------------------------------------------------------.
198 | Print the grammar's rules that match FILTER on OUT under TITLE. |
199 `-----------------------------------------------------------------*/
202 grammar_rules_partial_print (FILE *out
, const char *title
,
207 symbol
*previous_lhs
= NULL
;
209 /* rule # : LHS -> RHS */
210 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
212 if (filter
&& !filter (&rules
[r
]))
215 fprintf (out
, "%s\n\n", title
);
216 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
219 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
220 rule_rhs_print (&rules
[r
], out
);
221 previous_lhs
= rules
[r
].lhs
;
228 /*------------------------------------------.
229 | Print the grammar's useful rules on OUT. |
230 `------------------------------------------*/
233 grammar_rules_print (FILE *out
)
235 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_p
);
239 /*-------------------.
240 | Dump the grammar. |
241 `-------------------*/
244 grammar_dump (FILE *out
, const char *title
)
246 fprintf (out
, "%s\n\n", title
);
248 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
249 ntokens
, nvars
, nsyms
, nrules
, nritems
);
252 fprintf (out
, "Variables\n---------\n\n");
255 fprintf (out
, "Value Sprec Sassoc Tag\n");
257 for (i
= ntokens
; i
< nsyms
; i
++)
258 fprintf (out
, "%5d %5d %5d %s\n",
260 symbols
[i
]->prec
, symbols
[i
]->assoc
,
262 fprintf (out
, "\n\n");
265 fprintf (out
, "Rules\n-----\n\n");
268 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
269 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
271 rule
*rule_i
= &rules
[i
];
272 item_number
*r
= NULL
;
273 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
274 unsigned int rhs_count
= 0;
275 /* Find the last RHS index in ritems. */
276 for (r
= rule_i
->rhs
; *r
>= 0; ++r
)
278 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
280 rule_i
->prec
? rule_i
->prec
->prec
: 0,
281 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
284 rhs_itemno
+ rhs_count
- 1,
285 rule_i
->lhs
->number
);
286 /* Dumped the RHS. */
287 for (r
= rule_i
->rhs
; *r
>= 0; r
++)
288 fprintf (out
, " %3d", *r
);
289 fprintf (out
, " [%d]\n", item_number_as_rule_number (*r
));
292 fprintf (out
, "\n\n");
294 fprintf (out
, "Rules interpreted\n-----------------\n\n");
297 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
299 fprintf (out
, "%-5d ", r
);
300 rule_print (&rules
[r
], out
);
303 fprintf (out
, "\n\n");
307 /*------------------------------------------------------------------.
308 | Report on STDERR the rules that are not flagged USEFUL, using the |
309 | MESSAGE (which can be `useless rule' when invoked after grammar |
310 | reduction, or `never reduced' after conflicts were taken into |
312 `------------------------------------------------------------------*/
315 grammar_rules_never_reduced_report (const char *message
)
318 for (r
= 0; r
< nrules
; ++r
)
319 if (!rules
[r
].useful
)
321 location_print (stderr
, rules
[r
].location
);
322 fprintf (stderr
, ": %s: %s: ",
323 _("warning"), message
);
324 rule_print (&rules
[r
], stderr
);
333 XFREE (token_translations
);
334 /* Free the symbol table data structure. */
336 free_merger_functions ();