]> git.saurik.com Git - bison.git/blob - src/print_graph.c
* src/output.c (prepare): Drop the muscle `ntbase' which
[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 "closure.h"
32 #include "obstack.h"
33 #include "print_graph.h"
34 #include "vcg.h"
35
36 static graph_t graph;
37 static FILE *fgraph = NULL;
38
39 /* This part will construct the label of nodes. */
40 static void
41 print_core (int state, struct obstack *node_obstack)
42 {
43 int i;
44 short *sitems = state_table[state]->items;
45 int snitems = state_table[state]->nitems;
46
47 /* Output all the items of a state, not only its kernel. */
48 closure (sitems, snitems);
49 sitems = itemset;
50 snitems = nitemset;
51
52 obstack_fgrow1 (node_obstack, "%2d: ", state);
53 for (i = 0; i < snitems; i++)
54 {
55 short *sp;
56 short *sp1;
57 int rule;
58
59 sp1 = sp = ritem + sitems[i];
60
61 while (*sp > 0)
62 sp++;
63
64 rule = -(*sp);
65
66 if (i)
67 obstack_sgrow (node_obstack, "\n ");
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 for (/* Nothing */; *sp > 0; ++sp)
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
92 shifts *shiftp = state_table[state]->shifts;
93 reductions *redp = state_table[state]->reductions;
94 #if 0
95 errs *errp = state_table[state]->errs;
96 #endif
97
98 static char buff[10];
99 edge_t edge;
100
101 if (!shiftp->nshifts && !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 for (i = 0; i < shiftp->nshifts; i++)
113 if (!SHIFT_IS_DISABLED (shiftp, i))
114 {
115 int state1 = shiftp->shifts[i];
116 int symbol = state_table[state1]->accessing_symbol;
117
118 new_edge (&edge);
119
120 if (state > state1)
121 edge.type = back_edge;
122 open_edge (&edge, fgraph);
123 /* The edge source is the current node. */
124 edge.sourcename = node_name;
125 sprintf (buff, "%d", state1);
126 edge.targetname = buff;
127 /* Shifts are blue, gotos are red. */
128 edge.color = SHIFT_IS_SHIFT(shiftp, i) ? blue : red;
129 edge.label = tags[symbol];
130 output_edge (&edge, fgraph);
131 close_edge (fgraph);
132 }
133
134 #if 0
135 if (errp)
136 {
137 int j, nerrs;
138
139 nerrs = errp->nerrs;
140
141 for (j = 0; j < nerrs; j++)
142 {
143 if (!errp->errs[j])
144 continue;
145 symbol = errp->errs[j];
146 /* If something has been added in the NODE_OBSTACK after
147 the declaration of the label, then we need a `\n'.
148 if (obstack_object_size (node_obstack) > node_output_size)
149 obstack_sgrow (node_obstack, "\n");
150 */
151 obstack_fgrow1 (node_obstack, _("%-4s\terror (nonassociative)"),
152 tags[symbol]);
153 }
154 if (j > 0)
155 obstack_1grow (node_obstack, '\n');
156 }
157
158 if (state_table[state]->consistent && redp)
159 {
160 rule = redp->rules[0];
161 symbol = rule_table[rule].lhs;
162 /*
163 if (obstack_object_size (node_obstack) > node_output_size)
164 obstack_sgrow (node_obstack, "\n");
165 */
166 obstack_fgrow2 (node_obstack, _("$default\treduce using rule %d (%s)"),
167 rule, tags[symbol]);
168 }
169 #endif
170 }
171
172
173 /*-------------------------------------------------------------.
174 | Output in FGRAPH the current node specifications and exiting |
175 | edges. |
176 `-------------------------------------------------------------*/
177
178 static void
179 print_state (int state)
180 {
181 static char name[10];
182 struct obstack node_obstack;
183 node_t node;
184
185 /* The labels of the nodes are their the items. */
186 obstack_init (&node_obstack);
187 new_node (&node);
188 sprintf (name, "%d", state);
189 node.title = name;
190 print_core (state, &node_obstack);
191 obstack_1grow (&node_obstack, '\0');
192 node.label = obstack_finish (&node_obstack);
193
194 open_node (fgraph);
195 output_node (&node, fgraph);
196 close_node (fgraph);
197
198 /* Output the edges. */
199 print_actions (state, name);
200
201 obstack_free (&node_obstack, 0);
202 }
203 \f
204
205 void
206 print_graph (void)
207 {
208 int i;
209
210 /* Output file. */
211 fgraph = xfopen (spec_graph_file, "w");
212
213 new_graph (&graph);
214
215 #if 0
216 graph.smanhattan_edges = yes;
217 graph.manhattan_edges = yes;
218 #endif
219
220 graph.display_edge_labels = yes;
221 graph.layoutalgorithm = normal;
222
223 graph.port_sharing = no;
224 graph.finetuning = yes;
225 graph.straight_phase = yes;
226 graph.priority_phase = yes;
227 graph.splines = yes;
228
229 graph.crossing_weight = median;
230
231 /* Output graph options. */
232 open_graph (fgraph);
233 output_graph (&graph, fgraph);
234
235 /* Output nodes and edges. */
236 new_closure (nitems);
237 for (i = 0; i < nstates; i++)
238 print_state (i);
239 free_closure ();
240
241 /* Close graph. */
242 close_graph (&graph, fgraph);
243 xfclose (fgraph);
244 }