Don't let Bison leak memory except when it complains.
[bison.git] / src / main.c
bloba2056c82bac8854f3d4cc03e60a756b185742d92
1 /* Top level entry point of Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 1995, 2000, 2001, 2002, 2004,
4 2005, 2006 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include <config.h>
24 #include "system.h"
26 #include <bitset_stats.h>
27 #include <bitset.h>
28 #include <configmake.h>
29 #include <quotearg.h>
30 #include <timevar.h>
32 #include "LR0.h"
33 #include "complain.h"
34 #include "conflicts.h"
35 #include "derives.h"
36 #include "files.h"
37 #include "getargs.h"
38 #include "gram.h"
39 #include "lalr.h"
40 #include "muscle_tab.h"
41 #include "nullable.h"
42 #include "output.h"
43 #include "print.h"
44 #include "print_graph.h"
45 #include "reader.h"
46 #include "reduce.h"
47 #include "scan-code.h"
48 #include "scan-gram.h"
49 #include "symtab.h"
50 #include "tables.h"
51 #include "uniqstr.h"
53 /* The name this program was run with, for messages. */
54 char *program_name;
58 int
59 main (int argc, char *argv[])
61 program_name = argv[0];
62 setlocale (LC_ALL, "");
63 (void) bindtextdomain (PACKAGE, LOCALEDIR);
64 (void) bindtextdomain ("bison-runtime", LOCALEDIR);
65 (void) textdomain (PACKAGE);
67 uniqstrs_new ();
69 getargs (argc, argv);
71 timevar_report = trace_flag & trace_time;
72 init_timevar ();
73 timevar_start (TV_TOTAL);
75 if (trace_flag & trace_bitsets)
76 bitset_stats_enable ();
78 muscle_init ();
80 /* Read the input. Copy some parts of it to FGUARD, FACTION, FTABLE
81 and FATTRS. In file reader.c. The other parts are recorded in
82 the grammar; see gram.h. */
84 timevar_push (TV_READER);
85 reader ();
86 timevar_pop (TV_READER);
88 if (complaint_issued)
89 goto finish;
91 /* Find useless nonterminals and productions and reduce the grammar. */
92 timevar_push (TV_REDUCE);
93 reduce_grammar ();
94 timevar_pop (TV_REDUCE);
96 /* Record other info about the grammar. In files derives and
97 nullable. */
98 timevar_push (TV_SETS);
99 derives_compute ();
100 nullable_compute ();
101 timevar_pop (TV_SETS);
103 /* Convert to nondeterministic finite state machine. In file LR0.
104 See state.h for more info. */
105 timevar_push (TV_LR0);
106 generate_states ();
107 timevar_pop (TV_LR0);
109 /* make it deterministic. In file lalr. */
110 timevar_push (TV_LALR);
111 lalr ();
112 timevar_pop (TV_LALR);
114 /* Find and record any conflicts: places where one token of
115 lookahead is not enough to disambiguate the parsing. In file
116 conflicts. Also resolve s/r conflicts based on precedence
117 declarations. */
118 timevar_push (TV_CONFLICTS);
119 conflicts_solve ();
120 conflicts_print ();
121 timevar_pop (TV_CONFLICTS);
123 /* Compute the parser tables. */
124 timevar_push (TV_ACTIONS);
125 tables_generate ();
126 timevar_pop (TV_ACTIONS);
128 grammar_rules_never_reduced_report
129 (_("rule never reduced because of conflicts"));
131 /* Output file names. */
132 compute_output_file_names ();
134 /* Output the detailed report on the grammar. */
135 if (report_flag)
137 timevar_push (TV_REPORT);
138 print_results ();
139 timevar_pop (TV_REPORT);
142 /* Output the graph. */
143 if (graph_flag)
145 timevar_push (TV_GRAPH);
146 print_graph ();
147 timevar_pop (TV_GRAPH);
150 /* Stop if there were errors, to avoid trashing previous output
151 files. */
152 if (complaint_issued)
153 goto finish;
155 /* Lookahead tokens are no longer needed. */
156 timevar_push (TV_FREE);
157 lalr_free ();
158 timevar_pop (TV_FREE);
160 /* Output the tables and the parser to ftable. In file output. */
161 timevar_push (TV_PARSER);
162 output ();
163 timevar_pop (TV_PARSER);
165 timevar_push (TV_FREE);
166 nullable_free ();
167 derives_free ();
168 tables_free ();
169 states_free ();
170 reduce_free ();
171 conflicts_free ();
172 grammar_free ();
173 output_file_names_free ();
175 /* The scanner memory cannot be released right after parsing, as it
176 contains things such as user actions, prologue, epilogue etc. */
177 gram_scanner_free ();
178 muscle_free ();
179 uniqstrs_free ();
180 code_scanner_free ();
181 quotearg_free ();
182 timevar_pop (TV_FREE);
184 if (trace_flag & trace_bitsets)
185 bitset_stats_dump (stderr);
187 finish:
189 /* Stop timing and print the times. */
190 timevar_stop (TV_TOTAL);
191 timevar_print (stderr);
193 return complaint_issued ? EXIT_FAILURE : EXIT_SUCCESS;