]>
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-2012 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/>. */
27 #include "conflicts.h"
33 #include "print_graph.h"
39 /*----------------------------.
40 | Construct the node labels. |
41 `----------------------------*/
43 /* Print the lhs of a rule in such a manner that there is no vertical
44 repetition, like in *.output files. */
47 print_lhs (struct obstack
*oout
, rule
*previous_rule
, rule
*r
)
49 if (previous_rule
&& STREQ (previous_rule
->lhs
->tag
, r
->lhs
->tag
))
52 for (i
= 0; i
< strlen (r
->lhs
->tag
); ++i
)
53 obstack_1grow (oout
, ' ');
54 obstack_1grow (oout
, '|');
58 obstack_sgrow (oout
, escape (r
->lhs
->tag
));
59 obstack_1grow (oout
, ':');
61 obstack_1grow (oout
, ' ');
65 print_core (struct obstack
*oout
, state
*s
)
67 item_number
*sitems
= s
->items
;
68 rule
*previous_rule
= NULL
;
70 size_t snritems
= s
->nitems
;
72 /* Output all the items of a state, not only its kernel. */
73 if (report_flag
& report_itemsets
)
75 closure (sitems
, snritems
);
80 obstack_printf (oout
, _("State %d"), s
->number
);
81 obstack_sgrow (oout
, "\\n");
82 for (i
= 0; i
< snritems
; i
++)
88 sp1
= sp
= ritem
+ sitems
[i
];
93 r
= item_number_as_rule_number (*sp
);
95 obstack_printf (oout
, "%3d ", r
);
96 print_lhs (oout
, previous_rule
, &rules
[r
]);
97 previous_rule
= &rules
[r
];
99 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
100 obstack_printf (oout
, "%s ", escape (symbols
[*sp
]->tag
));
102 obstack_1grow (oout
, '.');
104 for (/* Nothing */; *sp
>= 0; ++sp
)
105 obstack_printf (oout
, " %s", escape (symbols
[*sp
]->tag
));
107 /* Experimental feature: display the lookahead tokens. */
108 if (report_flag
& report_lookahead_tokens
109 && item_number_is_rule_number (*sp1
))
111 /* Find the reduction we are handling. */
112 reductions
*reds
= s
->reductions
;
113 int redno
= state_reduction_find (s
, &rules
[r
]);
115 /* Print them if there are. */
116 if (reds
->lookahead_tokens
&& redno
!= -1)
118 bitset_iterator biter
;
120 char const *sep
= "";
121 obstack_sgrow (oout
, " [");
122 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[redno
], k
, 0)
124 obstack_sgrow (oout
, sep
);
125 obstack_sgrow (oout
, escape (symbols
[k
]->tag
));
128 obstack_1grow (oout
, ']');
131 obstack_sgrow (oout
, "\\l");
136 /*---------------------------------------------------------------.
137 | Output in graph_obstack edges specifications in incidence with |
139 `---------------------------------------------------------------*/
142 print_actions (state
const *s
, FILE *fgraph
)
144 transitions
const *trans
= s
->transitions
;
147 /* Display reductions. */
148 output_red (s
, s
->reductions
, fgraph
);
150 if (!trans
->num
&& !s
->reductions
)
153 for (i
= 0; i
< trans
->num
; i
++)
154 if (!TRANSITION_IS_DISABLED (trans
, i
))
156 state
*s1
= trans
->states
[i
];
157 symbol_number sym
= s1
->accessing_symbol
;
159 /* Shifts are solid, gotos are dashed, and error is dotted. */
161 (TRANSITION_IS_ERROR (trans
, i
) ? "dotted"
162 : TRANSITION_IS_SHIFT (trans
, i
) ? "solid"
165 if (TRANSITION_IS_ERROR (trans
, i
)
166 && STRNEQ (symbols
[sym
]->tag
, "error"))
168 output_edge (s
->number
, s1
->number
,
169 TRANSITION_IS_ERROR (trans
, i
) ? NULL
: symbols
[sym
]->tag
,
175 /*-------------------------------------------------------------.
176 | Output in FGRAPH the current node specifications and exiting |
178 `-------------------------------------------------------------*/
181 print_state (state
*s
, FILE *fgraph
)
183 struct obstack node_obstack
;
185 /* A node's label contains its items. */
186 obstack_init (&node_obstack
);
187 print_core (&node_obstack
, s
);
188 output_node (s
->number
, obstack_finish0 (&node_obstack
), fgraph
);
189 obstack_free (&node_obstack
, 0);
191 /* Output the edges. */
192 print_actions (s
, fgraph
);
200 FILE *fgraph
= xfopen (spec_graph_file
, "w");
201 start_graph (fgraph
);
203 /* Output nodes and edges. */
204 new_closure (nritems
);
205 for (i
= 0; i
< nstates
; i
++)
206 print_state (states
[i
], fgraph
);
209 finish_graph (fgraph
);