]> git.saurik.com Git - bison.git/blob - src/getargs.c
(grammar_current_rule_check):
[bison.git] / src / getargs.c
1 /* Parse command line arguments for bison.
2 Copyright 1984, 1986, 1989, 1992, 2000, 2001, 2002
3 Free Software Foundation, Inc.
4
5 This file is part of Bison, the GNU Compiler Compiler.
6
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.
11
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.
16
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. */
21
22 #include "system.h"
23 #include "getopt.h"
24 #include "argmatch.h"
25 #include "complain.h"
26 #include "getargs.h"
27 #include "files.h"
28
29 int debug_flag = 0;
30 int defines_flag = 0;
31 int locations_flag = 0;
32 int no_lines_flag = 0;
33 int no_parser_flag = 0;
34 int report_flag = report_none;
35 int token_table_flag = 0;
36 int yacc_flag = 0; /* for -y */
37 int graph_flag = 0;
38 int trace_flag = trace_none;
39
40 const char *skeleton = NULL;
41 const char *include = NULL;
42
43 extern char *program_name;
44
45
46 /*---------------------.
47 | --trace's handling. |
48 `---------------------*/
49
50 static 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",
58 "resource - memory consumption (where available)",
59 "sets - grammar sets: firsts, nullable etc.",
60 "time - time consumption",
61 "tools - m4 invocation and preserve the temporary file",
62 "all - all of the above",
63 0
64 };
65
66 static const int trace_types[] =
67 {
68 trace_none,
69 trace_automaton,
70 trace_bitsets,
71 trace_grammar,
72 trace_resource,
73 trace_sets,
74 trace_time,
75 trace_tools,
76 trace_all
77 };
78
79
80 static void
81 trace_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
103 /*----------------------.
104 | --report's handling. |
105 `----------------------*/
106
107 static 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",
115 "solved",
116 "all",
117 0
118 };
119
120 static 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,
126 report_states | report_solved_conflicts,
127 report_all
128 };
129
130
131 static void
132 report_argmatch (char *args)
133 {
134 ARGMATCH_ASSERT (report_args, report_types);
135 args = strtok (args, ",");
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
148 /*---------------------------.
149 | Display the help message. |
150 `---------------------------*/
151
152 static void
153 usage (FILE *stream)
154 {
155 /* Some efforts were made to ease the translators' task, please
156 continue. */
157 fputs (_("\
158 GNU bison generates parsers for LALR(1) grammars.\n"), stream);
159 putc ('\n', stream);
160
161 fprintf (stream, _("\
162 Usage: %s [OPTION]... FILE\n"), program_name);
163 putc ('\n', stream);
164
165 fputs (_("\
166 If a long option shows an argument as mandatory, then it is mandatory\n\
167 for the equivalent short option also. Similarly for optional arguments.\n"),
168 stream);
169 putc ('\n', stream);
170
171 fputs (_("\
172 Operation modes:\n\
173 -h, --help display this help and exit\n\
174 -V, --version output version information and exit\n\
175 -y, --yacc emulate POSIX yacc\n"), stream);
176 putc ('\n', stream);
177
178 fputs (_("\
179 Parser:\n\
180 -S, --skeleton=FILE specify the skeleton to use\n\
181 -t, --debug instrument the parser for debugging\n\
182 --locations enable locations computation\n\
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\
186 -k, --token-table include a table of token names\n\
187 "), stream);
188 putc ('\n', stream);
189
190 fputs (_("\
191 Output:\n\
192 -d, --defines also produce a header file\n\
193 -r, --report=THINGS also produce details on the automaton\n\
194 -v, --verbose same as `--report=state'\n\
195 -b, --file-prefix=PREFIX specify a PREFIX for output files\n\
196 -o, --output=FILE leave output to FILE\n\
197 -g, --graph also produce a VCG description of the automaton\n\
198 "), stream);
199 putc ('\n', stream);
200
201 fputs (_("\
202 THINGS 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\
206 `solved' describe shift/reduce conflicts solving\n\
207 `all' include all the above information\n\
208 `none' disable the report\n\
209 "), stream);
210 putc ('\n', stream);
211
212 fputs (_("\
213 Report bugs to <bug-bison@gnu.org>.\n"), stream);
214 }
215
216
217 /*------------------------------.
218 | Display the version message. |
219 `------------------------------*/
220
221 static void
222 version (FILE *stream)
223 {
224 /* Some efforts were made to ease the translators' task, please
225 continue. */
226 fprintf (stream, _("bison (GNU Bison) %s"), VERSION);
227 putc ('\n', stream);
228 fputs (_("Written by Robert Corbett and Richard Stallman.\n"), stream);
229 putc ('\n', stream);
230
231 fprintf (stream,
232 _("Copyright (C) %d Free Software Foundation, Inc.\n"), 2002);
233
234 fputs (_("\
235 This is free software; see the source for copying conditions. There is NO\n\
236 warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n\
237 "),
238 stream);
239 }
240
241
242 /*----------------------.
243 | Process the options. |
244 `----------------------*/
245
246 /* Shorts options. */
247 const char *short_options = "yvegdhr:ltknVo:b:p:S:T::";
248
249 static 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. */
268 { "trace", optional_argument, 0, 'T' },
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
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
309 void
310 getargs (int argc, char *argv[])
311 {
312 int c;
313
314 while ((c = getopt_long (argc, argv, short_options, long_options, NULL)) != EOF)
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
333 case 'g':
334 /* Here, the -g and --graph=FILE options are differentiated. */
335 graph_flag = 1;
336 spec_graph_file = AS_FILE_NAME (optarg);
337 break;
338
339 case 'v':
340 report_flag |= report_states;
341 break;
342
343 case 'S':
344 skeleton = AS_FILE_NAME (optarg);
345 break;
346
347 case 'I':
348 include = AS_FILE_NAME (optarg);
349 break;
350
351 case 'd':
352 /* Here, the -d and --defines options are differentiated. */
353 defines_flag = 1;
354 if (optarg)
355 spec_defines_file = AS_FILE_NAME (optarg);
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
366 case 'n':
367 no_parser_flag = 1;
368 break;
369
370 case 't':
371 debug_flag = 1;
372 break;
373
374 case 'o':
375 spec_outfile = AS_FILE_NAME (optarg);
376 break;
377
378 case 'b':
379 spec_file_prefix = AS_FILE_NAME (optarg);
380 break;
381
382 case 'p':
383 spec_name_prefix = optarg;
384 break;
385
386 case 'r':
387 report_argmatch (optarg);
388 break;
389
390 case 'T':
391 trace_argmatch (optarg);
392 break;
393
394 default:
395 fprintf (stderr, _("Try `%s --help' for more information.\n"),
396 program_name);
397 exit (1);
398 }
399
400 if (optind == argc)
401 {
402 fprintf (stderr, _("%s: no grammar file given\n"), program_name);
403 exit (1);
404 }
405 if (optind < argc - 1)
406 fprintf (stderr, _("%s: extra arguments ignored after `%s'\n"),
407 program_name, argv[optind]);
408
409 infile = argv[optind];
410 }