]> git.saurik.com Git - bison.git/blob - src/print_graph.c
version 3.0
[bison.git] / src / print_graph.c
1 /* Output a graph of the generated parser, for Bison.
2
3 Copyright (C) 2001-2007, 2009-2013 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 This program 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 3 of the License, or
10 (at your option) any later version.
11
12 This program 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 this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include <config.h>
21 #include "system.h"
22
23 #include "LR0.h"
24 #include "closure.h"
25 #include "complain.h"
26 #include "conflicts.h"
27 #include "files.h"
28 #include "getargs.h"
29 #include "gram.h"
30 #include "graphviz.h"
31 #include "lalr.h"
32 #include "print_graph.h"
33 #include "reader.h"
34 #include "state.h"
35 #include "symtab.h"
36
37
38 /*----------------------------.
39 | Construct the node labels. |
40 `----------------------------*/
41
42 /* Print the lhs of a rule in such a manner that there is no vertical
43 repetition, like in *.output files. */
44
45 static void
46 print_core (struct obstack *oout, state *s)
47 {
48 item_number const *sitems = s->items;
49 symbol *previous_lhs = NULL;
50 size_t i;
51 size_t snritems = s->nitems;
52
53 /* Output all the items of a state, not just its kernel. */
54 if (report_flag & report_itemsets)
55 {
56 closure (sitems, snritems);
57 sitems = itemset;
58 snritems = nitemset;
59 }
60
61 obstack_printf (oout, _("State %d"), s->number);
62 obstack_sgrow (oout, "\\n\\l");
63 for (i = 0; i < snritems; i++)
64 {
65 item_number const *sp1 = ritem + sitems[i];
66 item_number const *sp = sp1;
67 rule *r;
68
69 while (0 <= *sp)
70 sp++;
71
72 r = &rules[item_number_as_rule_number (*sp)];
73
74 obstack_printf (oout, "%3d ", r->number);
75 if (previous_lhs && UNIQSTR_EQ (previous_lhs->tag, r->lhs->tag))
76 obstack_printf (oout, "%*s| ",
77 (int) strlen (previous_lhs->tag), "");
78 else
79 obstack_printf (oout, "%s: ", escape (r->lhs->tag));
80 previous_lhs = r->lhs;
81
82 for (sp = r->rhs; sp < sp1; sp++)
83 obstack_printf (oout, "%s ", escape (symbols[*sp]->tag));
84
85 obstack_1grow (oout, '.');
86
87 for (/* Nothing */; *sp >= 0; ++sp)
88 obstack_printf (oout, " %s", escape (symbols[*sp]->tag));
89
90 /* Experimental feature: display the lookahead tokens. */
91 if (report_flag & report_lookahead_tokens
92 && item_number_is_rule_number (*sp1))
93 {
94 /* Find the reduction we are handling. */
95 reductions *reds = s->reductions;
96 int redno = state_reduction_find (s, r);
97
98 /* Print them if there are. */
99 if (reds->lookahead_tokens && redno != -1)
100 {
101 bitset_iterator biter;
102 int k;
103 char const *sep = "";
104 obstack_sgrow (oout, " [");
105 BITSET_FOR_EACH (biter, reds->lookahead_tokens[redno], k, 0)
106 {
107 obstack_sgrow (oout, sep);
108 obstack_sgrow (oout, escape (symbols[k]->tag));
109 sep = ", ";
110 }
111 obstack_1grow (oout, ']');
112 }
113 }
114 obstack_sgrow (oout, "\\l");
115 }
116 }
117
118
119 /*---------------------------------------------------------------.
120 | Output in graph_obstack edges specifications in incidence with |
121 | current node. |
122 `---------------------------------------------------------------*/
123
124 static void
125 print_actions (state const *s, FILE *fgraph)
126 {
127 transitions const *trans = s->transitions;
128 int i;
129
130 if (!trans->num && !s->reductions)
131 return;
132
133 for (i = 0; i < trans->num; i++)
134 if (!TRANSITION_IS_DISABLED (trans, i))
135 {
136 state *s1 = trans->states[i];
137 symbol_number sym = s1->accessing_symbol;
138
139 /* Shifts are solid, gotos are dashed, and error is dotted. */
140 char const *style =
141 (TRANSITION_IS_ERROR (trans, i) ? "dotted"
142 : TRANSITION_IS_SHIFT (trans, i) ? "solid"
143 : "dashed");
144
145 if (TRANSITION_IS_ERROR (trans, i)
146 && STRNEQ (symbols[sym]->tag, "error"))
147 abort ();
148 output_edge (s->number, s1->number,
149 TRANSITION_IS_ERROR (trans, i) ? NULL : symbols[sym]->tag,
150 style, fgraph);
151 }
152 /* Display reductions. */
153 output_red (s, s->reductions, fgraph);
154 }
155
156
157 /*-------------------------------------------------------------.
158 | Output in FGRAPH the current node specifications and exiting |
159 | edges. |
160 `-------------------------------------------------------------*/
161
162 static void
163 print_state (state *s, FILE *fgraph)
164 {
165 struct obstack node_obstack;
166
167 /* A node's label contains its items. */
168 obstack_init (&node_obstack);
169 print_core (&node_obstack, s);
170 output_node (s->number, obstack_finish0 (&node_obstack), fgraph);
171 obstack_free (&node_obstack, 0);
172
173 /* Output the edges. */
174 print_actions (s, fgraph);
175 }
176 \f
177
178 void
179 print_graph (void)
180 {
181 state_number i;
182 FILE *fgraph = xfopen (spec_graph_file, "w");
183 start_graph (fgraph);
184
185 /* Output nodes and edges. */
186 new_closure (nritems);
187 for (i = 0; i < nstates; i++)
188 print_state (states[i], fgraph);
189 free_closure ();
190
191 finish_graph (fgraph);
192 xfclose (fgraph);
193 }