]>
git.saurik.com Git - bison.git/blob - src/print_graph.c
2e3fd46035eee6349a0ccd4c896538dae3ef179c
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 `----------------------------*/
44 print_core (struct obstack
*oout
, state
*s
)
47 item_number
*sitems
= s
->items
;
48 size_t snritems
= s
->nitems
;
50 /* Output all the items of a state, not only its kernel. */
51 if (report_flag
& report_itemsets
)
53 closure (sitems
, snritems
);
58 obstack_printf (oout
, "state %d\\n", s
->number
);
59 for (i
= 0; i
< snritems
; i
++)
65 sp1
= sp
= ritem
+ sitems
[i
];
70 r
= item_number_as_rule_number (*sp
);
72 obstack_printf (oout
, "%d: %s -> ", r
, escape (rules
[r
].lhs
->tag
));
74 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
75 obstack_printf (oout
, "%s ", escape (symbols
[*sp
]->tag
));
77 obstack_1grow (oout
, '.');
79 for (/* Nothing */; *sp
>= 0; ++sp
)
80 obstack_printf (oout
, " %s", escape (symbols
[*sp
]->tag
));
82 /* Experimental feature: display the lookahead tokens. */
83 if (report_flag
& report_lookahead_tokens
84 && item_number_is_rule_number (*sp1
))
86 /* Find the reduction we are handling. */
87 reductions
*reds
= s
->reductions
;
88 int redno
= state_reduction_find (s
, &rules
[r
]);
90 /* Print them if there are. */
91 if (reds
->lookahead_tokens
&& redno
!= -1)
93 bitset_iterator biter
;
96 obstack_1grow (oout
, '[');
97 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[redno
], k
, 0)
99 obstack_sgrow (oout
, sep
);
100 obstack_sgrow (oout
, escape (symbols
[k
]->tag
));
103 obstack_1grow (oout
, ']');
106 obstack_sgrow (oout
, "\\l");
111 /*---------------------------------------------------------------.
112 | Output in graph_obstack edges specifications in incidence with |
114 `---------------------------------------------------------------*/
117 print_actions (state
const *s
, FILE *fgraph
)
121 transitions
const *trans
= s
->transitions
;
123 if (!trans
->num
&& !s
->reductions
)
126 for (i
= 0; i
< trans
->num
; i
++)
127 if (!TRANSITION_IS_DISABLED (trans
, i
))
129 state
*s1
= trans
->states
[i
];
130 symbol_number sym
= s1
->accessing_symbol
;
132 /* Shifts are solid, gotos are dashed, and error is dotted. */
134 (TRANSITION_IS_ERROR (trans
, i
) ? "dotted"
135 : TRANSITION_IS_SHIFT (trans
, i
) ? "solid"
138 if (TRANSITION_IS_ERROR (trans
, i
)
139 && strcmp (symbols
[sym
]->tag
, "error") != 0)
141 output_edge (s
->number
, s1
->number
,
142 TRANSITION_IS_ERROR (trans
, i
) ? NULL
: symbols
[sym
]->tag
,
148 /*-------------------------------------------------------------.
149 | Output in FGRAPH the current node specifications and exiting |
151 `-------------------------------------------------------------*/
154 print_state (state
*s
, FILE *fgraph
)
156 struct obstack node_obstack
;
158 /* A node's label contains its items. */
159 obstack_init (&node_obstack
);
160 print_core (&node_obstack
, s
);
161 obstack_1grow (&node_obstack
, '\0');
162 output_node (s
->number
, obstack_finish (&node_obstack
), fgraph
);
163 obstack_free (&node_obstack
, 0);
165 /* Output the edges. */
166 print_actions (s
, fgraph
);
174 FILE *fgraph
= xfopen (spec_graph_file
, "w");
175 start_graph (fgraph
);
177 /* Output nodes and edges. */
178 new_closure (nritems
);
179 for (i
= 0; i
< nstates
; i
++)
180 print_state (states
[i
], fgraph
);
183 finish_graph (fgraph
);