]> git.saurik.com Git - bison.git/blame_incremental - src/print_graph.c
We spend a lot of time in quotearg, in particular when --verbose.
[bison.git] / src / print_graph.c
... / ...
CommitLineData
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
38static graph_t graph;
39static FILE *fgraph = NULL;
40
41
42/*----------------------------.
43| Construct the node labels. |
44`----------------------------*/
45
46static void
47print_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 rules[rule].lhs->tag);
79
80 for (sp = rules[rule].rhs; sp < sp1; sp++)
81 obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag);
82
83 obstack_1grow (oout, '.');
84
85 for (/* Nothing */; *sp >= 0; ++sp)
86 obstack_fgrow1 (oout, " %s", symbols[*sp]->tag);
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 BITSET_EXECUTE (state->lookaheads[j], 0, k,
97 {
98 if (state->lookaheads_rule[j]->number == rule)
99 nlookaheads++;
100 });
101
102 if (nlookaheads)
103 {
104 obstack_sgrow (oout, " [");
105 for (j = 0; j < state->nlookaheads; ++j)
106 BITSET_EXECUTE (state->lookaheads[j], 0, k,
107 {
108 if (state->lookaheads_rule[j]->number == rule)
109 obstack_fgrow2 (oout, "%s%s",
110 symbols[k]->tag,
111 --nlookaheads ? ", " : "");
112 });
113 obstack_sgrow (oout, "]");
114 }
115 }
116 }
117}
118
119
120/*---------------------------------------------------------------.
121| Output in graph_obstack edges specifications in incidence with |
122| current node. |
123`---------------------------------------------------------------*/
124
125static void
126print_actions (state_t *state, const char *node_name)
127{
128 int i;
129
130 transitions_t *transitions = state->shifts;
131 reductions_t *redp = state->reductions;
132
133 static char buff[10];
134 edge_t edge;
135
136 if (!transitions->num && !redp)
137 return;
138
139 for (i = 0; i < transitions->num; i++)
140 if (!TRANSITION_IS_DISABLED (transitions, i))
141 {
142 state_number_t state1 = transitions->states[i];
143 symbol_number_t symbol = states[state1]->accessing_symbol;
144
145 new_edge (&edge);
146
147 if (state->number > state1)
148 edge.type = back_edge;
149 open_edge (&edge, fgraph);
150 /* The edge source is the current node. */
151 edge.sourcename = node_name;
152 sprintf (buff, "%d", state1);
153 edge.targetname = buff;
154 /* Shifts are blue, gotos are green, and error is red. */
155 if (TRANSITION_IS_ERROR (transitions, i))
156 edge.color = red;
157 else
158 edge.color = TRANSITION_IS_SHIFT(transitions, i) ? blue : green;
159 edge.label = symbols[symbol]->tag;
160 output_edge (&edge, fgraph);
161 close_edge (fgraph);
162 }
163}
164
165
166/*-------------------------------------------------------------.
167| Output in FGRAPH the current node specifications and exiting |
168| edges. |
169`-------------------------------------------------------------*/
170
171static void
172print_state (state_t *state)
173{
174 static char name[10];
175 struct obstack node_obstack;
176 node_t node;
177
178 /* The labels of the nodes are their the items. */
179 obstack_init (&node_obstack);
180 new_node (&node);
181 sprintf (name, "%d", state->number);
182 node.title = name;
183 print_core (&node_obstack, state);
184 obstack_1grow (&node_obstack, '\0');
185 node.label = obstack_finish (&node_obstack);
186
187 open_node (fgraph);
188 output_node (&node, fgraph);
189 close_node (fgraph);
190
191 /* Output the edges. */
192 print_actions (state, name);
193
194 obstack_free (&node_obstack, 0);
195}
196\f
197
198void
199print_graph (void)
200{
201 state_number_t i;
202
203 /* Output file. */
204 fgraph = xfopen (spec_graph_file, "w");
205
206 new_graph (&graph);
207
208#if 0
209 graph.smanhattan_edges = yes;
210 graph.manhattan_edges = yes;
211#endif
212
213 graph.display_edge_labels = yes;
214 graph.layoutalgorithm = normal;
215
216 graph.port_sharing = no;
217 graph.finetuning = yes;
218 graph.straight_phase = yes;
219 graph.priority_phase = yes;
220 graph.splines = yes;
221
222 graph.crossing_weight = median;
223
224 /* Output graph options. */
225 open_graph (fgraph);
226 output_graph (&graph, fgraph);
227
228 /* Output nodes and edges. */
229 new_closure (nritems);
230 for (i = 0; i < nstates; i++)
231 print_state (states[i]);
232 free_closure ();
233
234 /* Close graph. */
235 close_graph (&graph, fgraph);
236 xfclose (fgraph);
237}