]> git.saurik.com Git - bison.git/blame - src/print_graph.c
Try to make the use of the eoftoken valid. Given that its value
[bison.git] / src / print_graph.c
CommitLineData
22c2cbc0
AD
1/* Output a VCG description on generated parser, for Bison,
2 Copyright 2001 Free Software Foundation, Inc.
ce4d5ce0
AD
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"
8adfa272 22#include "quotearg.h"
ce4d5ce0
AD
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"
2e729273 32#include "closure.h"
ce4d5ce0
AD
33#include "obstack.h"
34#include "print_graph.h"
35#include "vcg.h"
36
37static graph_t graph;
342b8b6e 38static FILE *fgraph = NULL;
ce4d5ce0 39
8adfa272
AD
40static inline const char *
41escape (const char *s)
42{
43 return quotearg_n_style (1, escape_quoting_style, s);
44}
45
46
ce4d5ce0
AD
47/* This part will construct the label of nodes. */
48static void
065fbd27 49print_core (state_t *state, struct obstack *node_obstack)
ce4d5ce0
AD
50{
51 int i;
065fbd27
AD
52 short *sitems = state->items;
53 int snitems = state->nitems;
22c2cbc0 54
2e729273
AD
55 /* Output all the items of a state, not only its kernel. */
56 closure (sitems, snitems);
57 sitems = itemset;
58 snitems = nitemset;
22c2cbc0 59
8adfa272 60 obstack_fgrow1 (node_obstack, "state %2d\n", state->number);
2e729273 61 for (i = 0; i < snitems; i++)
ce4d5ce0 62 {
4bc30f78
AD
63 short *sp;
64 short *sp1;
65 int rule;
c4b66126 66
2e729273 67 sp1 = sp = ritem + sitems[i];
ce4d5ce0 68
75142d45 69 while (*sp >= 0)
ce4d5ce0
AD
70 sp++;
71
72 rule = -(*sp);
73
4bc30f78 74 if (i)
8adfa272 75 obstack_1grow (node_obstack, '\n');
065fbd27 76 obstack_fgrow1 (node_obstack, " %s -> ",
8adfa272 77 escape (tags[rule_table[rule].lhs]));
ce4d5ce0 78
b2ed6e58 79 for (sp = ritem + rule_table[rule].rhs; sp < sp1; sp++)
8adfa272 80 obstack_fgrow1 (node_obstack, "%s ", escape (tags[*sp]));
ce4d5ce0 81
600cad3b 82 obstack_1grow (node_obstack, '.');
22c2cbc0 83
75142d45 84 for (/* Nothing */; *sp >= 0; ++sp)
8adfa272 85 obstack_fgrow1 (node_obstack, " %s", escape (tags[*sp]));
ce4d5ce0 86 }
ce4d5ce0
AD
87}
88
08a946e0
AD
89
90/*---------------------------------------------------------------.
91| Output in graph_obstack edges specifications in incidence with |
92| current node. |
93`---------------------------------------------------------------*/
94
ce4d5ce0 95static void
065fbd27 96print_actions (state_t *state, const char *node_name)
ce4d5ce0
AD
97{
98 int i;
d954473d 99
065fbd27
AD
100 shifts *shiftp = state->shifts;
101 reductions *redp = state->reductions;
d954473d 102
ce4d5ce0
AD
103 static char buff[10];
104 edge_t edge;
22c2cbc0 105
d954473d 106 if (!shiftp->nshifts && !redp)
5092aba5 107 return;
ce4d5ce0 108
d954473d
AD
109 for (i = 0; i < shiftp->nshifts; i++)
110 if (!SHIFT_IS_DISABLED (shiftp, i))
111 {
112 int state1 = shiftp->shifts[i];
f693ad14 113 int symbol = state_table[state1]->accessing_symbol;
d954473d
AD
114
115 new_edge (&edge);
116
065fbd27 117 if (state->number > state1)
d954473d
AD
118 edge.type = back_edge;
119 open_edge (&edge, fgraph);
120 /* The edge source is the current node. */
121 edge.sourcename = node_name;
122 sprintf (buff, "%d", state1);
123 edge.targetname = buff;
8adfa272
AD
124 /* Shifts are blue, gotos are green, and error is red. */
125 if (SHIFT_IS_ERROR (shiftp, i))
126 edge.color = red;
127 else
128 edge.color = SHIFT_IS_SHIFT(shiftp, i) ? blue : green;
129 edge.label = escape (tags[symbol]);
d954473d
AD
130 output_edge (&edge, fgraph);
131 close_edge (fgraph);
132 }
ce4d5ce0
AD
133}
134
08a946e0
AD
135
136/*-------------------------------------------------------------.
137| Output in FGRAPH the current node specifications and exiting |
138| edges. |
139`-------------------------------------------------------------*/
140
ce4d5ce0 141static void
065fbd27 142print_state (state_t *state)
ce4d5ce0
AD
143{
144 static char name[10];
600cad3b 145 struct obstack node_obstack;
22c2cbc0 146 node_t node;
ce4d5ce0 147
08a946e0 148 /* The labels of the nodes are their the items. */
600cad3b 149 obstack_init (&node_obstack);
08a946e0 150 new_node (&node);
065fbd27 151 sprintf (name, "%d", state->number);
08a946e0
AD
152 node.title = name;
153 print_core (state, &node_obstack);
154 obstack_1grow (&node_obstack, '\0');
155 node.label = obstack_finish (&node_obstack);
342b8b6e
AD
156
157 open_node (fgraph);
342b8b6e 158 output_node (&node, fgraph);
342b8b6e
AD
159 close_node (fgraph);
160
08a946e0
AD
161 /* Output the edges. */
162 print_actions (state, name);
163
342b8b6e 164 obstack_free (&node_obstack, 0);
ce4d5ce0
AD
165}
166\f
167
168void
169print_graph (void)
170{
171 int i;
9703cc49 172
342b8b6e
AD
173 /* Output file. */
174 fgraph = xfopen (spec_graph_file, "w");
175
ce4d5ce0 176 new_graph (&graph);
22c2cbc0 177
600cad3b
MA
178#if 0
179 graph.smanhattan_edges = yes;
9703cc49 180 graph.manhattan_edges = yes;
600cad3b 181#endif
22c2cbc0 182
ce4d5ce0 183 graph.display_edge_labels = yes;
342b8b6e 184 graph.layoutalgorithm = normal;
22c2cbc0 185
ce4d5ce0
AD
186 graph.port_sharing = no;
187 graph.finetuning = yes;
188 graph.straight_phase = yes;
189 graph.priority_phase = yes;
190 graph.splines = yes;
22c2cbc0 191
ce4d5ce0
AD
192 graph.crossing_weight = median;
193
194 /* Output graph options. */
342b8b6e
AD
195 open_graph (fgraph);
196 output_graph (&graph, fgraph);
ce4d5ce0 197
08a946e0 198 /* Output nodes and edges. */
2e729273 199 new_closure (nitems);
ce4d5ce0 200 for (i = 0; i < nstates; i++)
065fbd27 201 print_state (state_table[i]);
2e729273 202 free_closure ();
ce4d5ce0
AD
203
204 /* Close graph. */
342b8b6e
AD
205 close_graph (&graph, fgraph);
206 xfclose (fgraph);
ce4d5ce0 207}