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