]>
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 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 Bison 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 2, or (at your option)
13 Bison 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 Bison; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
31 #include "conflicts.h"
37 #include "print_graph.h"
43 /*----------------------------.
44 | Construct the node labels. |
45 `----------------------------*/
48 print_core (struct obstack
*oout
, state
*s
)
51 item_number
*sitems
= s
->items
;
52 size_t snritems
= s
->nitems
;
54 /* Output all the items of a state, not only its kernel. */
55 if (report_flag
& report_itemsets
)
57 closure (sitems
, snritems
);
62 obstack_fgrow1 (oout
, "%d", s
->number
);
63 for (i
= 0; i
< snritems
; i
++)
69 sp1
= sp
= ritem
+ sitems
[i
];
74 r
= item_number_as_rule_number (*sp
);
76 obstack_fgrow1 (oout
, "\n%s -> ", rules
[r
].lhs
->tag
);
78 for (sp
= rules
[r
].rhs
; sp
< sp1
; sp
++)
79 obstack_fgrow1 (oout
, "%s ", symbols
[*sp
]->tag
);
81 obstack_1grow (oout
, '.');
83 for (/* Nothing */; *sp
>= 0; ++sp
)
84 obstack_fgrow1 (oout
, " %s", symbols
[*sp
]->tag
);
86 /* Experimental feature: display the lookahead tokens. */
87 if (report_flag
& report_lookahead_tokens
)
89 /* Find the reduction we are handling. */
90 reductions
*reds
= s
->reductions
;
91 int redno
= state_reduction_find (s
, &rules
[r
]);
93 /* Print them if there are. */
94 if (reds
->lookahead_tokens
&& redno
!= -1)
96 bitset_iterator biter
;
99 obstack_sgrow (oout
, "[");
100 BITSET_FOR_EACH (biter
, reds
->lookahead_tokens
[redno
], k
, 0)
102 obstack_fgrow2 (oout
, "%s%s", sep
, symbols
[k
]->tag
);
105 obstack_sgrow (oout
, "]");
112 /*---------------------------------------------------------------.
113 | Output in graph_obstack edges specifications in incidence with |
115 `---------------------------------------------------------------*/
118 print_actions (state
const *s
, FILE *fgraph
)
122 transitions
const *trans
= s
->transitions
;
124 if (!trans
->num
&& !s
->reductions
)
127 for (i
= 0; i
< trans
->num
; i
++)
128 if (!TRANSITION_IS_DISABLED (trans
, i
))
130 state
*s1
= trans
->states
[i
];
131 symbol_number sym
= s1
->accessing_symbol
;
133 /* Shifts are solid, gotos are dashed, and error is dotted. */
135 (TRANSITION_IS_ERROR (trans
, i
) ? "dotted"
136 : TRANSITION_IS_SHIFT (trans
, i
) ? "solid"
139 if (TRANSITION_IS_ERROR (trans
, i
)
140 && strcmp (symbols
[sym
]->tag
, "error") != 0)
142 output_edge (s
->number
, s1
->number
,
143 TRANSITION_IS_ERROR (trans
, i
) ? NULL
: symbols
[sym
]->tag
,
149 /*-------------------------------------------------------------.
150 | Output in FGRAPH the current node specifications and exiting |
152 `-------------------------------------------------------------*/
155 print_state (state
*s
, FILE *fgraph
)
157 struct obstack node_obstack
;
159 /* A node's label contains its items. */
160 obstack_init (&node_obstack
);
161 print_core (&node_obstack
, s
);
162 obstack_1grow (&node_obstack
, '\0');
163 output_node (s
->number
, obstack_finish (&node_obstack
), fgraph
);
164 obstack_free (&node_obstack
, 0);
166 /* Output the edges. */
167 print_actions (s
, fgraph
);
175 FILE *fgraph
= xfopen (spec_graph_file
, "w");
176 start_graph (fgraph
);
178 /* Output nodes and edges. */
179 new_closure (nritems
);
180 for (i
= 0; i
< nstates
; i
++)
181 print_state (states
[i
], fgraph
);
184 finish_graph (fgraph
);