]> git.saurik.com Git - bison.git/blame - src/print_graph.c
* src/print.c (print_actions): Better locality of variables.
[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;
93 int rule;
94 static char buff[10];
95 edge_t edge;
22c2cbc0 96
90b4416b
AD
97 shiftp = state_table[state].shift_table;
98 redp = state_table[state].reduction_table;
ce4d5ce0
AD
99 errp = err_table[state];
100
101 if (!shiftp && !redp)
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
112 if (shiftp)
113 {
114 k = shiftp->nshifts;
115
116 for (i = 0; i < k; i++)
117 {
118 if (!shiftp->shifts[i])
119 continue;
120 state1 = shiftp->shifts[i];
9703cc49 121 symbol = state_table[state1].accessing_symbol;
ce4d5ce0
AD
122
123 if (ISVAR (symbol))
124 break;
22c2cbc0 125
ce4d5ce0
AD
126 {
127 new_edge (&edge);
128
129 if (state > state1)
130 edge.type = back_edge;
342b8b6e 131 open_edge (&edge, fgraph);
600cad3b
MA
132 /* The edge source is the current node. */
133 edge.sourcename = node_name;
c4b66126 134 sprintf (buff, "%d", state1);
ce4d5ce0 135 edge.targetname = buff;
600cad3b 136 edge.color = (symbol == 0) ? red : blue;
08a946e0 137 edge.label = tags[symbol];
342b8b6e
AD
138 output_edge (&edge, fgraph);
139 close_edge (fgraph);
22c2cbc0 140 }
ce4d5ce0
AD
141 }
142 }
143 else
144 {
145 i = 0;
146 k = 0;
147 }
148
08a946e0 149#if 0
ce4d5ce0
AD
150 if (errp)
151 {
152 int j, nerrs;
153
154 nerrs = errp->nerrs;
155
156 for (j = 0; j < nerrs; j++)
157 {
158 if (!errp->errs[j])
159 continue;
160 symbol = errp->errs[j];
600cad3b 161 /* If something has been added in the NODE_OBSTACK after
08a946e0 162 the declaration of the label, then we need a `\n'.
600cad3b 163 if (obstack_object_size (node_obstack) > node_output_size)
08a946e0
AD
164 obstack_sgrow (node_obstack, "\n");
165 */
600cad3b
MA
166 obstack_fgrow1 (node_obstack, _("%-4s\terror (nonassociative)"),
167 tags[symbol]);
ce4d5ce0 168 }
600cad3b 169 if (j > 0)
08a946e0 170 obstack_1grow (node_obstack, '\n');
ce4d5ce0 171 }
22c2cbc0 172
de326cc0 173 if (state_table[state].consistent && redp)
ce4d5ce0
AD
174 {
175 rule = redp->rules[0];
b2ed6e58 176 symbol = rule_table[rule].lhs;
08a946e0 177 /*
600cad3b 178 if (obstack_object_size (node_obstack) > node_output_size)
08a946e0
AD
179 obstack_sgrow (node_obstack, "\n");
180 */
600cad3b
MA
181 obstack_fgrow2 (node_obstack, _("$default\treduce using rule %d (%s)"),
182 rule, tags[symbol]);
ce4d5ce0 183 }
08a946e0 184#endif
22c2cbc0 185
ce4d5ce0
AD
186 if (i < k)
187 {
188 for (; i < k; i++)
189 {
190 if (!shiftp->shifts[i])
191 continue;
192 state1 = shiftp->shifts[i];
9703cc49 193 symbol = state_table[state1].accessing_symbol;
ce4d5ce0
AD
194
195 new_edge (&edge);
342b8b6e 196 open_edge (&edge, fgraph);
600cad3b 197 edge.sourcename = node_name;
c4b66126 198 sprintf (buff, "%d", state1);
ce4d5ce0
AD
199 edge.targetname = buff;
200 edge.color = red;
08a946e0 201 edge.label = tags[symbol];
342b8b6e
AD
202 output_edge (&edge, fgraph);
203 close_edge (fgraph);
ce4d5ce0
AD
204 }
205 }
206}
207
08a946e0
AD
208
209/*-------------------------------------------------------------.
210| Output in FGRAPH the current node specifications and exiting |
211| edges. |
212`-------------------------------------------------------------*/
213
ce4d5ce0
AD
214static void
215print_state (int state)
216{
217 static char name[10];
600cad3b 218 struct obstack node_obstack;
22c2cbc0 219 node_t node;
ce4d5ce0 220
08a946e0 221 /* The labels of the nodes are their the items. */
600cad3b 222 obstack_init (&node_obstack);
08a946e0 223 new_node (&node);
3e3da797 224 sprintf (name, "%d", state);
08a946e0
AD
225 node.title = name;
226 print_core (state, &node_obstack);
227 obstack_1grow (&node_obstack, '\0');
228 node.label = obstack_finish (&node_obstack);
342b8b6e
AD
229
230 open_node (fgraph);
342b8b6e 231 output_node (&node, fgraph);
342b8b6e
AD
232 close_node (fgraph);
233
08a946e0
AD
234 /* Output the edges. */
235 print_actions (state, name);
236
342b8b6e 237 obstack_free (&node_obstack, 0);
ce4d5ce0
AD
238}
239\f
240
241void
242print_graph (void)
243{
244 int i;
9703cc49 245
ce4d5ce0 246 if (!graph_flag)
3e3da797 247 return;
342b8b6e
AD
248
249 /* Output file. */
250 fgraph = xfopen (spec_graph_file, "w");
251
ce4d5ce0 252 new_graph (&graph);
22c2cbc0 253
600cad3b
MA
254#if 0
255 graph.smanhattan_edges = yes;
9703cc49 256 graph.manhattan_edges = yes;
600cad3b 257#endif
22c2cbc0 258
ce4d5ce0 259 graph.display_edge_labels = yes;
342b8b6e 260 graph.layoutalgorithm = normal;
22c2cbc0 261
ce4d5ce0
AD
262 graph.port_sharing = no;
263 graph.finetuning = yes;
264 graph.straight_phase = yes;
265 graph.priority_phase = yes;
266 graph.splines = yes;
22c2cbc0 267
ce4d5ce0
AD
268 graph.crossing_weight = median;
269
270 /* Output graph options. */
342b8b6e
AD
271 open_graph (fgraph);
272 output_graph (&graph, fgraph);
ce4d5ce0 273
08a946e0 274 /* Output nodes and edges. */
ce4d5ce0 275 for (i = 0; i < nstates; i++)
3e3da797 276 print_state (i);
ce4d5ce0
AD
277
278 /* Close graph. */
342b8b6e
AD
279 close_graph (&graph, fgraph);
280 xfclose (fgraph);
ce4d5ce0 281}