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