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