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