]> git.saurik.com Git - bison.git/blame - src/print_graph.c
Renaming file to glr.c.
[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 41
6b98e4b5
AD
42/*----------------------------.
43| Construct the node labels. |
44`----------------------------*/
8adfa272 45
ce4d5ce0 46static void
d4e7d3a1 47print_core (struct obstack *oout, state_t *state)
ce4d5ce0
AD
48{
49 int i;
62a3e4f0 50 item_number_t *sitems = state->items;
5123689b 51 int snritems = state->nitems;
22c2cbc0 52
2e729273 53 /* Output all the items of a state, not only its kernel. */
30171f79
AD
54 if (trace_flag)
55 {
5123689b 56 closure (sitems, snritems);
30171f79 57 sitems = itemset;
5123689b 58 snritems = nritemset;
30171f79 59 }
22c2cbc0 60
d4e7d3a1 61 obstack_fgrow1 (oout, "state %2d\n", state->number);
5123689b 62 for (i = 0; i < snritems; i++)
ce4d5ce0 63 {
62a3e4f0
AD
64 item_number_t *sp;
65 item_number_t *sp1;
4bc30f78 66 int rule;
c4b66126 67
2e729273 68 sp1 = sp = ritem + sitems[i];
ce4d5ce0 69
75142d45 70 while (*sp >= 0)
ce4d5ce0
AD
71 sp++;
72
73 rule = -(*sp);
74
4bc30f78 75 if (i)
d4e7d3a1
AD
76 obstack_1grow (oout, '\n');
77 obstack_fgrow1 (oout, " %s -> ",
6b98e4b5 78 symbol_tag_get (rules[rule].lhs));
ce4d5ce0 79
99013900 80 for (sp = rules[rule].rhs; sp < sp1; sp++)
6b98e4b5 81 obstack_fgrow1 (oout, "%s ", symbol_tag_get (symbols[*sp]));
ce4d5ce0 82
d4e7d3a1 83 obstack_1grow (oout, '.');
22c2cbc0 84
75142d45 85 for (/* Nothing */; *sp >= 0; ++sp)
6b98e4b5 86 obstack_fgrow1 (oout, " %s", symbol_tag_get (symbols[*sp]));
d4e7d3a1
AD
87
88 /* Experimental feature: display the lookaheads. */
89 if (trace_flag && state->nlookaheads)
90 {
91 int j, k;
92 int nlookaheads = 0;
93 /* Look for lookaheads corresponding to this rule. */
94 for (j = 0; j < state->nlookaheads; ++j)
95 for (k = 0; k < ntokens; ++k)
c0263492
AD
96 if (bitset_test (state->lookaheads[j], k)
97 && state->lookaheads_rule[j]->number == rule)
d4e7d3a1
AD
98 nlookaheads++;
99 if (nlookaheads)
100 {
101 obstack_sgrow (oout, " [");
102 for (j = 0; j < state->nlookaheads; ++j)
103 for (k = 0; k < ntokens; ++k)
c0263492
AD
104 if (bitset_test (state->lookaheads[j], k)
105 && state->lookaheads_rule[j]->number == rule)
d4e7d3a1 106 obstack_fgrow2 (oout, "%s%s",
6b98e4b5 107 symbol_tag_get (symbols[k]),
d4e7d3a1
AD
108 --nlookaheads ? ", " : "");
109 obstack_sgrow (oout, "]");
110 }
111 }
ce4d5ce0 112 }
ce4d5ce0
AD
113}
114
08a946e0
AD
115
116/*---------------------------------------------------------------.
117| Output in graph_obstack edges specifications in incidence with |
118| current node. |
119`---------------------------------------------------------------*/
120
ce4d5ce0 121static void
065fbd27 122print_actions (state_t *state, const char *node_name)
ce4d5ce0
AD
123{
124 int i;
d954473d 125
065fbd27
AD
126 shifts *shiftp = state->shifts;
127 reductions *redp = state->reductions;
d954473d 128
ce4d5ce0
AD
129 static char buff[10];
130 edge_t edge;
22c2cbc0 131
d954473d 132 if (!shiftp->nshifts && !redp)
5092aba5 133 return;
ce4d5ce0 134
d954473d
AD
135 for (i = 0; i < shiftp->nshifts; i++)
136 if (!SHIFT_IS_DISABLED (shiftp, i))
137 {
138 int state1 = shiftp->shifts[i];
a49aecd5 139 symbol_number_t symbol = states[state1]->accessing_symbol;
d954473d
AD
140
141 new_edge (&edge);
142
065fbd27 143 if (state->number > state1)
d954473d
AD
144 edge.type = back_edge;
145 open_edge (&edge, fgraph);
146 /* The edge source is the current node. */
147 edge.sourcename = node_name;
148 sprintf (buff, "%d", state1);
149 edge.targetname = buff;
8adfa272
AD
150 /* Shifts are blue, gotos are green, and error is red. */
151 if (SHIFT_IS_ERROR (shiftp, i))
152 edge.color = red;
153 else
154 edge.color = SHIFT_IS_SHIFT(shiftp, i) ? blue : green;
6b98e4b5 155 edge.label = symbol_tag_get (symbols[symbol]);
d954473d
AD
156 output_edge (&edge, fgraph);
157 close_edge (fgraph);
158 }
ce4d5ce0
AD
159}
160
08a946e0
AD
161
162/*-------------------------------------------------------------.
163| Output in FGRAPH the current node specifications and exiting |
164| edges. |
165`-------------------------------------------------------------*/
166
ce4d5ce0 167static void
065fbd27 168print_state (state_t *state)
ce4d5ce0
AD
169{
170 static char name[10];
600cad3b 171 struct obstack node_obstack;
22c2cbc0 172 node_t node;
ce4d5ce0 173
08a946e0 174 /* The labels of the nodes are their the items. */
600cad3b 175 obstack_init (&node_obstack);
08a946e0 176 new_node (&node);
065fbd27 177 sprintf (name, "%d", state->number);
08a946e0 178 node.title = name;
d4e7d3a1 179 print_core (&node_obstack, state);
08a946e0
AD
180 obstack_1grow (&node_obstack, '\0');
181 node.label = obstack_finish (&node_obstack);
342b8b6e
AD
182
183 open_node (fgraph);
342b8b6e 184 output_node (&node, fgraph);
342b8b6e
AD
185 close_node (fgraph);
186
08a946e0
AD
187 /* Output the edges. */
188 print_actions (state, name);
189
342b8b6e 190 obstack_free (&node_obstack, 0);
ce4d5ce0
AD
191}
192\f
193
194void
195print_graph (void)
196{
602bbf31 197 size_t i;
9703cc49 198
342b8b6e
AD
199 /* Output file. */
200 fgraph = xfopen (spec_graph_file, "w");
201
ce4d5ce0 202 new_graph (&graph);
22c2cbc0 203
600cad3b
MA
204#if 0
205 graph.smanhattan_edges = yes;
9703cc49 206 graph.manhattan_edges = yes;
600cad3b 207#endif
22c2cbc0 208
ce4d5ce0 209 graph.display_edge_labels = yes;
342b8b6e 210 graph.layoutalgorithm = normal;
22c2cbc0 211
ce4d5ce0
AD
212 graph.port_sharing = no;
213 graph.finetuning = yes;
214 graph.straight_phase = yes;
215 graph.priority_phase = yes;
216 graph.splines = yes;
22c2cbc0 217
ce4d5ce0
AD
218 graph.crossing_weight = median;
219
220 /* Output graph options. */
342b8b6e
AD
221 open_graph (fgraph);
222 output_graph (&graph, fgraph);
ce4d5ce0 223
08a946e0 224 /* Output nodes and edges. */
9e7f6bbd 225 new_closure (nritems);
ce4d5ce0 226 for (i = 0; i < nstates; i++)
29e88316 227 print_state (states[i]);
2e729273 228 free_closure ();
ce4d5ce0
AD
229
230 /* Close graph. */
342b8b6e
AD
231 close_graph (&graph, fgraph);
232 xfclose (fgraph);
ce4d5ce0 233}