]>
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
29 /* Hack to get <getopt.h> to declare getopt with a prototype. */
30 #if lint && ! defined __GNU_LIBRARY__
31 # define __GNU_LIBRARY__
32 # define HACK_FOR___GNU_LIBRARY___PROTOTYPE 1
37 #ifdef HACK_FOR___GNU_LIBRARY___PROTOTYPE
38 # undef __GNU_LIBRARY__
39 # undef HACK_FOR___GNU_LIBRARY___PROTOTYPE
52 int report_flag
= report_none
;
53 bool token_table_flag
;
54 bool yacc_flag
; /* for -y */
56 int trace_flag
= trace_none
;
58 bool nondeterministic_parser
= false;
59 bool glr_parser
= false;
60 bool pure_parser
= false;
62 const char *skeleton
= NULL
;
63 const char *include
= NULL
;
65 extern char *program_name
;
68 /*---------------------.
69 | --trace's handling. |
70 `---------------------*/
72 static const char * const trace_args
[] =
74 /* In a series of synonyms, present the most meaningful first, so
75 that argmatch_valid be more readable. */
77 "scan - grammar scanner traces",
78 "parse - grammar parser traces",
79 "automaton - contruction of the automaton",
80 "bitsets - use of bitsets",
81 "grammar - reading, reducing of the grammar",
82 "resource - memory consumption (where available)",
83 "sets - grammar sets: firsts, nullable etc.",
84 "tools - m4 invocation and preserve the temporary file",
85 "skeleton - skeleton postprocessing",
86 "time - time consumption",
87 "all - all of the above",
91 static const int trace_types
[] =
107 ARGMATCH_VERIFY (trace_args
, trace_types
);
110 trace_argmatch (char *args
)
114 args
= strtok (args
, ",");
117 int trace
= XARGMATCH ("--trace", args
,
118 trace_args
, trace_types
);
119 if (trace
== trace_none
)
120 trace_flag
= trace_none
;
124 while ((args
= strtok (NULL
, ",")));
127 trace_flag
= trace_all
;
131 /*----------------------.
132 | --report's handling. |
133 `----------------------*/
135 static const char * const report_args
[] =
137 /* In a series of synonyms, present the most meaningful first, so
138 that argmatch_valid be more readable. */
141 "itemset", "itemsets",
142 "look-ahead", "lookahead", "lookaheads",
148 static const int report_types
[] =
151 report_states
, report_states
,
152 report_states
| report_itemsets
, report_states
| report_itemsets
,
153 report_states
| report_look_ahead_tokens
,
154 report_states
| report_look_ahead_tokens
,
155 report_states
| report_look_ahead_tokens
,
156 report_states
| report_solved_conflicts
,
160 ARGMATCH_VERIFY (report_args
, report_types
);
163 report_argmatch (char *args
)
165 args
= strtok (args
, ",");
168 int report
= XARGMATCH ("--report", args
,
169 report_args
, report_types
);
170 if (report
== report_none
)
171 report_flag
= report_none
;
173 report_flag
|= report
;
175 while ((args
= strtok (NULL
, ",")));
179 /*-------------------------------------------.
180 | Display the help message and exit STATUS. |
181 `-------------------------------------------*/
183 static void usage (int) ATTRIBUTE_NORETURN
;
189 fprintf (stderr
, _("Try `%s --help' for more information.\n"),
193 /* Some efforts were made to ease the translators' task, please
196 GNU bison generates parsers for LALR(1) grammars.\n"), stdout
);
199 fprintf (stdout
, _("\
200 Usage: %s [OPTION]... FILE\n"), program_name
);
204 If a long option shows an argument as mandatory, then it is mandatory\n\
205 for the equivalent short option also. Similarly for optional arguments.\n"),
211 -h, --help display this help and exit\n\
212 -V, --version output version information and exit\n\
213 --print-localedir output directory containing locale-dependent data\n\
214 -y, --yacc emulate POSIX yacc\n"), stdout
);
219 -S, --skeleton=FILE specify the skeleton to use\n\
220 -t, --debug instrument the parser for debugging\n\
221 --locations enable locations computation\n\
222 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
223 -l, --no-lines don't generate `#line' directives\n\
224 -n, --no-parser generate the tables only\n\
225 -k, --token-table include a table of token names\n\
231 -d, --defines also produce a header file\n\
232 -r, --report=THINGS also produce details on the automaton\n\
233 -v, --verbose same as `--report=state'\n\
234 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
235 -o, --output=FILE leave output to FILE\n\
236 -g, --graph also produce a VCG description of the automaton\n\
241 THINGS is a list of comma separated words that can include:\n\
242 `state' describe the states\n\
243 `itemset' complete the core item sets with their closure\n\
244 `look-ahead' explicitly associate look-ahead tokens to items\n\
245 `solved' describe shift/reduce conflicts solving\n\
246 `all' include all the above information\n\
247 `none' disable the report\n\
252 Report bugs to <bug-bison@gnu.org>.\n"), stdout
);
259 /*------------------------------.
260 | Display the version message. |
261 `------------------------------*/
266 /* Some efforts were made to ease the translators' task, please
268 printf (_("bison (GNU Bison) %s"), VERSION
);
270 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stdout
);
274 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2006);
277 This is free software; see the source for copying conditions. There is NO\n\
278 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
284 /*----------------------.
285 | Process the options. |
286 `----------------------*/
288 /* Shorts options. */
289 static char const short_options
[] = "yvegdhr:ltknVo:b:p:S:T::";
291 /* Values for long options that do not have single-letter equivalents. */
294 LOCATIONS_OPTION
= CHAR_MAX
+ 1,
295 PRINT_LOCALEDIR_OPTION
298 static struct option
const long_options
[] =
300 /* Operation modes. */
301 { "help", no_argument
, 0, 'h' },
302 { "version", no_argument
, 0, 'V' },
303 { "print-localedir", no_argument
, 0, PRINT_LOCALEDIR_OPTION
},
306 { "name-prefix", required_argument
, 0, 'p' },
307 { "include", required_argument
, 0, 'I' },
310 { "file-prefix", required_argument
, 0, 'b' },
311 { "output", required_argument
, 0, 'o' },
312 { "output-file", required_argument
, 0, 'o' },
313 { "graph", optional_argument
, 0, 'g' },
314 { "report", required_argument
, 0, 'r' },
315 { "verbose", no_argument
, 0, 'v' },
318 { "trace", optional_argument
, 0, 'T' },
321 { "defines", optional_argument
, 0, 'd' },
323 /* Operation modes. */
324 { "fixed-output-files", no_argument
, 0, 'y' },
325 { "yacc", no_argument
, 0, 'y' },
328 { "debug", no_argument
, 0, 't' },
329 { "locations", no_argument
, 0, LOCATIONS_OPTION
},
330 { "no-lines", no_argument
, 0, 'l' },
331 { "no-parser", no_argument
, 0, 'n' },
332 { "raw", no_argument
, 0, 0 },
333 { "skeleton", required_argument
, 0, 'S' },
334 { "token-table", no_argument
, 0, 'k' },
339 /* Under DOS, there is no difference on the case. This can be
340 troublesome when looking for `.tab' etc. */
342 # define AS_FILE_NAME(File) (strlwr (File), (File))
344 # define AS_FILE_NAME(File) (File)
348 getargs (int argc
, char *argv
[])
352 while ((c
= getopt_long (argc
, argv
, short_options
, long_options
, NULL
))
357 /* Certain long options cause getopt_long to return 0. */
365 usage (EXIT_SUCCESS
);
371 case PRINT_LOCALEDIR_OPTION
:
372 printf ("%s\n", LOCALEDIR
);
376 /* Here, the -g and --graph=FILE options are differentiated. */
379 spec_graph_file
= AS_FILE_NAME (optarg
);
383 report_flag
|= report_states
;
387 skeleton
= AS_FILE_NAME (optarg
);
391 include
= AS_FILE_NAME (optarg
);
395 /* Here, the -d and --defines options are differentiated. */
398 spec_defines_file
= AS_FILE_NAME (optarg
);
402 no_lines_flag
= true;
405 case LOCATIONS_OPTION
:
406 locations_flag
= true;
410 token_table_flag
= true;
414 no_parser_flag
= true;
422 spec_outfile
= AS_FILE_NAME (optarg
);
426 spec_file_prefix
= AS_FILE_NAME (optarg
);
430 spec_name_prefix
= optarg
;
434 report_argmatch (optarg
);
438 trace_argmatch (optarg
);
442 usage (EXIT_FAILURE
);
445 if (argc
- optind
!= 1)
447 if (argc
- optind
< 1)
448 error (0, 0, _("missing operand after `%s'"), argv
[argc
- 1]);
450 error (0, 0, _("extra operand `%s'"), argv
[optind
+ 1]);
451 usage (EXIT_FAILURE
);
454 current_file
= grammar_file
= uniqstr_new (argv
[optind
]);