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