]> git.saurik.com Git - bison.git/blob - src/print_graph.c
5671771e947c4b57b5b5e96c2b21601706ddc86e
[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 "xalloc.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 "obstack.h"
33 #include "print_graph.h"
34 #include "vcg.h"
35
36 static graph_t graph;
37
38 /* This part will construct the label of nodes. */
39 static void
40 print_core (int state)
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];
50 k = statep->nitems;
51
52 if (k == 0)
53 return;
54
55 obstack_sgrow (&graph_obstack, "\t\tlabel:\t\"");
56
57 for (i = 0; i < k; i++)
58 {
59 sp1 = sp = ritem + statep->items[i];
60
61 while (*sp > 0)
62 sp++;
63
64 rule = -(*sp);
65
66 obstack_fgrow1 (&graph_obstack, _("%d: "), rule);
67 obstack_fgrow1 (&graph_obstack, " %s -> ", tags[rlhs[rule]]);
68
69 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
70 obstack_fgrow1 (&graph_obstack, "%s ", tags[*sp]);
71
72 obstack_1grow (&graph_obstack, '.');
73
74 while (*sp > 0)
75 obstack_fgrow1 (&graph_obstack, " %s", tags[*sp++]);
76
77 obstack_sgrow (&graph_obstack, "\\n");
78 }
79 obstack_sgrow (&graph_obstack, "\"\n");
80 }
81
82 static void
83 print_actions (int state, node_t *node)
84 {
85 int i;
86 int k;
87 int state1;
88 int symbol;
89 shifts *shiftp;
90 errs *errp;
91 reductions *redp;
92 int rule;
93 static char buff[10];
94 edge_t edge;
95
96 shiftp = shift_table[state];
97 redp = reduction_table[state];
98 errp = err_table[state];
99
100 if (!shiftp && !redp)
101 {
102 #if 0
103 if (final_state == state)
104 fprintf (f, " $default\taccept\n");
105 else
106 fprintf (f, " NO ACTIONS\n");
107 #endif
108 return;
109 }
110
111 if (shiftp)
112 {
113 k = shiftp->nshifts;
114
115 for (i = 0; i < k; i++)
116 {
117 if (!shiftp->shifts[i])
118 continue;
119 state1 = shiftp->shifts[i];
120 symbol = accessing_symbol[state1];
121
122 if (ISVAR (symbol))
123 break;
124
125 {
126 new_edge (&edge);
127
128 if (state > state1)
129 edge.type = back_edge;
130 open_edge (&edge, &graph_obstack);
131 edge.sourcename = node->title;
132 edge.targetname = buff;
133 sprintf (edge.targetname, "%d", state1);
134 edge.color = (symbol == 0) ? blue : red;
135 edge.label = tags[symbol];
136 output_edge (&edge, &graph_obstack);
137 close_edge (&graph_obstack);
138 }
139 }
140 }
141 else
142 {
143 i = 0;
144 k = 0;
145 }
146
147 if (errp)
148 {
149 int j, nerrs;
150
151 nerrs = errp->nerrs;
152
153 for (j = 0; j < nerrs; j++)
154 {
155 if (!errp->errs[j])
156 continue;
157 symbol = errp->errs[j];
158 }
159 }
160
161 if (consistent[state] && redp)
162 {
163 rule = redp->rules[0];
164 symbol = rlhs[rule];
165 }
166
167 if (i < k)
168 {
169 for (; i < k; i++)
170 {
171 if (!shiftp->shifts[i])
172 continue;
173 state1 = shiftp->shifts[i];
174 symbol = accessing_symbol[state1];
175
176 new_edge (&edge);
177 open_edge (&edge, &graph_obstack);
178 edge.sourcename = node->title;
179 edge.targetname = buff;
180 edge.color = red;
181 sprintf (edge.targetname, "%d", state1);
182 edge.label = tags[symbol];
183 output_edge (&edge, &graph_obstack);
184 close_edge (&graph_obstack);
185 }
186 }
187 }
188
189 static void
190 print_state (int state)
191 {
192 static char name[10];
193 node_t node;
194
195 new_node (&node);
196 open_node (&graph_obstack);
197
198 sprintf (name, "%d", state);
199 node.title = name;
200 output_node (&node, &graph_obstack);
201
202 print_core (state); /* node label */
203
204 close_node (&graph_obstack);
205
206 print_actions (state, &node); /* edges */
207 }
208 \f
209
210 void
211 print_graph (void)
212 {
213 int i;
214
215 if (!graph_flag)
216 return;
217 new_graph (&graph);
218
219 /* graph.smanhattan_edges = yes;
220 graph.manhattan_edges = yes; */
221
222 graph.display_edge_labels = yes;
223 graph.layoutalgorithm = 0;
224
225 graph.port_sharing = no;
226 graph.finetuning = yes;
227 graph.straight_phase = yes;
228 graph.priority_phase = yes;
229 graph.splines = yes;
230
231 graph.crossing_weight = median;
232
233 /* Output graph options. */
234 open_graph (&graph_obstack);
235 output_graph (&graph, &graph_obstack);
236
237 for (i = 0; i < nstates; i++)
238 /* Output nodes & edges. */
239 print_state (i);
240
241 /* Close graph. */
242 close_graph (&graph, &graph_obstack);
243 }