]>
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
4 2007 Free 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/>. */
30 #include "print-xml.h"
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;
49 /*--------------------------------------------------------------.
50 | Return true IFF the rule has a `number' smaller than NRULES. |
51 `--------------------------------------------------------------*/
54 rule_useful_p (rule
*r
)
56 return r
->number
< nrules
;
60 /*-------------------------------------------------------------.
61 | Return true IFF the rule has a `number' higher than NRULES. |
62 `-------------------------------------------------------------*/
65 rule_useless_p (rule
*r
)
67 return !rule_useful_p (r
);
71 /*--------------------------------------------------------------------.
72 | Return true IFF the rule is not flagged as useful *and* is useful. |
73 | In other words, it was discarded because of conflicts. |
74 `--------------------------------------------------------------------*/
77 rule_never_reduced_p (rule
*r
)
79 return !r
->useful
&& rule_useful_p (r
);
83 /*----------------------------------------------------------------.
84 | Print this RULE's number and lhs on OUT. If a PREVIOUS_LHS was |
85 | already displayed (by a previous call for another rule), avoid |
86 | useless repetitions. |
87 `----------------------------------------------------------------*/
90 rule_lhs_print (rule
*r
, symbol
*previous_lhs
, FILE *out
)
92 fprintf (out
, " %3d ", r
->number
);
93 if (previous_lhs
!= r
->lhs
)
95 fprintf (out
, "%s:", r
->lhs
->tag
);
100 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
107 rule_lhs_print_xml (rule
*r
, FILE *out
, int level
)
109 xml_printf (out
, level
, "<lhs>%s</lhs>", r
->lhs
->tag
);
113 /*--------------------------------------.
114 | Return the number of symbols in RHS. |
115 `--------------------------------------*/
118 rule_rhs_length (rule
*r
)
122 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
128 /*-------------------------------.
129 | Print this rule's RHS on OUT. |
130 `-------------------------------*/
133 rule_rhs_print (rule
*r
, FILE *out
)
138 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
139 fprintf (out
, " %s", symbols
[*rp
]->tag
);
144 fprintf (out
, " /* %s */\n", _("empty"));
149 rule_rhs_print_xml (rule
*r
, FILE *out
, int level
)
154 xml_puts (out
, level
, "<rhs>");
155 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
156 xml_printf (out
, level
+ 1, "<symbol class=\"%s\">%s</symbol>",
157 symbol_class_get_string (symbols
[*rp
]),
158 xml_escape (symbols
[*rp
]->tag
));
159 xml_puts (out
, level
, "</rhs>");
163 xml_puts (out
, level
, "<rhs>");
164 xml_puts (out
, level
+ 1, "<empty/>");
165 xml_puts (out
, level
, "</rhs>");
169 /*-------------------------.
170 | Print this rule on OUT. |
171 `-------------------------*/
174 rule_print (rule
*r
, FILE *out
)
176 fprintf (out
, "%s:", r
->lhs
->tag
);
177 rule_rhs_print (r
, out
);
181 /*------------------------.
182 | Dump RITEM for traces. |
183 `------------------------*/
186 ritem_print (FILE *out
)
189 fputs ("RITEM\n", out
);
190 for (i
= 0; i
< nritems
; ++i
)
192 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
194 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
199 /*------------------------------------------.
200 | Return the size of the longest rule RHS. |
201 `------------------------------------------*/
204 ritem_longest_rhs (void)
209 for (r
= 0; r
< nrules
; ++r
)
211 int length
= rule_rhs_length (&rules
[r
]);
220 /*-----------------------------------------------------------------.
221 | Print the grammar's rules that match FILTER on OUT under TITLE. |
222 `-----------------------------------------------------------------*/
225 grammar_rules_partial_print (FILE *out
, const char *title
,
230 symbol
*previous_lhs
= NULL
;
232 /* rule # : LHS -> RHS */
233 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
235 if (filter
&& !filter (&rules
[r
]))
238 fprintf (out
, "%s\n\n", title
);
239 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
242 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
243 rule_rhs_print (&rules
[r
], out
);
244 previous_lhs
= rules
[r
].lhs
;
251 /*----------------------------------------------------------.
252 | Print the grammar's rules that match FILTER on OUT (XML). |
253 `-----------------------------------------------------------*/
256 grammar_rules_partial_print_xml (FILE *out
, int level
, bool rtag
,
262 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
264 if (filter
&& !filter (&rules
[r
]))
267 xml_puts (out
, level
+ 1, "<rules>");
270 xml_printf (out
, level
+ 2, "<rule number=\"%d\">",
272 rule_lhs_print_xml (&rules
[r
], out
, level
+ 3);
273 rule_rhs_print_xml (&rules
[r
], out
, level
+ 3);
274 xml_puts (out
, level
+ 2, "</rule>");
279 xml_puts (out
, level
+ 1, "</rules>");
281 xml_puts (out
, level
+ 1, "<rules/>");
285 /*------------------------------------------.
286 | Print the grammar's useful rules on OUT. |
287 `------------------------------------------*/
290 grammar_rules_print (FILE *out
)
292 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_p
);
296 grammar_rules_print_xml (FILE *out
, int level
)
298 grammar_rules_partial_print_xml (out
, level
, true, rule_useful_p
);
302 /*-------------------.
303 | Dump the grammar. |
304 `-------------------*/
307 grammar_dump (FILE *out
, const char *title
)
309 fprintf (out
, "%s\n\n", title
);
311 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
312 ntokens
, nvars
, nsyms
, nrules
, nritems
);
315 fprintf (out
, "Variables\n---------\n\n");
318 fprintf (out
, "Value Sprec Sassoc Tag\n");
320 for (i
= ntokens
; i
< nsyms
; i
++)
321 fprintf (out
, "%5d %5d %5d %s\n",
323 symbols
[i
]->prec
, symbols
[i
]->assoc
,
325 fprintf (out
, "\n\n");
328 fprintf (out
, "Rules\n-----\n\n");
331 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
332 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
334 rule
*rule_i
= &rules
[i
];
335 item_number
*rp
= NULL
;
336 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
337 unsigned int rhs_count
= 0;
338 /* Find the last RHS index in ritems. */
339 for (rp
= rule_i
->rhs
; *rp
>= 0; ++rp
)
341 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
343 rule_i
->prec
? rule_i
->prec
->prec
: 0,
344 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
347 rhs_itemno
+ rhs_count
- 1,
348 rule_i
->lhs
->number
);
349 /* Dumped the RHS. */
350 for (rp
= rule_i
->rhs
; *rp
>= 0; rp
++)
351 fprintf (out
, " %3d", *rp
);
352 fprintf (out
, " [%d]\n", item_number_as_rule_number (*rp
));
355 fprintf (out
, "\n\n");
357 fprintf (out
, "Rules interpreted\n-----------------\n\n");
360 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
362 fprintf (out
, "%-5d ", r
);
363 rule_print (&rules
[r
], out
);
366 fprintf (out
, "\n\n");
370 /*------------------------------------------------------------------.
371 | Report on STDERR the rules that are not flagged USEFUL, using the |
372 | MESSAGE (which can be `useless rule' when invoked after grammar |
373 | reduction, or `never reduced' after conflicts were taken into |
375 `------------------------------------------------------------------*/
378 grammar_rules_never_reduced_report (const char *message
)
381 for (r
= 0; r
< nrules
; ++r
)
382 if (!rules
[r
].useful
)
384 location_print (stderr
, rules
[r
].location
);
385 fprintf (stderr
, ": %s: %s: ", _("warning"), message
);
386 rule_print (&rules
[r
], stderr
);
396 free (token_translations
);
397 /* Free the symbol table data structure. */
399 free_merger_functions ();