]> git.saurik.com Git - bison.git/blob - src/main.c
(set_goto_map): Do not subtract from the returned value of malloc.
[bison.git] / src / main.c
1 /* Top level entry point of bison,
2 Copyright (C) 1984, 1986, 1989, 1992, 1995, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
7 Bison is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 Bison is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 #include "system.h"
24
25 #include <bitset_stats.h>
26 #include <bitset.h>
27 #include <timevar.h>
28
29 #include "LR0.h"
30 #include "complain.h"
31 #include "conflicts.h"
32 #include "derives.h"
33 #include "files.h"
34 #include "getargs.h"
35 #include "gram.h"
36 #include "lalr.h"
37 #include "muscle_tab.h"
38 #include "nullable.h"
39 #include "output.h"
40 #include "print.h"
41 #include "print_graph.h"
42 #include "reader.h"
43 #include "reduce.h"
44 #include "symtab.h"
45 #include "tables.h"
46 #include "uniqstr.h"
47
48 /* The name this program was run with, for messages. */
49 char *program_name;
50
51
52
53 int
54 main (int argc, char *argv[])
55 {
56 program_name = argv[0];
57 setlocale (LC_ALL, "");
58 (void) bindtextdomain (PACKAGE, LOCALEDIR);
59 (void) textdomain (PACKAGE);
60
61 uniqstrs_new ();
62
63 getargs (argc, argv);
64
65 timevar_report = trace_flag & trace_time;
66 init_timevar ();
67 timevar_start (TV_TOTAL);
68
69 if (trace_flag & trace_bitsets)
70 bitset_stats_enable ();
71
72 muscle_init ();
73
74 /* Read the input. Copy some parts of it to FGUARD, FACTION, FTABLE
75 and FATTRS. In file reader.c. The other parts are recorded in
76 the grammar; see gram.h. */
77
78 timevar_push (TV_READER);
79 reader ();
80 timevar_pop (TV_READER);
81
82 if (complaint_issued)
83 goto finish;
84
85 /* Find useless nonterminals and productions and reduce the grammar. */
86 timevar_push (TV_REDUCE);
87 reduce_grammar ();
88 timevar_pop (TV_REDUCE);
89
90 /* Record other info about the grammar. In files derives and
91 nullable. */
92 timevar_push (TV_SETS);
93 derives_compute ();
94 nullable_compute ();
95 timevar_pop (TV_SETS);
96
97 /* Convert to nondeterministic finite state machine. In file LR0.
98 See state.h for more info. */
99 timevar_push (TV_LR0);
100 generate_states ();
101 timevar_pop (TV_LR0);
102
103 /* make it deterministic. In file lalr. */
104 timevar_push (TV_LALR);
105 lalr ();
106 timevar_pop (TV_LALR);
107
108 /* Find and record any conflicts: places where one token of
109 lookahead is not enough to disambiguate the parsing. In file
110 conflicts. Also resolve s/r conflicts based on precedence
111 declarations. */
112 timevar_push (TV_CONFLICTS);
113 conflicts_solve ();
114 conflicts_print ();
115 timevar_pop (TV_CONFLICTS);
116
117 /* Compute the parser tables. */
118 timevar_push (TV_ACTIONS);
119 tables_generate ();
120 timevar_pop (TV_ACTIONS);
121
122 grammar_rules_never_reduced_report
123 (_("rule never reduced because of conflicts"));
124
125 /* Output file names. */
126 compute_output_file_names ();
127
128 /* Output the detailed report on the grammar. */
129 if (report_flag)
130 {
131 timevar_push (TV_REPORT);
132 print_results ();
133 timevar_pop (TV_REPORT);
134 }
135
136 /* Output the VCG graph. */
137 if (graph_flag)
138 {
139 timevar_push (TV_GRAPH);
140 print_graph ();
141 timevar_pop (TV_GRAPH);
142 }
143
144 /* Stop if there were errors, to avoid trashing previous output
145 files. */
146 if (complaint_issued)
147 goto finish;
148
149 /* Lookaheads are no longer needed. */
150 timevar_push (TV_FREE);
151 lalr_free ();
152 timevar_pop (TV_FREE);
153
154 /* Output the tables and the parser to ftable. In file output. */
155 timevar_push (TV_PARSER);
156 output ();
157 timevar_pop (TV_PARSER);
158
159 timevar_push (TV_FREE);
160 nullable_free ();
161 derives_free ();
162 tables_free ();
163 states_free ();
164 reduce_free ();
165 conflicts_free ();
166 grammar_free ();
167
168 /* The scanner memory cannot be released right after parsing, as it
169 contains things such as user actions, prologue, epilogue etc. */
170 scanner_free ();
171 muscle_free ();
172 uniqstrs_free ();
173 /* If using alloca.c, flush the alloca'ed memory for the benefit of
174 people running Bison as a library in IDEs. */
175 #if C_ALLOCA
176 alloca (0);
177 #endif
178 timevar_pop (TV_FREE);
179
180 if (trace_flag & trace_bitsets)
181 bitset_stats_dump (stderr);
182
183 finish:
184
185 /* Stop timing and print the times. */
186 timevar_stop (TV_TOTAL);
187 timevar_print (stderr);
188
189 return complaint_issued ? EXIT_FAILURE : EXIT_SUCCESS;
190 }