]>
git.saurik.com Git - bison.git/blob - src/injections.c
1 /* Grammar reduction for Bison.
2 Copyright (C) 2002 Free Software Foundation, Inc.
4 This file is part of Bison, the GNU Compiler Compiler.
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 /* Reduce the grammar: Look for injections. Akim Demaille. */
26 #include "bitsetv-print.h"
31 #include "injections.h"
33 /* Set of all nonterminals which are not useless. */
34 static bitsetv injects
;
36 #define INJECTS(Var) injects[(Var) - ntokens]
38 /*-----------------------------------------------------------.
39 | Free the global sets used to compute the reduced grammar. |
40 `-----------------------------------------------------------*/
46 injects
= bitsetv_create (nvars
, nvars
, BITSET_FIXED
);
48 bitsetv_zero (injects
);
52 injections_free (void)
54 bitsetv_free (injects
);
59 /*------------------------------.
60 | Dump the list of injections. |
61 `------------------------------*/
64 injections_print (const char *title
)
68 fprintf (stderr
, "%s\n", title
);
69 for (i
= ntokens
; i
< nsyms
; i
++)
70 if (bitset_count (INJECTS (i
)))
72 fprintf (stderr
, "\t%s injects\n",
73 quotearg_style (escape_quoting_style
, symbols
[i
]->tag
));
74 for (j
= 0; j
< nvars
; j
++)
75 if (bitset_test (INJECTS (i
), j
))
76 fprintf (stderr
, "\t\t%s\n",
77 quotearg_style (escape_quoting_style
,
78 symbols
[j
+ ntokens
]->tag
));
80 fprintf (stderr
, "\n\n");
84 /*-----------------------------------------------------------------.
85 | Set INJECTS[i, j] if the nonterminal j derives the nonterminal j |
86 | (typically, `i -> j' is a rule, plus transitive closure). |
87 `-----------------------------------------------------------------*/
90 injections_compute (void)
95 for (i
= ntokens
; i
< nsyms
; i
++)
96 for (j
= 0; derives
[i
][j
] >= 0; ++j
)
98 int symbol
= rules
[derives
[i
][j
]].rhs
[0];
99 if (ISVAR (symbol
) && rules
[derives
[i
][j
]].rhs
[1] < 0)
100 bitset_set (INJECTS (i
), symbol
- ntokens
);
103 if (trace_flag
& trace_sets
)
104 injections_print ("syntactic direct injections");
105 bitsetv_transitive_closure (injects
);
106 if (trace_flag
& trace_sets
)
107 injections_print ("syntactic injections");
112 /*--------------------------------------------------------------.
113 | Look for infinitely ambiguous grammars: they contain cycles. |
114 `--------------------------------------------------------------*/
117 injections_check_cycles (void)
121 injections_compute ();
122 for (i
= ntokens
; i
< nsyms
; i
++)
123 if (bitset_test (INJECTS (i
), i
- ntokens
))
124 complain (_("infinitely ambiguous grammar: %s derives itself"),