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