]>
Commit | Line | Data |
---|---|---|
1 | /* Output a graph of the generated parser, for Bison. | |
2 | ||
3 | Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software | |
4 | Foundation, Inc. | |
5 | ||
6 | This file is part of Bison, the GNU Compiler Compiler. | |
7 | ||
8 | Bison is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2, or (at your option) | |
11 | any later version. | |
12 | ||
13 | Bison is distributed in the hope that it will be useful, | |
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 | |
19 | along with Bison; see the file COPYING. If not, write to | |
20 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
21 | Boston, MA 02110-1301, USA. */ | |
22 | ||
23 | #include <config.h> | |
24 | #include "system.h" | |
25 | ||
26 | #include <quotearg.h> | |
27 | ||
28 | #include "LR0.h" | |
29 | #include "closure.h" | |
30 | #include "complain.h" | |
31 | #include "conflicts.h" | |
32 | #include "files.h" | |
33 | #include "getargs.h" | |
34 | #include "gram.h" | |
35 | #include "graphviz.h" | |
36 | #include "lalr.h" | |
37 | #include "print_graph.h" | |
38 | #include "reader.h" | |
39 | #include "state.h" | |
40 | #include "symtab.h" | |
41 | ||
42 | ||
43 | /*----------------------------. | |
44 | | Construct the node labels. | | |
45 | `----------------------------*/ | |
46 | ||
47 | static void | |
48 | print_core (struct obstack *oout, state *s) | |
49 | { | |
50 | size_t i; | |
51 | item_number *sitems = s->items; | |
52 | size_t snritems = s->nitems; | |
53 | ||
54 | /* Output all the items of a state, not only its kernel. */ | |
55 | if (report_flag & report_itemsets) | |
56 | { | |
57 | closure (sitems, snritems); | |
58 | sitems = itemset; | |
59 | snritems = nitemset; | |
60 | } | |
61 | ||
62 | obstack_fgrow1 (oout, "%d", s->number); | |
63 | for (i = 0; i < snritems; i++) | |
64 | { | |
65 | item_number *sp; | |
66 | item_number *sp1; | |
67 | rule_number r; | |
68 | ||
69 | sp1 = sp = ritem + sitems[i]; | |
70 | ||
71 | while (*sp >= 0) | |
72 | sp++; | |
73 | ||
74 | r = item_number_as_rule_number (*sp); | |
75 | ||
76 | obstack_fgrow1 (oout, "\n%s -> ", rules[r].lhs->tag); | |
77 | ||
78 | for (sp = rules[r].rhs; sp < sp1; sp++) | |
79 | obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag); | |
80 | ||
81 | obstack_1grow (oout, '.'); | |
82 | ||
83 | for (/* Nothing */; *sp >= 0; ++sp) | |
84 | obstack_fgrow1 (oout, " %s", symbols[*sp]->tag); | |
85 | ||
86 | /* Experimental feature: display the lookahead tokens. */ | |
87 | if (report_flag & report_lookahead_tokens) | |
88 | { | |
89 | /* Find the reduction we are handling. */ | |
90 | reductions *reds = s->reductions; | |
91 | int redno = state_reduction_find (s, &rules[r]); | |
92 | ||
93 | /* Print them if there are. */ | |
94 | if (reds->lookahead_tokens && redno != -1) | |
95 | { | |
96 | bitset_iterator biter; | |
97 | int k; | |
98 | char const *sep = ""; | |
99 | obstack_sgrow (oout, "["); | |
100 | BITSET_FOR_EACH (biter, reds->lookahead_tokens[redno], k, 0) | |
101 | { | |
102 | obstack_fgrow2 (oout, "%s%s", sep, symbols[k]->tag); | |
103 | sep = ", "; | |
104 | } | |
105 | obstack_sgrow (oout, "]"); | |
106 | } | |
107 | } | |
108 | } | |
109 | } | |
110 | ||
111 | ||
112 | /*---------------------------------------------------------------. | |
113 | | Output in graph_obstack edges specifications in incidence with | | |
114 | | current node. | | |
115 | `---------------------------------------------------------------*/ | |
116 | ||
117 | static void | |
118 | print_actions (state const *s, FILE *fgraph) | |
119 | { | |
120 | int i; | |
121 | ||
122 | transitions const *trans = s->transitions; | |
123 | ||
124 | if (!trans->num && !s->reductions) | |
125 | return; | |
126 | ||
127 | for (i = 0; i < trans->num; i++) | |
128 | if (!TRANSITION_IS_DISABLED (trans, i)) | |
129 | { | |
130 | state *s1 = trans->states[i]; | |
131 | symbol_number sym = s1->accessing_symbol; | |
132 | ||
133 | /* Shifts are solid, gotos are dashed, and error is dotted. */ | |
134 | char const *style = | |
135 | (TRANSITION_IS_ERROR (trans, i) ? "dotted" | |
136 | : TRANSITION_IS_SHIFT (trans, i) ? "solid" | |
137 | : "dashed"); | |
138 | ||
139 | if (TRANSITION_IS_ERROR (trans, i) | |
140 | && strcmp (symbols[sym]->tag, "error") != 0) | |
141 | abort (); | |
142 | output_edge (s->number, s1->number, | |
143 | TRANSITION_IS_ERROR (trans, i) ? NULL : symbols[sym]->tag, | |
144 | style, fgraph); | |
145 | } | |
146 | } | |
147 | ||
148 | ||
149 | /*-------------------------------------------------------------. | |
150 | | Output in FGRAPH the current node specifications and exiting | | |
151 | | edges. | | |
152 | `-------------------------------------------------------------*/ | |
153 | ||
154 | static void | |
155 | print_state (state *s, FILE *fgraph) | |
156 | { | |
157 | struct obstack node_obstack; | |
158 | ||
159 | /* A node's label contains its items. */ | |
160 | obstack_init (&node_obstack); | |
161 | print_core (&node_obstack, s); | |
162 | obstack_1grow (&node_obstack, '\0'); | |
163 | output_node (s->number, obstack_finish (&node_obstack), fgraph); | |
164 | obstack_free (&node_obstack, 0); | |
165 | ||
166 | /* Output the edges. */ | |
167 | print_actions (s, fgraph); | |
168 | } | |
169 | \f | |
170 | ||
171 | void | |
172 | print_graph (void) | |
173 | { | |
174 | state_number i; | |
175 | FILE *fgraph = xfopen (spec_graph_file, "w"); | |
176 | start_graph (fgraph); | |
177 | ||
178 | /* Output nodes and edges. */ | |
179 | new_closure (nritems); | |
180 | for (i = 0; i < nstates; i++) | |
181 | print_state (states[i], fgraph); | |
182 | free_closure (); | |
183 | ||
184 | finish_graph (fgraph); | |
185 | xfclose (fgraph); | |
186 | } |