1 /* Top level entry point of Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 1995, 2000-2002, 2004-2015,
4 2018-2021 Free Software Foundation, Inc.
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 <https://www.gnu.org/licenses/>. */
25 #include <bitset/stats.h>
27 #include <configmake.h>
31 #include <relocatable.h> /* relocate2 */
35 #include "conflicts.h"
36 #include "counterexample.h"
46 #include "muscle-tab.h"
49 #include "parse-gram.h"
50 #include "print-graph.h"
51 #include "print-xml.h"
55 #include "scan-code.h"
56 #include "scan-gram.h"
57 #include "scan-skel.h"
64 main (int argc
, char *argv
[])
66 #define DEPENDS_ON_LIBINTL 1
67 set_program_name (argv
[0]);
68 setlocale (LC_ALL
, "");
71 char const *localedir
= relocate2 (LOCALEDIR
, &cp
);
72 bindtextdomain ("bison", localedir
);
73 bindtextdomain ("bison-gnulib", localedir
);
74 bindtextdomain ("bison-runtime", localedir
);
80 char const *cp
= getenv ("LC_CTYPE");
81 if (cp
&& STREQ (cp
, "C"))
82 set_custom_quoting ("e_quoting_options
, "'", "'");
84 set_quoting_style ("e_quoting_options
, locale_quoting_style
);
87 atexit (close_stdout
);
98 fprintf (stderr
, "bison (GNU Bison) %s\n", VERSION
);
100 timevar_enabled
= trace_flag
& trace_time
;
102 timevar_start (tv_total
);
104 if (trace_flag
& trace_bitsets
)
105 bitset_stats_enable ();
107 /* Read the input. Copy some parts of it to FGUARD, FACTION, FTABLE
108 and FATTRS. In file reader.c. The other parts are recorded in
109 the grammar; see gram.h. */
111 timevar_push (tv_reader
);
112 reader (grammar_file
);
113 timevar_pop (tv_reader
);
115 if (complaint_status
== status_complaint
)
118 /* Find useless nonterminals and productions and reduce the grammar. */
119 timevar_push (tv_reduce
);
121 timevar_pop (tv_reduce
);
123 /* Record other info about the grammar. In files derives and
125 timevar_push (tv_sets
);
128 timevar_pop (tv_sets
);
130 /* Compute LR(0) parser states. See state.h for more info. */
131 timevar_push (tv_lr0
);
133 timevar_pop (tv_lr0
);
135 /* Add lookahead sets to parser states. Except when LALR(1) is
136 requested, split states to eliminate LR(1)-relative
140 /* Find and record any conflicts: places where one token of
141 lookahead is not enough to disambiguate the parsing. In file
142 conflicts. Also resolve s/r conflicts based on precedence
144 timevar_push (tv_conflicts
);
146 if (!muscle_percent_define_flag_if ("lr.keep-unreachable-state"))
148 state_number
*old_to_new
= xnmalloc (nstates
, sizeof *old_to_new
);
149 state_number nstates_old
= nstates
;
150 state_remove_unreachable_states (old_to_new
);
151 lalr_update_state_numbers (old_to_new
, nstates_old
);
152 conflicts_update_state_numbers (old_to_new
, nstates_old
);
155 if (report_flag
& report_cex
156 || warning_is_enabled (Wcounterexamples
))
157 counterexample_init ();
159 timevar_pop (tv_conflicts
);
161 /* Compute the parser tables. */
162 timevar_push (tv_actions
);
164 timevar_pop (tv_actions
);
166 grammar_rules_useless_report (_("rule useless in parser due to conflicts"));
168 print_precedence_warnings ();
170 /* Whether to generate output files. */
171 bool generate
= !(feature_flag
& feature_syntax_only
);
175 /* Output file names. */
176 compute_output_file_names ();
178 /* Output the detailed report on the grammar. */
181 timevar_push (tv_report
);
183 timevar_pop (tv_report
);
186 /* Output the graph. */
189 timevar_push (tv_graph
);
191 timevar_pop (tv_graph
);
195 if (html_flag
|| xml_flag
)
197 timevar_push (tv_xml
);
199 timevar_pop (tv_xml
);
205 timevar_push (tv_html
);
207 timevar_pop (tv_html
);
211 /* Stop if there were errors, to avoid trashing previous output
213 if (complaint_status
== status_complaint
)
216 /* Lookahead tokens are no longer needed. */
217 timevar_push (tv_free
);
219 timevar_pop (tv_free
);
221 /* Output the tables and the parser to ftable. In file output. */
224 timevar_push (tv_parser
);
226 timevar_pop (tv_parser
);
231 timevar_push (tv_free
);
239 counterexample_free ();
240 output_file_names_free ();
242 /* The scanner and parser memory cannot be released right after
243 parsing, as it contains things such as user actions, prologue,
245 gram_scanner_free ();
249 code_scanner_free ();
250 skel_scanner_free ();
251 timevar_pop (tv_free
);
253 if (trace_flag
& trace_bitsets
)
254 bitset_stats_dump (stderr
);
256 /* Stop timing and print the times. */
257 timevar_stop (tv_total
);
258 timevar_print (stderr
);
260 /* Fix input file now, even if there are errors: that's less
261 warnings in the following runs. */
262 if (!fixits_empty ())
267 complain (NULL
, Wother
,
268 _("fix-its can be applied. Rerun with option '--update'."));
276 return complaint_status
? EXIT_FAILURE
: EXIT_SUCCESS
;