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