]> git.saurik.com Git - bison.git/blob - src/print_graph.c
* data/lalr1.cc: Move the body of the ctor and dtor into the
[bison.git] / src / print_graph.c
1 /* Output a VCG description on generated parser, for Bison,
2
3 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
21
22 #include "system.h"
23
24 #include <quotearg.h>
25
26 #include "LR0.h"
27 #include "closure.h"
28 #include "complain.h"
29 #include "conflicts.h"
30 #include "files.h"
31 #include "getargs.h"
32 #include "gram.h"
33 #include "lalr.h"
34 #include "print_graph.h"
35 #include "reader.h"
36 #include "state.h"
37 #include "symtab.h"
38 #include "vcg.h"
39
40 static graph static_graph;
41 static FILE *fgraph = NULL;
42
43
44 /*----------------------------.
45 | Construct the node labels. |
46 `----------------------------*/
47
48 static void
49 print_core (struct obstack *oout, state *s)
50 {
51 size_t i;
52 item_number *sitems = s->items;
53 size_t snritems = s->nitems;
54
55 /* Output all the items of a state, not only its kernel. */
56 if (report_flag & report_itemsets)
57 {
58 closure (sitems, snritems);
59 sitems = itemset;
60 snritems = nritemset;
61 }
62
63 obstack_fgrow1 (oout, "state %2d\n", s->number);
64 for (i = 0; i < snritems; i++)
65 {
66 item_number *sp;
67 item_number *sp1;
68 rule_number r;
69
70 sp1 = sp = ritem + sitems[i];
71
72 while (*sp >= 0)
73 sp++;
74
75 r = item_number_as_rule_number (*sp);
76
77 if (i)
78 obstack_1grow (oout, '\n');
79 obstack_fgrow1 (oout, " %s -> ",
80 rules[r].lhs->tag);
81
82 for (sp = rules[r].rhs; sp < sp1; sp++)
83 obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag);
84
85 obstack_1grow (oout, '.');
86
87 for (/* Nothing */; *sp >= 0; ++sp)
88 obstack_fgrow1 (oout, " %s", symbols[*sp]->tag);
89
90 /* Experimental feature: display the look-ahead tokens. */
91 if (report_flag & report_look_ahead_tokens)
92 {
93 /* Find the reduction we are handling. */
94 reductions *reds = s->reductions;
95 int redno = state_reduction_find (s, &rules[r]);
96
97 /* Print them if there are. */
98 if (reds->look_ahead_tokens && redno != -1)
99 {
100 bitset_iterator biter;
101 int k;
102 char const *sep = "";
103 obstack_sgrow (oout, "[");
104 BITSET_FOR_EACH (biter, reds->look_ahead_tokens[redno], k, 0)
105 {
106 obstack_fgrow2 (oout, "%s%s", sep, symbols[k]->tag);
107 sep = ", ";
108 }
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 *s, const char *node_name)
123 {
124 int i;
125
126 transitions *trans = s->transitions;
127 reductions *reds = s->reductions;
128
129 static char buff[10];
130 edge e;
131
132 if (!trans->num && !reds)
133 return;
134
135 for (i = 0; i < trans->num; i++)
136 if (!TRANSITION_IS_DISABLED (trans, i))
137 {
138 state *s1 = trans->states[i];
139 symbol_number sym = s1->accessing_symbol;
140
141 new_edge (&e);
142
143 if (s->number > s1->number)
144 e.type = back_edge;
145 open_edge (&e, fgraph);
146 /* The edge source is the current node. */
147 e.sourcename = node_name;
148 sprintf (buff, "%d", s1->number);
149 e.targetname = buff;
150 /* Shifts are blue, gotos are green, and error is red. */
151 if (TRANSITION_IS_ERROR (trans, i))
152 e.color = red;
153 else
154 e.color = TRANSITION_IS_SHIFT (trans, i) ? blue : green;
155 e.label = symbols[sym]->tag;
156 output_edge (&e, 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 *s)
169 {
170 static char name[10];
171 struct obstack node_obstack;
172 node n;
173
174 /* The labels of the nodes are their the items. */
175 obstack_init (&node_obstack);
176 new_node (&n);
177 sprintf (name, "%d", s->number);
178 n.title = name;
179 print_core (&node_obstack, s);
180 obstack_1grow (&node_obstack, '\0');
181 n.label = obstack_finish (&node_obstack);
182
183 open_node (fgraph);
184 output_node (&n, fgraph);
185 close_node (fgraph);
186
187 /* Output the edges. */
188 print_actions (s, name);
189
190 obstack_free (&node_obstack, 0);
191 }
192 \f
193
194 void
195 print_graph (void)
196 {
197 state_number i;
198
199 /* Output file. */
200 fgraph = xfopen (spec_graph_file, "w");
201
202 new_graph (&static_graph);
203
204 static_graph.display_edge_labels = yes;
205
206 static_graph.port_sharing = no;
207 static_graph.finetuning = yes;
208 static_graph.priority_phase = yes;
209 static_graph.splines = yes;
210
211 static_graph.crossing_weight = median;
212
213 /* Output graph options. */
214 open_graph (fgraph);
215 output_graph (&static_graph, fgraph);
216
217 /* Output nodes and edges. */
218 new_closure (nritems);
219 for (i = 0; i < nstates; i++)
220 print_state (states[i]);
221 free_closure ();
222
223 /* Close graph. */
224 close_graph (&static_graph, fgraph);
225 xfclose (fgraph);
226 }