]>
git.saurik.com Git - bison.git/blob - src/gram.c
4ee568676a42a6b60c35eea455c548d86a88cec7
1 /* Allocate input grammar variables for Bison.
3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2012 Free Software
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/>. */
27 #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;
50 rule_useful_in_grammar_p (rule
const *r
)
52 return r
->number
< nrules
;
56 rule_useless_in_grammar_p (rule
const *r
)
58 return !rule_useful_in_grammar_p (r
);
62 rule_useless_in_parser_p (rule
const *r
)
64 return !r
->useful
&& rule_useful_in_grammar_p (r
);
68 rule_lhs_print (rule
const *r
, symbol
const *previous_lhs
, FILE *out
)
70 fprintf (out
, " %3d ", r
->number
);
71 if (previous_lhs
!= r
->lhs
)
72 fprintf (out
, "%s:", r
->lhs
->tag
);
74 fprintf (out
, "%*s|", (int) strlen (previous_lhs
->tag
), "");
78 rule_lhs_print_xml (rule
const *r
, FILE *out
, int level
)
80 xml_printf (out
, level
, "<lhs>%s</lhs>", r
->lhs
->tag
);
84 rule_rhs_length (rule
const *r
)
88 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
94 rule_rhs_print (rule
const *r
, FILE *out
)
99 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
100 fprintf (out
, " %s", symbols
[*rp
]->tag
);
104 fprintf (out
, " /* %s */", _("empty"));
109 rule_rhs_print_xml (rule
const *r
, FILE *out
, int level
)
114 xml_puts (out
, level
, "<rhs>");
115 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
116 xml_printf (out
, level
+ 1, "<symbol>%s</symbol>",
117 xml_escape (symbols
[*rp
]->tag
));
118 xml_puts (out
, level
, "</rhs>");
122 xml_puts (out
, level
, "<rhs>");
123 xml_puts (out
, level
+ 1, "<empty/>");
124 xml_puts (out
, level
, "</rhs>");
129 rule_print (rule
const *r
, FILE *out
)
131 fprintf (out
, "%s:", r
->lhs
->tag
);
132 rule_rhs_print (r
, out
);
136 ritem_print (FILE *out
)
139 fputs ("RITEM\n", out
);
140 for (i
= 0; i
< nritems
; ++i
)
142 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
144 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
149 ritem_longest_rhs (void)
154 for (r
= 0; r
< nrules
; ++r
)
156 int length
= rule_rhs_length (&rules
[r
]);
165 grammar_rules_partial_print (FILE *out
, const char *title
,
170 symbol
*previous_lhs
= NULL
;
172 /* rule # : LHS -> RHS */
173 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
175 if (filter
&& !filter (&rules
[r
]))
178 fprintf (out
, "%s\n\n", title
);
179 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
182 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
183 rule_rhs_print (&rules
[r
], out
);
185 previous_lhs
= rules
[r
].lhs
;
192 grammar_rules_print (FILE *out
)
194 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_in_grammar_p
);
198 grammar_rules_print_xml (FILE *out
, int level
)
203 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
206 xml_puts (out
, level
+ 1, "<rules>");
209 char const *usefulness
;
210 if (rule_useless_in_grammar_p (&rules
[r
]))
211 usefulness
= "useless-in-grammar";
212 else if (rule_useless_in_parser_p (&rules
[r
]))
213 usefulness
= "useless-in-parser";
215 usefulness
= "useful";
216 xml_indent (out
, level
+ 2);
217 fprintf (out
, "<rule number=\"%d\" usefulness=\"%s\"",
218 rules
[r
].number
, usefulness
);
219 if (rules
[r
].precsym
)
220 fprintf (out
, " percent_prec=\"%s\"",
221 xml_escape (rules
[r
].precsym
->tag
));
224 rule_lhs_print_xml (&rules
[r
], out
, level
+ 3);
225 rule_rhs_print_xml (&rules
[r
], out
, level
+ 3);
226 xml_puts (out
, level
+ 2, "</rule>");
229 xml_puts (out
, level
+ 1, "</rules>");
231 xml_puts (out
, level
+ 1, "<rules/>");
235 grammar_dump (FILE *out
, const char *title
)
237 fprintf (out
, "%s\n\n", title
);
239 "ntokens = %d, nvars = %d, nsyms = %d, nrules = %d, nritems = %d\n\n",
240 ntokens
, nvars
, nsyms
, nrules
, nritems
);
243 fprintf (out
, "Variables\n---------\n\n");
246 fprintf (out
, "Value Sprec Sassoc Tag\n");
248 for (i
= ntokens
; i
< nsyms
; i
++)
249 fprintf (out
, "%5d %5d %5d %s\n",
251 symbols
[i
]->prec
, symbols
[i
]->assoc
,
253 fprintf (out
, "\n\n");
256 fprintf (out
, "Rules\n-----\n\n");
260 "Num (Prec, Assoc, Useful, Ritem Range) Lhs"
261 " -> Rhs (Ritem range) [Num]\n");
262 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
264 rule
const *rule_i
= &rules
[i
];
265 item_number
*rp
= NULL
;
266 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
267 unsigned int rhs_count
= 0;
268 /* Find the last RHS index in ritems. */
269 for (rp
= rule_i
->rhs
; *rp
>= 0; ++rp
)
271 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
273 rule_i
->prec
? rule_i
->prec
->prec
: 0,
274 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
277 rhs_itemno
+ rhs_count
- 1,
278 rule_i
->lhs
->number
);
279 /* Dumped the RHS. */
280 for (rp
= rule_i
->rhs
; *rp
>= 0; rp
++)
281 fprintf (out
, " %3d", *rp
);
282 fprintf (out
, " [%d]\n", item_number_as_rule_number (*rp
));
285 fprintf (out
, "\n\n");
287 fprintf (out
, "Rules interpreted\n-----------------\n\n");
290 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
292 fprintf (out
, "%-5d ", r
);
293 rule_print (&rules
[r
], out
);
297 fprintf (out
, "\n\n");
301 grammar_rules_useless_report (const char *message
)
304 if (warnings_flag
& w
)
307 for (r
= 0; r
< nrules
; ++r
)
308 if (!rules
[r
].useful
)
310 if (feature_flag
& feature_caret
)
311 complain (&rules
[r
].location
, w
, "%s", message
);
314 complain (&rules
[r
].location
, w
| silent
, "%s: ", message
);
315 rule_print (&rules
[r
], stderr
);
316 warnings_print_categories (w
);
317 fprintf (stderr
, "\n");
329 free (token_translations
);
330 /* Free the symbol table data structure. */
332 free_merger_functions ();