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