Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: bison/src/bison/2.4.1/bison-2.4.1-src/src/print-xml.c

Issue 10807020: Add native Windows binary for bison. (Closed) Base URL: svn://chrome-svn/chrome/trunk/deps/third_party/
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 /* Print an xml on generated parser, for Bison,
2
3 Copyright (C) 2007 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
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)
10 any later version.
11
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.
16
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., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include <config.h>
23 #include "system.h"
24
25 #include <stdarg.h>
26
27 #include <bitset.h>
28 #include <quotearg.h>
29
30 #include "LR0.h"
31 #include "closure.h"
32 #include "conflicts.h"
33 #include "files.h"
34 #include "getargs.h"
35 #include "gram.h"
36 #include "lalr.h"
37 #include "print.h"
38 #include "print-xml.h"
39 #include "reader.h"
40 #include "reduce.h"
41 #include "state.h"
42 #include "symtab.h"
43 #include "tables.h"
44
45 static bitset no_reduce_set;
46 struct escape_buf
47 {
48 char *ptr;
49 size_t size;
50 };
51 static struct escape_buf escape_bufs[2];
52
53
54 /*--------------------------------.
55 | Report information on a state. |
56 `--------------------------------*/
57
58 static void
59 print_core (FILE *out, int level, state *s)
60 {
61 size_t i;
62 item_number *sitems = s->items;
63 size_t snritems = s->nitems;
64
65 /* Output all the items of a state, not only its kernel. */
66 closure (sitems, snritems);
67 sitems = itemset;
68 snritems = nitemset;
69
70 if (!snritems) {
71 xml_puts (out, level, "<itemset/>");
72 return;
73 }
74
75 xml_puts (out, level, "<itemset>");
76
77 for (i = 0; i < snritems; i++)
78 {
79 bool printed = false;
80 item_number *sp;
81 item_number *sp1;
82 rule_number r;
83
84 sp1 = sp = ritem + sitems[i];
85
86 while (*sp >= 0)
87 sp++;
88
89 r = item_number_as_rule_number (*sp);
90 sp = rules[r].rhs;
91
92 /* Display the lookahead tokens? */
93 if (item_number_is_rule_number (*sp1))
94 {
95 reductions *reds = s->reductions;
96 int red = state_reduction_find (s, &rules[r]);
97 /* Print item with lookaheads if there are. */
98 if (reds->lookahead_tokens && red != -1)
99 {
100 xml_printf (out, level + 1,
101 "<item rule-number=\"%d\" point=\"%d\">",
102 rules[r].number, sp1 - sp);
103 state_rule_lookahead_tokens_print_xml (s, &rules[r],
104 out, level + 2);
105 xml_puts (out, level + 1, "</item>");
106 printed = true;
107 }
108 }
109
110 if (!printed)
111 {
112 xml_printf (out, level + 1,
113 "<item rule-number=\"%d\" point=\"%d\"/>",
114 rules[r].number,
115 sp1 - sp);
116 }
117 }
118 xml_puts (out, level, "</itemset>");
119 }
120
121
122 /*-----------------------------------------------------------.
123 | Report the shifts if DISPLAY_SHIFTS_P or the gotos of S on |
124 | OUT. |
125 `-----------------------------------------------------------*/
126
127 static void
128 print_transitions (state *s, FILE *out, int level)
129 {
130 transitions *trans = s->transitions;
131 int n = 0;
132 int i;
133
134 for (i = 0; i < trans->num; i++)
135 if (!TRANSITION_IS_DISABLED (trans, i))
136 {
137 n++;
138 }
139
140 /* Nothing to report. */
141 if (!n) {
142 xml_puts (out, level, "<transitions/>");
143 return;
144 }
145
146 /* Report lookahead tokens and shifts. */
147 xml_puts (out, level, "<transitions>");
148
149 for (i = 0; i < trans->num; i++)
150 if (!TRANSITION_IS_DISABLED (trans, i)
151 && TRANSITION_IS_SHIFT (trans, i))
152 {
153 symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
154 char const *tag = sym->tag;
155 state *s1 = trans->states[i];
156
157 xml_printf (out, level + 1,
158 "<transition type=\"shift\" symbol=\"%s\" state=\"%d\"/>",
159 xml_escape (tag), s1->number);
160 }
161
162 for (i = 0; i < trans->num; i++)
163 if (!TRANSITION_IS_DISABLED (trans, i)
164 && !TRANSITION_IS_SHIFT (trans, i))
165 {
166 symbol *sym = symbols[TRANSITION_SYMBOL (trans, i)];
167 char const *tag = sym->tag;
168 state *s1 = trans->states[i];
169
170 xml_printf (out, level + 1,
171 "<transition type=\"goto\" symbol=\"%s\" state=\"%d\"/>",
172 xml_escape (tag), s1->number);
173 }
174
175 xml_puts (out, level, "</transitions>");
176 }
177
178
179 /*--------------------------------------------------------.
180 | Report the explicit errors of S raised from %nonassoc. |
181 `--------------------------------------------------------*/
182
183 static void
184 print_errs (FILE *out, int level, state *s)
185 {
186 errs *errp = s->errs;
187 bool count = false;
188 int i;
189
190 for (i = 0; i < errp->num; ++i)
191 if (errp->symbols[i])
192 count = true;
193
194 /* Nothing to report. */
195 if (!count) {
196 xml_puts (out, level, "<errors/>");
197 return;
198 }
199
200 /* Report lookahead tokens and errors. */
201 xml_puts (out, level, "<errors>");
202 for (i = 0; i < errp->num; ++i)
203 if (errp->symbols[i])
204 {
205 char const *tag = errp->symbols[i]->tag;
206 xml_printf (out, level + 1,
207 "<error symbol=\"%s\">nonassociative</error>",
208 xml_escape (tag));
209 }
210 xml_puts (out, level, "</errors>");
211 }
212
213
214 /*-------------------------------------------------------------------------.
215 | Report a reduction of RULE on LOOKAHEAD_TOKEN (which can be `default'). |
216 | If not ENABLED, the rule is masked by a shift or a reduce (S/R and |
217 | R/R conflicts). |
218 `-------------------------------------------------------------------------*/
219
220 static void
221 print_reduction (FILE *out, int level, char const *lookahead_token,
222 rule *r, bool enabled)
223 {
224 if (r->number)
225 xml_printf (out, level,
226 "<reduction symbol=\"%s\" rule=\"%d\" enabled=\"%s\"/>",
227 xml_escape (lookahead_token),
228 r->number,
229 enabled ? "true" : "false");
230 else
231 xml_printf (out, level,
232 "<reduction symbol=\"%s\" rule=\"accept\" enabled=\"%s\"/>",
233 xml_escape (lookahead_token),
234 enabled ? "true" : "false");
235 }
236
237
238 /*-------------------------------------------.
239 | Report on OUT the reduction actions of S. |
240 `-------------------------------------------*/
241
242 static void
243 print_reductions (FILE *out, int level, state *s)
244 {
245 transitions *trans = s->transitions;
246 reductions *reds = s->reductions;
247 rule *default_rule = NULL;
248 int report = false;
249 int i, j;
250
251 if (reds->num == 0) {
252 xml_puts (out, level, "<reductions/>");
253 return;
254 }
255
256 if (yydefact[s->number] != 0)
257 default_rule = &rules[yydefact[s->number] - 1];
258
259 bitset_zero (no_reduce_set);
260 FOR_EACH_SHIFT (trans, i)
261 bitset_set (no_reduce_set, TRANSITION_SYMBOL (trans, i));
262 for (i = 0; i < s->errs->num; ++i)
263 if (s->errs->symbols[i])
264 bitset_set (no_reduce_set, s->errs->symbols[i]->number);
265
266 if (default_rule)
267 report = true;
268
269 if (reds->lookahead_tokens)
270 for (i = 0; i < ntokens; i++)
271 {
272 bool count = bitset_test (no_reduce_set, i);
273
274 for (j = 0; j < reds->num; ++j)
275 if (bitset_test (reds->lookahead_tokens[j], i))
276 {
277 if (! count)
278 {
279 if (reds->rules[j] != default_rule)
280 report = true;
281 count = true;
282 }
283 else
284 {
285 report = true;
286 }
287 }
288 }
289
290 /* Nothing to report. */
291 if (!report) {
292 xml_puts (out, level, "<reductions/>");
293 return;
294 }
295
296 xml_puts (out, level, "<reductions>");
297
298 /* Report lookahead tokens (or $default) and reductions. */
299 if (reds->lookahead_tokens)
300 for (i = 0; i < ntokens; i++)
301 {
302 bool defaulted = false;
303 bool count = bitset_test (no_reduce_set, i);
304
305 for (j = 0; j < reds->num; ++j)
306 if (bitset_test (reds->lookahead_tokens[j], i))
307 {
308 if (! count)
309 {
310 if (reds->rules[j] != default_rule)
311 print_reduction (out, level + 1, symbols[i]->tag,
312 reds->rules[j], true);
313 else
314 defaulted = true;
315 count = true;
316 }
317 else
318 {
319 if (defaulted)
320 print_reduction (out, level + 1, symbols[i]->tag,
321 default_rule, true);
322 defaulted = false;
323 print_reduction (out, level + 1, symbols[i]->tag,
324 reds->rules[j], false);
325 }
326 }
327 }
328
329 if (default_rule)
330 print_reduction (out, level + 1,
331 "$default", default_rule, true);
332
333 xml_puts (out, level, "</reductions>");
334 }
335
336
337 /*--------------------------------------------------------------.
338 | Report on OUT all the actions (shifts, gotos, reductions, and |
339 | explicit erros from %nonassoc) of S. |
340 `--------------------------------------------------------------*/
341
342 static void
343 print_actions (FILE *out, int level, state *s)
344 {
345 xml_puts (out, level, "<actions>");
346 print_transitions (s, out, level + 1);
347 print_errs (out, level + 1, s);
348 print_reductions (out, level + 1, s);
349 xml_puts (out, level, "</actions>");
350 }
351
352
353 /*----------------------------------.
354 | Report all the data on S on OUT. |
355 `----------------------------------*/
356
357 static void
358 print_state (FILE *out, int level, state *s)
359 {
360 fputc ('\n', out);
361 xml_printf (out, level, "<state number=\"%d\">", s->number);
362 print_core (out, level + 1, s);
363 print_actions (out, level + 1, s);
364 if (s->solved_conflicts_xml)
365 {
366 xml_puts (out, level + 1, "<solved-conflicts>");
367 fputs (s->solved_conflicts_xml, out);
368 xml_puts (out, level + 1, "</solved-conflicts>");
369 }
370 else
371 xml_puts (out, level + 1, "<solved-conflicts/>");
372 xml_puts (out, level, "</state>");
373 }
374
375
376 /*-----------------------------------------.
377 | Print information on the whole grammar. |
378 `-----------------------------------------*/
379
380 static void
381 print_grammar (FILE *out, int level)
382 {
383 symbol_number i;
384
385 fputc ('\n', out);
386 xml_puts (out, level, "<grammar>");
387 grammar_rules_print_xml (out, level);
388
389 /* Terminals */
390 xml_puts (out, level + 1, "<terminals>");
391 for (i = 0; i < max_user_token_number + 1; i++)
392 if (token_translations[i] != undeftoken->number)
393 {
394 char const *tag = symbols[token_translations[i]]->tag;
395 int precedence = symbols[token_translations[i]]->prec;
396 assoc associativity = symbols[token_translations[i]]->assoc;
397 xml_indent (out, level + 2);
398 fprintf (out,
399 "<terminal symbol-number=\"%d\" token-number=\"%d\""
400 " name=\"%s\" usefulness=\"%s\"",
401 token_translations[i], i, xml_escape (tag),
402 reduce_token_unused_in_grammar (token_translations[i])
403 ? "unused-in-grammar" : "useful");
404 if (precedence)
405 fprintf (out, " prec=\"%d\"", precedence);
406 if (associativity != undef_assoc)
407 fprintf (out, " assoc=\"%s\"", assoc_to_string (associativity) + 1);
408 fputs ("/>\n", out);
409 }
410 xml_puts (out, level + 1, "</terminals>");
411
412 /* Nonterminals */
413 xml_puts (out, level + 1, "<nonterminals>");
414 for (i = ntokens; i < nsyms + nuseless_nonterminals; i++)
415 {
416 char const *tag = symbols[i]->tag;
417 xml_printf (out, level + 2,
418 "<nonterminal symbol-number=\"%d\" name=\"%s\""
419 " usefulness=\"%s\"/>",
420 i, xml_escape (tag),
421 reduce_nonterminal_useless_in_grammar (i)
422 ? "useless-in-grammar" : "useful");
423 }
424 xml_puts (out, level + 1, "</nonterminals>");
425 xml_puts (out, level, "</grammar>");
426 }
427
428 void
429 xml_indent (FILE *out, int level)
430 {
431 int i;
432 for (i = 0; i < level; i++)
433 fputs (" ", out);
434 }
435
436 void
437 xml_puts (FILE *out, int level, char const *s)
438 {
439 xml_indent (out, level);
440 fputs (s, out);
441 fputc ('\n', out);
442 }
443
444 void
445 xml_printf (FILE *out, int level, char const *fmt, ...)
446 {
447 va_list arglist;
448
449 xml_indent (out, level);
450
451 va_start (arglist, fmt);
452 vfprintf (out, fmt, arglist);
453 va_end (arglist);
454
455 fputc ('\n', out);
456 }
457
458 static char const *
459 xml_escape_string (struct escape_buf *buf, char const *str)
460 {
461 size_t len = strlen (str);
462 size_t max_expansion = sizeof "&quot;" - 1;
463 char *p;
464
465 if (buf->size <= max_expansion * len)
466 {
467 buf->size = max_expansion * len + 1;
468 buf->ptr = x2realloc (buf->ptr, &buf->size);
469 }
470 p = buf->ptr;
471
472 for (; *str; str++)
473 switch (*str)
474 {
475 default: *p++ = *str; break;
476 case '&': p = stpcpy (p, "&amp;" ); break;
477 case '<': p = stpcpy (p, "&lt;" ); break;
478 case '>': p = stpcpy (p, "&gt;" ); break;
479 case '"': p = stpcpy (p, "&quot;"); break;
480 }
481
482 *p = '\0';
483 return buf->ptr;
484 }
485
486 char const *
487 xml_escape_n (int n, char const *str)
488 {
489 return xml_escape_string (escape_bufs + n, str);
490 }
491
492 char const *
493 xml_escape (char const *str)
494 {
495 return xml_escape_n (0, str);
496 }
497
498 void
499 print_xml (void)
500 {
501 state_number i;
502 int level = 0;
503
504 FILE *out = xfopen (spec_xml_file, "w");
505
506 fputs ("<?xml version=\"1.0\"?>\n\n", out);
507 xml_printf (out, level, "<bison-xml-report version=\"%s\">",
508 xml_escape (VERSION));
509
510 fputc ('\n', out);
511 xml_printf (out, level + 1, "<filename>%s</filename>",
512 xml_escape (grammar_file));
513
514 /* print grammar */
515 print_grammar (out, level + 1);
516
517 new_closure (nritems);
518 no_reduce_set = bitset_create (ntokens, BITSET_FIXED);
519
520 /* print automaton */
521 fputc ('\n', out);
522 xml_puts (out, level + 1, "<automaton>");
523 for (i = 0; i < nstates; i++)
524 print_state (out, level + 2, states[i]);
525 xml_puts (out, level + 1, "</automaton>");
526
527 bitset_free (no_reduce_set);
528 free_closure ();
529
530 xml_puts (out, 0, "</bison-xml-report>");
531
532 free (escape_bufs[0].ptr);
533 free (escape_bufs[1].ptr);
534
535 xfclose (out);
536 }
OLDNEW
« no previous file with comments | « bison/src/bison/2.4.1/bison-2.4.1-src/src/print-xml.h ('k') | bison/src/bison/2.4.1/bison-2.4.1-src/src/print_graph.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698