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