]> git.saurik.com Git - bison.git/blame - src/getargs.c
For push mode, convert yyparse from a macro to a function, invoke yylex
[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>
3b2942e6 28#include <configmake.h>
d38a11a6 29#include <error.h>
f47dbf6b
PE
30
31/* Hack to get <getopt.h> to declare getopt with a prototype. */
32#if lint && ! defined __GNU_LIBRARY__
33# define __GNU_LIBRARY__
34# define HACK_FOR___GNU_LIBRARY___PROTOTYPE 1
35#endif
36
d38a11a6
PE
37#include <getopt.h>
38
f47dbf6b
PE
39#ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
40# undef __GNU_LIBRARY__
41# undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
42#endif
43
b0ce6046 44#include "complain.h"
ec3bc396 45#include "files.h"
d38a11a6
PE
46#include "getargs.h"
47#include "uniqstr.h"
3d8fc6ca 48
d0829076
PE
49bool debug_flag;
50bool defines_flag;
4e83ea15 51bool graph_flag;
d0829076
PE
52bool locations_flag;
53bool no_lines_flag;
54bool no_parser_flag;
d0829076
PE
55bool token_table_flag;
56bool yacc_flag; /* for -y */
4e83ea15
AD
57
58bool error_verbose = false;
d2729d44 59
916708d5
AD
60bool nondeterministic_parser = false;
61bool glr_parser = false;
7172e23e 62bool pull_parser = true;
916708d5 63bool pure_parser = false;
31c10e38 64bool push_parser = false;
916708d5 65
4e83ea15
AD
66int report_flag = report_none;
67int trace_flag = trace_none;
7b42569e 68int warnings_flag = warnings_none;
4e83ea15 69
0e021770
PE
70static struct bison_language const valid_languages[] = {
71 { "c", "c-skel.m4", ".c", ".h", true },
72 { "c++", "c++-skel.m4", ".cc", ".hh", true },
73 { "", "", "", "", false }
74};
75
76static int skeleton_prio = 2;
b0ce6046 77const char *skeleton = NULL;
0e021770
PE
78static int language_prio = 2;
79struct bison_language const *language = &valid_languages[0];
f6bd5427 80const char *include = NULL;
b0ce6046 81
0e021770 82char *program_name;
3d8fc6ca 83
273a74fa 84
7b42569e
AD
85/** Decode an option's set of keys.
86 *
87 * \param option option being decoded.
07c39ae9 88 * \param keys array of valid subarguments.
7b42569e 89 * \param values array of corresponding (int) values.
07c39ae9 90 * \param flags the flags to update
7b42569e
AD
91 * \param args colon separated list of effective subarguments to decode.
92 * If 0, then activate all the flags.
93 *
94 * The special value 0 resets the flags to 0.
95 */
80ac75bc
PE
96static void
97flags_argmatch (const char *option,
7b42569e
AD
98 const char * const keys[], const int values[],
99 int *flags, char *args)
100{
101 if (args)
102 {
103 args = strtok (args, ",");
ba7560e2 104 while (args)
7b42569e
AD
105 {
106 int value = XARGMATCH (option, args, keys, values);
107 if (value == 0)
108 *flags = 0;
109 else
110 *flags |= value;
eb095650 111 args = strtok (NULL, ",");
7b42569e 112 }
7b42569e
AD
113 }
114 else
115 *flags = ~0;
116}
117
80ac75bc 118/** Decode a set of sub arguments.
7b42569e
AD
119 *
120 * \param FlagName the flag familly to update.
07c39ae9 121 * \param Args the effective sub arguments to decode.
7b42569e
AD
122 *
123 * \arg FlagName_args the list of keys.
124 * \arg FlagName_types the list of values.
125 * \arg FlagName_flag the flag to update.
126 */
127#define FLAGS_ARGMATCH(FlagName, Args) \
128 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
129 &FlagName ## _flag, Args)
130
131
b8a41559
AD
132/*----------------------.
133| --report's handling. |
134`----------------------*/
135
136static const char * const report_args[] =
137{
138 /* In a series of synonyms, present the most meaningful first, so
139 that argmatch_valid be more readable. */
140 "none",
141 "state", "states",
142 "itemset", "itemsets",
143 "lookahead", "lookaheads", "look-ahead",
144 "solved",
145 "all",
146 0
147};
148
149static const int report_types[] =
150{
151 report_none,
152 report_states, report_states,
153 report_states | report_itemsets, report_states | report_itemsets,
154 report_states | report_lookahead_tokens,
155 report_states | report_lookahead_tokens,
156 report_states | report_lookahead_tokens,
157 report_states | report_solved_conflicts,
158 report_all
159};
160
161ARGMATCH_VERIFY (report_args, report_types);
162
b8a41559 163
273a74fa
AD
164/*---------------------.
165| --trace's handling. |
166`---------------------*/
167
168static const char * const trace_args[] =
169{
170 /* In a series of synonyms, present the most meaningful first, so
171 that argmatch_valid be more readable. */
b8a41559 172 "none - no traces",
c5e3e510
AD
173 "scan - grammar scanner traces",
174 "parse - grammar parser traces",
b8a41559 175 "automaton - construction of the automaton",
273a74fa 176 "bitsets - use of bitsets",
b8a41559 177 "grammar - reading, reducing the grammar",
1509d42f 178 "resource - memory consumption (where available)",
273a74fa 179 "sets - grammar sets: firsts, nullable etc.",
327afc7c
AD
180 "tools - m4 invocation",
181 "m4 - m4 traces",
c5e3e510
AD
182 "skeleton - skeleton postprocessing",
183 "time - time consumption",
273a74fa
AD
184 "all - all of the above",
185 0
186};
187
188static const int trace_types[] =
189{
190 trace_none,
473d0a75
AD
191 trace_scan,
192 trace_parse,
273a74fa
AD
193 trace_automaton,
194 trace_bitsets,
195 trace_grammar,
196 trace_resource,
197 trace_sets,
198 trace_tools,
327afc7c 199 trace_m4,
c5e3e510
AD
200 trace_skeleton,
201 trace_time,
273a74fa
AD
202 trace_all
203};
204
8a6f72f3 205ARGMATCH_VERIFY (trace_args, trace_types);
273a74fa 206
7b42569e
AD
207
208/*------------------------.
209| --warnings's handling. |
210`------------------------*/
211
212static const char * const warnings_args[] =
273a74fa 213{
7b42569e
AD
214 /* In a series of synonyms, present the most meaningful first, so
215 that argmatch_valid be more readable. */
17bd8a73
JD
216 "none - no warnings",
217 "midrule-values - unset or unused midrule values",
218 "yacc - incompatibilities with POSIX YACC",
219 "all - all of the above",
220 "error - warnings are errors",
7b42569e
AD
221 0
222};
223
224static const int warnings_types[] =
225{
226 warnings_none,
17bd8a73 227 warnings_midrule_values,
7b42569e 228 warnings_yacc,
89eb3c76
JD
229 warnings_all,
230 warnings_error
7b42569e
AD
231};
232
233ARGMATCH_VERIFY (warnings_args, warnings_types);
273a74fa
AD
234
235
0e575721
AD
236/*-------------------------------------------.
237| Display the help message and exit STATUS. |
238`-------------------------------------------*/
e79137ac 239
0df27e8b
PE
240static void usage (int) ATTRIBUTE_NORETURN;
241
4a120d45 242static void
0e575721 243usage (int status)
cbd8ffc5 244{
0e575721
AD
245 if (status != 0)
246 fprintf (stderr, _("Try `%s --help' for more information.\n"),
247 program_name);
248 else
249 {
a92be413 250 printf (_("Usage: %s [OPTION]... FILE\n"), program_name);
0e575721 251 fputs (_("\
a92be413
PE
252Generate LALR(1) and GLR parsers.\n\
253\n\
254"), stdout);
9f306f2a 255
0e575721 256 fputs (_("\
a92be413
PE
257Mandatory arguments to long options are mandatory for short options too.\n\
258"), stdout);
9f306f2a 259
0e575721 260 fputs (_("\
a92be413 261\n\
9f306f2a 262Operation modes:\n\
f7ab6a50
PE
263 -h, --help display this help and exit\n\
264 -V, --version output version information and exit\n\
265 --print-localedir output directory containing locale-dependent data\n\
a92be413
PE
266 -y, --yacc emulate POSIX Yacc\n\
267\n\
268"), stdout);
9f306f2a 269
0e575721 270 fputs (_("\
9f306f2a 271Parser:\n\
cd5bd6ac 272 -S, --skeleton=FILE specify the skeleton to use\n\
9f306f2a 273 -t, --debug instrument the parser for debugging\n\
89cab50d 274 --locations enable locations computation\n\
9f306f2a
AD
275 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
276 -l, --no-lines don't generate `#line' directives\n\
277 -n, --no-parser generate the tables only\n\
9f306f2a 278 -k, --token-table include a table of token names\n\
a92be413 279\n\
0e575721 280"), stdout);
9f306f2a 281
0e575721 282 fputs (_("\
9f306f2a
AD
283Output:\n\
284 -d, --defines also produce a header file\n\
ec3bc396
AD
285 -r, --report=THINGS also produce details on the automaton\n\
286 -v, --verbose same as `--report=state'\n\
9f306f2a 287 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
951366c1 288 -o, --output=FILE leave output to FILE\n\
35fe0834 289 -g, --graph also output a graph of the automaton\n\
a92be413 290\n\
0e575721 291"), stdout);
86eff183 292
0e575721 293 fputs (_("\
ec3bc396
AD
294THINGS is a list of comma separated words that can include:\n\
295 `state' describe the states\n\
296 `itemset' complete the core item sets with their closure\n\
742e4900 297 `lookahead' explicitly associate lookahead tokens to items\n\
b408954b 298 `solved' describe shift/reduce conflicts solving\n\
ec3bc396
AD
299 `all' include all the above information\n\
300 `none' disable the report\n\
0e575721 301"), stdout);
9f306f2a 302
a92be413 303 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
0e575721
AD
304 }
305
306 exit (status);
cbd8ffc5
DM
307}
308
e79137ac
AD
309
310/*------------------------------.
311| Display the version message. |
312`------------------------------*/
313
314static void
0e575721 315version (void)
e79137ac
AD
316{
317 /* Some efforts were made to ease the translators' task, please
318 continue. */
0e575721
AD
319 printf (_("bison (GNU Bison) %s"), VERSION);
320 putc ('\n', stdout);
7891a7c4 321 printf ("%s", revision);
0e575721
AD
322 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout);
323 putc ('\n', stdout);
e79137ac 324
0e575721 325 fprintf (stdout,
e2a21b6f 326 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
e79137ac
AD
327
328 fputs (_("\
329This is free software; see the source for copying conditions. There is NO\n\
330warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
331"),
0e575721 332 stdout);
e79137ac
AD
333}
334
335
0e021770
PE
336/*-------------------------------------.
337| --skeleton and --language handling. |
338`--------------------------------------*/
339
340void
341skeleton_arg (char const *arg, int prio, location const *loc)
342{
343 if (prio < skeleton_prio)
344 {
345 skeleton_prio = prio;
346 skeleton = arg;
347 }
348 else if (prio == skeleton_prio)
349 {
350 char const *msg =
351 _("multiple skeleton declarations are invalid");
352 if (loc)
353 complain_at (*loc, msg);
354 else
355 complain (msg);
356 }
357}
358
359void
360language_argmatch (char const *arg, int prio, location const *loc)
361{
362 char const *msg;
363
364 if (prio < language_prio)
365 {
366 int i;
367 for (i = 0; valid_languages[i].language[0]; i++)
368 if (strcasecmp (arg, valid_languages[i].language) == 0)
369 {
370 language_prio = prio;
371 language = &valid_languages[i];
372 return;
373 }
374 msg = _("invalid language `%s'");
375 }
376 else if (language_prio == prio)
377 msg = _("multiple language declarations are invalid");
378 else
379 return;
380
381 if (loc)
382 complain_at (*loc, msg, arg);
383 else
384 complain (msg, arg);
385}
386
e79137ac
AD
387/*----------------------.
388| Process the options. |
389`----------------------*/
390
e2aaf4c4 391/* Shorts options. */
0e021770 392static char const short_options[] = "yvegdhr:L:ltknVo:b:p:S:T::W";
e2aaf4c4 393
d0829076
PE
394/* Values for long options that do not have single-letter equivalents. */
395enum
396{
f7ab6a50
PE
397 LOCATIONS_OPTION = CHAR_MAX + 1,
398 PRINT_LOCALEDIR_OPTION
d0829076
PE
399};
400
e2aaf4c4
AD
401static struct option const long_options[] =
402{
403 /* Operation modes. */
7b42569e
AD
404 { "help", no_argument, 0, 'h' },
405 { "version", no_argument, 0, 'V' },
406 { "print-localedir", no_argument, 0, PRINT_LOCALEDIR_OPTION },
407 { "warnings", optional_argument, 0, 'W' },
e2aaf4c4
AD
408
409 /* Parser. */
410 { "name-prefix", required_argument, 0, 'p' },
411 { "include", required_argument, 0, 'I' },
412
413 /* Output. */
414 { "file-prefix", required_argument, 0, 'b' },
415 { "output", required_argument, 0, 'o' },
416 { "output-file", required_argument, 0, 'o' },
417 { "graph", optional_argument, 0, 'g' },
418 { "report", required_argument, 0, 'r' },
419 { "verbose", no_argument, 0, 'v' },
420
421 /* Hidden. */
273a74fa 422 { "trace", optional_argument, 0, 'T' },
e2aaf4c4 423
e2aaf4c4
AD
424 /* Output. */
425 { "defines", optional_argument, 0, 'd' },
426
427 /* Operation modes. */
428 { "fixed-output-files", no_argument, 0, 'y' },
429 { "yacc", no_argument, 0, 'y' },
430
431 /* Parser. */
432 { "debug", no_argument, 0, 't' },
d0829076 433 { "locations", no_argument, 0, LOCATIONS_OPTION },
e2aaf4c4
AD
434 { "no-lines", no_argument, 0, 'l' },
435 { "no-parser", no_argument, 0, 'n' },
436 { "raw", no_argument, 0, 0 },
437 { "skeleton", required_argument, 0, 'S' },
0e021770 438 { "language", required_argument, 0, 'L' },
e2aaf4c4
AD
439 { "token-table", no_argument, 0, 'k' },
440
441 {0, 0, 0, 0}
442};
443
ae404801
AD
444/* Under DOS, there is no difference on the case. This can be
445 troublesome when looking for `.tab' etc. */
446#ifdef MSDOS
447# define AS_FILE_NAME(File) (strlwr (File), (File))
448#else
449# define AS_FILE_NAME(File) (File)
450#endif
451
3d8fc6ca 452void
d2729d44 453getargs (int argc, char *argv[])
3d8fc6ca 454{
1916f98e 455 int c;
3d8fc6ca 456
08721544
PE
457 while ((c = getopt_long (argc, argv, short_options, long_options, NULL))
458 != -1)
cd5bd6ac
AD
459 switch (c)
460 {
461 case 0:
462 /* Certain long options cause getopt_long to return 0. */
463 break;
464
7b42569e
AD
465 case 'b':
466 spec_file_prefix = AS_FILE_NAME (optarg);
cd5bd6ac
AD
467 break;
468
22c2cbc0 469 case 'g':
342b8b6e 470 /* Here, the -g and --graph=FILE options are differentiated. */
d0829076 471 graph_flag = true;
fcbfa6b0 472 if (optarg)
eb095650 473 spec_graph_file = xstrdup (AS_FILE_NAME (optarg));
22c2cbc0
AD
474 break;
475
7b42569e
AD
476 case 'h':
477 usage (EXIT_SUCCESS);
cd5bd6ac 478
0e021770
PE
479 case 'L':
480 language_argmatch (optarg, 0, NULL);
481 break;
482
cd5bd6ac 483 case 'S':
0e021770 484 skeleton_arg (AS_FILE_NAME (optarg), 0, NULL);
cd5bd6ac
AD
485 break;
486
f6bd5427 487 case 'I':
ae404801 488 include = AS_FILE_NAME (optarg);
f6bd5427
MA
489 break;
490
cd5bd6ac 491 case 'd':
342b8b6e 492 /* Here, the -d and --defines options are differentiated. */
d0829076 493 defines_flag = true;
ae404801 494 if (optarg)
eb095650 495 spec_defines_file = xstrdup (AS_FILE_NAME (optarg));
cd5bd6ac
AD
496 break;
497
7b42569e
AD
498 case 'k':
499 token_table_flag = true;
500 break;
501
cd5bd6ac 502 case 'l':
d0829076
PE
503 no_lines_flag = true;
504 break;
505
7b42569e
AD
506 case 'n':
507 no_parser_flag = true;
cd5bd6ac
AD
508 break;
509
7b42569e
AD
510 case 'o':
511 spec_outfile = AS_FILE_NAME (optarg);
cd5bd6ac
AD
512 break;
513
7b42569e
AD
514 case 'p':
515 spec_name_prefix = optarg;
516 break;
517
518 case 'r':
519 FLAGS_ARGMATCH (report, optarg);
520 break;
521
522 case 'T':
523 FLAGS_ARGMATCH (trace, optarg);
cd5bd6ac
AD
524 break;
525
526 case 't':
d0829076 527 debug_flag = true;
cd5bd6ac
AD
528 break;
529
7b42569e
AD
530 case 'V':
531 version ();
532 exit (EXIT_SUCCESS);
cd5bd6ac 533
7b42569e
AD
534 case 'v':
535 report_flag |= report_states;
cd5bd6ac
AD
536 break;
537
7b42569e
AD
538 case 'y':
539 yacc_flag = true;
cd5bd6ac
AD
540 break;
541
7b42569e 542 case 'W':
89eb3c76
JD
543 if (optarg)
544 FLAGS_ARGMATCH (warnings, optarg);
545 else
546 warnings_flag |= warnings_all;
ec3bc396
AD
547 break;
548
7b42569e
AD
549 case LOCATIONS_OPTION:
550 locations_flag = true;
273a74fa
AD
551 break;
552
7b42569e
AD
553 case PRINT_LOCALEDIR_OPTION:
554 printf ("%s\n", LOCALEDIR);
555 exit (EXIT_SUCCESS);
556
cd5bd6ac 557 default:
0df27e8b 558 usage (EXIT_FAILURE);
cd5bd6ac 559 }
3d8fc6ca 560
a4b6efd4 561 if (argc - optind != 1)
3d8fc6ca 562 {
a4b6efd4
PE
563 if (argc - optind < 1)
564 error (0, 0, _("missing operand after `%s'"), argv[argc - 1]);
565 else
566 error (0, 0, _("extra operand `%s'"), argv[optind + 1]);
0e575721 567 usage (EXIT_FAILURE);
3d8fc6ca 568 }
3d8fc6ca 569
d38a11a6 570 current_file = grammar_file = uniqstr_new (argv[optind]);
3d8fc6ca 571}