]> git.saurik.com Git - bison.git/blame - src/print_graph.c
graphs: address an issue with R/R conflicts
[bison.git] / src / print_graph.c
CommitLineData
35fe0834 1/* Output a graph of the generated parser, for Bison.
16bb742f 2
c932d613 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>
a13121f7 21#include <quotearg.h>
ce4d5ce0 22#include "system.h"
16bb742f 23
ce4d5ce0 24#include "LR0.h"
16bb742f 25#include "closure.h"
ce4d5ce0 26#include "complain.h"
16bb742f
PE
27#include "conflicts.h"
28#include "files.h"
ce4d5ce0 29#include "getargs.h"
16bb742f 30#include "gram.h"
35fe0834 31#include "graphviz.h"
16bb742f 32#include "lalr.h"
ce4d5ce0 33#include "print_graph.h"
16bb742f
PE
34#include "reader.h"
35#include "state.h"
36#include "symtab.h"
ce4d5ce0 37
8adfa272 38
6b98e4b5
AD
39/*----------------------------.
40| Construct the node labels. |
41`----------------------------*/
8adfa272 42
ce4d5ce0 43static void
16bb742f 44print_core (struct obstack *oout, state *s)
ce4d5ce0 45{
f6fbd3da 46 size_t i;
16bb742f 47 item_number *sitems = s->items;
f6fbd3da 48 size_t snritems = s->nitems;
22c2cbc0 49
2e729273 50 /* Output all the items of a state, not only its kernel. */
05811fd7 51 if (report_flag & report_itemsets)
30171f79 52 {
5123689b 53 closure (sitems, snritems);
30171f79 54 sitems = itemset;
b09f4f48 55 snritems = nitemset;
30171f79 56 }
22c2cbc0 57
9fc99ca3 58 obstack_printf (oout, "state %d\\n", s->number);
5123689b 59 for (i = 0; i < snritems; i++)
ce4d5ce0 60 {
16bb742f
PE
61 item_number *sp;
62 item_number *sp1;
63 rule_number r;
c4b66126 64
2e729273 65 sp1 = sp = ritem + sitems[i];
ce4d5ce0 66
75142d45 67 while (*sp >= 0)
a13121f7 68 sp++;
ce4d5ce0 69
16bb742f 70 r = item_number_as_rule_number (*sp);
ce4d5ce0 71
a13121f7 72 obstack_printf (oout, "%d: %s -> ", r, escape (rules[r].lhs->tag));
ce4d5ce0 73
16bb742f 74 for (sp = rules[r].rhs; sp < sp1; sp++)
a13121f7 75 obstack_printf (oout, "%s ", escape (symbols[*sp]->tag));
ce4d5ce0 76
d4e7d3a1 77 obstack_1grow (oout, '.');
22c2cbc0 78
75142d45 79 for (/* Nothing */; *sp >= 0; ++sp)
a13121f7 80 obstack_printf (oout, " %s", escape (symbols[*sp]->tag));
d4e7d3a1 81
742e4900 82 /* Experimental feature: display the lookahead tokens. */
255a6b90
JD
83 if (report_flag & report_lookahead_tokens
84 && item_number_is_rule_number (*sp1))
a13121f7
TR
85 {
86 /* Find the reduction we are handling. */
87 reductions *reds = s->reductions;
88 int redno = state_reduction_find (s, &rules[r]);
89
90 /* Print them if there are. */
91 if (reds->lookahead_tokens && redno != -1)
92 {
93 bitset_iterator biter;
94 int k;
95 char const *sep = "";
96 obstack_1grow (oout, '[');
97 BITSET_FOR_EACH (biter, reds->lookahead_tokens[redno], k, 0)
98 {
99 obstack_sgrow (oout, sep);
100 obstack_sgrow (oout, escape (symbols[k]->tag));
101 sep = ", ";
102 }
103 obstack_1grow (oout, ']');
104 }
105 }
106 obstack_sgrow (oout, "\\l");
ce4d5ce0 107 }
ce4d5ce0
AD
108}
109
08a946e0
AD
110
111/*---------------------------------------------------------------.
112| Output in graph_obstack edges specifications in incidence with |
113| current node. |
114`---------------------------------------------------------------*/
115
ce4d5ce0 116static void
35fe0834 117print_actions (state const *s, FILE *fgraph)
ce4d5ce0
AD
118{
119 int i;
d954473d 120
35fe0834 121 transitions const *trans = s->transitions;
22c2cbc0 122
83bae26d
TR
123 /* Display reductions. */
124 output_red (s, s->reductions, fgraph);
125
35fe0834 126 if (!trans->num && !s->reductions)
5092aba5 127 return;
ce4d5ce0 128
16bb742f
PE
129 for (i = 0; i < trans->num; i++)
130 if (!TRANSITION_IS_DISABLED (trans, i))
d954473d 131 {
16bb742f
PE
132 state *s1 = trans->states[i];
133 symbol_number sym = s1->accessing_symbol;
d954473d 134
35fe0834
PE
135 /* Shifts are solid, gotos are dashed, and error is dotted. */
136 char const *style =
137 (TRANSITION_IS_ERROR (trans, i) ? "dotted"
138 : TRANSITION_IS_SHIFT (trans, i) ? "solid"
139 : "dashed");
140
141 if (TRANSITION_IS_ERROR (trans, i)
142 && strcmp (symbols[sym]->tag, "error") != 0)
143 abort ();
144 output_edge (s->number, s1->number,
145 TRANSITION_IS_ERROR (trans, i) ? NULL : symbols[sym]->tag,
146 style, fgraph);
d954473d 147 }
ce4d5ce0
AD
148}
149
08a946e0
AD
150
151/*-------------------------------------------------------------.
152| Output in FGRAPH the current node specifications and exiting |
153| edges. |
154`-------------------------------------------------------------*/
155
ce4d5ce0 156static void
35fe0834 157print_state (state *s, FILE *fgraph)
ce4d5ce0 158{
600cad3b 159 struct obstack node_obstack;
ce4d5ce0 160
35fe0834 161 /* A node's label contains its items. */
600cad3b 162 obstack_init (&node_obstack);
16bb742f 163 print_core (&node_obstack, s);
08a946e0 164 obstack_1grow (&node_obstack, '\0');
35fe0834
PE
165 output_node (s->number, obstack_finish (&node_obstack), fgraph);
166 obstack_free (&node_obstack, 0);
342b8b6e 167
08a946e0 168 /* Output the edges. */
35fe0834 169 print_actions (s, fgraph);
ce4d5ce0
AD
170}
171\f
172
173void
174print_graph (void)
175{
16bb742f 176 state_number i;
35fe0834
PE
177 FILE *fgraph = xfopen (spec_graph_file, "w");
178 start_graph (fgraph);
ce4d5ce0 179
08a946e0 180 /* Output nodes and edges. */
9e7f6bbd 181 new_closure (nritems);
ce4d5ce0 182 for (i = 0; i < nstates; i++)
35fe0834 183 print_state (states[i], fgraph);
2e729273 184 free_closure ();
ce4d5ce0 185
35fe0834 186 finish_graph (fgraph);
342b8b6e 187 xfclose (fgraph);
ce4d5ce0 188}