]>
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, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
29 #include "conflicts.h"
35 #include "print_graph.h"
41 /*----------------------------.
42 | Construct the node labels. |
43 `----------------------------*/
46 print_core (struct obstack
*oout
, state
*s
)
49 item_number
*sitems
= s
->items
;
50 size_t snritems
= s
->nitems
;
52 /* Output all the items of a state, not only its kernel. */
53 if (report_flag
& report_itemsets
)
55 closure (sitems
, snritems
);
60 obstack_fgrow1 (oout
, "%d", s
->number
);
61 for (i
= 0; i
< snritems
; i
++)
67 sp1
= sp
= ritem
+ sitems
[i
];
72 r
= item_number_as_rule_number (*sp
);
74 obstack_fgrow1 (oout
, "\n%s -> ", rules
[r
].lhs
->tag
);
76 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
77 obstack_fgrow1 (oout
, "%s ", symbols
[*sp
]->tag
);
79 obstack_1grow (oout
, '.');
81 for (/* Nothing */; *sp
>= 0; ++sp
)
82 obstack_fgrow1 (oout
, " %s", symbols
[*sp
]->tag
);
84 /* Experimental feature: display the lookahead tokens. */
85 if (report_flag
& report_lookahead_tokens
)
87 /* Find the reduction we are handling. */
88 reductions
*reds
= s
->reductions
;
89 int redno
= state_reduction_find (s
, &rules
[r
]);
91 /* Print them if there are. */
92 if (reds
->lookahead_tokens
&& redno
!= -1)
94 bitset_iterator biter
;
97 obstack_sgrow (oout
, "[");
98 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[redno
], k
, 0)
100 obstack_fgrow2 (oout
, "%s%s", sep
, symbols
[k
]->tag
);
103 obstack_sgrow (oout
, "]");
110 /*---------------------------------------------------------------.
111 | Output in graph_obstack edges specifications in incidence with |
113 `---------------------------------------------------------------*/
116 print_actions (state
const *s
, FILE *fgraph
)
120 transitions
const *trans
= s
->transitions
;
122 if (!trans
->num
&& !s
->reductions
)
125 for (i
= 0; i
< trans
->num
; i
++)
126 if (!TRANSITION_IS_DISABLED (trans
, i
))
128 state
*s1
= trans
->states
[i
];
129 symbol_number sym
= s1
->accessing_symbol
;
131 /* Shifts are solid, gotos are dashed, and error is dotted. */
133 (TRANSITION_IS_ERROR (trans
, i
) ? "dotted"
134 : TRANSITION_IS_SHIFT (trans
, i
) ? "solid"
137 if (TRANSITION_IS_ERROR (trans
, i
)
138 && strcmp (symbols
[sym
]->tag
, "error") != 0)
140 output_edge (s
->number
, s1
->number
,
141 TRANSITION_IS_ERROR (trans
, i
) ? NULL
: symbols
[sym
]->tag
,
147 /*-------------------------------------------------------------.
148 | Output in FGRAPH the current node specifications and exiting |
150 `-------------------------------------------------------------*/
153 print_state (state
*s
, FILE *fgraph
)
155 struct obstack node_obstack
;
157 /* A node's label contains its items. */
158 obstack_init (&node_obstack
);
159 print_core (&node_obstack
, s
);
160 obstack_1grow (&node_obstack
, '\0');
161 output_node (s
->number
, obstack_finish (&node_obstack
), fgraph
);
162 obstack_free (&node_obstack
, 0);
164 /* Output the edges. */
165 print_actions (s
, fgraph
);
173 FILE *fgraph
= xfopen (spec_graph_file
, "w");
174 start_graph (fgraph
);
176 /* Output nodes and edges. */
177 new_closure (nritems
);
178 for (i
= 0; i
< nstates
; i
++)
179 print_state (states
[i
], fgraph
);
182 finish_graph (fgraph
);