]>
Commit | Line | Data |
---|---|---|
35fe0834 | 1 | /* Output a graph of the generated parser, for Bison. |
16bb742f | 2 | |
34136e65 | 3 | Copyright (C) 2001-2007, 2009-2012 Free Software Foundation, Inc. |
ce4d5ce0 AD |
4 | |
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
f16b0819 | 7 | This program is free software: you can redistribute it and/or modify |
ce4d5ce0 | 8 | it under the terms of the GNU General Public License as published by |
f16b0819 PE |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
ce4d5ce0 | 11 | |
f16b0819 | 12 | This program is distributed in the hope that it will be useful, |
ce4d5ce0 AD |
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 | |
f16b0819 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
ce4d5ce0 | 19 | |
2cec9080 | 20 | #include <config.h> |
ce4d5ce0 | 21 | #include "system.h" |
16bb742f | 22 | |
ce4d5ce0 | 23 | #include "LR0.h" |
16bb742f | 24 | #include "closure.h" |
ce4d5ce0 | 25 | #include "complain.h" |
16bb742f PE |
26 | #include "conflicts.h" |
27 | #include "files.h" | |
ce4d5ce0 | 28 | #include "getargs.h" |
16bb742f | 29 | #include "gram.h" |
35fe0834 | 30 | #include "graphviz.h" |
16bb742f | 31 | #include "lalr.h" |
ce4d5ce0 | 32 | #include "print_graph.h" |
16bb742f PE |
33 | #include "reader.h" |
34 | #include "state.h" | |
35 | #include "symtab.h" | |
ce4d5ce0 | 36 | |
8adfa272 | 37 | |
6b98e4b5 AD |
38 | /*----------------------------. |
39 | | Construct the node labels. | | |
40 | `----------------------------*/ | |
8adfa272 | 41 | |
ce6cf10f TR |
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_lhs (struct obstack *oout, rule *previous_rule, rule *r) | |
47 | { | |
48 | if (previous_rule && STREQ (previous_rule->lhs->tag, r->lhs->tag)) | |
49 | { | |
50 | int i; | |
51 | for (i = 0; i < strlen (r->lhs->tag); ++i) | |
52 | obstack_1grow (oout, ' '); | |
53 | obstack_1grow (oout, '|'); | |
54 | } | |
55 | else | |
56 | { | |
57 | obstack_sgrow (oout, escape (r->lhs->tag)); | |
58 | obstack_1grow (oout, ':'); | |
59 | } | |
32288c8c | 60 | obstack_1grow (oout, ' '); |
ce6cf10f TR |
61 | } |
62 | ||
ce4d5ce0 | 63 | static void |
16bb742f | 64 | print_core (struct obstack *oout, state *s) |
ce4d5ce0 | 65 | { |
16bb742f | 66 | item_number *sitems = s->items; |
ce6cf10f TR |
67 | rule *previous_rule = NULL; |
68 | size_t i; | |
f6fbd3da | 69 | size_t snritems = s->nitems; |
22c2cbc0 | 70 | |
2e729273 | 71 | /* Output all the items of a state, not only its kernel. */ |
05811fd7 | 72 | if (report_flag & report_itemsets) |
30171f79 | 73 | { |
5123689b | 74 | closure (sitems, snritems); |
30171f79 | 75 | sitems = itemset; |
b09f4f48 | 76 | snritems = nitemset; |
30171f79 | 77 | } |
22c2cbc0 | 78 | |
8048226f | 79 | obstack_printf (oout, _("State %d"), s->number); |
be3517b0 | 80 | obstack_sgrow (oout, "\\n\\l"); |
5123689b | 81 | for (i = 0; i < snritems; i++) |
ce4d5ce0 | 82 | { |
16bb742f PE |
83 | item_number *sp; |
84 | item_number *sp1; | |
85 | rule_number r; | |
c4b66126 | 86 | |
2e729273 | 87 | sp1 = sp = ritem + sitems[i]; |
ce4d5ce0 | 88 | |
75142d45 | 89 | while (*sp >= 0) |
e9690142 | 90 | sp++; |
ce4d5ce0 | 91 | |
16bb742f | 92 | r = item_number_as_rule_number (*sp); |
ce4d5ce0 | 93 | |
ce6cf10f TR |
94 | obstack_printf (oout, "%3d ", r); |
95 | print_lhs (oout, previous_rule, &rules[r]); | |
96 | previous_rule = &rules[r]; | |
ce4d5ce0 | 97 | |
16bb742f | 98 | for (sp = rules[r].rhs; sp < sp1; sp++) |
a13121f7 | 99 | obstack_printf (oout, "%s ", escape (symbols[*sp]->tag)); |
ce4d5ce0 | 100 | |
32288c8c | 101 | obstack_1grow (oout, '.'); |
22c2cbc0 | 102 | |
75142d45 | 103 | for (/* Nothing */; *sp >= 0; ++sp) |
a13121f7 | 104 | obstack_printf (oout, " %s", escape (symbols[*sp]->tag)); |
d4e7d3a1 | 105 | |
742e4900 | 106 | /* Experimental feature: display the lookahead tokens. */ |
255a6b90 JD |
107 | if (report_flag & report_lookahead_tokens |
108 | && item_number_is_rule_number (*sp1)) | |
e9690142 JD |
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 = ""; | |
8048226f | 120 | obstack_sgrow (oout, " ["); |
e9690142 JD |
121 | BITSET_FOR_EACH (biter, reds->lookahead_tokens[redno], k, 0) |
122 | { | |
a13121f7 TR |
123 | obstack_sgrow (oout, sep); |
124 | obstack_sgrow (oout, escape (symbols[k]->tag)); | |
e9690142 JD |
125 | sep = ", "; |
126 | } | |
a13121f7 | 127 | obstack_1grow (oout, ']'); |
e9690142 JD |
128 | } |
129 | } | |
a13121f7 | 130 | obstack_sgrow (oout, "\\l"); |
ce4d5ce0 | 131 | } |
ce4d5ce0 AD |
132 | } |
133 | ||
08a946e0 AD |
134 | |
135 | /*---------------------------------------------------------------. | |
136 | | Output in graph_obstack edges specifications in incidence with | | |
137 | | current node. | | |
138 | `---------------------------------------------------------------*/ | |
139 | ||
ce4d5ce0 | 140 | static void |
35fe0834 | 141 | print_actions (state const *s, FILE *fgraph) |
ce4d5ce0 | 142 | { |
35fe0834 | 143 | transitions const *trans = s->transitions; |
8048226f | 144 | int i; |
22c2cbc0 | 145 | |
35fe0834 | 146 | if (!trans->num && !s->reductions) |
5092aba5 | 147 | return; |
ce4d5ce0 | 148 | |
16bb742f PE |
149 | for (i = 0; i < trans->num; i++) |
150 | if (!TRANSITION_IS_DISABLED (trans, i)) | |
d954473d | 151 | { |
e9690142 JD |
152 | state *s1 = trans->states[i]; |
153 | symbol_number sym = s1->accessing_symbol; | |
154 | ||
155 | /* Shifts are solid, gotos are dashed, and error is dotted. */ | |
156 | char const *style = | |
157 | (TRANSITION_IS_ERROR (trans, i) ? "dotted" | |
158 | : TRANSITION_IS_SHIFT (trans, i) ? "solid" | |
159 | : "dashed"); | |
160 | ||
161 | if (TRANSITION_IS_ERROR (trans, i) | |
f518dbaf | 162 | && STRNEQ (symbols[sym]->tag, "error")) |
e9690142 JD |
163 | abort (); |
164 | output_edge (s->number, s1->number, | |
165 | TRANSITION_IS_ERROR (trans, i) ? NULL : symbols[sym]->tag, | |
166 | style, fgraph); | |
d954473d | 167 | } |
be3517b0 TR |
168 | /* Display reductions. */ |
169 | output_red (s, s->reductions, fgraph); | |
ce4d5ce0 AD |
170 | } |
171 | ||
08a946e0 AD |
172 | |
173 | /*-------------------------------------------------------------. | |
174 | | Output in FGRAPH the current node specifications and exiting | | |
175 | | edges. | | |
176 | `-------------------------------------------------------------*/ | |
177 | ||
ce4d5ce0 | 178 | static void |
35fe0834 | 179 | print_state (state *s, FILE *fgraph) |
ce4d5ce0 | 180 | { |
600cad3b | 181 | struct obstack node_obstack; |
ce4d5ce0 | 182 | |
35fe0834 | 183 | /* A node's label contains its items. */ |
600cad3b | 184 | obstack_init (&node_obstack); |
16bb742f | 185 | print_core (&node_obstack, s); |
6fbe73b6 | 186 | output_node (s->number, obstack_finish0 (&node_obstack), fgraph); |
35fe0834 | 187 | obstack_free (&node_obstack, 0); |
342b8b6e | 188 | |
08a946e0 | 189 | /* Output the edges. */ |
35fe0834 | 190 | print_actions (s, fgraph); |
ce4d5ce0 AD |
191 | } |
192 | \f | |
193 | ||
194 | void | |
195 | print_graph (void) | |
196 | { | |
16bb742f | 197 | state_number i; |
35fe0834 PE |
198 | FILE *fgraph = xfopen (spec_graph_file, "w"); |
199 | start_graph (fgraph); | |
ce4d5ce0 | 200 | |
08a946e0 | 201 | /* Output nodes and edges. */ |
9e7f6bbd | 202 | new_closure (nritems); |
ce4d5ce0 | 203 | for (i = 0; i < nstates; i++) |
35fe0834 | 204 | print_state (states[i], fgraph); |
2e729273 | 205 | free_closure (); |
ce4d5ce0 | 206 | |
35fe0834 | 207 | finish_graph (fgraph); |
342b8b6e | 208 | xfclose (fgraph); |
ce4d5ce0 | 209 | } |