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