]> git.saurik.com Git - bison.git/blob - src/print_graph.c
* src/print_graph.c (print_core): Better locality of variables.
[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 core *statep = state_table[state].state;
44
45 if (!statep->nitems)
46 return;
47
48 for (i = 0; i < statep->nitems; i++)
49 {
50 short *sp;
51 short *sp1;
52 int rule;
53
54 sp1 = sp = ritem + statep->items[i];
55
56 while (*sp > 0)
57 sp++;
58
59 rule = -(*sp);
60
61 if (i)
62 obstack_1grow (node_obstack, '\n');
63 obstack_fgrow1 (node_obstack, "%d: ", rule);
64 obstack_fgrow1 (node_obstack, " %s -> ",
65 tags[rule_table[rule].lhs]);
66
67 for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++)
68 obstack_fgrow1 (node_obstack, "%s ", tags[*sp]);
69
70 obstack_1grow (node_obstack, '.');
71
72 for (/* Nothing */; *sp > 0; ++sp)
73 obstack_fgrow1 (node_obstack, " %s", tags[*sp]);
74 }
75 }
76
77
78 /*---------------------------------------------------------------.
79 | Output in graph_obstack edges specifications in incidence with |
80 | current node. |
81 `---------------------------------------------------------------*/
82
83 static void
84 print_actions (int state, const char *node_name)
85 {
86 int i;
87 int k;
88 int state1;
89 int symbol;
90 shifts *shiftp;
91 errs *errp;
92 reductions *redp;
93 int rule;
94 static char buff[10];
95 edge_t edge;
96
97 shiftp = state_table[state].shift_table;
98 redp = state_table[state].reduction_table;
99 errp = err_table[state];
100
101 if (!shiftp && !redp)
102 {
103 #if 0
104 if (final_state == state)
105 obstack_sgrow (node_obstack, "$default: accept");
106 else
107 obstack_sgrow (node_obstack, "NO ACTIONS");
108 #endif
109 return;
110 }
111
112 if (shiftp)
113 {
114 k = shiftp->nshifts;
115
116 for (i = 0; i < k; i++)
117 {
118 if (!shiftp->shifts[i])
119 continue;
120 state1 = shiftp->shifts[i];
121 symbol = state_table[state1].accessing_symbol;
122
123 if (ISVAR (symbol))
124 break;
125
126 {
127 new_edge (&edge);
128
129 if (state > state1)
130 edge.type = back_edge;
131 open_edge (&edge, fgraph);
132 /* The edge source is the current node. */
133 edge.sourcename = node_name;
134 sprintf (buff, "%d", state1);
135 edge.targetname = buff;
136 edge.color = (symbol == 0) ? red : blue;
137 edge.label = tags[symbol];
138 output_edge (&edge, fgraph);
139 close_edge (fgraph);
140 }
141 }
142 }
143 else
144 {
145 i = 0;
146 k = 0;
147 }
148
149 #if 0
150 if (errp)
151 {
152 int j, nerrs;
153
154 nerrs = errp->nerrs;
155
156 for (j = 0; j < nerrs; j++)
157 {
158 if (!errp->errs[j])
159 continue;
160 symbol = errp->errs[j];
161 /* If something has been added in the NODE_OBSTACK after
162 the declaration of the label, then we need a `\n'.
163 if (obstack_object_size (node_obstack) > node_output_size)
164 obstack_sgrow (node_obstack, "\n");
165 */
166 obstack_fgrow1 (node_obstack, _("%-4s\terror (nonassociative)"),
167 tags[symbol]);
168 }
169 if (j > 0)
170 obstack_1grow (node_obstack, '\n');
171 }
172
173 if (state_table[state].consistent && redp)
174 {
175 rule = redp->rules[0];
176 symbol = rule_table[rule].lhs;
177 /*
178 if (obstack_object_size (node_obstack) > node_output_size)
179 obstack_sgrow (node_obstack, "\n");
180 */
181 obstack_fgrow2 (node_obstack, _("$default\treduce using rule %d (%s)"),
182 rule, tags[symbol]);
183 }
184 #endif
185
186 if (i < k)
187 {
188 for (; i < k; i++)
189 {
190 if (!shiftp->shifts[i])
191 continue;
192 state1 = shiftp->shifts[i];
193 symbol = state_table[state1].accessing_symbol;
194
195 new_edge (&edge);
196 open_edge (&edge, fgraph);
197 edge.sourcename = node_name;
198 sprintf (buff, "%d", state1);
199 edge.targetname = buff;
200 edge.color = red;
201 edge.label = tags[symbol];
202 output_edge (&edge, fgraph);
203 close_edge (fgraph);
204 }
205 }
206 }
207
208
209 /*-------------------------------------------------------------.
210 | Output in FGRAPH the current node specifications and exiting |
211 | edges. |
212 `-------------------------------------------------------------*/
213
214 static void
215 print_state (int state)
216 {
217 static char name[10];
218 struct obstack node_obstack;
219 node_t node;
220
221 /* The labels of the nodes are their the items. */
222 obstack_init (&node_obstack);
223 new_node (&node);
224 sprintf (name, "%d", state);
225 node.title = name;
226 print_core (state, &node_obstack);
227 obstack_1grow (&node_obstack, '\0');
228 node.label = obstack_finish (&node_obstack);
229
230 open_node (fgraph);
231 output_node (&node, fgraph);
232 close_node (fgraph);
233
234 /* Output the edges. */
235 print_actions (state, name);
236
237 obstack_free (&node_obstack, 0);
238 }
239 \f
240
241 void
242 print_graph (void)
243 {
244 int i;
245
246 if (!graph_flag)
247 return;
248
249 /* Output file. */
250 fgraph = xfopen (spec_graph_file, "w");
251
252 new_graph (&graph);
253
254 #if 0
255 graph.smanhattan_edges = yes;
256 graph.manhattan_edges = yes;
257 #endif
258
259 graph.display_edge_labels = yes;
260 graph.layoutalgorithm = normal;
261
262 graph.port_sharing = no;
263 graph.finetuning = yes;
264 graph.straight_phase = yes;
265 graph.priority_phase = yes;
266 graph.splines = yes;
267
268 graph.crossing_weight = median;
269
270 /* Output graph options. */
271 open_graph (fgraph);
272 output_graph (&graph, fgraph);
273
274 /* Output nodes and edges. */
275 for (i = 0; i < nstates; i++)
276 print_state (i);
277
278 /* Close graph. */
279 close_graph (&graph, fgraph);
280 xfclose (fgraph);
281 }