]>
Commit | Line | Data |
---|---|---|
54bd0db4 | 1 | /* Top level entry point of bison, |
a98ad01e | 2 | Copyright (C) 1984, 1986, 1989, 1992, 1995 Free Software Foundation, Inc. |
54bd0db4 RS |
3 | |
4 | This file is part of Bison, the GNU Compiler Compiler. | |
5 | ||
6 | Bison is free software; you can redistribute it and/or modify | |
7 | it under the terms of the GNU General Public License as published by | |
8 | the Free Software Foundation; either version 2, or (at your option) | |
9 | any later version. | |
10 | ||
11 | Bison is distributed in the hope that it will be useful, | |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | GNU General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with Bison; see the file COPYING. If not, write to | |
18 | the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | ||
20 | ||
21 | #include <stdio.h> | |
22 | #include "system.h" | |
a98ad01e | 23 | #include "machine.h" /* for MAXSHORT */ |
54bd0db4 RS |
24 | |
25 | extern int lineno; | |
26 | extern int verboseflag; | |
d2729d44 | 27 | extern char *infile; |
54bd0db4 RS |
28 | |
29 | /* Nonzero means failure has been detected; don't write a parser file. */ | |
30 | int failure; | |
31 | ||
a98ad01e | 32 | /* The name this program was run with, for messages. */ |
54bd0db4 RS |
33 | char *program_name; |
34 | ||
d2729d44 JT |
35 | char *printable_version PARAMS((int)); |
36 | char *int_to_string PARAMS((int)); | |
37 | void fatal PARAMS((char *)); | |
38 | void fatals PARAMS((char *, char *)); | |
39 | void warn PARAMS((char *)); | |
40 | void warni PARAMS((char *, int)); | |
41 | void warns PARAMS((char *, char *)); | |
42 | void warnss PARAMS((char *, char *, char *)); | |
43 | void warnsss PARAMS((char *, char *, char *, char *)); | |
44 | void toomany PARAMS((char *)); | |
45 | void berror PARAMS((char *)); | |
46 | ||
47 | extern void getargs PARAMS((int, char *[])); | |
48 | extern void openfiles PARAMS((void)); | |
49 | extern void reader PARAMS((void)); | |
50 | extern void reduce_grammar PARAMS((void)); | |
51 | extern void set_derives PARAMS((void)); | |
52 | extern void set_nullable PARAMS((void)); | |
53 | extern void generate_states PARAMS((void)); | |
54 | extern void lalr PARAMS((void)); | |
55 | extern void initialize_conflicts PARAMS((void)); | |
56 | extern void verbose PARAMS((void)); | |
57 | extern void terse PARAMS((void)); | |
58 | extern void output PARAMS((void)); | |
59 | extern void done PARAMS((int)); | |
54bd0db4 RS |
60 | |
61 | ||
62 | /* VMS complained about using `int'. */ | |
a98ad01e | 63 | |
54bd0db4 | 64 | int |
d2729d44 | 65 | main (int argc, char *argv[]) |
54bd0db4 RS |
66 | { |
67 | program_name = argv[0]; | |
29340571 RS |
68 | setlocale (LC_ALL, ""); |
69 | bindtextdomain (PACKAGE, LOCALEDIR); | |
70 | textdomain (PACKAGE); | |
71 | ||
54bd0db4 RS |
72 | failure = 0; |
73 | lineno = 0; | |
74 | getargs(argc, argv); | |
75 | openfiles(); | |
76 | ||
77 | /* read the input. Copy some parts of it to fguard, faction, ftable and fattrs. | |
78 | In file reader.c. | |
79 | The other parts are recorded in the grammar; see gram.h. */ | |
80 | reader(); | |
a98ad01e RS |
81 | if (failure) |
82 | done(failure); | |
54bd0db4 RS |
83 | |
84 | /* find useless nonterminals and productions and reduce the grammar. In | |
85 | file reduce.c */ | |
86 | reduce_grammar(); | |
87 | ||
88 | /* record other info about the grammar. In files derives and nullable. */ | |
89 | set_derives(); | |
90 | set_nullable(); | |
91 | ||
92 | /* convert to nondeterministic finite state machine. In file LR0. | |
93 | See state.h for more info. */ | |
94 | generate_states(); | |
95 | ||
96 | /* make it deterministic. In file lalr. */ | |
97 | lalr(); | |
98 | ||
99 | /* Find and record any conflicts: places where one token of lookahead is not | |
100 | enough to disambiguate the parsing. In file conflicts. | |
fe01694d | 101 | Also resolve s/r conflicts based on precedence declarations. */ |
54bd0db4 RS |
102 | initialize_conflicts(); |
103 | ||
104 | /* print information about results, if requested. In file print. */ | |
105 | if (verboseflag) | |
106 | verbose(); | |
107 | else | |
108 | terse(); | |
109 | ||
110 | /* output the tables and the parser to ftable. In file output. */ | |
111 | output(); | |
112 | done(failure); | |
42f95d63 | 113 | return failure; |
54bd0db4 | 114 | } |
a98ad01e | 115 | \f |
54bd0db4 RS |
116 | /* functions to report errors which prevent a parser from being generated */ |
117 | ||
a98ad01e RS |
118 | |
119 | /* Return a string containing a printable version of C: | |
120 | either C itself, or the corresponding \DDD code. */ | |
121 | ||
122 | char * | |
d2729d44 | 123 | printable_version (int c) |
a98ad01e RS |
124 | { |
125 | static char buf[10]; | |
126 | if (c < ' ' || c >= '\177') | |
127 | sprintf(buf, "\\%o", c); | |
128 | else | |
129 | { | |
130 | buf[0] = c; | |
131 | buf[1] = '\0'; | |
132 | } | |
133 | return buf; | |
134 | } | |
135 | ||
136 | /* Generate a string from the integer I. | |
137 | Return a ptr to internal memory containing the string. */ | |
138 | ||
139 | char * | |
d2729d44 | 140 | int_to_string (int i) |
a98ad01e RS |
141 | { |
142 | static char buf[20]; | |
143 | sprintf(buf, "%d", i); | |
144 | return buf; | |
145 | } | |
146 | ||
147 | /* Print the message S for a fatal error. */ | |
148 | ||
54bd0db4 | 149 | void |
d2729d44 | 150 | fatal (char *s) |
54bd0db4 | 151 | { |
54bd0db4 | 152 | if (infile == 0) |
319ce533 | 153 | fprintf(stderr, _("%s: fatal error: %s\n"), program_name, s); |
54bd0db4 | 154 | else |
3d05a5de | 155 | fprintf(stderr, _("%s:%d: fatal error: %s\n"), infile, lineno, s); |
54bd0db4 RS |
156 | done(1); |
157 | } | |
158 | ||
159 | ||
a98ad01e RS |
160 | /* Print a message for a fatal error. Use FMT to construct the message |
161 | and incorporate string X1. */ | |
54bd0db4 RS |
162 | |
163 | void | |
d2729d44 | 164 | fatals (char *fmt, char *x1) |
54bd0db4 RS |
165 | { |
166 | char buffer[200]; | |
a98ad01e | 167 | sprintf(buffer, fmt, x1); |
54bd0db4 RS |
168 | fatal(buffer); |
169 | } | |
170 | ||
a98ad01e | 171 | /* Print a warning message S. */ |
54bd0db4 RS |
172 | |
173 | void | |
d2729d44 | 174 | warn (char *s) |
a98ad01e | 175 | { |
a98ad01e | 176 | if (infile == 0) |
319ce533 | 177 | fprintf(stderr, _("%s: %s\n"), program_name, s); |
a98ad01e | 178 | else |
3d05a5de | 179 | fprintf(stderr, _("%s:%d: %s\n"), |
a98ad01e RS |
180 | infile, lineno, s); |
181 | ||
182 | failure = 1; | |
183 | } | |
184 | ||
185 | /* Print a warning message containing the string for the integer X1. | |
186 | The message is given by the format FMT. */ | |
187 | ||
188 | void | |
d2729d44 | 189 | warni (char *fmt, int x1) |
54bd0db4 RS |
190 | { |
191 | char buffer[200]; | |
a98ad01e RS |
192 | sprintf(buffer, fmt, x1); |
193 | warn(buffer); | |
194 | } | |
195 | ||
196 | /* Print a warning message containing the string X1. | |
197 | The message is given by the format FMT. */ | |
198 | ||
199 | void | |
d2729d44 | 200 | warns (char *fmt, char *x1) |
a98ad01e RS |
201 | { |
202 | char buffer[200]; | |
203 | sprintf(buffer, fmt, x1); | |
204 | warn(buffer); | |
205 | } | |
206 | ||
207 | /* Print a warning message containing the two strings X1 and X2. | |
208 | The message is given by the format FMT. */ | |
209 | ||
210 | void | |
d2729d44 | 211 | warnss (char *fmt, char *x1, char *x2) |
a98ad01e RS |
212 | { |
213 | char buffer[200]; | |
214 | sprintf(buffer, fmt, x1, x2); | |
215 | warn(buffer); | |
216 | } | |
217 | ||
218 | /* Print a warning message containing the 3 strings X1, X2, X3. | |
219 | The message is given by the format FMT. */ | |
220 | ||
221 | void | |
d2729d44 | 222 | warnsss (char *fmt, char *x1, char *x2, char *x3) |
a98ad01e RS |
223 | { |
224 | char buffer[200]; | |
225 | sprintf(buffer, fmt, x1, x2, x3); | |
226 | warn(buffer); | |
227 | } | |
228 | ||
229 | /* Print a message for the fatal occurence of more than MAXSHORT | |
230 | instances of whatever is denoted by the string S. */ | |
54bd0db4 | 231 | |
a98ad01e | 232 | void |
d2729d44 | 233 | toomany (char *s) |
a98ad01e RS |
234 | { |
235 | char buffer[200]; | |
3d05a5de | 236 | sprintf(buffer, _("too many %s (max %d)"), s, MAXSHORT); |
54bd0db4 RS |
237 | fatal(buffer); |
238 | } | |
239 | ||
a98ad01e | 240 | /* Abort for an internal error denoted by string S. */ |
54bd0db4 RS |
241 | |
242 | void | |
d2729d44 | 243 | berror (char *s) |
54bd0db4 | 244 | { |
319ce533 | 245 | fprintf(stderr, _("%s: internal error: %s\n"), program_name, s); |
54bd0db4 RS |
246 | abort(); |
247 | } |