]>
Commit | Line | Data |
---|---|---|
1 | /* Parse command line arguments for Bison. | |
2 | ||
3 | Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002 | |
4 | Free Software Foundation, Inc. | |
5 | ||
6 | This file is part of Bison, the GNU Compiler Compiler. | |
7 | ||
8 | Bison is free software; you can redistribute it and/or modify it | |
9 | 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. | |
12 | ||
13 | Bison is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
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 the Free | |
20 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA | |
21 | 02111-1307, USA. */ | |
22 | ||
23 | #include "system.h" | |
24 | ||
25 | #include <argmatch.h> | |
26 | #include <error.h> | |
27 | #include <getopt.h> | |
28 | ||
29 | #include "complain.h" | |
30 | #include "files.h" | |
31 | #include "getargs.h" | |
32 | #include "uniqstr.h" | |
33 | ||
34 | int debug_flag = 0; | |
35 | int defines_flag = 0; | |
36 | int locations_flag = 0; | |
37 | int no_lines_flag = 0; | |
38 | int no_parser_flag = 0; | |
39 | int report_flag = report_none; | |
40 | int token_table_flag = 0; | |
41 | int yacc_flag = 0; /* for -y */ | |
42 | int graph_flag = 0; | |
43 | int trace_flag = trace_none; | |
44 | ||
45 | const char *skeleton = NULL; | |
46 | const char *include = NULL; | |
47 | ||
48 | extern char *program_name; | |
49 | ||
50 | ||
51 | /*---------------------. | |
52 | | --trace's handling. | | |
53 | `---------------------*/ | |
54 | ||
55 | static const char * const trace_args[] = | |
56 | { | |
57 | /* In a series of synonyms, present the most meaningful first, so | |
58 | that argmatch_valid be more readable. */ | |
59 | "none - no report", | |
60 | "scan - grammar scanner traces", | |
61 | "parse - grammar parser traces", | |
62 | "automaton - contruction of the automaton", | |
63 | "bitsets - use of bitsets", | |
64 | "grammar - reading, reducing of the grammar", | |
65 | "resource - memory consumption (where available)", | |
66 | "sets - grammar sets: firsts, nullable etc.", | |
67 | "tools - m4 invocation and preserve the temporary file", | |
68 | "skeleton - skeleton postprocessing", | |
69 | "time - time consumption", | |
70 | "all - all of the above", | |
71 | 0 | |
72 | }; | |
73 | ||
74 | static const int trace_types[] = | |
75 | { | |
76 | trace_none, | |
77 | trace_scan, | |
78 | trace_parse, | |
79 | trace_automaton, | |
80 | trace_bitsets, | |
81 | trace_grammar, | |
82 | trace_resource, | |
83 | trace_sets, | |
84 | trace_tools, | |
85 | trace_skeleton, | |
86 | trace_time, | |
87 | trace_all | |
88 | }; | |
89 | ||
90 | ||
91 | static void | |
92 | trace_argmatch (char *args) | |
93 | { | |
94 | verify (trace_constraint, ARGMATCH_CONSTRAINT (trace_args, trace_types)); | |
95 | if (args) | |
96 | { | |
97 | args = strtok (args, ","); | |
98 | do | |
99 | { | |
100 | int trace = XARGMATCH ("--trace", args, | |
101 | trace_args, trace_types); | |
102 | if (trace == trace_none) | |
103 | trace_flag = trace_none; | |
104 | else | |
105 | trace_flag |= trace; | |
106 | } | |
107 | while ((args = strtok (NULL, ","))); | |
108 | } | |
109 | else | |
110 | trace_flag = trace_all; | |
111 | } | |
112 | ||
113 | ||
114 | /*----------------------. | |
115 | | --report's handling. | | |
116 | `----------------------*/ | |
117 | ||
118 | static const char * const report_args[] = | |
119 | { | |
120 | /* In a series of synonyms, present the most meaningful first, so | |
121 | that argmatch_valid be more readable. */ | |
122 | "none", | |
123 | "state", "states", | |
124 | "itemset", "itemsets", | |
125 | "lookahead", "lookaheads", | |
126 | "solved", | |
127 | "all", | |
128 | 0 | |
129 | }; | |
130 | ||
131 | static const int report_types[] = | |
132 | { | |
133 | report_none, | |
134 | report_states, report_states, | |
135 | report_states | report_itemsets, report_states | report_itemsets, | |
136 | report_states | report_lookaheads, report_states | report_lookaheads, | |
137 | report_states | report_solved_conflicts, | |
138 | report_all | |
139 | }; | |
140 | ||
141 | ||
142 | static void | |
143 | report_argmatch (char *args) | |
144 | { | |
145 | verify (report_constraint, ARGMATCH_CONSTRAINT (report_args, report_types)); | |
146 | args = strtok (args, ","); | |
147 | do | |
148 | { | |
149 | int report = XARGMATCH ("--report", args, | |
150 | report_args, report_types); | |
151 | if (report == report_none) | |
152 | report_flag = report_none; | |
153 | else | |
154 | report_flag |= report; | |
155 | } | |
156 | while ((args = strtok (NULL, ","))); | |
157 | } | |
158 | ||
159 | ||
160 | /*-------------------------------------------. | |
161 | | Display the help message and exit STATUS. | | |
162 | `-------------------------------------------*/ | |
163 | ||
164 | static void usage (int) ATTRIBUTE_NORETURN; | |
165 | ||
166 | static void | |
167 | usage (int status) | |
168 | { | |
169 | if (status != 0) | |
170 | fprintf (stderr, _("Try `%s --help' for more information.\n"), | |
171 | program_name); | |
172 | else | |
173 | { | |
174 | /* Some efforts were made to ease the translators' task, please | |
175 | continue. */ | |
176 | fputs (_("\ | |
177 | GNU bison generates parsers for LALR(1) grammars.\n"), stdout); | |
178 | putc ('\n', stdout); | |
179 | ||
180 | fprintf (stdout, _("\ | |
181 | Usage: %s [OPTION]... FILE\n"), program_name); | |
182 | putc ('\n', stdout); | |
183 | ||
184 | fputs (_("\ | |
185 | If a long option shows an argument as mandatory, then it is mandatory\n\ | |
186 | for the equivalent short option also. Similarly for optional arguments.\n"), | |
187 | stdout); | |
188 | putc ('\n', stdout); | |
189 | ||
190 | fputs (_("\ | |
191 | Operation modes:\n\ | |
192 | -h, --help display this help and exit\n\ | |
193 | -V, --version output version information and exit\n\ | |
194 | -y, --yacc emulate POSIX yacc\n"), stdout); | |
195 | putc ('\n', stdout); | |
196 | ||
197 | fputs (_("\ | |
198 | Parser:\n\ | |
199 | -S, --skeleton=FILE specify the skeleton to use\n\ | |
200 | -t, --debug instrument the parser for debugging\n\ | |
201 | --locations enable locations computation\n\ | |
202 | -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\ | |
203 | -l, --no-lines don't generate `#line' directives\n\ | |
204 | -n, --no-parser generate the tables only\n\ | |
205 | -k, --token-table include a table of token names\n\ | |
206 | "), stdout); | |
207 | putc ('\n', stdout); | |
208 | ||
209 | fputs (_("\ | |
210 | Output:\n\ | |
211 | -d, --defines also produce a header file\n\ | |
212 | -r, --report=THINGS also produce details on the automaton\n\ | |
213 | -v, --verbose same as `--report=state'\n\ | |
214 | -b, --file-prefix=PREFIX specify a PREFIX for output files\n\ | |
215 | -o, --output=FILE leave output to FILE\n\ | |
216 | -g, --graph also produce a VCG description of the automaton\n\ | |
217 | "), stdout); | |
218 | putc ('\n', stdout); | |
219 | ||
220 | fputs (_("\ | |
221 | THINGS is a list of comma separated words that can include:\n\ | |
222 | `state' describe the states\n\ | |
223 | `itemset' complete the core item sets with their closure\n\ | |
224 | `lookahead' explicitly associate lookaheads to items\n\ | |
225 | `solved' describe shift/reduce conflicts solving\n\ | |
226 | `all' include all the above information\n\ | |
227 | `none' disable the report\n\ | |
228 | "), stdout); | |
229 | putc ('\n', stdout); | |
230 | ||
231 | fputs (_("\ | |
232 | Report bugs to <bug-bison@gnu.org>.\n"), stdout); | |
233 | } | |
234 | ||
235 | exit (status); | |
236 | } | |
237 | ||
238 | ||
239 | /*------------------------------. | |
240 | | Display the version message. | | |
241 | `------------------------------*/ | |
242 | ||
243 | static void | |
244 | version (void) | |
245 | { | |
246 | /* Some efforts were made to ease the translators' task, please | |
247 | continue. */ | |
248 | printf (_("bison (GNU Bison) %s"), VERSION); | |
249 | putc ('\n', stdout); | |
250 | fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout); | |
251 | putc ('\n', stdout); | |
252 | ||
253 | fprintf (stdout, | |
254 | _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2002); | |
255 | ||
256 | fputs (_("\ | |
257 | This is free software; see the source for copying conditions. There is NO\n\ | |
258 | warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\ | |
259 | "), | |
260 | stdout); | |
261 | } | |
262 | ||
263 | ||
264 | /*----------------------. | |
265 | | Process the options. | | |
266 | `----------------------*/ | |
267 | ||
268 | /* Shorts options. */ | |
269 | const char *short_options = "yvegdhr:ltknVo:b:p:S:T::"; | |
270 | ||
271 | static struct option const long_options[] = | |
272 | { | |
273 | /* Operation modes. */ | |
274 | { "help", no_argument, 0, 'h' }, | |
275 | { "version", no_argument, 0, 'V' }, | |
276 | ||
277 | /* Parser. */ | |
278 | { "name-prefix", required_argument, 0, 'p' }, | |
279 | { "include", required_argument, 0, 'I' }, | |
280 | ||
281 | /* Output. */ | |
282 | { "file-prefix", required_argument, 0, 'b' }, | |
283 | { "output", required_argument, 0, 'o' }, | |
284 | { "output-file", required_argument, 0, 'o' }, | |
285 | { "graph", optional_argument, 0, 'g' }, | |
286 | { "report", required_argument, 0, 'r' }, | |
287 | { "verbose", no_argument, 0, 'v' }, | |
288 | ||
289 | /* Hidden. */ | |
290 | { "trace", optional_argument, 0, 'T' }, | |
291 | ||
292 | /* Output. */ | |
293 | { "defines", optional_argument, 0, 'd' }, | |
294 | ||
295 | /* Operation modes. */ | |
296 | { "fixed-output-files", no_argument, 0, 'y' }, | |
297 | { "yacc", no_argument, 0, 'y' }, | |
298 | ||
299 | /* Parser. */ | |
300 | { "debug", no_argument, 0, 't' }, | |
301 | { "locations", no_argument, &locations_flag, 1 }, | |
302 | { "no-lines", no_argument, 0, 'l' }, | |
303 | { "no-parser", no_argument, 0, 'n' }, | |
304 | { "raw", no_argument, 0, 0 }, | |
305 | { "skeleton", required_argument, 0, 'S' }, | |
306 | { "token-table", no_argument, 0, 'k' }, | |
307 | ||
308 | {0, 0, 0, 0} | |
309 | }; | |
310 | ||
311 | /* Under DOS, there is no difference on the case. This can be | |
312 | troublesome when looking for `.tab' etc. */ | |
313 | #ifdef MSDOS | |
314 | # define AS_FILE_NAME(File) (strlwr (File), (File)) | |
315 | #else | |
316 | # define AS_FILE_NAME(File) (File) | |
317 | #endif | |
318 | ||
319 | void | |
320 | getargs (int argc, char *argv[]) | |
321 | { | |
322 | int c; | |
323 | ||
324 | while ((c = getopt_long (argc, argv, short_options, long_options, NULL)) | |
325 | != -1) | |
326 | switch (c) | |
327 | { | |
328 | case 0: | |
329 | /* Certain long options cause getopt_long to return 0. */ | |
330 | break; | |
331 | ||
332 | case 'y': | |
333 | yacc_flag = 1; | |
334 | break; | |
335 | ||
336 | case 'h': | |
337 | usage (EXIT_SUCCESS); | |
338 | ||
339 | case 'V': | |
340 | version (); | |
341 | exit (EXIT_SUCCESS); | |
342 | ||
343 | case 'g': | |
344 | /* Here, the -g and --graph=FILE options are differentiated. */ | |
345 | graph_flag = 1; | |
346 | spec_graph_file = AS_FILE_NAME (optarg); | |
347 | break; | |
348 | ||
349 | case 'v': | |
350 | report_flag |= report_states; | |
351 | break; | |
352 | ||
353 | case 'S': | |
354 | skeleton = AS_FILE_NAME (optarg); | |
355 | break; | |
356 | ||
357 | case 'I': | |
358 | include = AS_FILE_NAME (optarg); | |
359 | break; | |
360 | ||
361 | case 'd': | |
362 | /* Here, the -d and --defines options are differentiated. */ | |
363 | defines_flag = 1; | |
364 | if (optarg) | |
365 | spec_defines_file = AS_FILE_NAME (optarg); | |
366 | break; | |
367 | ||
368 | case 'l': | |
369 | no_lines_flag = 1; | |
370 | break; | |
371 | ||
372 | case 'k': | |
373 | token_table_flag = 1; | |
374 | break; | |
375 | ||
376 | case 'n': | |
377 | no_parser_flag = 1; | |
378 | break; | |
379 | ||
380 | case 't': | |
381 | debug_flag = 1; | |
382 | break; | |
383 | ||
384 | case 'o': | |
385 | spec_outfile = AS_FILE_NAME (optarg); | |
386 | break; | |
387 | ||
388 | case 'b': | |
389 | spec_file_prefix = AS_FILE_NAME (optarg); | |
390 | break; | |
391 | ||
392 | case 'p': | |
393 | spec_name_prefix = optarg; | |
394 | break; | |
395 | ||
396 | case 'r': | |
397 | report_argmatch (optarg); | |
398 | break; | |
399 | ||
400 | case 'T': | |
401 | trace_argmatch (optarg); | |
402 | break; | |
403 | ||
404 | default: | |
405 | usage (EXIT_FAILURE); | |
406 | } | |
407 | ||
408 | if (argc - optind != 1) | |
409 | { | |
410 | if (argc - optind < 1) | |
411 | error (0, 0, _("missing operand after `%s'"), argv[argc - 1]); | |
412 | else | |
413 | error (0, 0, _("extra operand `%s'"), argv[optind + 1]); | |
414 | usage (EXIT_FAILURE); | |
415 | } | |
416 | ||
417 | current_file = grammar_file = uniqstr_new (argv[optind]); | |
418 | } |