]> git.saurik.com Git - bison.git/blob - src/print_graph.c
24ddab168b589aac2f2a72c1484b790cf7c80ec2
[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
42 /*----------------------------.
43 | Construct the node labels. |
44 `----------------------------*/
45
46 static void
47 print_core (struct obstack *oout, state_t *state)
48 {
49 int i;
50 item_number_t *sitems = state->items;
51 int snritems = state->nitems;
52
53 /* Output all the items of a state, not only its kernel. */
54 if (report_flag & report_itemsets)
55 {
56 closure (sitems, snritems);
57 sitems = itemset;
58 snritems = nritemset;
59 }
60
61 obstack_fgrow1 (oout, "state %2d\n", state->number);
62 for (i = 0; i < snritems; i++)
63 {
64 item_number_t *sp;
65 item_number_t *sp1;
66 rule_number_t rule;
67
68 sp1 = sp = ritem + sitems[i];
69
70 while (*sp >= 0)
71 sp++;
72
73 rule = rule_number_of_item_number (*sp);
74
75 if (i)
76 obstack_1grow (oout, '\n');
77 obstack_fgrow1 (oout, " %s -> ",
78 symbol_tag_get (rules[rule].lhs));
79
80 for (sp = rules[rule].rhs; sp < sp1; sp++)
81 obstack_fgrow1 (oout, "%s ", symbol_tag_get (symbols[*sp]));
82
83 obstack_1grow (oout, '.');
84
85 for (/* Nothing */; *sp >= 0; ++sp)
86 obstack_fgrow1 (oout, " %s", symbol_tag_get (symbols[*sp]));
87
88 /* Experimental feature: display the lookaheads. */
89 if ((report_flag & report_lookaheads)
90 && state->nlookaheads)
91 {
92 int j, k;
93 int nlookaheads = 0;
94 /* Look for lookaheads corresponding to this rule. */
95 for (j = 0; j < state->nlookaheads; ++j)
96 for (k = 0; k < ntokens; ++k)
97 if (bitset_test (state->lookaheads[j], k)
98 && state->lookaheads_rule[j]->number == rule)
99 nlookaheads++;
100 if (nlookaheads)
101 {
102 obstack_sgrow (oout, " [");
103 for (j = 0; j < state->nlookaheads; ++j)
104 for (k = 0; k < ntokens; ++k)
105 if (bitset_test (state->lookaheads[j], k)
106 && state->lookaheads_rule[j]->number == rule)
107 obstack_fgrow2 (oout, "%s%s",
108 symbol_tag_get (symbols[k]),
109 --nlookaheads ? ", " : "");
110 obstack_sgrow (oout, "]");
111 }
112 }
113 }
114 }
115
116
117 /*---------------------------------------------------------------.
118 | Output in graph_obstack edges specifications in incidence with |
119 | current node. |
120 `---------------------------------------------------------------*/
121
122 static void
123 print_actions (state_t *state, const char *node_name)
124 {
125 int i;
126
127 shifts_t *shiftp = state->shifts;
128 reductions_t *redp = state->reductions;
129
130 static char buff[10];
131 edge_t edge;
132
133 if (!shiftp->nshifts && !redp)
134 return;
135
136 for (i = 0; i < shiftp->nshifts; i++)
137 if (!SHIFT_IS_DISABLED (shiftp, i))
138 {
139 state_number_t state1 = shiftp->shifts[i];
140 symbol_number_t symbol = states[state1]->accessing_symbol;
141
142 new_edge (&edge);
143
144 if (state->number > state1)
145 edge.type = back_edge;
146 open_edge (&edge, fgraph);
147 /* The edge source is the current node. */
148 edge.sourcename = node_name;
149 sprintf (buff, "%d", state1);
150 edge.targetname = buff;
151 /* Shifts are blue, gotos are green, and error is red. */
152 if (SHIFT_IS_ERROR (shiftp, i))
153 edge.color = red;
154 else
155 edge.color = SHIFT_IS_SHIFT(shiftp, i) ? blue : green;
156 edge.label = symbol_tag_get (symbols[symbol]);
157 output_edge (&edge, fgraph);
158 close_edge (fgraph);
159 }
160 }
161
162
163 /*-------------------------------------------------------------.
164 | Output in FGRAPH the current node specifications and exiting |
165 | edges. |
166 `-------------------------------------------------------------*/
167
168 static void
169 print_state (state_t *state)
170 {
171 static char name[10];
172 struct obstack node_obstack;
173 node_t node;
174
175 /* The labels of the nodes are their the items. */
176 obstack_init (&node_obstack);
177 new_node (&node);
178 sprintf (name, "%d", state->number);
179 node.title = name;
180 print_core (&node_obstack, state);
181 obstack_1grow (&node_obstack, '\0');
182 node.label = obstack_finish (&node_obstack);
183
184 open_node (fgraph);
185 output_node (&node, fgraph);
186 close_node (fgraph);
187
188 /* Output the edges. */
189 print_actions (state, name);
190
191 obstack_free (&node_obstack, 0);
192 }
193 \f
194
195 void
196 print_graph (void)
197 {
198 state_number_t i;
199
200 /* Output file. */
201 fgraph = xfopen (spec_graph_file, "w");
202
203 new_graph (&graph);
204
205 #if 0
206 graph.smanhattan_edges = yes;
207 graph.manhattan_edges = yes;
208 #endif
209
210 graph.display_edge_labels = yes;
211 graph.layoutalgorithm = normal;
212
213 graph.port_sharing = no;
214 graph.finetuning = yes;
215 graph.straight_phase = yes;
216 graph.priority_phase = yes;
217 graph.splines = yes;
218
219 graph.crossing_weight = median;
220
221 /* Output graph options. */
222 open_graph (fgraph);
223 output_graph (&graph, fgraph);
224
225 /* Output nodes and edges. */
226 new_closure (nritems);
227 for (i = 0; i < nstates; i++)
228 print_state (states[i]);
229 free_closure ();
230
231 /* Close graph. */
232 close_graph (&graph, fgraph);
233 xfclose (fgraph);
234 }