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
main(int argc,char * argv[])64 main (int argc, char *argv[])
65 {
66 #define DEPENDS_ON_LIBINTL 1
67   set_program_name (argv[0]);
68   setlocale (LC_ALL, "");
69   {
70     char *cp = NULL;
71     char const *localedir = relocate2 (LOCALEDIR, &cp);
72     bindtextdomain ("bison", localedir);
73     bindtextdomain ("bison-gnulib", localedir);
74     bindtextdomain ("bison-runtime", localedir);
75     free (cp);
76   }
77   textdomain ("bison");
78 
79   {
80     char const *cp = getenv ("LC_CTYPE");
81     if (cp && STREQ (cp, "C"))
82       set_custom_quoting (&quote_quoting_options, "'", "'");
83     else
84       set_quoting_style (&quote_quoting_options, locale_quoting_style);
85   }
86 
87   atexit (close_stdout);
88 
89   glyphs_init ();
90   uniqstrs_new ();
91   muscle_init ();
92   complain_init ();
93 
94   getargs (argc, argv);
95 
96   timevar_enabled = trace_flag & trace_time;
97   timevar_init ();
98   timevar_start (tv_total);
99 
100   if (trace_flag & trace_bitsets)
101     bitset_stats_enable ();
102 
103   /* Read the input.  Copy some parts of it to FGUARD, FACTION, FTABLE
104      and FATTRS.  In file reader.c.  The other parts are recorded in
105      the grammar; see gram.h.  */
106 
107   timevar_push (tv_reader);
108   reader (grammar_file);
109   timevar_pop (tv_reader);
110 
111   if (complaint_status == status_complaint)
112     goto finish;
113 
114   /* Find useless nonterminals and productions and reduce the grammar. */
115   timevar_push (tv_reduce);
116   reduce_grammar ();
117   timevar_pop (tv_reduce);
118 
119   /* Record other info about the grammar.  In files derives and
120      nullable.  */
121   timevar_push (tv_sets);
122   derives_compute ();
123   nullable_compute ();
124   timevar_pop (tv_sets);
125 
126   /* Compute LR(0) parser states.  See state.h for more info.  */
127   timevar_push (tv_lr0);
128   generate_states ();
129   timevar_pop (tv_lr0);
130 
131   /* Add lookahead sets to parser states.  Except when LALR(1) is
132      requested, split states to eliminate LR(1)-relative
133      inadequacies.  */
134   ielr ();
135 
136   /* Find and record any conflicts: places where one token of
137      lookahead is not enough to disambiguate the parsing.  In file
138      conflicts.  Also resolve s/r conflicts based on precedence
139      declarations.  */
140   timevar_push (tv_conflicts);
141   conflicts_solve ();
142   if (!muscle_percent_define_flag_if ("lr.keep-unreachable-state"))
143     {
144       state_number *old_to_new = xnmalloc (nstates, sizeof *old_to_new);
145       state_number nstates_old = nstates;
146       state_remove_unreachable_states (old_to_new);
147       lalr_update_state_numbers (old_to_new, nstates_old);
148       conflicts_update_state_numbers (old_to_new, nstates_old);
149       free (old_to_new);
150     }
151   if (report_flag & report_cex
152       || warning_is_enabled (Wcounterexamples))
153     counterexample_init ();
154   conflicts_print ();
155   timevar_pop (tv_conflicts);
156 
157   /* Compute the parser tables.  */
158   timevar_push (tv_actions);
159   tables_generate ();
160   timevar_pop (tv_actions);
161 
162   grammar_rules_useless_report (_("rule useless in parser due to conflicts"));
163 
164   print_precedence_warnings ();
165 
166   /* Whether to generate output files.  */
167   bool generate = !(feature_flag & feature_syntax_only);
168 
169   if (generate)
170     {
171       /* Output file names. */
172       compute_output_file_names ();
173 
174       /* Output the detailed report on the grammar.  */
175       if (report_flag)
176         {
177           timevar_push (tv_report);
178           print_results ();
179           timevar_pop (tv_report);
180         }
181 
182       /* Output the graph.  */
183       if (graph_flag)
184         {
185           timevar_push (tv_graph);
186           print_graph ();
187           timevar_pop (tv_graph);
188         }
189 
190       /* Output xml.  */
191       if (xml_flag)
192         {
193           timevar_push (tv_xml);
194           print_xml ();
195           timevar_pop (tv_xml);
196         }
197     }
198 
199   /* Stop if there were errors, to avoid trashing previous output
200      files.  */
201   if (complaint_status == status_complaint)
202     goto finish;
203 
204   /* Lookahead tokens are no longer needed. */
205   timevar_push (tv_free);
206   lalr_free ();
207   timevar_pop (tv_free);
208 
209   /* Output the tables and the parser to ftable.  In file output.  */
210   if (generate)
211     {
212       timevar_push (tv_parser);
213       output ();
214       timevar_pop (tv_parser);
215     }
216 
217  finish:
218 
219   timevar_push (tv_free);
220   nullable_free ();
221   derives_free ();
222   tables_free ();
223   states_free ();
224   reduce_free ();
225   conflicts_free ();
226   grammar_free ();
227   counterexample_free ();
228   output_file_names_free ();
229 
230   /* The scanner and parser memory cannot be released right after
231      parsing, as it contains things such as user actions, prologue,
232      epilogue etc.  */
233   gram_scanner_free ();
234   parser_free ();
235 
236   muscle_free ();
237   code_scanner_free ();
238   skel_scanner_free ();
239   timevar_pop (tv_free);
240 
241   if (trace_flag & trace_bitsets)
242     bitset_stats_dump (stderr);
243 
244   /* Stop timing and print the times.  */
245   timevar_stop (tv_total);
246   timevar_print (stderr);
247 
248   /* Fix input file now, even if there are errors: that's less
249      warnings in the following runs.  */
250   if (!fixits_empty ())
251     {
252       if (update_flag)
253         fixits_run ();
254       else
255         complain (NULL, Wother,
256                   _("fix-its can be applied.  Rerun with option '--update'."));
257       fixits_free ();
258     }
259   uniqstrs_free ();
260 
261   complain_free ();
262   quotearg_free ();
263 
264   return complaint_status ? EXIT_FAILURE : EXIT_SUCCESS;
265 }
266