1 |
/* Output a VCG description on generated parser, for Bison, |
/* Output a VCG description on generated parser, for Bison, |
2 |
Copyright 2001 Free Software Foundation, Inc. |
|
3 |
|
Copyright (C) 2001, 2002 Free Software Foundation, Inc. |
4 |
|
|
5 |
This file is part of Bison, the GNU Compiler Compiler. |
This file is part of Bison, the GNU Compiler Compiler. |
6 |
|
|
20 |
Boston, MA 02111-1307, USA. */ |
Boston, MA 02111-1307, USA. */ |
21 |
|
|
22 |
#include "system.h" |
#include "system.h" |
23 |
#include "quotearg.h" |
|
24 |
#include "files.h" |
#include <obstack.h> |
25 |
#include "symtab.h" |
#include <quotearg.h> |
26 |
#include "gram.h" |
|
27 |
#include "LR0.h" |
#include "LR0.h" |
28 |
#include "lalr.h" |
#include "closure.h" |
|
#include "conflicts.h" |
|
29 |
#include "complain.h" |
#include "complain.h" |
30 |
|
#include "conflicts.h" |
31 |
|
#include "files.h" |
32 |
#include "getargs.h" |
#include "getargs.h" |
33 |
#include "state.h" |
#include "gram.h" |
34 |
#include "reader.h" |
#include "lalr.h" |
|
#include "closure.h" |
|
|
#include "obstack.h" |
|
35 |
#include "print_graph.h" |
#include "print_graph.h" |
36 |
|
#include "reader.h" |
37 |
|
#include "state.h" |
38 |
|
#include "symtab.h" |
39 |
#include "vcg.h" |
#include "vcg.h" |
40 |
|
|
41 |
static graph_t graph; |
static graph static_graph; |
42 |
static FILE *fgraph = NULL; |
static FILE *fgraph = NULL; |
43 |
|
|
44 |
|
|
47 |
`----------------------------*/ |
`----------------------------*/ |
48 |
|
|
49 |
static void |
static void |
50 |
print_core (struct obstack *oout, state_t *state) |
print_core (struct obstack *oout, state *s) |
51 |
{ |
{ |
52 |
int i; |
int i; |
53 |
item_number_t *sitems = state->items; |
item_number *sitems = s->items; |
54 |
int snritems = state->nitems; |
int snritems = s->nitems; |
55 |
|
|
56 |
/* Output all the items of a state, not only its kernel. */ |
/* Output all the items of a state, not only its kernel. */ |
57 |
if (report_flag & report_itemsets) |
if (report_flag & report_itemsets) |
61 |
snritems = nritemset; |
snritems = nritemset; |
62 |
} |
} |
63 |
|
|
64 |
obstack_fgrow1 (oout, "state %2d\n", state->number); |
obstack_fgrow1 (oout, "state %2d\n", s->number); |
65 |
for (i = 0; i < snritems; i++) |
for (i = 0; i < snritems; i++) |
66 |
{ |
{ |
67 |
item_number_t *sp; |
item_number *sp; |
68 |
item_number_t *sp1; |
item_number *sp1; |
69 |
rule_number_t rule; |
rule_number r; |
70 |
|
|
71 |
sp1 = sp = ritem + sitems[i]; |
sp1 = sp = ritem + sitems[i]; |
72 |
|
|
73 |
while (*sp >= 0) |
while (*sp >= 0) |
74 |
sp++; |
sp++; |
75 |
|
|
76 |
rule = item_number_as_rule_number (*sp); |
r = item_number_as_rule_number (*sp); |
77 |
|
|
78 |
if (i) |
if (i) |
79 |
obstack_1grow (oout, '\n'); |
obstack_1grow (oout, '\n'); |
80 |
obstack_fgrow1 (oout, " %s -> ", |
obstack_fgrow1 (oout, " %s -> ", |
81 |
rules[rule].lhs->tag); |
rules[r].lhs->tag); |
82 |
|
|
83 |
for (sp = rules[rule].rhs; sp < sp1; sp++) |
for (sp = rules[r].rhs; sp < sp1; sp++) |
84 |
obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag); |
obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag); |
85 |
|
|
86 |
obstack_1grow (oout, '.'); |
obstack_1grow (oout, '.'); |
92 |
if (report_flag & report_lookaheads) |
if (report_flag & report_lookaheads) |
93 |
{ |
{ |
94 |
/* Find the reduction we are handling. */ |
/* Find the reduction we are handling. */ |
95 |
reductions_t *reds = state->reductions; |
reductions *reds = s->reductions; |
96 |
int redno = state_reduction_find (state, &rules[rule]); |
int redno = state_reduction_find (s, &rules[r]); |
97 |
|
|
98 |
/* Print them if there are. */ |
/* Print them if there are. */ |
99 |
if (reds->lookaheads && redno != -1) |
if (reds->lookaheads && redno != -1) |
119 |
`---------------------------------------------------------------*/ |
`---------------------------------------------------------------*/ |
120 |
|
|
121 |
static void |
static void |
122 |
print_actions (state_t *state, const char *node_name) |
print_actions (state *s, const char *node_name) |
123 |
{ |
{ |
124 |
int i; |
int i; |
125 |
|
|
126 |
transitions_t *transitions = state->transitions; |
transitions *trans = s->transitions; |
127 |
reductions_t *redp = state->reductions; |
reductions *reds = s->reductions; |
128 |
|
|
129 |
static char buff[10]; |
static char buff[10]; |
130 |
edge_t edge; |
edge e; |
131 |
|
|
132 |
if (!transitions->num && !redp) |
if (!trans->num && !reds) |
133 |
return; |
return; |
134 |
|
|
135 |
for (i = 0; i < transitions->num; i++) |
for (i = 0; i < trans->num; i++) |
136 |
if (!TRANSITION_IS_DISABLED (transitions, i)) |
if (!TRANSITION_IS_DISABLED (trans, i)) |
137 |
{ |
{ |
138 |
state_t *state1 = transitions->states[i]; |
state *s1 = trans->states[i]; |
139 |
symbol_number_t symbol = state1->accessing_symbol; |
symbol_number sym = s1->accessing_symbol; |
140 |
|
|
141 |
new_edge (&edge); |
new_edge (&e); |
142 |
|
|
143 |
if (state->number > state1->number) |
if (s->number > s1->number) |
144 |
edge.type = back_edge; |
e.type = back_edge; |
145 |
open_edge (&edge, fgraph); |
open_edge (&e, fgraph); |
146 |
/* The edge source is the current node. */ |
/* The edge source is the current node. */ |
147 |
edge.sourcename = node_name; |
e.sourcename = node_name; |
148 |
sprintf (buff, "%d", state1->number); |
sprintf (buff, "%d", s1->number); |
149 |
edge.targetname = buff; |
e.targetname = buff; |
150 |
/* Shifts are blue, gotos are green, and error is red. */ |
/* Shifts are blue, gotos are green, and error is red. */ |
151 |
if (TRANSITION_IS_ERROR (transitions, i)) |
if (TRANSITION_IS_ERROR (trans, i)) |
152 |
edge.color = red; |
e.color = red; |
153 |
else |
else |
154 |
edge.color = TRANSITION_IS_SHIFT(transitions, i) ? blue : green; |
e.color = TRANSITION_IS_SHIFT (trans, i) ? blue : green; |
155 |
edge.label = symbols[symbol]->tag; |
e.label = symbols[sym]->tag; |
156 |
output_edge (&edge, fgraph); |
output_edge (&e, fgraph); |
157 |
close_edge (fgraph); |
close_edge (fgraph); |
158 |
} |
} |
159 |
} |
} |
165 |
`-------------------------------------------------------------*/ |
`-------------------------------------------------------------*/ |
166 |
|
|
167 |
static void |
static void |
168 |
print_state (state_t *state) |
print_state (state *s) |
169 |
{ |
{ |
170 |
static char name[10]; |
static char name[10]; |
171 |
struct obstack node_obstack; |
struct obstack node_obstack; |
172 |
node_t node; |
node n; |
173 |
|
|
174 |
/* The labels of the nodes are their the items. */ |
/* The labels of the nodes are their the items. */ |
175 |
obstack_init (&node_obstack); |
obstack_init (&node_obstack); |
176 |
new_node (&node); |
new_node (&n); |
177 |
sprintf (name, "%d", state->number); |
sprintf (name, "%d", s->number); |
178 |
node.title = name; |
n.title = name; |
179 |
print_core (&node_obstack, state); |
print_core (&node_obstack, s); |
180 |
obstack_1grow (&node_obstack, '\0'); |
obstack_1grow (&node_obstack, '\0'); |
181 |
node.label = obstack_finish (&node_obstack); |
n.label = obstack_finish (&node_obstack); |
182 |
|
|
183 |
open_node (fgraph); |
open_node (fgraph); |
184 |
output_node (&node, fgraph); |
output_node (&n, fgraph); |
185 |
close_node (fgraph); |
close_node (fgraph); |
186 |
|
|
187 |
/* Output the edges. */ |
/* Output the edges. */ |
188 |
print_actions (state, name); |
print_actions (s, name); |
189 |
|
|
190 |
obstack_free (&node_obstack, 0); |
obstack_free (&node_obstack, 0); |
191 |
} |
} |
194 |
void |
void |
195 |
print_graph (void) |
print_graph (void) |
196 |
{ |
{ |
197 |
state_number_t i; |
state_number i; |
198 |
|
|
199 |
/* Output file. */ |
/* Output file. */ |
200 |
fgraph = xfopen (spec_graph_file, "w"); |
fgraph = xfopen (spec_graph_file, "w"); |
201 |
|
|
202 |
new_graph (&graph); |
new_graph (&static_graph); |
203 |
|
|
204 |
#if 0 |
#if 0 |
205 |
graph.smanhattan_edges = yes; |
static_graph.smanhattan_edges = yes; |
206 |
graph.manhattan_edges = yes; |
static_graph.manhattan_edges = yes; |
207 |
#endif |
#endif |
208 |
|
|
209 |
graph.display_edge_labels = yes; |
static_graph.display_edge_labels = yes; |
210 |
graph.layoutalgorithm = normal; |
static_graph.layoutalgorithm = normal; |
211 |
|
|
212 |
graph.port_sharing = no; |
static_graph.port_sharing = no; |
213 |
graph.finetuning = yes; |
static_graph.finetuning = yes; |
214 |
graph.straight_phase = yes; |
static_graph.long_straight_phase = yes; |
215 |
graph.priority_phase = yes; |
static_graph.priority_phase = yes; |
216 |
graph.splines = yes; |
static_graph.splines = yes; |
217 |
|
|
218 |
graph.crossing_weight = median; |
static_graph.crossing_weight = median; |
219 |
|
|
220 |
/* Output graph options. */ |
/* Output graph options. */ |
221 |
open_graph (fgraph); |
open_graph (fgraph); |
222 |
output_graph (&graph, fgraph); |
output_graph (&static_graph, fgraph); |
223 |
|
|
224 |
/* Output nodes and edges. */ |
/* Output nodes and edges. */ |
225 |
new_closure (nritems); |
new_closure (nritems); |
228 |
free_closure (); |
free_closure (); |
229 |
|
|
230 |
/* Close graph. */ |
/* Close graph. */ |
231 |
close_graph (&graph, fgraph); |
close_graph (&static_graph, fgraph); |
232 |
xfclose (fgraph); |
xfclose (fgraph); |
233 |
} |
} |