]> git.saurik.com Git - bison.git/blob - src/print_graph.c
ced951b0386f16f02b4d8ba757642d7bbf274afe
[bison.git] / src / print_graph.c
1 /* Output a VCG description on generated parser, for Bison,
2 Copyright 2001 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 #include "system.h"
22 #include "files.h"
23 #include "gram.h"
24 #include "LR0.h"
25 #include "lalr.h"
26 #include "conflicts.h"
27 #include "complain.h"
28 #include "getargs.h"
29 #include "state.h"
30 #include "reader.h"
31 #include "obstack.h"
32 #include "print_graph.h"
33 #include "vcg.h"
34
35 static graph_t graph;
36 static FILE *fgraph = NULL;
37
38 /* This part will construct the label of nodes. */
39 static void
40 print_core (int state, struct obstack *node_obstack)
41 {
42 int i;
43 int k;
44 int rule;
45 core *statep;
46 short *sp;
47 short *sp1;
48
49 statep = state_table[state].state;
50 k = statep->nitems;
51
52 if (k == 0)
53 return;
54
55 for (i = 0; i < k; i++)
56 {
57 if (i)
58 obstack_1grow (node_obstack, '\n');
59
60 sp1 = sp = ritem + statep->items[i];
61
62 while (*sp > 0)
63 sp++;
64
65 rule = -(*sp);
66
67 obstack_fgrow1 (node_obstack, "%d: ", rule);
68 obstack_fgrow1 (node_obstack, " %s -> ",
69 tags[rule_table[rule].lhs]);
70
71 for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++)
72 obstack_fgrow1 (node_obstack, "%s ", tags[*sp]);
73
74 obstack_1grow (node_obstack, '.');
75
76 while (*sp > 0)
77 obstack_fgrow1 (node_obstack, " %s", tags[*sp++]);
78 }
79 }
80
81
82 /*---------------------------------------------------------------.
83 | Output in graph_obstack edges specifications in incidence with |
84 | current node. |
85 `---------------------------------------------------------------*/
86
87 static void
88 print_actions (int state, const char *node_name)
89 {
90 int i;
91 int k;
92 int state1;
93 int symbol;
94 shifts *shiftp;
95 errs *errp;
96 reductions *redp;
97 int rule;
98 static char buff[10];
99 edge_t edge;
100
101 shiftp = state_table[state].shift_table;
102 redp = state_table[state].reduction_table;
103 errp = err_table[state];
104
105 if (!shiftp && !redp)
106 {
107 #if 0
108 if (final_state == state)
109 obstack_sgrow (node_obstack, "$default: accept");
110 else
111 obstack_sgrow (node_obstack, "NO ACTIONS");
112 #endif
113 return;
114 }
115
116 if (shiftp)
117 {
118 k = shiftp->nshifts;
119
120 for (i = 0; i < k; i++)
121 {
122 if (!shiftp->shifts[i])
123 continue;
124 state1 = shiftp->shifts[i];
125 symbol = state_table[state1].accessing_symbol;
126
127 if (ISVAR (symbol))
128 break;
129
130 {
131 new_edge (&edge);
132
133 if (state > state1)
134 edge.type = back_edge;
135 open_edge (&edge, fgraph);
136 /* The edge source is the current node. */
137 edge.sourcename = node_name;
138 sprintf (buff, "%d", state1);
139 edge.targetname = buff;
140 edge.color = (symbol == 0) ? red : blue;
141 edge.label = tags[symbol];
142 output_edge (&edge, fgraph);
143 close_edge (fgraph);
144 }
145 }
146 }
147 else
148 {
149 i = 0;
150 k = 0;
151 }
152
153 #if 0
154 if (errp)
155 {
156 int j, nerrs;
157
158 nerrs = errp->nerrs;
159
160 for (j = 0; j < nerrs; j++)
161 {
162 if (!errp->errs[j])
163 continue;
164 symbol = errp->errs[j];
165 /* If something has been added in the NODE_OBSTACK after
166 the declaration of the label, then we need a `\n'.
167 if (obstack_object_size (node_obstack) > node_output_size)
168 obstack_sgrow (node_obstack, "\n");
169 */
170 obstack_fgrow1 (node_obstack, _("%-4s\terror (nonassociative)"),
171 tags[symbol]);
172 }
173 if (j > 0)
174 obstack_1grow (node_obstack, '\n');
175 }
176
177 if (state_table[state].consistent && redp)
178 {
179 rule = redp->rules[0];
180 symbol = rule_table[rule].lhs;
181 /*
182 if (obstack_object_size (node_obstack) > node_output_size)
183 obstack_sgrow (node_obstack, "\n");
184 */
185 obstack_fgrow2 (node_obstack, _("$default\treduce using rule %d (%s)"),
186 rule, tags[symbol]);
187 }
188 #endif
189
190 if (i < k)
191 {
192 for (; i < k; i++)
193 {
194 if (!shiftp->shifts[i])
195 continue;
196 state1 = shiftp->shifts[i];
197 symbol = state_table[state1].accessing_symbol;
198
199 new_edge (&edge);
200 open_edge (&edge, fgraph);
201 edge.sourcename = node_name;
202 sprintf (buff, "%d", state1);
203 edge.targetname = buff;
204 edge.color = red;
205 edge.label = tags[symbol];
206 output_edge (&edge, fgraph);
207 close_edge (fgraph);
208 }
209 }
210 }
211
212
213 /*-------------------------------------------------------------.
214 | Output in FGRAPH the current node specifications and exiting |
215 | edges. |
216 `-------------------------------------------------------------*/
217
218 static void
219 print_state (int state)
220 {
221 static char name[10];
222 struct obstack node_obstack;
223 node_t node;
224
225 /* The labels of the nodes are their the items. */
226 obstack_init (&node_obstack);
227 new_node (&node);
228 sprintf (name, "%d", state);
229 node.title = name;
230 print_core (state, &node_obstack);
231 obstack_1grow (&node_obstack, '\0');
232 node.label = obstack_finish (&node_obstack);
233
234 open_node (fgraph);
235 output_node (&node, fgraph);
236 close_node (fgraph);
237
238 /* Output the edges. */
239 print_actions (state, name);
240
241 obstack_free (&node_obstack, 0);
242 }
243 \f
244
245 void
246 print_graph (void)
247 {
248 int i;
249
250 if (!graph_flag)
251 return;
252
253 /* Output file. */
254 fgraph = xfopen (spec_graph_file, "w");
255
256 new_graph (&graph);
257
258 #if 0
259 graph.smanhattan_edges = yes;
260 graph.manhattan_edges = yes;
261 #endif
262
263 graph.display_edge_labels = yes;
264 graph.layoutalgorithm = normal;
265
266 graph.port_sharing = no;
267 graph.finetuning = yes;
268 graph.straight_phase = yes;
269 graph.priority_phase = yes;
270 graph.splines = yes;
271
272 graph.crossing_weight = median;
273
274 /* Output graph options. */
275 open_graph (fgraph);
276 output_graph (&graph, fgraph);
277
278 /* Output nodes and edges. */
279 for (i = 0; i < nstates; i++)
280 print_state (i);
281
282 /* Close graph. */
283 close_graph (&graph, fgraph);
284 xfclose (fgraph);
285 }