]>
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 AD |
41 | static inline const char * |
42 | escape (const char *s) | |
43 | { | |
44 | return quotearg_n_style (1, escape_quoting_style, s); | |
45 | } | |
46 | ||
47 | ||
ce4d5ce0 AD |
48 | /* This part will construct the label of nodes. */ |
49 | static void | |
d4e7d3a1 | 50 | print_core (struct obstack *oout, state_t *state) |
ce4d5ce0 AD |
51 | { |
52 | int i; | |
62a3e4f0 | 53 | item_number_t *sitems = state->items; |
5123689b | 54 | int snritems = state->nitems; |
22c2cbc0 | 55 | |
2e729273 | 56 | /* Output all the items of a state, not only its kernel. */ |
30171f79 AD |
57 | if (trace_flag) |
58 | { | |
5123689b | 59 | closure (sitems, snritems); |
30171f79 | 60 | sitems = itemset; |
5123689b | 61 | snritems = nritemset; |
30171f79 | 62 | } |
22c2cbc0 | 63 | |
d4e7d3a1 | 64 | obstack_fgrow1 (oout, "state %2d\n", state->number); |
5123689b | 65 | for (i = 0; i < snritems; i++) |
ce4d5ce0 | 66 | { |
62a3e4f0 AD |
67 | item_number_t *sp; |
68 | item_number_t *sp1; | |
4bc30f78 | 69 | int rule; |
c4b66126 | 70 | |
2e729273 | 71 | sp1 = sp = ritem + sitems[i]; |
ce4d5ce0 | 72 | |
75142d45 | 73 | while (*sp >= 0) |
ce4d5ce0 AD |
74 | sp++; |
75 | ||
76 | rule = -(*sp); | |
77 | ||
4bc30f78 | 78 | if (i) |
d4e7d3a1 AD |
79 | obstack_1grow (oout, '\n'); |
80 | obstack_fgrow1 (oout, " %s -> ", | |
bba97eb2 | 81 | escape (rules[rule].lhs->tag)); |
ce4d5ce0 | 82 | |
99013900 | 83 | for (sp = rules[rule].rhs; sp < sp1; sp++) |
d4e7d3a1 | 84 | obstack_fgrow1 (oout, "%s ", escape (symbols[*sp]->tag)); |
ce4d5ce0 | 85 | |
d4e7d3a1 | 86 | obstack_1grow (oout, '.'); |
22c2cbc0 | 87 | |
75142d45 | 88 | for (/* Nothing */; *sp >= 0; ++sp) |
d4e7d3a1 AD |
89 | obstack_fgrow1 (oout, " %s", escape (symbols[*sp]->tag)); |
90 | ||
91 | /* Experimental feature: display the lookaheads. */ | |
92 | if (trace_flag && state->nlookaheads) | |
93 | { | |
94 | int j, k; | |
95 | int nlookaheads = 0; | |
96 | /* Look for lookaheads corresponding to this rule. */ | |
97 | for (j = 0; j < state->nlookaheads; ++j) | |
98 | for (k = 0; k < ntokens; ++k) | |
99 | if (bitset_test (LA[state->lookaheadsp + j], k) | |
100 | && LArule[state->lookaheadsp + j]->number == rule) | |
101 | nlookaheads++; | |
102 | if (nlookaheads) | |
103 | { | |
104 | obstack_sgrow (oout, " ["); | |
105 | for (j = 0; j < state->nlookaheads; ++j) | |
106 | for (k = 0; k < ntokens; ++k) | |
107 | if (bitset_test (LA[state->lookaheadsp + j], k) | |
108 | && LArule[state->lookaheadsp + j]->number == rule) | |
109 | obstack_fgrow2 (oout, "%s%s", | |
110 | quotearg_style (escape_quoting_style, | |
111 | symbols[k]->tag), | |
112 | --nlookaheads ? ", " : ""); | |
113 | obstack_sgrow (oout, "]"); | |
114 | } | |
115 | } | |
ce4d5ce0 | 116 | } |
ce4d5ce0 AD |
117 | } |
118 | ||
08a946e0 AD |
119 | |
120 | /*---------------------------------------------------------------. | |
121 | | Output in graph_obstack edges specifications in incidence with | | |
122 | | current node. | | |
123 | `---------------------------------------------------------------*/ | |
124 | ||
ce4d5ce0 | 125 | static void |
065fbd27 | 126 | print_actions (state_t *state, const char *node_name) |
ce4d5ce0 AD |
127 | { |
128 | int i; | |
d954473d | 129 | |
065fbd27 AD |
130 | shifts *shiftp = state->shifts; |
131 | reductions *redp = state->reductions; | |
d954473d | 132 | |
ce4d5ce0 AD |
133 | static char buff[10]; |
134 | edge_t edge; | |
22c2cbc0 | 135 | |
d954473d | 136 | if (!shiftp->nshifts && !redp) |
5092aba5 | 137 | return; |
ce4d5ce0 | 138 | |
d954473d AD |
139 | for (i = 0; i < shiftp->nshifts; i++) |
140 | if (!SHIFT_IS_DISABLED (shiftp, i)) | |
141 | { | |
142 | int state1 = shiftp->shifts[i]; | |
5fbb0954 | 143 | token_number_t symbol = states[state1]->accessing_symbol; |
d954473d AD |
144 | |
145 | new_edge (&edge); | |
146 | ||
065fbd27 | 147 | if (state->number > state1) |
d954473d AD |
148 | edge.type = back_edge; |
149 | open_edge (&edge, fgraph); | |
150 | /* The edge source is the current node. */ | |
151 | edge.sourcename = node_name; | |
152 | sprintf (buff, "%d", state1); | |
153 | edge.targetname = buff; | |
8adfa272 AD |
154 | /* Shifts are blue, gotos are green, and error is red. */ |
155 | if (SHIFT_IS_ERROR (shiftp, i)) | |
156 | edge.color = red; | |
157 | else | |
158 | edge.color = SHIFT_IS_SHIFT(shiftp, i) ? blue : green; | |
ad949da9 | 159 | edge.label = escape (symbols[symbol]->tag); |
d954473d AD |
160 | output_edge (&edge, fgraph); |
161 | close_edge (fgraph); | |
162 | } | |
ce4d5ce0 AD |
163 | } |
164 | ||
08a946e0 AD |
165 | |
166 | /*-------------------------------------------------------------. | |
167 | | Output in FGRAPH the current node specifications and exiting | | |
168 | | edges. | | |
169 | `-------------------------------------------------------------*/ | |
170 | ||
ce4d5ce0 | 171 | static void |
065fbd27 | 172 | print_state (state_t *state) |
ce4d5ce0 AD |
173 | { |
174 | static char name[10]; | |
600cad3b | 175 | struct obstack node_obstack; |
22c2cbc0 | 176 | node_t node; |
ce4d5ce0 | 177 | |
08a946e0 | 178 | /* The labels of the nodes are their the items. */ |
600cad3b | 179 | obstack_init (&node_obstack); |
08a946e0 | 180 | new_node (&node); |
065fbd27 | 181 | sprintf (name, "%d", state->number); |
08a946e0 | 182 | node.title = name; |
d4e7d3a1 | 183 | print_core (&node_obstack, state); |
08a946e0 AD |
184 | obstack_1grow (&node_obstack, '\0'); |
185 | node.label = obstack_finish (&node_obstack); | |
342b8b6e AD |
186 | |
187 | open_node (fgraph); | |
342b8b6e | 188 | output_node (&node, fgraph); |
342b8b6e AD |
189 | close_node (fgraph); |
190 | ||
08a946e0 AD |
191 | /* Output the edges. */ |
192 | print_actions (state, name); | |
193 | ||
342b8b6e | 194 | obstack_free (&node_obstack, 0); |
ce4d5ce0 AD |
195 | } |
196 | \f | |
197 | ||
198 | void | |
199 | print_graph (void) | |
200 | { | |
602bbf31 | 201 | size_t i; |
9703cc49 | 202 | |
342b8b6e AD |
203 | /* Output file. */ |
204 | fgraph = xfopen (spec_graph_file, "w"); | |
205 | ||
ce4d5ce0 | 206 | new_graph (&graph); |
22c2cbc0 | 207 | |
600cad3b MA |
208 | #if 0 |
209 | graph.smanhattan_edges = yes; | |
9703cc49 | 210 | graph.manhattan_edges = yes; |
600cad3b | 211 | #endif |
22c2cbc0 | 212 | |
ce4d5ce0 | 213 | graph.display_edge_labels = yes; |
342b8b6e | 214 | graph.layoutalgorithm = normal; |
22c2cbc0 | 215 | |
ce4d5ce0 AD |
216 | graph.port_sharing = no; |
217 | graph.finetuning = yes; | |
218 | graph.straight_phase = yes; | |
219 | graph.priority_phase = yes; | |
220 | graph.splines = yes; | |
22c2cbc0 | 221 | |
ce4d5ce0 AD |
222 | graph.crossing_weight = median; |
223 | ||
224 | /* Output graph options. */ | |
342b8b6e AD |
225 | open_graph (fgraph); |
226 | output_graph (&graph, fgraph); | |
ce4d5ce0 | 227 | |
08a946e0 | 228 | /* Output nodes and edges. */ |
9e7f6bbd | 229 | new_closure (nritems); |
ce4d5ce0 | 230 | for (i = 0; i < nstates; i++) |
29e88316 | 231 | print_state (states[i]); |
2e729273 | 232 | free_closure (); |
ce4d5ce0 AD |
233 | |
234 | /* Close graph. */ | |
342b8b6e AD |
235 | close_graph (&graph, fgraph); |
236 | xfclose (fgraph); | |
ce4d5ce0 | 237 | } |