]> git.saurik.com Git - bison.git/blame - src/getargs.c
* src/muscle_tab.h (MUSCLE_TAB_H_): Was misspelled as MUSCLE_TAB_H.
[bison.git] / src / getargs.c
CommitLineData
9f306f2a 1/* Parse command line arguments for bison.
06b00abc
AD
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
3d8fc6ca 4
9f306f2a 5 This file is part of Bison, the GNU Compiler Compiler.
3d8fc6ca 6
9f306f2a
AD
7 Bison is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
3d8fc6ca 11
9f306f2a
AD
12 Bison is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
3d8fc6ca 16
9f306f2a
AD
17 You should have received a copy of the GNU General Public License
18 along with Bison; see the file COPYING. If not, write to the Free
19 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
3d8fc6ca 21
3d8fc6ca 22#include "system.h"
ec3bc396
AD
23#include "getopt.h"
24#include "argmatch.h"
b0ce6046 25#include "complain.h"
ceed8467 26#include "getargs.h"
ec3bc396 27#include "files.h"
3d8fc6ca 28
89cab50d
AD
29int debug_flag = 0;
30int defines_flag = 0;
31int locations_flag = 0;
32int no_lines_flag = 0;
33int no_parser_flag = 0;
273a74fa 34int report_flag = report_none;
89cab50d 35int token_table_flag = 0;
89cab50d 36int yacc_flag = 0; /* for -y */
22c2cbc0 37int graph_flag = 0;
273a74fa 38int trace_flag = trace_none;
d2729d44 39
b0ce6046 40const char *skeleton = NULL;
f6bd5427 41const char *include = NULL;
b0ce6046 42
cbd8ffc5 43extern char *program_name;
3d8fc6ca 44
273a74fa
AD
45
46/*---------------------.
47| --trace's handling. |
48`---------------------*/
49
50static const char * const trace_args[] =
51{
52 /* In a series of synonyms, present the most meaningful first, so
53 that argmatch_valid be more readable. */
54 "none - no report",
55 "automaton - contruction of the automaton",
56 "bitsets - use of bitsets",
57 "grammar - reading, reducing of the grammar",
1509d42f 58 "resource - memory consumption (where available)",
273a74fa 59 "sets - grammar sets: firsts, nullable etc.",
1509d42f 60 "time - time consumption",
273a74fa
AD
61 "tools - m4 invocation and preserve the temporary file",
62 "all - all of the above",
63 0
64};
65
66static const int trace_types[] =
67{
68 trace_none,
69 trace_automaton,
70 trace_bitsets,
71 trace_grammar,
72 trace_resource,
73 trace_sets,
1509d42f 74 trace_time,
273a74fa
AD
75 trace_tools,
76 trace_all
77};
78
79
80static void
81trace_argmatch (char *args)
82{
83 ARGMATCH_ASSERT (trace_args, trace_types);
84 if (args)
85 {
86 args = strtok (args, ",");
87 do
88 {
89 int trace = XARGMATCH ("--trace", args,
90 trace_args, trace_types);
91 if (trace == trace_none)
92 trace_flag = trace_none;
93 else
94 trace_flag |= trace;
95 }
96 while ((args = strtok (NULL, ",")));
97 }
98 else
99 trace_flag = trace_all;
100}
101
102
ec3bc396
AD
103/*----------------------.
104| --report's handling. |
105`----------------------*/
106
107static const char * const report_args[] =
108{
109 /* In a series of synonyms, present the most meaningful first, so
110 that argmatch_valid be more readable. */
111 "none",
112 "state", "states",
113 "itemset", "itemsets",
114 "lookahead", "lookaheads",
b408954b 115 "solved",
ec3bc396
AD
116 "all",
117 0
118};
119
120static const int report_types[] =
121{
122 report_none,
123 report_states, report_states,
124 report_states | report_itemsets, report_states | report_itemsets,
125 report_states | report_lookaheads, report_states | report_lookaheads,
b408954b 126 report_states | report_solved_conflicts,
ec3bc396
AD
127 report_all
128};
129
130
131static void
132report_argmatch (char *args)
133{
134 ARGMATCH_ASSERT (report_args, report_types);
9be0c25b 135 args = strtok (args, ",");
ec3bc396
AD
136 do
137 {
138 int report = XARGMATCH ("--report", args,
139 report_args, report_types);
140 if (report == report_none)
141 report_flag = report_none;
142 else
143 report_flag |= report;
144 }
145 while ((args = strtok (NULL, ",")));
146}
147
9f306f2a
AD
148/*---------------------------.
149| Display the help message. |
150`---------------------------*/
e79137ac 151
4a120d45 152static void
d2729d44 153usage (FILE *stream)
cbd8ffc5 154{
9f306f2a
AD
155 /* Some efforts were made to ease the translators' task, please
156 continue. */
a0f6b076
AD
157 fputs (_("\
158GNU bison generates parsers for LALR(1) grammars.\n"), stream);
9f306f2a
AD
159 putc ('\n', stream);
160
161 fprintf (stream, _("\
162Usage: %s [OPTION]... FILE\n"), program_name);
163 putc ('\n', stream);
164
165 fputs (_("\
166If a long option shows an argument as mandatory, then it is mandatory\n\
167for the equivalent short option also. Similarly for optional arguments.\n"),
168 stream);
169 putc ('\n', stream);
170
a0f6b076 171 fputs (_("\
9f306f2a
AD
172Operation modes:\n\
173 -h, --help display this help and exit\n\
174 -V, --version output version information and exit\n\
a0f6b076 175 -y, --yacc emulate POSIX yacc\n"), stream);
9f306f2a
AD
176 putc ('\n', stream);
177
a0f6b076 178 fputs (_("\
9f306f2a 179Parser:\n\
cd5bd6ac 180 -S, --skeleton=FILE specify the skeleton to use\n\
9f306f2a 181 -t, --debug instrument the parser for debugging\n\
89cab50d 182 --locations enable locations computation\n\
9f306f2a
AD
183 -p, --name-prefix=PREFIX prepend PREFIX to the external symbols\n\
184 -l, --no-lines don't generate `#line' directives\n\
185 -n, --no-parser generate the tables only\n\
9f306f2a 186 -k, --token-table include a table of token names\n\
a0f6b076 187"), stream);
9f306f2a
AD
188 putc ('\n', stream);
189
a0f6b076 190 fputs (_("\
9f306f2a
AD
191Output:\n\
192 -d, --defines also produce a header file\n\
ec3bc396
AD
193 -r, --report=THINGS also produce details on the automaton\n\
194 -v, --verbose same as `--report=state'\n\
9f306f2a 195 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
951366c1
AD
196 -o, --output=FILE leave output to FILE\n\
197 -g, --graph also produce a VCG description of the automaton\n\
86eff183
AD
198"), stream);
199 putc ('\n', stream);
200
201 fputs (_("\
ec3bc396
AD
202THINGS is a list of comma separated words that can include:\n\
203 `state' describe the states\n\
204 `itemset' complete the core item sets with their closure\n\
205 `lookahead' explicitly associate lookaheads to items\n\
b408954b 206 `solved' describe shift/reduce conflicts solving\n\
ec3bc396
AD
207 `all' include all the above information\n\
208 `none' disable the report\n\
951366c1 209"), stream);
9f306f2a
AD
210 putc ('\n', stream);
211
a0f6b076
AD
212 fputs (_("\
213Report bugs to <bug-bison@gnu.org>.\n"), stream);
cbd8ffc5
DM
214}
215
e79137ac
AD
216
217/*------------------------------.
218| Display the version message. |
219`------------------------------*/
220
221static void
222version (FILE *stream)
223{
224 /* Some efforts were made to ease the translators' task, please
225 continue. */
06b00abc 226 fprintf (stream, _("bison (GNU Bison) %s"), VERSION);
e79137ac 227 putc ('\n', stream);
06b00abc 228 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stream);
e79137ac
AD
229 putc ('\n', stream);
230
06b00abc
AD
231 fprintf (stream,
232 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2002);
e79137ac
AD
233
234 fputs (_("\
235This is free software; see the source for copying conditions. There is NO\n\
236warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
237"),
238 stream);
239}
240
241
242/*----------------------.
243| Process the options. |
244`----------------------*/
245
e2aaf4c4 246/* Shorts options. */
273a74fa 247const char *short_options = "yvegdhr:ltknVo:b:p:S:T::";
e2aaf4c4
AD
248
249static struct option const long_options[] =
250{
251 /* Operation modes. */
252 { "help", no_argument, 0, 'h' },
253 { "version", no_argument, 0, 'V' },
254
255 /* Parser. */
256 { "name-prefix", required_argument, 0, 'p' },
257 { "include", required_argument, 0, 'I' },
258
259 /* Output. */
260 { "file-prefix", required_argument, 0, 'b' },
261 { "output", required_argument, 0, 'o' },
262 { "output-file", required_argument, 0, 'o' },
263 { "graph", optional_argument, 0, 'g' },
264 { "report", required_argument, 0, 'r' },
265 { "verbose", no_argument, 0, 'v' },
266
267 /* Hidden. */
273a74fa 268 { "trace", optional_argument, 0, 'T' },
e2aaf4c4
AD
269
270 /* FIXME: semantic parsers will output an `include' of an
271 output file: be sure that the naem included is indeed the name of
272 the output file. */ /* FIXME Should we activate this options ?
273 */
274 { "output", required_argument, 0, 'o' },
275 { "file-prefix", required_argument, 0, 'b' },
276 { "name-prefix", required_argument, 0, 'p' },
277
278 /*
279 * Percent and command line declarations.
280 */
281
282 /* Output. */
283 { "defines", optional_argument, 0, 'd' },
284
285 /* Operation modes. */
286 { "fixed-output-files", no_argument, 0, 'y' },
287 { "yacc", no_argument, 0, 'y' },
288
289 /* Parser. */
290 { "debug", no_argument, 0, 't' },
291 { "locations", no_argument, &locations_flag, 1 },
292 { "no-lines", no_argument, 0, 'l' },
293 { "no-parser", no_argument, 0, 'n' },
294 { "raw", no_argument, 0, 0 },
295 { "skeleton", required_argument, 0, 'S' },
296 { "token-table", no_argument, 0, 'k' },
297
298 {0, 0, 0, 0}
299};
300
ae404801
AD
301/* Under DOS, there is no difference on the case. This can be
302 troublesome when looking for `.tab' etc. */
303#ifdef MSDOS
304# define AS_FILE_NAME(File) (strlwr (File), (File))
305#else
306# define AS_FILE_NAME(File) (File)
307#endif
308
3d8fc6ca 309void
d2729d44 310getargs (int argc, char *argv[])
3d8fc6ca 311{
1916f98e 312 int c;
3d8fc6ca 313
e2aaf4c4 314 while ((c = getopt_long (argc, argv, short_options, long_options, NULL)) != EOF)
cd5bd6ac
AD
315 switch (c)
316 {
317 case 0:
318 /* Certain long options cause getopt_long to return 0. */
319 break;
320
321 case 'y':
322 yacc_flag = 1;
323 break;
324
325 case 'h':
326 usage (stdout);
327 exit (0);
328
329 case 'V':
330 version (stdout);
331 exit (0);
332
22c2cbc0 333 case 'g':
342b8b6e 334 /* Here, the -g and --graph=FILE options are differentiated. */
22c2cbc0 335 graph_flag = 1;
ae404801 336 spec_graph_file = AS_FILE_NAME (optarg);
22c2cbc0
AD
337 break;
338
cd5bd6ac 339 case 'v':
ec3bc396 340 report_flag |= report_states;
cd5bd6ac
AD
341 break;
342
343 case 'S':
ae404801 344 skeleton = AS_FILE_NAME (optarg);
cd5bd6ac
AD
345 break;
346
f6bd5427 347 case 'I':
ae404801 348 include = AS_FILE_NAME (optarg);
f6bd5427
MA
349 break;
350
cd5bd6ac 351 case 'd':
342b8b6e 352 /* Here, the -d and --defines options are differentiated. */
cd5bd6ac 353 defines_flag = 1;
ae404801
AD
354 if (optarg)
355 spec_defines_file = AS_FILE_NAME (optarg);
cd5bd6ac
AD
356 break;
357
358 case 'l':
359 no_lines_flag = 1;
360 break;
361
362 case 'k':
363 token_table_flag = 1;
364 break;
365
cd5bd6ac
AD
366 case 'n':
367 no_parser_flag = 1;
368 break;
369
370 case 't':
371 debug_flag = 1;
372 break;
373
374 case 'o':
ae404801 375 spec_outfile = AS_FILE_NAME (optarg);
cd5bd6ac
AD
376 break;
377
378 case 'b':
ae404801 379 spec_file_prefix = AS_FILE_NAME (optarg);
cd5bd6ac
AD
380 break;
381
382 case 'p':
383 spec_name_prefix = optarg;
384 break;
385
ec3bc396
AD
386 case 'r':
387 report_argmatch (optarg);
388 break;
389
273a74fa
AD
390 case 'T':
391 trace_argmatch (optarg);
392 break;
393
cd5bd6ac
AD
394 default:
395 fprintf (stderr, _("Try `%s --help' for more information.\n"),
396 program_name);
397 exit (1);
398 }
3d8fc6ca
RS
399
400 if (optind == argc)
401 {
e79137ac
AD
402 fprintf (stderr, _("%s: no grammar file given\n"), program_name);
403 exit (1);
3d8fc6ca
RS
404 }
405 if (optind < argc - 1)
e79137ac
AD
406 fprintf (stderr, _("%s: extra arguments ignored after `%s'\n"),
407 program_name, argv[optind]);
3d8fc6ca
RS
408
409 infile = argv[optind];
410}