]> git.saurik.com Git - bison.git/blame - src/print_graph.c
* src/getargs.c (version): Update copyright year.
[bison.git] / src / print_graph.c
CommitLineData
35fe0834 1/* Output a graph of the generated parser, for Bison.
16bb742f 2
75ad86ee 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
35fe0834 4 Foundation, Inc.
ce4d5ce0
AD
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
f16b0819 8 This program is free software: you can redistribute it and/or modify
ce4d5ce0 9 it under the terms of the GNU General Public License as published by
f16b0819
PE
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
ce4d5ce0 12
f16b0819 13 This program is distributed in the hope that it will be useful,
ce4d5ce0
AD
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
f16b0819 19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
ce4d5ce0 20
2cec9080 21#include <config.h>
ce4d5ce0 22#include "system.h"
16bb742f 23
16bb742f
PE
24#include <quotearg.h>
25
ce4d5ce0 26#include "LR0.h"
16bb742f 27#include "closure.h"
ce4d5ce0 28#include "complain.h"
16bb742f
PE
29#include "conflicts.h"
30#include "files.h"
ce4d5ce0 31#include "getargs.h"
16bb742f 32#include "gram.h"
35fe0834 33#include "graphviz.h"
16bb742f 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 39
8adfa272 40
6b98e4b5
AD
41/*----------------------------.
42| Construct the node labels. |
43`----------------------------*/
8adfa272 44
ce4d5ce0 45static void
16bb742f 46print_core (struct obstack *oout, state *s)
ce4d5ce0 47{
f6fbd3da 48 size_t i;
16bb742f 49 item_number *sitems = s->items;
f6fbd3da 50 size_t snritems = s->nitems;
22c2cbc0 51
2e729273 52 /* Output all the items of a state, not only its kernel. */
05811fd7 53 if (report_flag & report_itemsets)
30171f79 54 {
5123689b 55 closure (sitems, snritems);
30171f79 56 sitems = itemset;
b09f4f48 57 snritems = nitemset;
30171f79 58 }
22c2cbc0 59
35fe0834 60 obstack_fgrow1 (oout, "%d", s->number);
5123689b 61 for (i = 0; i < snritems; i++)
ce4d5ce0 62 {
16bb742f
PE
63 item_number *sp;
64 item_number *sp1;
65 rule_number r;
c4b66126 66
2e729273 67 sp1 = sp = ritem + sitems[i];
ce4d5ce0 68
75142d45 69 while (*sp >= 0)
ce4d5ce0
AD
70 sp++;
71
16bb742f 72 r = item_number_as_rule_number (*sp);
ce4d5ce0 73
35fe0834 74 obstack_fgrow1 (oout, "\n%s -> ", rules[r].lhs->tag);
ce4d5ce0 75
16bb742f 76 for (sp = rules[r].rhs; sp < sp1; sp++)
97650f4e 77 obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag);
ce4d5ce0 78
d4e7d3a1 79 obstack_1grow (oout, '.');
22c2cbc0 80
75142d45 81 for (/* Nothing */; *sp >= 0; ++sp)
97650f4e 82 obstack_fgrow1 (oout, " %s", symbols[*sp]->tag);
d4e7d3a1 83
742e4900
JD
84 /* Experimental feature: display the lookahead tokens. */
85 if (report_flag & report_lookahead_tokens)
d4e7d3a1 86 {
cd08e51e 87 /* Find the reduction we are handling. */
16bb742f
PE
88 reductions *reds = s->reductions;
89 int redno = state_reduction_find (s, &rules[r]);
613f5e1a 90
cd08e51e 91 /* Print them if there are. */
742e4900 92 if (reds->lookahead_tokens && redno != -1)
d4e7d3a1 93 {
cd08e51e
AD
94 bitset_iterator biter;
95 int k;
d0829076 96 char const *sep = "";
cd08e51e 97 obstack_sgrow (oout, "[");
742e4900 98 BITSET_FOR_EACH (biter, reds->lookahead_tokens[redno], k, 0)
d0829076
PE
99 {
100 obstack_fgrow2 (oout, "%s%s", sep, symbols[k]->tag);
101 sep = ", ";
102 }
d4e7d3a1
AD
103 obstack_sgrow (oout, "]");
104 }
105 }
ce4d5ce0 106 }
ce4d5ce0
AD
107}
108
08a946e0
AD
109
110/*---------------------------------------------------------------.
111| Output in graph_obstack edges specifications in incidence with |
112| current node. |
113`---------------------------------------------------------------*/
114
ce4d5ce0 115static void
35fe0834 116print_actions (state const *s, FILE *fgraph)
ce4d5ce0
AD
117{
118 int i;
d954473d 119
35fe0834 120 transitions const *trans = s->transitions;
22c2cbc0 121
35fe0834 122 if (!trans->num && !s->reductions)
5092aba5 123 return;
ce4d5ce0 124
16bb742f
PE
125 for (i = 0; i < trans->num; i++)
126 if (!TRANSITION_IS_DISABLED (trans, i))
d954473d 127 {
16bb742f
PE
128 state *s1 = trans->states[i];
129 symbol_number sym = s1->accessing_symbol;
d954473d 130
35fe0834
PE
131 /* Shifts are solid, gotos are dashed, and error is dotted. */
132 char const *style =
133 (TRANSITION_IS_ERROR (trans, i) ? "dotted"
134 : TRANSITION_IS_SHIFT (trans, i) ? "solid"
135 : "dashed");
136
137 if (TRANSITION_IS_ERROR (trans, i)
138 && strcmp (symbols[sym]->tag, "error") != 0)
139 abort ();
140 output_edge (s->number, s1->number,
141 TRANSITION_IS_ERROR (trans, i) ? NULL : symbols[sym]->tag,
142 style, fgraph);
d954473d 143 }
ce4d5ce0
AD
144}
145
08a946e0
AD
146
147/*-------------------------------------------------------------.
148| Output in FGRAPH the current node specifications and exiting |
149| edges. |
150`-------------------------------------------------------------*/
151
ce4d5ce0 152static void
35fe0834 153print_state (state *s, FILE *fgraph)
ce4d5ce0 154{
600cad3b 155 struct obstack node_obstack;
ce4d5ce0 156
35fe0834 157 /* A node's label contains its items. */
600cad3b 158 obstack_init (&node_obstack);
16bb742f 159 print_core (&node_obstack, s);
08a946e0 160 obstack_1grow (&node_obstack, '\0');
35fe0834
PE
161 output_node (s->number, obstack_finish (&node_obstack), fgraph);
162 obstack_free (&node_obstack, 0);
342b8b6e 163
08a946e0 164 /* Output the edges. */
35fe0834 165 print_actions (s, fgraph);
ce4d5ce0
AD
166}
167\f
168
169void
170print_graph (void)
171{
16bb742f 172 state_number i;
35fe0834
PE
173 FILE *fgraph = xfopen (spec_graph_file, "w");
174 start_graph (fgraph);
ce4d5ce0 175
08a946e0 176 /* Output nodes and edges. */
9e7f6bbd 177 new_closure (nritems);
ce4d5ce0 178 for (i = 0; i < nstates; i++)
35fe0834 179 print_state (states[i], fgraph);
2e729273 180 free_closure ();
ce4d5ce0 181
35fe0834 182 finish_graph (fgraph);
342b8b6e 183 xfclose (fgraph);
ce4d5ce0 184}