]> git.saurik.com Git - bison.git/blame - src/print_graph.c
* src/files.c (output_files): Free the output_obstack.
[bison.git] / src / print_graph.c
CommitLineData
22c2cbc0
AD
1/* Output a VCG description on generated parser, for Bison,
2 Copyright 2001 Free Software Foundation, Inc.
ce4d5ce0
AD
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"
ce4d5ce0
AD
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"
2e729273 31#include "closure.h"
ce4d5ce0
AD
32#include "obstack.h"
33#include "print_graph.h"
34#include "vcg.h"
35
36static graph_t graph;
342b8b6e 37static FILE *fgraph = NULL;
ce4d5ce0
AD
38
39/* This part will construct the label of nodes. */
40static void
600cad3b 41print_core (int state, struct obstack *node_obstack)
ce4d5ce0
AD
42{
43 int i;
2e729273
AD
44 short *sitems = state_table[state]->items;
45 int snitems = state_table[state]->nitems;
22c2cbc0 46
2e729273
AD
47 /* Output all the items of a state, not only its kernel. */
48 closure (sitems, snitems);
49 sitems = itemset;
50 snitems = nitemset;
22c2cbc0 51
2e729273
AD
52 obstack_fgrow1 (node_obstack, "%2d: ", state);
53 for (i = 0; i < snitems; i++)
ce4d5ce0 54 {
4bc30f78
AD
55 short *sp;
56 short *sp1;
57 int rule;
c4b66126 58
2e729273 59 sp1 = sp = ritem + sitems[i];
ce4d5ce0
AD
60
61 while (*sp > 0)
62 sp++;
63
64 rule = -(*sp);
65
4bc30f78 66 if (i)
2e729273 67 obstack_sgrow (node_obstack, "\n ");
b2ed6e58 68 obstack_fgrow1 (node_obstack, " %s -> ",
08a946e0 69 tags[rule_table[rule].lhs]);
ce4d5ce0 70
b2ed6e58 71 for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++)
08a946e0 72 obstack_fgrow1 (node_obstack, "%s ", tags[*sp]);
ce4d5ce0 73
600cad3b 74 obstack_1grow (node_obstack, '.');
22c2cbc0 75
4bc30f78
AD
76 for (/* Nothing */; *sp > 0; ++sp)
77 obstack_fgrow1 (node_obstack, " %s", tags[*sp]);
ce4d5ce0 78 }
ce4d5ce0
AD
79}
80
08a946e0
AD
81
82/*---------------------------------------------------------------.
83| Output in graph_obstack edges specifications in incidence with |
84| current node. |
85`---------------------------------------------------------------*/
86
ce4d5ce0 87static void
08a946e0 88print_actions (int state, const char *node_name)
ce4d5ce0
AD
89{
90 int i;
d954473d 91
f693ad14
AD
92 shifts *shiftp = state_table[state]->shifts;
93 reductions *redp = state_table[state]->reductions;
d954473d 94#if 0
f693ad14 95 errs *errp = state_table[state]->errs;
d954473d
AD
96#endif
97
ce4d5ce0
AD
98 static char buff[10];
99 edge_t edge;
22c2cbc0 100
d954473d 101 if (!shiftp->nshifts && !redp)
ce4d5ce0 102 {
08a946e0 103#if 0
ce4d5ce0 104 if (final_state == state)
600cad3b 105 obstack_sgrow (node_obstack, "$default: accept");
ce4d5ce0 106 else
600cad3b 107 obstack_sgrow (node_obstack, "NO ACTIONS");
08a946e0 108#endif
ce4d5ce0
AD
109 return;
110 }
111
d954473d
AD
112 for (i = 0; i < shiftp->nshifts; i++)
113 if (!SHIFT_IS_DISABLED (shiftp, i))
114 {
115 int state1 = shiftp->shifts[i];
f693ad14 116 int symbol = state_table[state1]->accessing_symbol;
d954473d
AD
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;
2e729273
AD
127 /* Shifts are blue, gotos are red. */
128 edge.color = SHIFT_IS_SHIFT(shiftp, i) ? blue : red;
d954473d
AD
129 edge.label = tags[symbol];
130 output_edge (&edge, fgraph);
131 close_edge (fgraph);
132 }
ce4d5ce0 133
08a946e0 134#if 0
ce4d5ce0
AD
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];
600cad3b 146 /* If something has been added in the NODE_OBSTACK after
08a946e0 147 the declaration of the label, then we need a `\n'.
600cad3b 148 if (obstack_object_size (node_obstack) > node_output_size)
08a946e0
AD
149 obstack_sgrow (node_obstack, "\n");
150 */
600cad3b
MA
151 obstack_fgrow1 (node_obstack, _("%-4s\terror (nonassociative)"),
152 tags[symbol]);
ce4d5ce0 153 }
600cad3b 154 if (j > 0)
08a946e0 155 obstack_1grow (node_obstack, '\n');
ce4d5ce0 156 }
22c2cbc0 157
f693ad14 158 if (state_table[state]->consistent && redp)
ce4d5ce0
AD
159 {
160 rule = redp->rules[0];
b2ed6e58 161 symbol = rule_table[rule].lhs;
08a946e0 162 /*
600cad3b 163 if (obstack_object_size (node_obstack) > node_output_size)
08a946e0
AD
164 obstack_sgrow (node_obstack, "\n");
165 */
600cad3b
MA
166 obstack_fgrow2 (node_obstack, _("$default\treduce using rule %d (%s)"),
167 rule, tags[symbol]);
ce4d5ce0 168 }
08a946e0 169#endif
ce4d5ce0
AD
170}
171
08a946e0
AD
172
173/*-------------------------------------------------------------.
174| Output in FGRAPH the current node specifications and exiting |
175| edges. |
176`-------------------------------------------------------------*/
177
ce4d5ce0
AD
178static void
179print_state (int state)
180{
181 static char name[10];
600cad3b 182 struct obstack node_obstack;
22c2cbc0 183 node_t node;
ce4d5ce0 184
08a946e0 185 /* The labels of the nodes are their the items. */
600cad3b 186 obstack_init (&node_obstack);
08a946e0 187 new_node (&node);
3e3da797 188 sprintf (name, "%d", state);
08a946e0
AD
189 node.title = name;
190 print_core (state, &node_obstack);
191 obstack_1grow (&node_obstack, '\0');
192 node.label = obstack_finish (&node_obstack);
342b8b6e
AD
193
194 open_node (fgraph);
342b8b6e 195 output_node (&node, fgraph);
342b8b6e
AD
196 close_node (fgraph);
197
08a946e0
AD
198 /* Output the edges. */
199 print_actions (state, name);
200
342b8b6e 201 obstack_free (&node_obstack, 0);
ce4d5ce0
AD
202}
203\f
204
205void
206print_graph (void)
207{
208 int i;
9703cc49 209
342b8b6e
AD
210 /* Output file. */
211 fgraph = xfopen (spec_graph_file, "w");
212
ce4d5ce0 213 new_graph (&graph);
22c2cbc0 214
600cad3b
MA
215#if 0
216 graph.smanhattan_edges = yes;
9703cc49 217 graph.manhattan_edges = yes;
600cad3b 218#endif
22c2cbc0 219
ce4d5ce0 220 graph.display_edge_labels = yes;
342b8b6e 221 graph.layoutalgorithm = normal;
22c2cbc0 222
ce4d5ce0
AD
223 graph.port_sharing = no;
224 graph.finetuning = yes;
225 graph.straight_phase = yes;
226 graph.priority_phase = yes;
227 graph.splines = yes;
22c2cbc0 228
ce4d5ce0
AD
229 graph.crossing_weight = median;
230
231 /* Output graph options. */
342b8b6e
AD
232 open_graph (fgraph);
233 output_graph (&graph, fgraph);
ce4d5ce0 234
08a946e0 235 /* Output nodes and edges. */
2e729273 236 new_closure (nitems);
ce4d5ce0 237 for (i = 0; i < nstates; i++)
3e3da797 238 print_state (i);
2e729273 239 free_closure ();
ce4d5ce0
AD
240
241 /* Close graph. */
342b8b6e
AD
242 close_graph (&graph, fgraph);
243 xfclose (fgraph);
ce4d5ce0 244}