]>
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
;
41 static inline const char *
42 escape (const char *s
)
44 return quotearg_n_style (1, escape_quoting_style
, s
);
48 /* This part will construct the label of nodes. */
50 print_core (state_t
*state
, struct obstack
*node_obstack
)
53 short *sitems
= state
->items
;
54 int snitems
= state
->nitems
;
56 /* Output all the items of a state, not only its kernel. */
59 closure (sitems
, snitems
);
64 obstack_fgrow1 (node_obstack
, "state %2d\n", state
->number
);
65 for (i
= 0; i
< snitems
; i
++)
71 sp1
= sp
= ritem
+ sitems
[i
];
79 obstack_1grow (node_obstack
, '\n');
80 obstack_fgrow1 (node_obstack
, " %s -> ",
81 escape (symbols
[rules
[rule
].lhs
]->tag
));
83 for (sp
= ritem
+ rules
[rule
].rhs
; sp
< sp1
; sp
++)
84 obstack_fgrow1 (node_obstack
, "%s ", escape (symbols
[*sp
]->tag
));
86 obstack_1grow (node_obstack
, '.');
88 for (/* Nothing */; *sp
>= 0; ++sp
)
89 obstack_fgrow1 (node_obstack
, " %s", escape (symbols
[*sp
]->tag
));
94 /*---------------------------------------------------------------.
95 | Output in graph_obstack edges specifications in incidence with |
97 `---------------------------------------------------------------*/
100 print_actions (state_t
*state
, const char *node_name
)
104 shifts
*shiftp
= state
->shifts
;
105 reductions
*redp
= state
->reductions
;
107 static char buff
[10];
110 if (!shiftp
->nshifts
&& !redp
)
113 for (i
= 0; i
< shiftp
->nshifts
; i
++)
114 if (!SHIFT_IS_DISABLED (shiftp
, i
))
116 int state1
= shiftp
->shifts
[i
];
117 int symbol
= states
[state1
]->accessing_symbol
;
121 if (state
->number
> state1
)
122 edge
.type
= back_edge
;
123 open_edge (&edge
, fgraph
);
124 /* The edge source is the current node. */
125 edge
.sourcename
= node_name
;
126 sprintf (buff
, "%d", state1
);
127 edge
.targetname
= buff
;
128 /* Shifts are blue, gotos are green, and error is red. */
129 if (SHIFT_IS_ERROR (shiftp
, i
))
132 edge
.color
= SHIFT_IS_SHIFT(shiftp
, i
) ? blue
: green
;
133 edge
.label
= escape (symbols
[symbol
]->tag
);
134 output_edge (&edge
, fgraph
);
140 /*-------------------------------------------------------------.
141 | Output in FGRAPH the current node specifications and exiting |
143 `-------------------------------------------------------------*/
146 print_state (state_t
*state
)
148 static char name
[10];
149 struct obstack node_obstack
;
152 /* The labels of the nodes are their the items. */
153 obstack_init (&node_obstack
);
155 sprintf (name
, "%d", state
->number
);
157 print_core (state
, &node_obstack
);
158 obstack_1grow (&node_obstack
, '\0');
159 node
.label
= obstack_finish (&node_obstack
);
162 output_node (&node
, fgraph
);
165 /* Output the edges. */
166 print_actions (state
, name
);
168 obstack_free (&node_obstack
, 0);
178 fgraph
= xfopen (spec_graph_file
, "w");
183 graph
.smanhattan_edges
= yes
;
184 graph
.manhattan_edges
= yes
;
187 graph
.display_edge_labels
= yes
;
188 graph
.layoutalgorithm
= normal
;
190 graph
.port_sharing
= no
;
191 graph
.finetuning
= yes
;
192 graph
.straight_phase
= yes
;
193 graph
.priority_phase
= yes
;
196 graph
.crossing_weight
= median
;
198 /* Output graph options. */
200 output_graph (&graph
, fgraph
);
202 /* Output nodes and edges. */
203 new_closure (nritems
);
204 for (i
= 0; i
< nstates
; i
++)
205 print_state (states
[i
]);
209 close_graph (&graph
, fgraph
);