]> git.saurik.com Git - bison.git/blame - src/print_graph.c
(check_and_convert_grammar): New function.
[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 21
2cec9080 22#include <config.h>
ce4d5ce0 23#include "system.h"
16bb742f 24
16bb742f
PE
25#include <quotearg.h>
26
ce4d5ce0 27#include "LR0.h"
16bb742f 28#include "closure.h"
ce4d5ce0 29#include "complain.h"
16bb742f
PE
30#include "conflicts.h"
31#include "files.h"
ce4d5ce0 32#include "getargs.h"
16bb742f
PE
33#include "gram.h"
34#include "lalr.h"
ce4d5ce0 35#include "print_graph.h"
16bb742f
PE
36#include "reader.h"
37#include "state.h"
38#include "symtab.h"
ce4d5ce0
AD
39#include "vcg.h"
40
16bb742f 41static graph static_graph;
342b8b6e 42static FILE *fgraph = NULL;
ce4d5ce0 43
8adfa272 44
6b98e4b5
AD
45/*----------------------------.
46| Construct the node labels. |
47`----------------------------*/
8adfa272 48
ce4d5ce0 49static void
16bb742f 50print_core (struct obstack *oout, state *s)
ce4d5ce0 51{
f6fbd3da 52 size_t i;
16bb742f 53 item_number *sitems = s->items;
f6fbd3da 54 size_t snritems = s->nitems;
22c2cbc0 55
2e729273 56 /* Output all the items of a state, not only its kernel. */
05811fd7 57 if (report_flag & report_itemsets)
30171f79 58 {
5123689b 59 closure (sitems, snritems);
30171f79 60 sitems = itemset;
5123689b 61 snritems = nritemset;
30171f79 62 }
22c2cbc0 63
16bb742f 64 obstack_fgrow1 (oout, "state %2d\n", s->number);
5123689b 65 for (i = 0; i < snritems; i++)
ce4d5ce0 66 {
16bb742f
PE
67 item_number *sp;
68 item_number *sp1;
69 rule_number r;
c4b66126 70
2e729273 71 sp1 = sp = ritem + sitems[i];
ce4d5ce0 72
75142d45 73 while (*sp >= 0)
ce4d5ce0
AD
74 sp++;
75
16bb742f 76 r = item_number_as_rule_number (*sp);
ce4d5ce0 77
4bc30f78 78 if (i)
d4e7d3a1
AD
79 obstack_1grow (oout, '\n');
80 obstack_fgrow1 (oout, " %s -> ",
16bb742f 81 rules[r].lhs->tag);
ce4d5ce0 82
16bb742f 83 for (sp = rules[r].rhs; sp < sp1; sp++)
97650f4e 84 obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag);
ce4d5ce0 85
d4e7d3a1 86 obstack_1grow (oout, '.');
22c2cbc0 87
75142d45 88 for (/* Nothing */; *sp >= 0; ++sp)
97650f4e 89 obstack_fgrow1 (oout, " %s", symbols[*sp]->tag);
d4e7d3a1 90
8dd162d3
PE
91 /* Experimental feature: display the look-ahead tokens. */
92 if (report_flag & report_look_ahead_tokens)
d4e7d3a1 93 {
cd08e51e 94 /* Find the reduction we are handling. */
16bb742f
PE
95 reductions *reds = s->reductions;
96 int redno = state_reduction_find (s, &rules[r]);
613f5e1a 97
cd08e51e 98 /* Print them if there are. */
8dd162d3 99 if (reds->look_ahead_tokens && redno != -1)
d4e7d3a1 100 {
cd08e51e
AD
101 bitset_iterator biter;
102 int k;
d0829076 103 char const *sep = "";
cd08e51e 104 obstack_sgrow (oout, "[");
8dd162d3 105 BITSET_FOR_EACH (biter, reds->look_ahead_tokens[redno], k, 0)
d0829076
PE
106 {
107 obstack_fgrow2 (oout, "%s%s", sep, symbols[k]->tag);
108 sep = ", ";
109 }
d4e7d3a1
AD
110 obstack_sgrow (oout, "]");
111 }
112 }
ce4d5ce0 113 }
ce4d5ce0
AD
114}
115
08a946e0
AD
116
117/*---------------------------------------------------------------.
118| Output in graph_obstack edges specifications in incidence with |
119| current node. |
120`---------------------------------------------------------------*/
121
ce4d5ce0 122static void
16bb742f 123print_actions (state *s, const char *node_name)
ce4d5ce0
AD
124{
125 int i;
d954473d 126
16bb742f
PE
127 transitions *trans = s->transitions;
128 reductions *reds = s->reductions;
d954473d 129
ce4d5ce0 130 static char buff[10];
16bb742f 131 edge e;
22c2cbc0 132
16bb742f 133 if (!trans->num && !reds)
5092aba5 134 return;
ce4d5ce0 135
16bb742f
PE
136 for (i = 0; i < trans->num; i++)
137 if (!TRANSITION_IS_DISABLED (trans, i))
d954473d 138 {
16bb742f
PE
139 state *s1 = trans->states[i];
140 symbol_number sym = s1->accessing_symbol;
d954473d 141
16bb742f 142 new_edge (&e);
d954473d 143
16bb742f
PE
144 if (s->number > s1->number)
145 e.type = back_edge;
146 open_edge (&e, fgraph);
d954473d 147 /* The edge source is the current node. */
16bb742f
PE
148 e.sourcename = node_name;
149 sprintf (buff, "%d", s1->number);
150 e.targetname = buff;
8adfa272 151 /* Shifts are blue, gotos are green, and error is red. */
16bb742f
PE
152 if (TRANSITION_IS_ERROR (trans, i))
153 e.color = red;
8adfa272 154 else
16bb742f
PE
155 e.color = TRANSITION_IS_SHIFT (trans, i) ? blue : green;
156 e.label = symbols[sym]->tag;
157 output_edge (&e, fgraph);
d954473d
AD
158 close_edge (fgraph);
159 }
ce4d5ce0
AD
160}
161
08a946e0
AD
162
163/*-------------------------------------------------------------.
164| Output in FGRAPH the current node specifications and exiting |
165| edges. |
166`-------------------------------------------------------------*/
167
ce4d5ce0 168static void
16bb742f 169print_state (state *s)
ce4d5ce0
AD
170{
171 static char name[10];
600cad3b 172 struct obstack node_obstack;
16bb742f 173 node n;
ce4d5ce0 174
08a946e0 175 /* The labels of the nodes are their the items. */
600cad3b 176 obstack_init (&node_obstack);
16bb742f
PE
177 new_node (&n);
178 sprintf (name, "%d", s->number);
179 n.title = name;
180 print_core (&node_obstack, s);
08a946e0 181 obstack_1grow (&node_obstack, '\0');
16bb742f 182 n.label = obstack_finish (&node_obstack);
342b8b6e
AD
183
184 open_node (fgraph);
16bb742f 185 output_node (&n, fgraph);
342b8b6e
AD
186 close_node (fgraph);
187
08a946e0 188 /* Output the edges. */
16bb742f 189 print_actions (s, name);
08a946e0 190
342b8b6e 191 obstack_free (&node_obstack, 0);
ce4d5ce0
AD
192}
193\f
194
195void
196print_graph (void)
197{
16bb742f 198 state_number i;
9703cc49 199
342b8b6e
AD
200 /* Output file. */
201 fgraph = xfopen (spec_graph_file, "w");
202
16bb742f 203 new_graph (&static_graph);
22c2cbc0 204
16bb742f 205 static_graph.display_edge_labels = yes;
22c2cbc0 206
16bb742f
PE
207 static_graph.port_sharing = no;
208 static_graph.finetuning = yes;
16bb742f
PE
209 static_graph.priority_phase = yes;
210 static_graph.splines = yes;
22c2cbc0 211
16bb742f 212 static_graph.crossing_weight = median;
ce4d5ce0
AD
213
214 /* Output graph options. */
342b8b6e 215 open_graph (fgraph);
16bb742f 216 output_graph (&static_graph, fgraph);
ce4d5ce0 217
08a946e0 218 /* Output nodes and edges. */
9e7f6bbd 219 new_closure (nritems);
ce4d5ce0 220 for (i = 0; i < nstates; i++)
29e88316 221 print_state (states[i]);
2e729273 222 free_closure ();
ce4d5ce0
AD
223
224 /* Close graph. */
16bb742f 225 close_graph (&static_graph, fgraph);
342b8b6e 226 xfclose (fgraph);
ce4d5ce0 227}