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