1 /* Top level entry point of Bison.
2
3 Copyright (C) 1984, 1986, 1989, 1992, 1995, 2000-2002, 2004-2015,
4 2018-2021 Free Software Foundation, Inc.
5
6 This file is part of Bison, the GNU Compiler Compiler.
7
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.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <https://www.gnu.org/licenses/>. */
20
21 #include <config.h>
22 #include "system.h"
23
24 #include <bitset.h>
25 #include <bitset/stats.h>
26 #include <closeout.h>
27 #include <configmake.h>
28 #include <progname.h>
29 #include <quote.h>
30 #include <quotearg.h>
31 #include <relocatable.h> /* relocate2 */
32 #include <timevar.h>
33
34 #include "complain.h"
35 #include "conflicts.h"
36 #include "counterexample.h"
37 #include "derives.h"
38 #include "files.h"
39 #include "fixits.h"
40 #include "getargs.h"
41 #include "glyphs.h"
42 #include "gram.h"
43 #include "ielr.h"
44 #include "lalr.h"
45 #include "lr0.h"
46 #include "muscle-tab.h"
47 #include "nullable.h"
48 #include "output.h"
49 #include "parse-gram.h"
50 #include "print-graph.h"
51 #include "print-xml.h"
52 #include "print.h"
53 #include "reader.h"
54 #include "reduce.h"
55 #include "scan-code.h"
56 #include "scan-gram.h"
57 #include "scan-skel.h"
58 #include "symtab.h"
59 #include "tables.h"
60 #include "uniqstr.h"
61
62
63 int
64 main (int argc, char *argv[])
65 {
66 {
67 char *cp = getenv ("BISON_PROGRAM_NAME");
68 if (cp)
69 argv[0] = cp;
70 }
71
72 #define DEPENDS_ON_LIBINTL 1
73 set_program_name (argv[0]);
74 setlocale (LC_ALL, "");
75 {
76 char *cp = NULL;
77 char const *localedir = relocate2 (LOCALEDIR, &cp);
78 bindtextdomain ("bison", localedir);
79 bindtextdomain ("bison-gnulib", localedir);
80 bindtextdomain ("bison-runtime", localedir);
81 free (cp);
82 }
83 textdomain ("bison");
84
85 {
86 char const *cp = getenv ("LC_CTYPE");
87 if (cp && STREQ (cp, "C"))
88 set_custom_quoting ("e_quoting_options, "'", "'");
89 else
90 set_quoting_style ("e_quoting_options, locale_quoting_style);
91 }
92
93 atexit (close_stdout);
94
95 glyphs_init ();
96 uniqstrs_new ();
97 muscle_init ();
98 complain_init ();
99 code_scanner_init ();
100
101 getargs (argc, argv);
102
103 if (trace_flag)
104 fprintf (stderr, "bison (GNU Bison) %s\n", VERSION);
105
106 timevar_enabled = trace_flag & trace_time;
107 timevar_init ();
108 timevar_start (tv_total);
109
110 if (trace_flag & trace_bitsets)
111 bitset_stats_enable ();
112
113 /* Read the input. Copy some parts of it to FGUARD, FACTION, FTABLE
114 and FATTRS. In file reader.c. The other parts are recorded in
115 the grammar; see gram.h. */
116
117 timevar_push (tv_reader);
118 reader (grammar_file);
119 timevar_pop (tv_reader);
120
121 if (complaint_status == status_complaint)
122 goto finish;
123
124 /* Find useless nonterminals and productions and reduce the grammar. */
125 timevar_push (tv_reduce);
126 reduce_grammar ();
127 timevar_pop (tv_reduce);
128
129 /* Record other info about the grammar. In files derives and
130 nullable. */
131 timevar_push (tv_sets);
132 derives_compute ();
133 nullable_compute ();
134 timevar_pop (tv_sets);
135
136 /* Compute LR(0) parser states. See state.h for more info. */
137 timevar_push (tv_lr0);
138 generate_states ();
139 timevar_pop (tv_lr0);
140
141 /* Add lookahead sets to parser states. Except when LALR(1) is
142 requested, split states to eliminate LR(1)-relative
143 inadequacies. */
144 ielr ();
145
146 /* Find and record any conflicts: places where one token of
147 lookahead is not enough to disambiguate the parsing. In file
148 conflicts. Also resolve s/r conflicts based on precedence
149 declarations. */
150 timevar_push (tv_conflicts);
151 conflicts_solve ();
152 if (!muscle_percent_define_flag_if ("lr.keep-unreachable-state"))
153 {
154 state_number *old_to_new = xnmalloc (nstates, sizeof *old_to_new);
155 state_number nstates_old = nstates;
156 state_remove_unreachable_states (old_to_new);
157 lalr_update_state_numbers (old_to_new, nstates_old);
158 conflicts_update_state_numbers (old_to_new, nstates_old);
159 free (old_to_new);
160 }
161 if (report_flag & report_cex
162 || warning_is_enabled (Wcounterexamples))
163 counterexample_init ();
164 conflicts_print ();
165 timevar_pop (tv_conflicts);
166
167 /* Compute the parser tables. */
168 timevar_push (tv_actions);
169 tables_generate ();
170 timevar_pop (tv_actions);
171
172 grammar_rules_useless_report (_("rule useless in parser due to conflicts"));
173
174 print_precedence_warnings ();
175
176 /* Whether to generate output files. */
177 bool generate = !(feature_flag & feature_syntax_only);
178
179 if (generate)
180 {
181 /* Output file names. */
182 compute_output_file_names ();
183
184 /* Output the detailed report on the grammar. */
185 if (report_flag)
186 {
187 timevar_push (tv_report);
188 print_results ();
189 timevar_pop (tv_report);
190 }
191
192 /* Output the graph. */
193 if (graph_flag)
194 {
195 timevar_push (tv_graph);
196 print_graph ();
197 timevar_pop (tv_graph);
198 }
199
200 /* Output xml. */
201 if (html_flag || xml_flag)
202 {
203 timevar_push (tv_xml);
204 print_xml ();
205 timevar_pop (tv_xml);
206 }
207
208 /* Output html. */
209 if (html_flag)
210 {
211 timevar_push (tv_html);
212 print_html ();
213 timevar_pop (tv_html);
214 }
215 }
216
217 /* Stop if there were errors, to avoid trashing previous output
218 files. */
219 if (complaint_status == status_complaint)
220 goto finish;
221
222 /* Lookahead tokens are no longer needed. */
223 timevar_push (tv_free);
224 lalr_free ();
225 timevar_pop (tv_free);
226
227 /* Output the tables and the parser to ftable. In file output. */
228 if (generate)
229 {
230 timevar_push (tv_parser);
231 output ();
232 timevar_pop (tv_parser);
233 }
234
235 finish:
236
237 timevar_push (tv_free);
238 nullable_free ();
239 derives_free ();
240 tables_free ();
241 states_free ();
242 reduce_free ();
243 conflicts_free ();
244 grammar_free ();
245 counterexample_free ();
246 output_file_names_free ();
247
248 /* The scanner and parser memory cannot be released right after
249 parsing, as it contains things such as user actions, prologue,
250 epilogue etc. */
251 gram_scanner_free ();
252 parser_free ();
253
254 muscle_free ();
255 code_scanner_free ();
256 skel_scanner_free ();
257 timevar_pop (tv_free);
258
259 if (trace_flag & trace_bitsets)
260 bitset_stats_dump (stderr);
261
262 /* Stop timing and print the times. */
263 timevar_stop (tv_total);
264 timevar_print (stderr);
265
266 /* Fix input file now, even if there are errors: that's less
267 warnings in the following runs. */
268 if (!fixits_empty ())
269 {
270 if (update_flag)
271 fixits_run ();
272 else
273 complain (NULL, Wother,
274 _("fix-its can be applied. Rerun with option '--update'."));
275 fixits_free ();
276 }
277 uniqstrs_free ();
278
279 complain_free ();
280 quotearg_free ();
281
282 return complaint_status ? EXIT_FAILURE : EXIT_SUCCESS;
283 }