]> git.saurik.com Git - bison.git/blob - src/print_graph.c
remove useless include.
[bison.git] / src / print_graph.c
1 /* Output a graph of the generated parser, for Bison.
2
3 Copyright (C) 2001-2007, 2009-2010 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program 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 3 of the License, or
10 (at your option) any later version.
11
12 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21 #include "system.h"
22
23 #include <quotearg.h>
24
25 #include "LR0.h"
26 #include "closure.h"
27 #include "complain.h"
28 #include "conflicts.h"
29 #include "files.h"
30 #include "getargs.h"
31 #include "gram.h"
32 #include "graphviz.h"
33 #include "lalr.h"
34 #include "print_graph.h"
35 #include "reader.h"
36 #include "state.h"
37 #include "symtab.h"
38
39
40 /*----------------------------.
41 | Construct the node labels. |
42 `----------------------------*/
43
44 static void
45 print_core (struct obstack *oout, state *s)
46 {
47 size_t i;
48 item_number *sitems = s->items;
49 size_t snritems = s->nitems;
50
51 /* Output all the items of a state, not only its kernel. */
52 if (report_flag & report_itemsets)
53 {
54 closure (sitems, snritems);
55 sitems = itemset;
56 snritems = nitemset;
57 }
58
59 obstack_fgrow1 (oout, "%d", s->number);
60 for (i = 0; i < snritems; i++)
61 {
62 item_number *sp;
63 item_number *sp1;
64 rule_number r;
65
66 sp1 = sp = ritem + sitems[i];
67
68 while (*sp >= 0)
69 sp++;
70
71 r = item_number_as_rule_number (*sp);
72
73 obstack_fgrow1 (oout, "\n%s -> ", rules[r].lhs->tag);
74
75 for (sp = rules[r].rhs; sp < sp1; sp++)
76 obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag);
77
78 obstack_1grow (oout, '.');
79
80 for (/* Nothing */; *sp >= 0; ++sp)
81 obstack_fgrow1 (oout, " %s", symbols[*sp]->tag);
82
83 /* Experimental feature: display the lookahead tokens. */
84 if (report_flag & report_lookahead_tokens
85 && item_number_is_rule_number (*sp1))
86 {
87 /* Find the reduction we are handling. */
88 reductions *reds = s->reductions;
89 int redno = state_reduction_find (s, &rules[r]);
90
91 /* Print them if there are. */
92 if (reds->lookahead_tokens && redno != -1)
93 {
94 bitset_iterator biter;
95 int k;
96 char const *sep = "";
97 obstack_sgrow (oout, "[");
98 BITSET_FOR_EACH (biter, reds->lookahead_tokens[redno], k, 0)
99 {
100 obstack_fgrow2 (oout, "%s%s", sep, symbols[k]->tag);
101 sep = ", ";
102 }
103 obstack_sgrow (oout, "]");
104 }
105 }
106 }
107 }
108
109
110 /*---------------------------------------------------------------.
111 | Output in graph_obstack edges specifications in incidence with |
112 | current node. |
113 `---------------------------------------------------------------*/
114
115 static void
116 print_actions (state const *s, FILE *fgraph)
117 {
118 int i;
119
120 transitions const *trans = s->transitions;
121
122 if (!trans->num && !s->reductions)
123 return;
124
125 for (i = 0; i < trans->num; i++)
126 if (!TRANSITION_IS_DISABLED (trans, i))
127 {
128 state *s1 = trans->states[i];
129 symbol_number sym = s1->accessing_symbol;
130
131 /* Shifts are solid, gotos are dashed, and error is dotted. */
132 char const *style =
133 (TRANSITION_IS_ERROR (trans, i) ? "dotted"
134 : TRANSITION_IS_SHIFT (trans, i) ? "solid"
135 : "dashed");
136
137 if (TRANSITION_IS_ERROR (trans, i)
138 && strcmp (symbols[sym]->tag, "error") != 0)
139 abort ();
140 output_edge (s->number, s1->number,
141 TRANSITION_IS_ERROR (trans, i) ? NULL : symbols[sym]->tag,
142 style, fgraph);
143 }
144 }
145
146
147 /*-------------------------------------------------------------.
148 | Output in FGRAPH the current node specifications and exiting |
149 | edges. |
150 `-------------------------------------------------------------*/
151
152 static void
153 print_state (state *s, FILE *fgraph)
154 {
155 struct obstack node_obstack;
156
157 /* A node's label contains its items. */
158 obstack_init (&node_obstack);
159 print_core (&node_obstack, s);
160 obstack_1grow (&node_obstack, '\0');
161 output_node (s->number, obstack_finish (&node_obstack), fgraph);
162 obstack_free (&node_obstack, 0);
163
164 /* Output the edges. */
165 print_actions (s, fgraph);
166 }
167 \f
168
169 void
170 print_graph (void)
171 {
172 state_number i;
173 FILE *fgraph = xfopen (spec_graph_file, "w");
174 start_graph (fgraph);
175
176 /* Output nodes and edges. */
177 new_closure (nritems);
178 for (i = 0; i < nstates; i++)
179 print_state (states[i], fgraph);
180 free_closure ();
181
182 finish_graph (fgraph);
183 xfclose (fgraph);
184 }