]>
git.saurik.com Git - bison.git/blob - src/print_graph.c
1 /* Output a graph of the generated parser, for Bison.
3 Copyright (C) 2001-2007, 2009-2015 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26 #include "conflicts.h"
32 #include "print_graph.h"
38 /*----------------------------.
39 | Construct the node labels. |
40 `----------------------------*/
42 /* Print the lhs of a rule in such a manner that there is no vertical
43 repetition, like in *.output files. */
46 print_core (struct obstack
*oout
, state
*s
)
48 item_number
const *sitems
= s
->items
;
49 symbol
*previous_lhs
= NULL
;
51 size_t snritems
= s
->nitems
;
53 /* Output all the items of a state, not just its kernel. */
54 if (report_flag
& report_itemsets
)
56 closure (sitems
, snritems
);
61 obstack_printf (oout
, _("State %d"), s
->number
);
62 obstack_sgrow (oout
, "\\n\\l");
63 for (i
= 0; i
< snritems
; i
++)
65 item_number
const *sp1
= ritem
+ sitems
[i
];
66 item_number
const *sp
= sp1
;
72 r
= &rules
[item_number_as_rule_number (*sp
)];
74 obstack_printf (oout
, "%3d ", r
->number
);
75 if (previous_lhs
&& UNIQSTR_EQ (previous_lhs
->tag
, r
->lhs
->tag
))
76 obstack_printf (oout
, "%*s| ",
77 (int) strlen (previous_lhs
->tag
), "");
79 obstack_printf (oout
, "%s: ", escape (r
->lhs
->tag
));
80 previous_lhs
= r
->lhs
;
82 for (sp
= r
->rhs
; sp
< sp1
; sp
++)
83 obstack_printf (oout
, "%s ", escape (symbols
[*sp
]->tag
));
85 obstack_1grow (oout
, '.');
88 for (/* Nothing */; *sp
>= 0; ++sp
)
89 obstack_printf (oout
, " %s", escape (symbols
[*sp
]->tag
));
91 obstack_printf (oout
, " %%empty");
93 /* Experimental feature: display the lookahead tokens. */
94 if (report_flag
& report_lookahead_tokens
95 && item_number_is_rule_number (*sp1
))
97 /* Find the reduction we are handling. */
98 reductions
*reds
= s
->reductions
;
99 int redno
= state_reduction_find (s
, r
);
101 /* Print them if there are. */
102 if (reds
->lookahead_tokens
&& redno
!= -1)
104 bitset_iterator biter
;
106 char const *sep
= "";
107 obstack_sgrow (oout
, " [");
108 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[redno
], k
, 0)
110 obstack_sgrow (oout
, sep
);
111 obstack_sgrow (oout
, escape (symbols
[k
]->tag
));
114 obstack_1grow (oout
, ']');
117 obstack_sgrow (oout
, "\\l");
122 /*---------------------------------------------------------------.
123 | Output in graph_obstack edges specifications in incidence with |
125 `---------------------------------------------------------------*/
128 print_actions (state
const *s
, FILE *fgraph
)
130 transitions
const *trans
= s
->transitions
;
133 if (!trans
->num
&& !s
->reductions
)
136 for (i
= 0; i
< trans
->num
; i
++)
137 if (!TRANSITION_IS_DISABLED (trans
, i
))
139 state
*s1
= trans
->states
[i
];
140 symbol_number sym
= s1
->accessing_symbol
;
142 /* Shifts are solid, gotos are dashed, and error is dotted. */
144 (TRANSITION_IS_ERROR (trans
, i
) ? "dotted"
145 : TRANSITION_IS_SHIFT (trans
, i
) ? "solid"
148 if (TRANSITION_IS_ERROR (trans
, i
)
149 && STRNEQ (symbols
[sym
]->tag
, "error"))
151 output_edge (s
->number
, s1
->number
,
152 TRANSITION_IS_ERROR (trans
, i
) ? NULL
: symbols
[sym
]->tag
,
155 /* Display reductions. */
156 output_red (s
, s
->reductions
, fgraph
);
160 /*-------------------------------------------------------------.
161 | Output in FGRAPH the current node specifications and exiting |
163 `-------------------------------------------------------------*/
166 print_state (state
*s
, FILE *fgraph
)
168 struct obstack node_obstack
;
170 /* A node's label contains its items. */
171 obstack_init (&node_obstack
);
172 print_core (&node_obstack
, s
);
173 output_node (s
->number
, obstack_finish0 (&node_obstack
), fgraph
);
174 obstack_free (&node_obstack
, 0);
176 /* Output the edges. */
177 print_actions (s
, fgraph
);
185 FILE *fgraph
= xfopen (spec_graph_file
, "w");
186 start_graph (fgraph
);
188 /* Output nodes and edges. */
189 new_closure (nritems
);
190 for (i
= 0; i
< nstates
; i
++)
191 print_state (states
[i
], fgraph
);
194 finish_graph (fgraph
);