]> git.saurik.com Git - bison.git/blob - src/main.c
(AT_TEST_CPP_GUARD_H): Compile with -I., as Forte 7 C requires it,
[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 #include "bitset_stats.h"
25 #include "bitset.h"
26 #include "getargs.h"
27 #include "struniq.h"
28 #include "symtab.h"
29 #include "gram.h"
30 #include "files.h"
31 #include "complain.h"
32 #include "derives.h"
33 #include "tables.h"
34 #include "output.h"
35 #include "reader.h"
36 #include "lalr.h"
37 #include "reduce.h"
38 #include "nullable.h"
39 #include "print.h"
40 #include "LR0.h"
41 #include "conflicts.h"
42 #include "print_graph.h"
43 #include "muscle_tab.h"
44
45 /* The name this program was run with, for messages. */
46 char *program_name;
47
48
49
50 int
51 main (int argc, char *argv[])
52 {
53 program_name = argv[0];
54 setlocale (LC_ALL, "");
55 (void) bindtextdomain (PACKAGE, LOCALEDIR);
56 (void) textdomain (PACKAGE);
57
58 struniqs_new ();
59
60 getargs (argc, argv);
61
62 time_report = trace_flag & trace_time;
63 init_timevar ();
64 timevar_start (TV_TOTAL);
65
66 if (trace_flag & trace_bitsets)
67 bitset_stats_enable ();
68
69 muscle_init ();
70
71 /* Read the input. Copy some parts of it to FGUARD, FACTION, FTABLE
72 and FATTRS. In file reader.c. The other parts are recorded in
73 the grammar; see gram.h. */
74
75 timevar_push (TV_READER);
76 reader ();
77 timevar_pop (TV_READER);
78
79 if (complaint_issued)
80 goto finish;
81
82 /* Find useless nonterminals and productions and reduce the grammar. */
83 timevar_push (TV_REDUCE);
84 reduce_grammar ();
85 timevar_pop (TV_REDUCE);
86
87 /* Record other info about the grammar. In files derives and
88 nullable. */
89 timevar_push (TV_SETS);
90 derives_compute ();
91 nullable_compute ();
92 timevar_pop (TV_SETS);
93
94 /* Convert to nondeterministic finite state machine. In file LR0.
95 See state.h for more info. */
96 timevar_push (TV_LR0);
97 generate_states ();
98 timevar_pop (TV_LR0);
99
100 /* make it deterministic. In file lalr. */
101 timevar_push (TV_LALR);
102 lalr ();
103 timevar_pop (TV_LALR);
104
105 /* Find and record any conflicts: places where one token of
106 lookahead is not enough to disambiguate the parsing. In file
107 conflicts. Also resolve s/r conflicts based on precedence
108 declarations. */
109 timevar_push (TV_CONFLICTS);
110 conflicts_solve ();
111 conflicts_print ();
112 timevar_pop (TV_CONFLICTS);
113
114 /* Compute the parser tables. */
115 timevar_push (TV_ACTIONS);
116 tables_generate ();
117 timevar_pop (TV_ACTIONS);
118
119 grammar_rules_never_reduced_report
120 (_("rule never reduced because of conflicts"));
121
122 /* Output file names. */
123 compute_output_file_names ();
124
125 /* Output the detailed report on the grammar. */
126 if (report_flag)
127 {
128 timevar_push (TV_REPORT);
129 print_results ();
130 timevar_pop (TV_REPORT);
131 }
132
133 /* Output the VCG graph. */
134 if (graph_flag)
135 {
136 timevar_push (TV_GRAPH);
137 print_graph ();
138 timevar_pop (TV_GRAPH);
139 }
140
141 /* Stop if there were errors, to avoid trashing previous output
142 files. */
143 if (complaint_issued)
144 goto finish;
145
146 /* Lookaheads are no longer needed. */
147 timevar_push (TV_FREE);
148 lalr_free ();
149 timevar_pop (TV_FREE);
150
151 /* Output the tables and the parser to ftable. In file output. */
152 timevar_push (TV_PARSER);
153 output ();
154 timevar_pop (TV_PARSER);
155
156 timevar_push (TV_FREE);
157 nullable_free ();
158 derives_free ();
159 tables_free ();
160 states_free ();
161 reduce_free ();
162 conflicts_free ();
163 grammar_free ();
164
165 /* The scanner memory cannot be released right after parsing, as it
166 contains things such as user actions, prologue, epilogue etc. */
167 scanner_free ();
168 muscle_free ();
169 struniqs_free ();
170 /* If using alloca.c, flush the alloca'ed memory for the benefit of
171 people running Bison as a library in IDEs. */
172 #if C_ALLOCA
173 alloca (0);
174 #endif
175 timevar_pop (TV_FREE);
176
177 if (trace_flag & trace_bitsets)
178 bitset_stats_dump (stderr);
179
180 finish:
181
182 /* Stop timing and print the times. */
183 timevar_stop (TV_TOTAL);
184 timevar_print (stderr);
185
186 return complaint_issued ? EXIT_FAILURE : EXIT_SUCCESS;
187 }