]>
git.saurik.com Git - bison.git/blob - src/getargs.c
1 /* Parse command line arguments for Bison.
3 Copyright (C) 1984, 1986, 1989, 1992, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006 Free Software Foundation, Inc.
6 This file is part of Bison, the GNU Compiler Compiler.
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)
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.
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
28 #include <configmake.h>
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
39 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
40 # undef __GNU_LIBRARY__
41 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
55 bool token_table_flag
;
56 bool yacc_flag
; /* for -y */
58 bool error_verbose
= false;
60 bool nondeterministic_parser
= false;
61 bool glr_parser
= false;
62 bool pure_parser
= false;
63 bool push_parser
= false;
65 int report_flag
= report_none
;
66 int trace_flag
= trace_none
;
67 int warnings_flag
= warnings_none
;
69 const char *skeleton
= NULL
;
70 const char *include
= NULL
;
72 extern char *program_name
;
75 /** Decode an option's set of keys.
77 * \param option option being decoded.
78 * \paran keys array of valid subarguments.
79 * \param values array of corresponding (int) values.
80 * \param flag the flags to update
81 * \param args colon separated list of effective subarguments to decode.
82 * If 0, then activate all the flags.
84 * The special value 0 resets the flags to 0.
87 flags_argmatch (const char *option
,
88 const char * const keys
[], const int values
[],
89 int *flags
, char *args
)
93 args
= strtok (args
, ",");
96 int value
= XARGMATCH (option
, args
, keys
, values
);
101 args
= strtok (NULL
, ",");
108 /** Decode a set of sub arguments.
110 * \param FlagName the flag familly to update.
111 * \param args the effective sub arguments to decode.
113 * \arg FlagName_args the list of keys.
114 * \arg FlagName_types the list of values.
115 * \arg FlagName_flag the flag to update.
117 #define FLAGS_ARGMATCH(FlagName, Args) \
118 flags_argmatch ("--" #FlagName, FlagName ## _args, FlagName ## _types, \
119 &FlagName ## _flag, Args)
122 /*----------------------.
123 | --report's handling. |
124 `----------------------*/
126 static const char * const report_args
[] =
128 /* In a series of synonyms, present the most meaningful first, so
129 that argmatch_valid be more readable. */
132 "itemset", "itemsets",
133 "lookahead", "lookaheads", "look-ahead",
139 static const int report_types
[] =
142 report_states
, report_states
,
143 report_states
| report_itemsets
, report_states
| report_itemsets
,
144 report_states
| report_lookahead_tokens
,
145 report_states
| report_lookahead_tokens
,
146 report_states
| report_lookahead_tokens
,
147 report_states
| report_solved_conflicts
,
151 ARGMATCH_VERIFY (report_args
, report_types
);
154 /*---------------------.
155 | --trace's handling. |
156 `---------------------*/
158 static const char * const trace_args
[] =
160 /* In a series of synonyms, present the most meaningful first, so
161 that argmatch_valid be more readable. */
163 "scan - grammar scanner traces",
164 "parse - grammar parser traces",
165 "automaton - construction of the automaton",
166 "bitsets - use of bitsets",
167 "grammar - reading, reducing the grammar",
168 "resource - memory consumption (where available)",
169 "sets - grammar sets: firsts, nullable etc.",
170 "tools - m4 invocation",
172 "skeleton - skeleton postprocessing",
173 "time - time consumption",
174 "all - all of the above",
178 static const int trace_types
[] =
195 ARGMATCH_VERIFY (trace_args
, trace_types
);
198 /*------------------------.
199 | --warnings's handling. |
200 `------------------------*/
202 static const char * const warnings_args
[] =
204 /* In a series of synonyms, present the most meaningful first, so
205 that argmatch_valid be more readable. */
206 "none - no warnings",
207 "midrule-values - unset or unused midrule values",
208 "yacc - incompatibilities with POSIX YACC",
209 "all - all of the above",
210 "error - warnings are errors",
214 static const int warnings_types
[] =
217 warnings_midrule_values
,
223 ARGMATCH_VERIFY (warnings_args
, warnings_types
);
226 /*-------------------------------------------.
227 | Display the help message and exit STATUS. |
228 `-------------------------------------------*/
230 static void usage (int) ATTRIBUTE_NORETURN
;
236 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
240 printf (_("Usage: %s [OPTION]... FILE\n"), program_name
);
242 Generate LALR(1) and GLR parsers.\n\
247 Mandatory arguments to long options are mandatory for short options too.\n\
253 -h, --help display this help and exit\n\
254 -V, --version output version information and exit\n\
255 --print-localedir output directory containing locale-dependent data\n\
256 -y, --yacc emulate POSIX Yacc\n\
262 -S, --skeleton=FILE specify the skeleton to use\n\
263 -t, --debug instrument the parser for debugging\n\
264 --locations enable locations computation\n\
265 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
266 -l, --no-lines don't generate `#line' directives\n\
267 -n, --no-parser generate the tables only\n\
268 -k, --token-table include a table of token names\n\
274 -d, --defines also produce a header file\n\
275 -r, --report=THINGS also produce details on the automaton\n\
276 -v, --verbose same as `--report=state'\n\
277 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
278 -o, --output=FILE leave output to FILE\n\
279 -g, --graph also output a graph of the automaton\n\
284 THINGS is a list of comma separated words that can include:\n\
285 `state' describe the states\n\
286 `itemset' complete the core item sets with their closure\n\
287 `lookahead' explicitly associate lookahead tokens to items\n\
288 `solved' describe shift/reduce conflicts solving\n\
289 `all' include all the above information\n\
290 `none' disable the report\n\
293 printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT
);
300 /*------------------------------.
301 | Display the version message. |
302 `------------------------------*/
307 /* Some efforts were made to ease the translators' task, please
309 printf (_("bison (GNU Bison) %s"), VERSION
);
311 printf ("%s", revision
);
312 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
316 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
319 This is free software; see the source for copying conditions. There is NO\n\
320 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
326 /*----------------------.
327 | Process the options. |
328 `----------------------*/
330 /* Shorts options. */
331 static char const short_options
[] = "yvegdhr:ltknVo:b:p:S:T::W";
333 /* Values for long options that do not have single-letter equivalents. */
336 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
337 PRINT_LOCALEDIR_OPTION
340 static struct option
const long_options
[] =
342 /* Operation modes. */
343 { "help", no_argument
, 0, 'h' },
344 { "version", no_argument
, 0, 'V' },
345 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
346 { "warnings", optional_argument
, 0, 'W' },
349 { "name-prefix", required_argument
, 0, 'p' },
350 { "include", required_argument
, 0, 'I' },
353 { "file-prefix", required_argument
, 0, 'b' },
354 { "output", required_argument
, 0, 'o' },
355 { "output-file", required_argument
, 0, 'o' },
356 { "graph", optional_argument
, 0, 'g' },
357 { "report", required_argument
, 0, 'r' },
358 { "verbose", no_argument
, 0, 'v' },
361 { "trace", optional_argument
, 0, 'T' },
364 { "defines", optional_argument
, 0, 'd' },
366 /* Operation modes. */
367 { "fixed-output-files", no_argument
, 0, 'y' },
368 { "yacc", no_argument
, 0, 'y' },
371 { "debug", no_argument
, 0, 't' },
372 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
373 { "no-lines", no_argument
, 0, 'l' },
374 { "no-parser", no_argument
, 0, 'n' },
375 { "raw", no_argument
, 0, 0 },
376 { "skeleton", required_argument
, 0, 'S' },
377 { "token-table", no_argument
, 0, 'k' },
382 /* Under DOS, there is no difference on the case. This can be
383 troublesome when looking for `.tab' etc. */
385 # define AS_FILE_NAME(File) (strlwr (File), (File))
387 # define AS_FILE_NAME(File) (File)
391 getargs (int argc
, char *argv
[])
395 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
400 /* Certain long options cause getopt_long to return 0. */
404 spec_file_prefix
= AS_FILE_NAME (optarg
);
408 /* Here, the -g and --graph=FILE options are differentiated. */
411 spec_graph_file
= AS_FILE_NAME (optarg
);
415 usage (EXIT_SUCCESS
);
418 skeleton
= AS_FILE_NAME (optarg
);
422 include
= AS_FILE_NAME (optarg
);
426 /* Here, the -d and --defines options are differentiated. */
429 spec_defines_file
= AS_FILE_NAME (optarg
);
433 token_table_flag
= true;
437 no_lines_flag
= true;
441 no_parser_flag
= true;
445 spec_outfile
= AS_FILE_NAME (optarg
);
449 spec_name_prefix
= optarg
;
453 FLAGS_ARGMATCH (report
, optarg
);
457 FLAGS_ARGMATCH (trace
, optarg
);
469 report_flag
|= report_states
;
478 FLAGS_ARGMATCH (warnings
, optarg
);
480 warnings_flag
|= warnings_all
;
483 case LOCATIONS_OPTION
:
484 locations_flag
= true;
487 case PRINT_LOCALEDIR_OPTION
:
488 printf ("%s\n", LOCALEDIR
);
492 usage (EXIT_FAILURE
);
495 if (argc
- optind
!= 1)
497 if (argc
- optind
< 1)
498 error (0, 0, _("missing operand after `%s'"), argv
[argc
- 1]);
500 error (0, 0, _("extra operand `%s'"), argv
[optind
+ 1]);
501 usage (EXIT_FAILURE
);
504 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);