]>
git.saurik.com Git - bison.git/blob - src/gram.c
1 /* Allocate input grammar variables for Bison.
3 Copyright (C) 1984, 1986, 1989, 2001-2003, 2005-2013 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
*r
)
52 return r
->number
< nrules
;
56 rule_useless_in_grammar_p (rule
*r
)
58 return !rule_useful_in_grammar_p (r
);
62 rule_useless_in_parser_p (rule
*r
)
64 return !r
->useful
&& rule_useful_in_grammar_p (r
);
68 rule_lhs_print (rule
*r
, symbol
*previous_lhs
, FILE *out
)
70 fprintf (out
, " %3d ", r
->number
);
71 if (previous_lhs
!= r
->lhs
)
73 fprintf (out
, "%s:", r
->lhs
->tag
);
78 for (n
= strlen (previous_lhs
->tag
); n
> 0; --n
)
85 rule_lhs_print_xml (rule
*r
, FILE *out
, int level
)
87 xml_printf (out
, level
, "<lhs>%s</lhs>", r
->lhs
->tag
);
91 rule_rhs_length (rule
*r
)
95 for (rhsp
= r
->rhs
; *rhsp
>= 0; ++rhsp
)
101 rule_rhs_print (rule
*r
, FILE *out
)
106 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
107 fprintf (out
, " %s", symbols
[*rp
]->tag
);
112 fprintf (out
, " /* %s */\n", _("empty"));
117 rule_rhs_print_xml (rule
*r
, FILE *out
, int level
)
122 xml_puts (out
, level
, "<rhs>");
123 for (rp
= r
->rhs
; *rp
>= 0; rp
++)
124 xml_printf (out
, level
+ 1, "<symbol>%s</symbol>",
125 xml_escape (symbols
[*rp
]->tag
));
126 xml_puts (out
, level
, "</rhs>");
130 xml_puts (out
, level
, "<rhs>");
131 xml_puts (out
, level
+ 1, "<empty/>");
132 xml_puts (out
, level
, "</rhs>");
137 rule_print (rule
*r
, FILE *out
)
139 fprintf (out
, "%s:", r
->lhs
->tag
);
140 rule_rhs_print (r
, out
);
144 ritem_print (FILE *out
)
147 fputs ("RITEM\n", out
);
148 for (i
= 0; i
< nritems
; ++i
)
150 fprintf (out
, " %s", symbols
[ritem
[i
]]->tag
);
152 fprintf (out
, " (rule %d)\n", item_number_as_rule_number (ritem
[i
]));
157 ritem_longest_rhs (void)
162 for (r
= 0; r
< nrules
; ++r
)
164 int length
= rule_rhs_length (&rules
[r
]);
173 grammar_rules_partial_print (FILE *out
, const char *title
,
178 symbol
*previous_lhs
= NULL
;
180 /* rule # : LHS -> RHS */
181 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
183 if (filter
&& !filter (&rules
[r
]))
186 fprintf (out
, "%s\n\n", title
);
187 else if (previous_lhs
&& previous_lhs
!= rules
[r
].lhs
)
190 rule_lhs_print (&rules
[r
], previous_lhs
, out
);
191 rule_rhs_print (&rules
[r
], out
);
192 previous_lhs
= rules
[r
].lhs
;
199 grammar_rules_print (FILE *out
)
201 grammar_rules_partial_print (out
, _("Grammar"), rule_useful_in_grammar_p
);
205 grammar_rules_print_xml (FILE *out
, int level
)
210 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
213 xml_puts (out
, level
+ 1, "<rules>");
216 char const *usefulness
;
217 if (rule_useless_in_grammar_p (&rules
[r
]))
218 usefulness
= "useless-in-grammar";
219 else if (rule_useless_in_parser_p (&rules
[r
]))
220 usefulness
= "useless-in-parser";
222 usefulness
= "useful";
223 xml_indent (out
, level
+ 2);
224 fprintf (out
, "<rule number=\"%d\" usefulness=\"%s\"",
225 rules
[r
].number
, usefulness
);
226 if (rules
[r
].precsym
)
227 fprintf (out
, " percent_prec=\"%s\"",
228 xml_escape (rules
[r
].precsym
->tag
));
231 rule_lhs_print_xml (&rules
[r
], out
, level
+ 3);
232 rule_rhs_print_xml (&rules
[r
], out
, level
+ 3);
233 xml_puts (out
, level
+ 2, "</rule>");
236 xml_puts (out
, level
+ 1, "</rules>");
238 xml_puts (out
, level
+ 1, "<rules/>");
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 fprintf (out
, "Variables\n---------\n\n");
253 fprintf (out
, "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
,
260 fprintf (out
, "\n\n");
263 fprintf (out
, "Rules\n-----\n\n");
266 fprintf (out
, "Num (Prec, Assoc, Useful, Ritem Range) Lhs -> Rhs (Ritem range) [Num]\n");
267 for (i
= 0; i
< nrules
+ nuseless_productions
; i
++)
269 rule
*rule_i
= &rules
[i
];
270 item_number
*rp
= NULL
;
271 unsigned int rhs_itemno
= rule_i
->rhs
- ritem
;
272 unsigned int rhs_count
= 0;
273 /* Find the last RHS index in ritems. */
274 for (rp
= rule_i
->rhs
; *rp
>= 0; ++rp
)
276 fprintf (out
, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
278 rule_i
->prec
? rule_i
->prec
->prec
: 0,
279 rule_i
->prec
? rule_i
->prec
->assoc
: 0,
282 rhs_itemno
+ rhs_count
- 1,
283 rule_i
->lhs
->number
);
284 /* Dumped the RHS. */
285 for (rp
= rule_i
->rhs
; *rp
>= 0; rp
++)
286 fprintf (out
, " %3d", *rp
);
287 fprintf (out
, " [%d]\n", item_number_as_rule_number (*rp
));
290 fprintf (out
, "\n\n");
292 fprintf (out
, "Rules interpreted\n-----------------\n\n");
295 for (r
= 0; r
< nrules
+ nuseless_productions
; r
++)
297 fprintf (out
, "%-5d ", r
);
298 rule_print (&rules
[r
], out
);
301 fprintf (out
, "\n\n");
305 grammar_rules_useless_report (const char *message
)
308 for (r
= 0; r
< nrules
; ++r
)
309 if (!rules
[r
].useful
)
311 if (feature_flag
& feature_caret
)
312 warn_at (rules
[r
].location
, "%s", message
);
315 warn_at (rules
[r
].location
, "%s: ", message
);
316 if (warnings_flag
& warnings_other
)
318 rule_print (&rules
[r
], stderr
);
331 free (token_translations
);
332 /* Free the symbol table data structure. */
334 free_merger_functions ();