]> git.saurik.com Git - bison.git/blame_incremental - src/print_graph.c
java: use api.location.type and api.position.type
[bison.git] / src / print_graph.c
... / ...
CommitLineData
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
43static void
44print_core (struct obstack *oout, state *s)
45{
46 size_t i;
47 item_number *sitems = s->items;
48 size_t snritems = s->nitems;
49
50 /* Output all the items of a state, not only its kernel. */
51 if (report_flag & report_itemsets)
52 {
53 closure (sitems, snritems);
54 sitems = itemset;
55 snritems = nitemset;
56 }
57
58 obstack_printf (oout, "state %d\\n", s->number);
59 for (i = 0; i < snritems; i++)
60 {
61 item_number *sp;
62 item_number *sp1;
63 rule_number r;
64
65 sp1 = sp = ritem + sitems[i];
66
67 while (*sp >= 0)
68 sp++;
69
70 r = item_number_as_rule_number (*sp);
71
72 obstack_printf (oout, "%d: %s -> ", r, escape (rules[r].lhs->tag));
73
74 for (sp = rules[r].rhs; sp < sp1; sp++)
75 obstack_printf (oout, "%s ", escape (symbols[*sp]->tag));
76
77 obstack_1grow (oout, '.');
78
79 for (/* Nothing */; *sp >= 0; ++sp)
80 obstack_printf (oout, " %s", escape (symbols[*sp]->tag));
81
82 /* Experimental feature: display the lookahead tokens. */
83 if (report_flag & report_lookahead_tokens
84 && item_number_is_rule_number (*sp1))
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");
107 }
108}
109
110
111/*---------------------------------------------------------------.
112| Output in graph_obstack edges specifications in incidence with |
113| current node. |
114`---------------------------------------------------------------*/
115
116static void
117print_actions (state const *s, FILE *fgraph)
118{
119 int i;
120
121 transitions const *trans = s->transitions;
122
123 /* Display reductions. */
124 output_red (s, s->reductions, fgraph);
125
126 if (!trans->num && !s->reductions)
127 return;
128
129 for (i = 0; i < trans->num; i++)
130 if (!TRANSITION_IS_DISABLED (trans, i))
131 {
132 state *s1 = trans->states[i];
133 symbol_number sym = s1->accessing_symbol;
134
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);
147 }
148}
149
150
151/*-------------------------------------------------------------.
152| Output in FGRAPH the current node specifications and exiting |
153| edges. |
154`-------------------------------------------------------------*/
155
156static void
157print_state (state *s, FILE *fgraph)
158{
159 struct obstack node_obstack;
160
161 /* A node's label contains its items. */
162 obstack_init (&node_obstack);
163 print_core (&node_obstack, s);
164 obstack_1grow (&node_obstack, '\0');
165 output_node (s->number, obstack_finish (&node_obstack), fgraph);
166 obstack_free (&node_obstack, 0);
167
168 /* Output the edges. */
169 print_actions (s, fgraph);
170}
171\f
172
173void
174print_graph (void)
175{
176 state_number i;
177 FILE *fgraph = xfopen (spec_graph_file, "w");
178 start_graph (fgraph);
179
180 /* Output nodes and edges. */
181 new_closure (nritems);
182 for (i = 0; i < nstates; i++)
183 print_state (states[i], fgraph);
184 free_closure ();
185
186 finish_graph (fgraph);
187 xfclose (fgraph);
188}