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