]> git.saurik.com Git - bison.git/blame - src/print_graph.c
* src/print_graph.c, src/print_graph.h (graph): New.
[bison.git] / src / print_graph.c
CommitLineData
ce4d5ce0
AD
1/* Output a VCG description on generated parser, for bison,
2 Copyright 1984, 1986, 1989, 2000, 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 "xalloc.h"
23#include "files.h"
24#include "gram.h"
25#include "LR0.h"
26#include "lalr.h"
27#include "conflicts.h"
28#include "complain.h"
29#include "getargs.h"
30#include "state.h"
31#include "reader.h"
32#include "obstack.h"
33#include "print_graph.h"
34#include "vcg.h"
35
36static graph_t graph;
37
38/* This part will construct the label of nodes. */
39static void
40print_core (int state)
41{
42 int i;
43 int k;
44 int rule;
45 core *statep;
46 short *sp;
47 short *sp1;
48
49 statep = state_table[state];
50 k = statep->nitems;
51
52 if (k == 0)
53 return;
54
55 obstack_sgrow(&graph_obstack, "\t\tlabel:\t\"");
56
57 for (i = 0; i < k; i++)
58 {
59 sp1 = sp = ritem + statep->items[i];
60
61 while (*sp > 0)
62 sp++;
63
64 rule = -(*sp);
65
66 obstack_fgrow1(&graph_obstack, _("%d: "), rule);
67 obstack_fgrow1(&graph_obstack, " %s -> ", tags[rlhs[rule]]);
68
69 for (sp = ritem + rrhs[rule]; sp < sp1; sp++)
70 obstack_fgrow1(&graph_obstack, "%s ", tags[*sp]);
71
72 obstack_1grow(&graph_obstack, '.');
73
74 while (*sp > 0)
75 obstack_fgrow1(&graph_obstack, " %s", tags[*sp++]);
76
77 obstack_sgrow(&graph_obstack, "\\n");
78 }
79 obstack_sgrow(&graph_obstack, "\"\n");
80}
81
82static void
83print_actions (int state, node_t *node)
84{
85 int i;
86 int k;
87 int state1;
88 int symbol;
89 shifts *shiftp;
90 errs *errp;
91 reductions *redp;
92 int rule;
93 static char buff[10];
94 edge_t edge;
95
96 shiftp = shift_table[state];
97 redp = reduction_table[state];
98 errp = err_table[state];
99
100 if (!shiftp && !redp)
101 {
102 if (final_state == state)
103 ;//fprintf(f, " $default\taccept\n");
104 else
105 ;//fprintf(f, " NO ACTIONS\n");
106 return;
107 }
108
109 if (shiftp)
110 {
111 k = shiftp->nshifts;
112
113 for (i = 0; i < k; i++)
114 {
115 if (!shiftp->shifts[i])
116 continue;
117 state1 = shiftp->shifts[i];
118 symbol = accessing_symbol[state1];
119
120 if (ISVAR (symbol))
121 break;
122
123 {
124 new_edge (&edge);
125
126 if (state > state1)
127 edge.type = back_edge;
128 open_edge (&edge, &graph_obstack);
129 edge.sourcename = node->title;
130 edge.targetname = buff;
131 sprintf (edge.targetname, "%d", state1);
132 edge.color = (symbol == 0) ? blue : red;
133 edge.label = tags[symbol];
134 output_edge (&edge, &graph_obstack);
135 close_edge (&graph_obstack);
136 }
137 }
138 }
139 else
140 {
141 i = 0;
142 k = 0;
143 }
144
145 if (errp)
146 {
147 int j, nerrs;
148
149 nerrs = errp->nerrs;
150
151 for (j = 0; j < nerrs; j++)
152 {
153 if (!errp->errs[j])
154 continue;
155 symbol = errp->errs[j];
156 }
157 }
158
159 if (consistent[state] && redp)
160 {
161 rule = redp->rules[0];
162 symbol = rlhs[rule];
163 }
164
165 if (i < k)
166 {
167 for (; i < k; i++)
168 {
169 if (!shiftp->shifts[i])
170 continue;
171 state1 = shiftp->shifts[i];
172 symbol = accessing_symbol[state1];
173
174 new_edge (&edge);
175 open_edge (&edge, &graph_obstack);
176 edge.sourcename = node->title;
177 edge.targetname = buff;
178 edge.color = red;
179 sprintf (edge.targetname, "%d", state1);
180 edge.label = tags[symbol];
181 output_edge (&edge, &graph_obstack);
182 close_edge (&graph_obstack);
183 }
184 }
185}
186
187static void
188print_state (int state)
189{
190 static char name[10];
191 node_t node;
192
193 new_node (&node);
194 open_node (&graph_obstack);
195
196 sprintf(name, "%d", state);
197 node.title = name;
198 output_node (&node, &graph_obstack);
199
200 print_core (state); /* node label */
201
202 close_node (&graph_obstack);
203
204 print_actions (state, &node); /* edges */
205}
206\f
207
208void
209print_graph (void)
210{
211 int i;
212
213 if (!graph_flag)
214 return ;
215 new_graph (&graph);
216
217 /* graph.smanhattan_edges = yes;
218 graph.manhattan_edges = yes; */
219
220 graph.display_edge_labels = yes;
221 graph.layoutalgorithm = 0;
222
223 graph.port_sharing = no;
224 graph.finetuning = yes;
225 graph.straight_phase = yes;
226 graph.priority_phase = yes;
227 graph.splines = yes;
228
229 graph.crossing_weight = median;
230
231 /* Output graph options. */
232 open_graph (&graph_obstack);
233 output_graph (&graph, &graph_obstack);
234
235 for (i = 0; i < nstates; i++)
236 /* Output nodes & edges. */
237 print_state (i);
238
239 /* Close graph. */
240 close_graph (&graph, &graph_obstack);
241}
242