]> git.saurik.com Git - bison.git/blame - src/print_graph.c
* LR0.c (new_itemsets): Fix wording in comments: say item index rather
[bison.git] / src / print_graph.c
CommitLineData
35fe0834 1/* Output a graph of the generated parser, for Bison.
16bb742f 2
75ad86ee 3 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software
35fe0834 4 Foundation, Inc.
ce4d5ce0
AD
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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)
11 any later version.
12
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.
17
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
0fb669f9
PE
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
ce4d5ce0 22
2cec9080 23#include <config.h>
ce4d5ce0 24#include "system.h"
16bb742f 25
16bb742f
PE
26#include <quotearg.h>
27
ce4d5ce0 28#include "LR0.h"
16bb742f 29#include "closure.h"
ce4d5ce0 30#include "complain.h"
16bb742f
PE
31#include "conflicts.h"
32#include "files.h"
ce4d5ce0 33#include "getargs.h"
16bb742f 34#include "gram.h"
35fe0834 35#include "graphviz.h"
16bb742f 36#include "lalr.h"
ce4d5ce0 37#include "print_graph.h"
16bb742f
PE
38#include "reader.h"
39#include "state.h"
40#include "symtab.h"
ce4d5ce0 41
8adfa272 42
6b98e4b5
AD
43/*----------------------------.
44| Construct the node labels. |
45`----------------------------*/
8adfa272 46
ce4d5ce0 47static void
16bb742f 48print_core (struct obstack *oout, state *s)
ce4d5ce0 49{
f6fbd3da 50 size_t i;
16bb742f 51 item_number *sitems = s->items;
f6fbd3da 52 size_t snritems = s->nitems;
22c2cbc0 53
2e729273 54 /* Output all the items of a state, not only its kernel. */
05811fd7 55 if (report_flag & report_itemsets)
30171f79 56 {
5123689b 57 closure (sitems, snritems);
30171f79 58 sitems = itemset;
b09f4f48 59 snritems = nitemset;
30171f79 60 }
22c2cbc0 61
35fe0834 62 obstack_fgrow1 (oout, "%d", s->number);
5123689b 63 for (i = 0; i < snritems; i++)
ce4d5ce0 64 {
16bb742f
PE
65 item_number *sp;
66 item_number *sp1;
67 rule_number r;
c4b66126 68
2e729273 69 sp1 = sp = ritem + sitems[i];
ce4d5ce0 70
75142d45 71 while (*sp >= 0)
ce4d5ce0
AD
72 sp++;
73
16bb742f 74 r = item_number_as_rule_number (*sp);
ce4d5ce0 75
35fe0834 76 obstack_fgrow1 (oout, "\n%s -> ", rules[r].lhs->tag);
ce4d5ce0 77
16bb742f 78 for (sp = rules[r].rhs; sp < sp1; sp++)
97650f4e 79 obstack_fgrow1 (oout, "%s ", symbols[*sp]->tag);
ce4d5ce0 80
d4e7d3a1 81 obstack_1grow (oout, '.');
22c2cbc0 82
75142d45 83 for (/* Nothing */; *sp >= 0; ++sp)
97650f4e 84 obstack_fgrow1 (oout, " %s", symbols[*sp]->tag);
d4e7d3a1 85
742e4900
JD
86 /* Experimental feature: display the lookahead tokens. */
87 if (report_flag & report_lookahead_tokens)
d4e7d3a1 88 {
cd08e51e 89 /* Find the reduction we are handling. */
16bb742f
PE
90 reductions *reds = s->reductions;
91 int redno = state_reduction_find (s, &rules[r]);
613f5e1a 92
cd08e51e 93 /* Print them if there are. */
742e4900 94 if (reds->lookahead_tokens && redno != -1)
d4e7d3a1 95 {
cd08e51e
AD
96 bitset_iterator biter;
97 int k;
d0829076 98 char const *sep = "";
cd08e51e 99 obstack_sgrow (oout, "[");
742e4900 100 BITSET_FOR_EACH (biter, reds->lookahead_tokens[redno], k, 0)
d0829076
PE
101 {
102 obstack_fgrow2 (oout, "%s%s", sep, symbols[k]->tag);
103 sep = ", ";
104 }
d4e7d3a1
AD
105 obstack_sgrow (oout, "]");
106 }
107 }
ce4d5ce0 108 }
ce4d5ce0
AD
109}
110
08a946e0
AD
111
112/*---------------------------------------------------------------.
113| Output in graph_obstack edges specifications in incidence with |
114| current node. |
115`---------------------------------------------------------------*/
116
ce4d5ce0 117static void
35fe0834 118print_actions (state const *s, FILE *fgraph)
ce4d5ce0
AD
119{
120 int i;
d954473d 121
35fe0834 122 transitions const *trans = s->transitions;
22c2cbc0 123
35fe0834 124 if (!trans->num && !s->reductions)
5092aba5 125 return;
ce4d5ce0 126
16bb742f
PE
127 for (i = 0; i < trans->num; i++)
128 if (!TRANSITION_IS_DISABLED (trans, i))
d954473d 129 {
16bb742f
PE
130 state *s1 = trans->states[i];
131 symbol_number sym = s1->accessing_symbol;
d954473d 132
35fe0834
PE
133 /* Shifts are solid, gotos are dashed, and error is dotted. */
134 char const *style =
135 (TRANSITION_IS_ERROR (trans, i) ? "dotted"
136 : TRANSITION_IS_SHIFT (trans, i) ? "solid"
137 : "dashed");
138
139 if (TRANSITION_IS_ERROR (trans, i)
140 && strcmp (symbols[sym]->tag, "error") != 0)
141 abort ();
142 output_edge (s->number, s1->number,
143 TRANSITION_IS_ERROR (trans, i) ? NULL : symbols[sym]->tag,
144 style, fgraph);
d954473d 145 }
ce4d5ce0
AD
146}
147
08a946e0
AD
148
149/*-------------------------------------------------------------.
150| Output in FGRAPH the current node specifications and exiting |
151| edges. |
152`-------------------------------------------------------------*/
153
ce4d5ce0 154static void
35fe0834 155print_state (state *s, FILE *fgraph)
ce4d5ce0 156{
600cad3b 157 struct obstack node_obstack;
ce4d5ce0 158
35fe0834 159 /* A node's label contains its items. */
600cad3b 160 obstack_init (&node_obstack);
16bb742f 161 print_core (&node_obstack, s);
08a946e0 162 obstack_1grow (&node_obstack, '\0');
35fe0834
PE
163 output_node (s->number, obstack_finish (&node_obstack), fgraph);
164 obstack_free (&node_obstack, 0);
342b8b6e 165
08a946e0 166 /* Output the edges. */
35fe0834 167 print_actions (s, fgraph);
ce4d5ce0
AD
168}
169\f
170
171void
172print_graph (void)
173{
16bb742f 174 state_number i;
35fe0834
PE
175 FILE *fgraph = xfopen (spec_graph_file, "w");
176 start_graph (fgraph);
ce4d5ce0 177
08a946e0 178 /* Output nodes and edges. */
9e7f6bbd 179 new_closure (nritems);
ce4d5ce0 180 for (i = 0; i < nstates; i++)
35fe0834 181 print_state (states[i], fgraph);
2e729273 182 free_closure ();
ce4d5ce0 183
35fe0834 184 finish_graph (fgraph);
342b8b6e 185 xfclose (fgraph);
ce4d5ce0 186}