Commit | Line | Data |
---|---|---|
22c2cbc0 AD |
1 | /* Output a VCG description on generated parser, for Bison, |
2 | Copyright 2001 Free Software Foundation, Inc. | |
ce4d5ce0 AD |
3 | |
4 | This file is part of Bison, the GNU Compiler Compiler. | |
5 | ||
6 | Bison is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | Bison is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with Bison; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | Boston, MA 02111-1307, USA. */ | |
20 | ||
21 | #include "system.h" | |
8adfa272 | 22 | #include "quotearg.h" |
ce4d5ce0 | 23 | #include "files.h" |
ad949da9 | 24 | #include "symtab.h" |
ce4d5ce0 AD |
25 | #include "gram.h" |
26 | #include "LR0.h" | |
27 | #include "lalr.h" | |
28 | #include "conflicts.h" | |
29 | #include "complain.h" | |
30 | #include "getargs.h" | |
31 | #include "state.h" | |
32 | #include "reader.h" | |
2e729273 | 33 | #include "closure.h" |
ce4d5ce0 AD |
34 | #include "obstack.h" |
35 | #include "print_graph.h" | |
36 | #include "vcg.h" | |
37 | ||
38 | static graph_t graph; | |
342b8b6e | 39 | static FILE *fgraph = NULL; |
ce4d5ce0 | 40 | |
8adfa272 | 41 | |
6b98e4b5 AD |
42 | /*----------------------------. |
43 | | Construct the node labels. | | |
44 | `----------------------------*/ | |
8adfa272 | 45 | |
ce4d5ce0 | 46 | static void |
d4e7d3a1 | 47 | print_core (struct obstack *oout, state_t *state) |
ce4d5ce0 AD |
48 | { |
49 | int i; | |
62a3e4f0 | 50 | item_number_t *sitems = state->items; |
5123689b | 51 | int snritems = state->nitems; |
22c2cbc0 | 52 | |
2e729273 | 53 | /* Output all the items of a state, not only its kernel. */ |
05811fd7 | 54 | if (report_flag & report_itemsets) |
30171f79 | 55 | { |
5123689b | 56 | closure (sitems, snritems); |
30171f79 | 57 | sitems = itemset; |
5123689b | 58 | snritems = nritemset; |
30171f79 | 59 | } |
22c2cbc0 | 60 | |
d4e7d3a1 | 61 | obstack_fgrow1 (oout, "state %2d\n", state->number); |
5123689b | 62 | for (i = 0; i < snritems; i++) |
ce4d5ce0 | 63 | { |
62a3e4f0 AD |
64 | item_number_t *sp; |
65 | item_number_t *sp1; | |
9222837b | 66 | rule_number_t rule; |
c4b66126 | 67 | |
2e729273 | 68 | sp1 = sp = ritem + sitems[i]; |
ce4d5ce0 | 69 | |
75142d45 | 70 | while (*sp >= 0) |
ce4d5ce0 AD |
71 | sp++; |
72 | ||
12b0043a | 73 | rule = item_number_as_rule_number (*sp); |
ce4d5ce0 | 74 | |
4bc30f78 | 75 | if (i) |
d4e7d3a1 AD |
76 | obstack_1grow (oout, '\n'); |
77 | obstack_fgrow1 (oout, " %s -> ", | |
97650f4e | 78 | rules[rule].lhs->tag); |
ce4d5ce0 | 79 | |
99013900 | 80 | for (sp = rules[rule].rhs; sp < sp1; sp++) |
97650f4e | 81 | obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag); |
ce4d5ce0 | 82 | |
d4e7d3a1 | 83 | obstack_1grow (oout, '.'); |
22c2cbc0 | 84 | |
75142d45 | 85 | for (/* Nothing */; *sp >= 0; ++sp) |
97650f4e | 86 | obstack_fgrow1 (oout, " %s", symbols[*sp]->tag); |
d4e7d3a1 AD |
87 | |
88 | /* Experimental feature: display the lookaheads. */ | |
cd08e51e | 89 | if (report_flag & report_lookaheads) |
d4e7d3a1 | 90 | { |
cd08e51e AD |
91 | /* Find the reduction we are handling. */ |
92 | reductions_t *reds = state->reductions; | |
93 | int redno = state_reduction_find (state, &rules[rule]); | |
613f5e1a | 94 | |
cd08e51e AD |
95 | /* Print them if there are. */ |
96 | if (reds->lookaheads && redno != -1) | |
d4e7d3a1 | 97 | { |
cd08e51e AD |
98 | bitset_iterator biter; |
99 | int k; | |
100 | int not_first = 0; | |
101 | obstack_sgrow (oout, "["); | |
102 | BITSET_FOR_EACH (biter, reds->lookaheads[redno], k, 0) | |
103 | obstack_fgrow2 (oout, "%s%s", | |
104 | not_first++ ? ", " : "", | |
105 | symbols[k]->tag); | |
d4e7d3a1 AD |
106 | obstack_sgrow (oout, "]"); |
107 | } | |
108 | } | |
ce4d5ce0 | 109 | } |
ce4d5ce0 AD |
110 | } |
111 | ||
08a946e0 AD |
112 | |
113 | /*---------------------------------------------------------------. | |
114 | | Output in graph_obstack edges specifications in incidence with | | |
115 | | current node. | | |
116 | `---------------------------------------------------------------*/ | |
117 | ||
ce4d5ce0 | 118 | static void |
065fbd27 | 119 | print_actions (state_t *state, const char *node_name) |
ce4d5ce0 AD |
120 | { |
121 | int i; | |
d954473d | 122 | |
8b752b00 | 123 | transitions_t *transitions = state->transitions; |
8a731ca8 | 124 | reductions_t *redp = state->reductions; |
d954473d | 125 | |
ce4d5ce0 AD |
126 | static char buff[10]; |
127 | edge_t edge; | |
22c2cbc0 | 128 | |
ccaf65bc | 129 | if (!transitions->num && !redp) |
5092aba5 | 130 | return; |
ce4d5ce0 | 131 | |
ccaf65bc AD |
132 | for (i = 0; i < transitions->num; i++) |
133 | if (!TRANSITION_IS_DISABLED (transitions, i)) | |
d954473d | 134 | { |
640748ee AD |
135 | state_t *state1 = transitions->states[i]; |
136 | symbol_number_t symbol = state1->accessing_symbol; | |
d954473d AD |
137 | |
138 | new_edge (&edge); | |
139 | ||
640748ee | 140 | if (state->number > state1->number) |
d954473d AD |
141 | edge.type = back_edge; |
142 | open_edge (&edge, fgraph); | |
143 | /* The edge source is the current node. */ | |
144 | edge.sourcename = node_name; | |
640748ee | 145 | sprintf (buff, "%d", state1->number); |
d954473d | 146 | edge.targetname = buff; |
8adfa272 | 147 | /* Shifts are blue, gotos are green, and error is red. */ |
ccaf65bc | 148 | if (TRANSITION_IS_ERROR (transitions, i)) |
8adfa272 AD |
149 | edge.color = red; |
150 | else | |
ccaf65bc | 151 | edge.color = TRANSITION_IS_SHIFT(transitions, i) ? blue : green; |
97650f4e | 152 | edge.label = symbols[symbol]->tag; |
d954473d AD |
153 | output_edge (&edge, fgraph); |
154 | close_edge (fgraph); | |
155 | } | |
ce4d5ce0 AD |
156 | } |
157 | ||
08a946e0 AD |
158 | |
159 | /*-------------------------------------------------------------. | |
160 | | Output in FGRAPH the current node specifications and exiting | | |
161 | | edges. | | |
162 | `-------------------------------------------------------------*/ | |
163 | ||
ce4d5ce0 | 164 | static void |
065fbd27 | 165 | print_state (state_t *state) |
ce4d5ce0 AD |
166 | { |
167 | static char name[10]; | |
600cad3b | 168 | struct obstack node_obstack; |
22c2cbc0 | 169 | node_t node; |
ce4d5ce0 | 170 | |
08a946e0 | 171 | /* The labels of the nodes are their the items. */ |
600cad3b | 172 | obstack_init (&node_obstack); |
08a946e0 | 173 | new_node (&node); |
065fbd27 | 174 | sprintf (name, "%d", state->number); |
08a946e0 | 175 | node.title = name; |
d4e7d3a1 | 176 | print_core (&node_obstack, state); |
08a946e0 AD |
177 | obstack_1grow (&node_obstack, '\0'); |
178 | node.label = obstack_finish (&node_obstack); | |
342b8b6e AD |
179 | |
180 | open_node (fgraph); | |
342b8b6e | 181 | output_node (&node, fgraph); |
342b8b6e AD |
182 | close_node (fgraph); |
183 | ||
08a946e0 AD |
184 | /* Output the edges. */ |
185 | print_actions (state, name); | |
186 | ||
342b8b6e | 187 | obstack_free (&node_obstack, 0); |
ce4d5ce0 AD |
188 | } |
189 | \f | |
190 | ||
191 | void | |
192 | print_graph (void) | |
193 | { | |
d57650a5 | 194 | state_number_t i; |
9703cc49 | 195 | |
342b8b6e AD |
196 | /* Output file. */ |
197 | fgraph = xfopen (spec_graph_file, "w"); | |
198 | ||
ce4d5ce0 | 199 | new_graph (&graph); |
22c2cbc0 | 200 | |
600cad3b MA |
201 | #if 0 |
202 | graph.smanhattan_edges = yes; | |
9703cc49 | 203 | graph.manhattan_edges = yes; |
600cad3b | 204 | #endif |
22c2cbc0 | 205 | |
ce4d5ce0 | 206 | graph.display_edge_labels = yes; |
342b8b6e | 207 | graph.layoutalgorithm = normal; |
22c2cbc0 | 208 | |
ce4d5ce0 AD |
209 | graph.port_sharing = no; |
210 | graph.finetuning = yes; | |
211 | graph.straight_phase = yes; | |
212 | graph.priority_phase = yes; | |
213 | graph.splines = yes; | |
22c2cbc0 | 214 | |
ce4d5ce0 AD |
215 | graph.crossing_weight = median; |
216 | ||
217 | /* Output graph options. */ | |
342b8b6e AD |
218 | open_graph (fgraph); |
219 | output_graph (&graph, fgraph); | |
ce4d5ce0 | 220 | |
08a946e0 | 221 | /* Output nodes and edges. */ |
9e7f6bbd | 222 | new_closure (nritems); |
ce4d5ce0 | 223 | for (i = 0; i < nstates; i++) |
29e88316 | 224 | print_state (states[i]); |
2e729273 | 225 | free_closure (); |
ce4d5ce0 AD |
226 | |
227 | /* Close graph. */ | |
342b8b6e AD |
228 | close_graph (&graph, fgraph); |
229 | xfclose (fgraph); | |
ce4d5ce0 | 230 | } |