]> git.saurik.com Git - bison.git/blob - src/print_graph.c
* src/derives.c (print_derives): Be sure to use `>= 0', not `> 0',
[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 "quotearg.h"
23 #include "files.h"
24 #include "gram.h"
25 #include "LR0.h"
26 #include "lalr.h"
27 #include "conflicts.h"
28 #include "complain.h"
29 #include "getargs.h"
30 #include "state.h"
31 #include "reader.h"
32 #include "closure.h"
33 #include "obstack.h"
34 #include "print_graph.h"
35 #include "vcg.h"
36
37 static graph_t graph;
38 static FILE *fgraph = NULL;
39
40 static inline const char *
41 escape (const char *s)
42 {
43 return quotearg_n_style (1, escape_quoting_style, s);
44 }
45
46
47 /* This part will construct the label of nodes. */
48 static void
49 print_core (state_t *state, struct obstack *node_obstack)
50 {
51 int i;
52 short *sitems = state->items;
53 int snitems = state->nitems;
54
55 /* Output all the items of a state, not only its kernel. */
56 if (trace_flag)
57 {
58 closure (sitems, snitems);
59 sitems = itemset;
60 snitems = nitemset;
61 }
62
63 obstack_fgrow1 (node_obstack, "state %2d\n", state->number);
64 for (i = 0; i < snitems; i++)
65 {
66 short *sp;
67 short *sp1;
68 int rule;
69
70 sp1 = sp = ritem + sitems[i];
71
72 while (*sp >= 0)
73 sp++;
74
75 rule = -(*sp);
76
77 if (i)
78 obstack_1grow (node_obstack, '\n');
79 obstack_fgrow1 (node_obstack, " %s -> ",
80 escape (tags[rule_table[rule].lhs]));
81
82 for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++)
83 obstack_fgrow1 (node_obstack, "%s ", escape (tags[*sp]));
84
85 obstack_1grow (node_obstack, '.');
86
87 for (/* Nothing */; *sp >= 0; ++sp)
88 obstack_fgrow1 (node_obstack, " %s", escape (tags[*sp]));
89 }
90 }
91
92
93 /*---------------------------------------------------------------.
94 | Output in graph_obstack edges specifications in incidence with |
95 | current node. |
96 `---------------------------------------------------------------*/
97
98 static void
99 print_actions (state_t *state, const char *node_name)
100 {
101 int i;
102
103 shifts *shiftp = state->shifts;
104 reductions *redp = state->reductions;
105
106 static char buff[10];
107 edge_t edge;
108
109 if (!shiftp->nshifts && !redp)
110 return;
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->number > 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 green, and error is red. */
128 if (SHIFT_IS_ERROR (shiftp, i))
129 edge.color = red;
130 else
131 edge.color = SHIFT_IS_SHIFT(shiftp, i) ? blue : green;
132 edge.label = escape (tags[symbol]);
133 output_edge (&edge, fgraph);
134 close_edge (fgraph);
135 }
136 }
137
138
139 /*-------------------------------------------------------------.
140 | Output in FGRAPH the current node specifications and exiting |
141 | edges. |
142 `-------------------------------------------------------------*/
143
144 static void
145 print_state (state_t *state)
146 {
147 static char name[10];
148 struct obstack node_obstack;
149 node_t node;
150
151 /* The labels of the nodes are their the items. */
152 obstack_init (&node_obstack);
153 new_node (&node);
154 sprintf (name, "%d", state->number);
155 node.title = name;
156 print_core (state, &node_obstack);
157 obstack_1grow (&node_obstack, '\0');
158 node.label = obstack_finish (&node_obstack);
159
160 open_node (fgraph);
161 output_node (&node, fgraph);
162 close_node (fgraph);
163
164 /* Output the edges. */
165 print_actions (state, name);
166
167 obstack_free (&node_obstack, 0);
168 }
169 \f
170
171 void
172 print_graph (void)
173 {
174 int i;
175
176 /* Output file. */
177 fgraph = xfopen (spec_graph_file, "w");
178
179 new_graph (&graph);
180
181 #if 0
182 graph.smanhattan_edges = yes;
183 graph.manhattan_edges = yes;
184 #endif
185
186 graph.display_edge_labels = yes;
187 graph.layoutalgorithm = normal;
188
189 graph.port_sharing = no;
190 graph.finetuning = yes;
191 graph.straight_phase = yes;
192 graph.priority_phase = yes;
193 graph.splines = yes;
194
195 graph.crossing_weight = median;
196
197 /* Output graph options. */
198 open_graph (fgraph);
199 output_graph (&graph, fgraph);
200
201 /* Output nodes and edges. */
202 new_closure (nritems);
203 for (i = 0; i < nstates; i++)
204 print_state (state_table[i]);
205 free_closure ();
206
207 /* Close graph. */
208 close_graph (&graph, fgraph);
209 xfclose (fgraph);
210 }