]>
git.saurik.com Git - bison.git/blob - src/print_graph.c
1 /* Output a VCG description on generated parser, for Bison,
2 Copyright 2001 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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. */
28 #include "conflicts.h"
35 #include "print_graph.h"
39 static FILE *fgraph
= NULL
;
42 /*----------------------------.
43 | Construct the node labels. |
44 `----------------------------*/
47 print_core (struct obstack
*oout
, state_t
*state
)
50 item_number_t
*sitems
= state
->items
;
51 int snritems
= state
->nitems
;
53 /* Output all the items of a state, not only its kernel. */
56 closure (sitems
, snritems
);
61 obstack_fgrow1 (oout
, "state %2d\n", state
->number
);
62 for (i
= 0; i
< snritems
; i
++)
68 sp1
= sp
= ritem
+ sitems
[i
];
76 obstack_1grow (oout
, '\n');
77 obstack_fgrow1 (oout
, " %s -> ",
78 symbol_tag_get (rules
[rule
].lhs
));
80 for (sp
= rules
[rule
].rhs
; sp
< sp1
; sp
++)
81 obstack_fgrow1 (oout
, "%s ", symbol_tag_get (symbols
[*sp
]));
83 obstack_1grow (oout
, '.');
85 for (/* Nothing */; *sp
>= 0; ++sp
)
86 obstack_fgrow1 (oout
, " %s", symbol_tag_get (symbols
[*sp
]));
88 /* Experimental feature: display the lookaheads. */
89 if (trace_flag
&& state
->nlookaheads
)
93 /* Look for lookaheads corresponding to this rule. */
94 for (j
= 0; j
< state
->nlookaheads
; ++j
)
95 for (k
= 0; k
< ntokens
; ++k
)
96 if (bitset_test (state
->lookaheads
[j
], k
)
97 && state
->lookaheads_rule
[j
]->number
== rule
)
101 obstack_sgrow (oout
, " [");
102 for (j
= 0; j
< state
->nlookaheads
; ++j
)
103 for (k
= 0; k
< ntokens
; ++k
)
104 if (bitset_test (state
->lookaheads
[j
], k
)
105 && state
->lookaheads_rule
[j
]->number
== rule
)
106 obstack_fgrow2 (oout
, "%s%s",
107 symbol_tag_get (symbols
[k
]),
108 --nlookaheads
? ", " : "");
109 obstack_sgrow (oout
, "]");
116 /*---------------------------------------------------------------.
117 | Output in graph_obstack edges specifications in incidence with |
119 `---------------------------------------------------------------*/
122 print_actions (state_t
*state
, const char *node_name
)
126 shifts
*shiftp
= state
->shifts
;
127 reductions
*redp
= state
->reductions
;
129 static char buff
[10];
132 if (!shiftp
->nshifts
&& !redp
)
135 for (i
= 0; i
< shiftp
->nshifts
; i
++)
136 if (!SHIFT_IS_DISABLED (shiftp
, i
))
138 state_number_t state1
= shiftp
->shifts
[i
];
139 symbol_number_t symbol
= states
[state1
]->accessing_symbol
;
143 if (state
->number
> state1
)
144 edge
.type
= back_edge
;
145 open_edge (&edge
, fgraph
);
146 /* The edge source is the current node. */
147 edge
.sourcename
= node_name
;
148 sprintf (buff
, "%d", state1
);
149 edge
.targetname
= buff
;
150 /* Shifts are blue, gotos are green, and error is red. */
151 if (SHIFT_IS_ERROR (shiftp
, i
))
154 edge
.color
= SHIFT_IS_SHIFT(shiftp
, i
) ? blue
: green
;
155 edge
.label
= symbol_tag_get (symbols
[symbol
]);
156 output_edge (&edge
, fgraph
);
162 /*-------------------------------------------------------------.
163 | Output in FGRAPH the current node specifications and exiting |
165 `-------------------------------------------------------------*/
168 print_state (state_t
*state
)
170 static char name
[10];
171 struct obstack node_obstack
;
174 /* The labels of the nodes are their the items. */
175 obstack_init (&node_obstack
);
177 sprintf (name
, "%d", state
->number
);
179 print_core (&node_obstack
, state
);
180 obstack_1grow (&node_obstack
, '\0');
181 node
.label
= obstack_finish (&node_obstack
);
184 output_node (&node
, fgraph
);
187 /* Output the edges. */
188 print_actions (state
, name
);
190 obstack_free (&node_obstack
, 0);
200 fgraph
= xfopen (spec_graph_file
, "w");
205 graph
.smanhattan_edges
= yes
;
206 graph
.manhattan_edges
= yes
;
209 graph
.display_edge_labels
= yes
;
210 graph
.layoutalgorithm
= normal
;
212 graph
.port_sharing
= no
;
213 graph
.finetuning
= yes
;
214 graph
.straight_phase
= yes
;
215 graph
.priority_phase
= yes
;
218 graph
.crossing_weight
= median
;
220 /* Output graph options. */
222 output_graph (&graph
, fgraph
);
224 /* Output nodes and edges. */
225 new_closure (nritems
);
226 for (i
= 0; i
< nstates
; i
++)
227 print_state (states
[i
]);
231 close_graph (&graph
, fgraph
);