]>
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 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
32 /* Comments for these variables are in gram.h. */
34 item_number
*ritem
= NULL
;
35 unsigned int nritems
= 0;
38 rule_number nrules
= 0;
40 symbol
**symbols
= NULL
;
45 symbol_number
*token_translations
= NULL
;
47 int max_user_token_number
= 256;
53 /*--------------------------------------------------------------.
54 | Return true IFF the rule has a `number' smaller than NRULES. |
55 `--------------------------------------------------------------*/
58 rule_useful_p (rule
*r
)
60 return r
->number
< nrules
;
64 /*-------------------------------------------------------------.
65 | Return true IFF the rule has a `number' higher than NRULES. |
66 `-------------------------------------------------------------*/
69 rule_useless_p (rule
*r
)
71 return r
->number
>= nrules
;
75 /*--------------------------------------------------------------------.
76 | Return true IFF the rule is not flagged as useful *and* is useful. |
77 | In other words, it was discarded because of conflicts. |
78 `--------------------------------------------------------------------*/
81 rule_never_reduced_p (rule
*r
)
83 return !r
->useful
&& r
->number
< nrules
;
87 /*----------------------------------------------------------------.
88 | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
89 | already displayed (by a previous call for another rule), avoid |
90 | useless repetitions. |
91 `----------------------------------------------------------------*/
94 rule_lhs_print (rule
*r
, symbol
*previous_lhs
, FILE *out
)
96 fprintf (out
, " %3d ", r
->number
);
97 if (previous_lhs
!= r
->lhs
)
99 fprintf (out
, "%s:", r
->lhs
->tag
);
104 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
111 /*--------------------------------------.
112 | Return the number of symbols in RHS. |
113 `--------------------------------------*/
116 rule_rhs_length (rule
*r
)
120 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
126 /*-------------------------------.
127 | Print this rule's RHS on OUT. |
128 `-------------------------------*/
131 rule_rhs_print (rule
*r
, FILE *out
)
136 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
137 fprintf (out
, " %s", symbols
[*rp
]->tag
);
142 fprintf (out
, " /* %s */\n", _("empty"));
147 /*-------------------------.
148 | Print this rule on OUT. |
149 `-------------------------*/
152 rule_print (rule
*r
, FILE *out
)
154 fprintf (out
, "%s:", r
->lhs
->tag
);
155 rule_rhs_print (r
, out
);
159 /*------------------------.
160 | Dump RITEM for traces. |
161 `------------------------*/
164 ritem_print (FILE *out
)
167 fputs ("RITEM\n", out
);
168 for (i
= 0; i
< nritems
; ++i
)
170 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
172 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
177 /*------------------------------------------.
178 | Return the size of the longest rule RHS. |
179 `------------------------------------------*/
182 ritem_longest_rhs (void)
187 for (r
= 0; r
< nrules
; ++r
)
189 int length
= rule_rhs_length (&rules
[r
]);
198 /*-----------------------------------------------------------------.
199 | Print the grammar's rules that match FILTER on OUT under TITLE. |
200 `-----------------------------------------------------------------*/
203 grammar_rules_partial_print (FILE *out
, const char *title
,
208 symbol
*previous_lhs
= NULL
;
210 /* rule # : LHS -> RHS */
211 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
213 if (filter
&& !filter (&rules
[r
]))
216 fprintf (out
, "%s\n\n", title
);
217 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
220 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
221 rule_rhs_print (&rules
[r
], out
);
222 previous_lhs
= rules
[r
].lhs
;
229 /*------------------------------------------.
230 | Print the grammar's useful rules on OUT. |
231 `------------------------------------------*/
234 grammar_rules_print (FILE *out
)
236 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_p
);
240 /*-------------------.
241 | Dump the grammar. |
242 `-------------------*/
245 grammar_dump (FILE *out
, const char *title
)
247 fprintf (out
, "%s\n\n", title
);
249 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
250 ntokens
, nvars
, nsyms
, nrules
, nritems
);
253 fprintf (out
, "Variables\n---------\n\n");
256 fprintf (out
, "Value Sprec Sassoc Tag\n");
258 for (i
= ntokens
; i
< nsyms
; i
++)
259 fprintf (out
, "%5d %5d %5d %s\n",
261 symbols
[i
]->prec
, symbols
[i
]->assoc
,
263 fprintf (out
, "\n\n");
266 fprintf (out
, "Rules\n-----\n\n");
269 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
270 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
272 rule
*rule_i
= &rules
[i
];
273 item_number
*r
= NULL
;
274 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
275 unsigned int rhs_count
= 0;
276 /* Find the last RHS index in ritems. */
277 for (r
= rule_i
->rhs
; *r
>= 0; ++r
)
279 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
281 rule_i
->prec
? rule_i
->prec
->prec
: 0,
282 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
285 rhs_itemno
+ rhs_count
- 1,
286 rule_i
->lhs
->number
);
287 /* Dumped the RHS. */
288 for (r
= rule_i
->rhs
; *r
>= 0; r
++)
289 fprintf (out
, " %3d", *r
);
290 fprintf (out
, " [%d]\n", item_number_as_rule_number (*r
));
293 fprintf (out
, "\n\n");
295 fprintf (out
, "Rules interpreted\n-----------------\n\n");
298 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
300 fprintf (out
, "%-5d ", r
);
301 rule_print (&rules
[r
], out
);
304 fprintf (out
, "\n\n");
308 /*------------------------------------------------------------------.
309 | Report on STDERR the rules that are not flagged USEFUL, using the |
310 | MESSAGE (which can be `useless rule' when invoked after grammar |
311 | reduction, or `never reduced' after conflicts were taken into |
313 `------------------------------------------------------------------*/
316 grammar_rules_never_reduced_report (const char *message
)
319 for (r
= 0; r
< nrules
; ++r
)
320 if (!rules
[r
].useful
)
322 location_print (stderr
, rules
[r
].location
);
323 fprintf (stderr
, ": %s: %s: ",
324 _("warning"), message
);
325 rule_print (&rules
[r
], stderr
);
334 XFREE (token_translations
);
335 /* Free the symbol table data structure. */
337 free_merger_functions ();