]> git.saurik.com Git - bison.git/blame - src/print_graph.c
Port to SunOS 4.1.4, which lacks strtoul and strerror.
[bison.git] / src / print_graph.c
CommitLineData
22c2cbc0 1/* Output a VCG description on generated parser, for Bison,
16bb742f 2
04098407 3 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
ce4d5ce0
AD
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
0fb669f9
PE
19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
ce4d5ce0
AD
21
22#include "system.h"
16bb742f 23
16bb742f
PE
24#include <quotearg.h>
25
ce4d5ce0 26#include "LR0.h"
16bb742f 27#include "closure.h"
ce4d5ce0 28#include "complain.h"
16bb742f
PE
29#include "conflicts.h"
30#include "files.h"
ce4d5ce0 31#include "getargs.h"
16bb742f
PE
32#include "gram.h"
33#include "lalr.h"
ce4d5ce0 34#include "print_graph.h"
16bb742f
PE
35#include "reader.h"
36#include "state.h"
37#include "symtab.h"
ce4d5ce0
AD
38#include "vcg.h"
39
16bb742f 40static graph static_graph;
342b8b6e 41static FILE *fgraph = NULL;
ce4d5ce0 42
8adfa272 43
6b98e4b5
AD
44/*----------------------------.
45| Construct the node labels. |
46`----------------------------*/
8adfa272 47
ce4d5ce0 48static void
16bb742f 49print_core (struct obstack *oout, state *s)
ce4d5ce0 50{
f6fbd3da 51 size_t i;
16bb742f 52 item_number *sitems = s->items;
f6fbd3da 53 size_t snritems = s->nitems;
22c2cbc0 54
2e729273 55 /* Output all the items of a state, not only its kernel. */
05811fd7 56 if (report_flag & report_itemsets)
30171f79 57 {
5123689b 58 closure (sitems, snritems);
30171f79 59 sitems = itemset;
5123689b 60 snritems = nritemset;
30171f79 61 }
22c2cbc0 62
16bb742f 63 obstack_fgrow1 (oout, "state %2d\n", s->number);
5123689b 64 for (i = 0; i < snritems; i++)
ce4d5ce0 65 {
16bb742f
PE
66 item_number *sp;
67 item_number *sp1;
68 rule_number r;
c4b66126 69
2e729273 70 sp1 = sp = ritem + sitems[i];
ce4d5ce0 71
75142d45 72 while (*sp >= 0)
ce4d5ce0
AD
73 sp++;
74
16bb742f 75 r = item_number_as_rule_number (*sp);
ce4d5ce0 76
4bc30f78 77 if (i)
d4e7d3a1
AD
78 obstack_1grow (oout, '\n');
79 obstack_fgrow1 (oout, " %s -> ",
16bb742f 80 rules[r].lhs->tag);
ce4d5ce0 81
16bb742f 82 for (sp = rules[r].rhs; sp < sp1; sp++)
97650f4e 83 obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag);
ce4d5ce0 84
d4e7d3a1 85 obstack_1grow (oout, '.');
22c2cbc0 86
75142d45 87 for (/* Nothing */; *sp >= 0; ++sp)
97650f4e 88 obstack_fgrow1 (oout, " %s", symbols[*sp]->tag);
d4e7d3a1 89
8dd162d3
PE
90 /* Experimental feature: display the look-ahead tokens. */
91 if (report_flag & report_look_ahead_tokens)
d4e7d3a1 92 {
cd08e51e 93 /* Find the reduction we are handling. */
16bb742f
PE
94 reductions *reds = s->reductions;
95 int redno = state_reduction_find (s, &rules[r]);
613f5e1a 96
cd08e51e 97 /* Print them if there are. */
8dd162d3 98 if (reds->look_ahead_tokens && redno != -1)
d4e7d3a1 99 {
cd08e51e
AD
100 bitset_iterator biter;
101 int k;
d0829076 102 char const *sep = "";
cd08e51e 103 obstack_sgrow (oout, "[");
8dd162d3 104 BITSET_FOR_EACH (biter, reds->look_ahead_tokens[redno], k, 0)
d0829076
PE
105 {
106 obstack_fgrow2 (oout, "%s%s", sep, symbols[k]->tag);
107 sep = ", ";
108 }
d4e7d3a1
AD
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
16bb742f 122print_actions (state *s, const char *node_name)
ce4d5ce0
AD
123{
124 int i;
d954473d 125
16bb742f
PE
126 transitions *trans = s->transitions;
127 reductions *reds = s->reductions;
d954473d 128
ce4d5ce0 129 static char buff[10];
16bb742f 130 edge e;
22c2cbc0 131
16bb742f 132 if (!trans->num && !reds)
5092aba5 133 return;
ce4d5ce0 134
16bb742f
PE
135 for (i = 0; i < trans->num; i++)
136 if (!TRANSITION_IS_DISABLED (trans, i))
d954473d 137 {
16bb742f
PE
138 state *s1 = trans->states[i];
139 symbol_number sym = s1->accessing_symbol;
d954473d 140
16bb742f 141 new_edge (&e);
d954473d 142
16bb742f
PE
143 if (s->number > s1->number)
144 e.type = back_edge;
145 open_edge (&e, fgraph);
d954473d 146 /* The edge source is the current node. */
16bb742f
PE
147 e.sourcename = node_name;
148 sprintf (buff, "%d", s1->number);
149 e.targetname = buff;
8adfa272 150 /* Shifts are blue, gotos are green, and error is red. */
16bb742f
PE
151 if (TRANSITION_IS_ERROR (trans, i))
152 e.color = red;
8adfa272 153 else
16bb742f
PE
154 e.color = TRANSITION_IS_SHIFT (trans, i) ? blue : green;
155 e.label = symbols[sym]->tag;
156 output_edge (&e, fgraph);
d954473d
AD
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
16bb742f 168print_state (state *s)
ce4d5ce0
AD
169{
170 static char name[10];
600cad3b 171 struct obstack node_obstack;
16bb742f 172 node n;
ce4d5ce0 173
08a946e0 174 /* The labels of the nodes are their the items. */
600cad3b 175 obstack_init (&node_obstack);
16bb742f
PE
176 new_node (&n);
177 sprintf (name, "%d", s->number);
178 n.title = name;
179 print_core (&node_obstack, s);
08a946e0 180 obstack_1grow (&node_obstack, '\0');
16bb742f 181 n.label = obstack_finish (&node_obstack);
342b8b6e
AD
182
183 open_node (fgraph);
16bb742f 184 output_node (&n, fgraph);
342b8b6e
AD
185 close_node (fgraph);
186
08a946e0 187 /* Output the edges. */
16bb742f 188 print_actions (s, name);
08a946e0 189
342b8b6e 190 obstack_free (&node_obstack, 0);
ce4d5ce0
AD
191}
192\f
193
194void
195print_graph (void)
196{
16bb742f 197 state_number i;
9703cc49 198
342b8b6e
AD
199 /* Output file. */
200 fgraph = xfopen (spec_graph_file, "w");
201
16bb742f 202 new_graph (&static_graph);
22c2cbc0 203
16bb742f 204 static_graph.display_edge_labels = yes;
22c2cbc0 205
16bb742f
PE
206 static_graph.port_sharing = no;
207 static_graph.finetuning = yes;
16bb742f
PE
208 static_graph.priority_phase = yes;
209 static_graph.splines = yes;
22c2cbc0 210
16bb742f 211 static_graph.crossing_weight = median;
ce4d5ce0
AD
212
213 /* Output graph options. */
342b8b6e 214 open_graph (fgraph);
16bb742f 215 output_graph (&static_graph, fgraph);
ce4d5ce0 216
08a946e0 217 /* Output nodes and edges. */
9e7f6bbd 218 new_closure (nritems);
ce4d5ce0 219 for (i = 0; i < nstates; i++)
29e88316 220 print_state (states[i]);
2e729273 221 free_closure ();
ce4d5ce0
AD
222
223 /* Close graph. */
16bb742f 224 close_graph (&static_graph, fgraph);
342b8b6e 225 xfclose (fgraph);
ce4d5ce0 226}