]>
Commit | Line | Data |
---|---|---|
1 | %{/* Bison Grammar Parser -*- C -*- | |
2 | ||
3 | Copyright (C) 2002-2012 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of Bison, the GNU Compiler Compiler. | |
6 | ||
7 | This program is free software: you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | This program is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include <config.h> | |
21 | #include "system.h" | |
22 | ||
23 | #include "complain.h" | |
24 | #include "conflicts.h" | |
25 | #include "files.h" | |
26 | #include "getargs.h" | |
27 | #include "gram.h" | |
28 | #include "muscle-tab.h" | |
29 | #include "named-ref.h" | |
30 | #include "quotearg.h" | |
31 | #include "reader.h" | |
32 | #include "symlist.h" | |
33 | #include "scan-gram.h" | |
34 | #include "scan-code.h" | |
35 | #include "xmemdup0.h" | |
36 | ||
37 | #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N) | |
38 | static YYLTYPE lloc_default (YYLTYPE const *, int); | |
39 | ||
40 | #define YY_LOCATION_PRINT(File, Loc) \ | |
41 | location_print (File, Loc) | |
42 | ||
43 | static void version_check (location const *loc, char const *version); | |
44 | ||
45 | /* Request detailed syntax error messages, and pass them to GRAM_ERROR. | |
46 | FIXME: depends on the undocumented availability of YYLLOC. */ | |
47 | #undef yyerror | |
48 | #define yyerror(Msg) \ | |
49 | gram_error (&yylloc, Msg) | |
50 | static void gram_error (location const *, char const *); | |
51 | ||
52 | static char const *char_name (char); | |
53 | ||
54 | /** Add a lex-param or a parse-param. | |
55 | * | |
56 | * \param type \a lex_param or \a parse_param | |
57 | * \param decl the formal argument | |
58 | * \param loc the location in the source. | |
59 | */ | |
60 | static void add_param (char const *type, char *decl, location loc); | |
61 | ||
62 | ||
63 | static symbol_class current_class = unknown_sym; | |
64 | static uniqstr current_type = NULL; | |
65 | static symbol *current_lhs_symbol; | |
66 | static location current_lhs_location; | |
67 | static named_ref *current_lhs_named_ref; | |
68 | static int current_prec = 0; | |
69 | ||
70 | /** Set the new current left-hand side symbol, possibly common | |
71 | * to several right-hand side parts of rule. | |
72 | */ | |
73 | static | |
74 | void | |
75 | current_lhs(symbol *sym, location loc, named_ref *ref) | |
76 | { | |
77 | current_lhs_symbol = sym; | |
78 | current_lhs_location = loc; | |
79 | /* In order to simplify memory management, named references for lhs | |
80 | are always assigned by deep copy into the current symbol_list | |
81 | node. This is because a single named-ref in the grammar may | |
82 | result in several uses when the user factors lhs between several | |
83 | rules using "|". Therefore free the parser's original copy. */ | |
84 | free (current_lhs_named_ref); | |
85 | current_lhs_named_ref = ref; | |
86 | } | |
87 | ||
88 | ||
89 | #define YYTYPE_INT16 int_fast16_t | |
90 | #define YYTYPE_INT8 int_fast8_t | |
91 | #define YYTYPE_UINT16 uint_fast16_t | |
92 | #define YYTYPE_UINT8 uint_fast8_t | |
93 | %} | |
94 | ||
95 | %debug | |
96 | %verbose | |
97 | %defines | |
98 | %locations | |
99 | %pure-parser | |
100 | %error-verbose | |
101 | %define parse.lac full | |
102 | %name-prefix="gram_" | |
103 | %expect 0 | |
104 | ||
105 | %initial-action | |
106 | { | |
107 | /* Bison's grammar can initial empty locations, hence a default | |
108 | location is needed. */ | |
109 | boundary_set (&@$.start, current_file, 1, 1); | |
110 | boundary_set (&@$.end, current_file, 1, 1); | |
111 | } | |
112 | ||
113 | %union | |
114 | { | |
115 | symbol *symbol; | |
116 | symbol_list *list; | |
117 | int integer; | |
118 | char const *chars; | |
119 | char *code; | |
120 | assoc assoc; | |
121 | uniqstr uniqstr; | |
122 | unsigned char character; | |
123 | named_ref *named_ref; | |
124 | }; | |
125 | ||
126 | /* Define the tokens together with their human representation. */ | |
127 | %token GRAM_EOF 0 "end of file" | |
128 | %token STRING "string" | |
129 | %token INT "integer" | |
130 | ||
131 | %token PERCENT_TOKEN "%token" | |
132 | %token PERCENT_NTERM "%nterm" | |
133 | ||
134 | %token PERCENT_TYPE "%type" | |
135 | %token PERCENT_DESTRUCTOR "%destructor" | |
136 | %token PERCENT_PRINTER "%printer" | |
137 | ||
138 | %token PERCENT_LEFT "%left" | |
139 | %token PERCENT_RIGHT "%right" | |
140 | %token PERCENT_NONASSOC "%nonassoc" | |
141 | ||
142 | %token PERCENT_PREC "%prec" | |
143 | %token PERCENT_DPREC "%dprec" | |
144 | %token PERCENT_MERGE "%merge" | |
145 | ||
146 | ||
147 | /*----------------------. | |
148 | | Global Declarations. | | |
149 | `----------------------*/ | |
150 | ||
151 | %token | |
152 | PERCENT_CODE "%code" | |
153 | PERCENT_DEBUG "%debug" | |
154 | PERCENT_DEFAULT_PREC "%default-prec" | |
155 | PERCENT_DEFINE "%define" | |
156 | PERCENT_DEFINES "%defines" | |
157 | PERCENT_ERROR_VERBOSE "%error-verbose" | |
158 | PERCENT_EXPECT "%expect" | |
159 | PERCENT_EXPECT_RR "%expect-rr" | |
160 | PERCENT_FILE_PREFIX "%file-prefix" | |
161 | PERCENT_GLR_PARSER "%glr-parser" | |
162 | PERCENT_INITIAL_ACTION "%initial-action" | |
163 | PERCENT_LANGUAGE "%language" | |
164 | PERCENT_LEX_PARAM "%lex-param" | |
165 | PERCENT_LOCATIONS "%locations" | |
166 | PERCENT_NAME_PREFIX "%name-prefix" | |
167 | PERCENT_NO_DEFAULT_PREC "%no-default-prec" | |
168 | PERCENT_NO_LINES "%no-lines" | |
169 | PERCENT_NONDETERMINISTIC_PARSER | |
170 | "%nondeterministic-parser" | |
171 | PERCENT_OUTPUT "%output" | |
172 | PERCENT_PARSE_PARAM "%parse-param" | |
173 | PERCENT_PURE_PARSER "%pure-parser" | |
174 | PERCENT_REQUIRE "%require" | |
175 | PERCENT_SKELETON "%skeleton" | |
176 | PERCENT_START "%start" | |
177 | PERCENT_TOKEN_TABLE "%token-table" | |
178 | PERCENT_VERBOSE "%verbose" | |
179 | PERCENT_YACC "%yacc" | |
180 | ; | |
181 | ||
182 | %token BRACED_CODE "{...}" | |
183 | %token BRACKETED_ID "[identifier]" | |
184 | %token CHAR "char" | |
185 | %token EPILOGUE "epilogue" | |
186 | %token EQUAL "=" | |
187 | %token ID "identifier" | |
188 | %token ID_COLON "identifier:" | |
189 | %token PERCENT_PERCENT "%%" | |
190 | %token PIPE "|" | |
191 | %token PROLOGUE "%{...%}" | |
192 | %token SEMICOLON ";" | |
193 | %token TYPE "type" | |
194 | %token TYPE_TAG_ANY "<*>" | |
195 | %token TYPE_TAG_NONE "<>" | |
196 | ||
197 | %type <character> CHAR | |
198 | %printer { fputs (char_name ($$), stderr); } CHAR | |
199 | ||
200 | /* braceless is not to be used for rule or symbol actions, as it | |
201 | calls code_props_plain_init. */ | |
202 | %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt | |
203 | %type <code> "{...}" | |
204 | %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); } | |
205 | STRING | |
206 | %printer { fprintf (stderr, "{\n%s\n}", $$); } | |
207 | braceless content.opt "{...}" "%{...%}" EPILOGUE | |
208 | ||
209 | %type <uniqstr> BRACKETED_ID ID ID_COLON TYPE variable | |
210 | %printer { fputs ($$, stderr); } <uniqstr> | |
211 | %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID | |
212 | %printer { fprintf (stderr, "%s:", $$); } ID_COLON | |
213 | %printer { fprintf (stderr, "<%s>", $$); } TYPE | |
214 | ||
215 | %type <integer> INT | |
216 | %printer { fprintf (stderr, "%d", $$); } <integer> | |
217 | ||
218 | %type <symbol> id id_colon string_as_id symbol symbol.prec | |
219 | %printer { fprintf (stderr, "%s", $$->tag); } <symbol> | |
220 | %printer { fprintf (stderr, "%s:", $$->tag); } id_colon | |
221 | ||
222 | %type <assoc> precedence_declarator | |
223 | %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item | |
224 | %type <named_ref> named_ref.opt | |
225 | ||
226 | %% | |
227 | ||
228 | input: | |
229 | prologue_declarations "%%" grammar epilogue.opt | |
230 | ; | |
231 | ||
232 | ||
233 | /*------------------------------------. | |
234 | | Declarations: before the first %%. | | |
235 | `------------------------------------*/ | |
236 | ||
237 | prologue_declarations: | |
238 | /* Nothing */ | |
239 | | prologue_declarations prologue_declaration | |
240 | ; | |
241 | ||
242 | prologue_declaration: | |
243 | grammar_declaration | |
244 | | "%{...%}" | |
245 | { | |
246 | code_props plain_code; | |
247 | code_props_plain_init (&plain_code, $1, @1); | |
248 | code_props_translate_code (&plain_code); | |
249 | gram_scanner_last_string_free (); | |
250 | muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue", | |
251 | plain_code.code, @1); | |
252 | code_scanner_last_string_free (); | |
253 | } | |
254 | | "%debug" { debug_flag = true; } | |
255 | | "%define" variable content.opt | |
256 | { | |
257 | muscle_percent_define_insert ($2, @2, $3, | |
258 | MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE); | |
259 | } | |
260 | | "%defines" { defines_flag = true; } | |
261 | | "%defines" STRING | |
262 | { | |
263 | defines_flag = true; | |
264 | spec_defines_file = xstrdup ($2); | |
265 | } | |
266 | | "%error-verbose" { error_verbose = true; } | |
267 | | "%expect" INT { expected_sr_conflicts = $2; } | |
268 | | "%expect-rr" INT { expected_rr_conflicts = $2; } | |
269 | | "%file-prefix" STRING { spec_file_prefix = $2; } | |
270 | | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */ | |
271 | | "%glr-parser" | |
272 | { | |
273 | nondeterministic_parser = true; | |
274 | glr_parser = true; | |
275 | } | |
276 | | "%initial-action" "{...}" | |
277 | { | |
278 | code_props action; | |
279 | code_props_symbol_action_init (&action, $2, @2); | |
280 | code_props_translate_code (&action); | |
281 | gram_scanner_last_string_free (); | |
282 | muscle_code_grow ("initial_action", action.code, @2); | |
283 | code_scanner_last_string_free (); | |
284 | } | |
285 | | "%language" STRING { language_argmatch ($2, grammar_prio, @1); } | |
286 | | "%lex-param" "{...}" { add_param ("lex_param", $2, @2); } | |
287 | | "%locations" { locations_flag = true; } | |
288 | | "%name-prefix" STRING { spec_name_prefix = $2; } | |
289 | | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */ | |
290 | | "%no-lines" { no_lines_flag = true; } | |
291 | | "%nondeterministic-parser" { nondeterministic_parser = true; } | |
292 | | "%output" STRING { spec_outfile = $2; } | |
293 | | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */ | |
294 | | "%parse-param" "{...}" { add_param ("parse_param", $2, @2); } | |
295 | | "%pure-parser" | |
296 | { | |
297 | /* %pure-parser is deprecated in favor of `%define api.pure', so use | |
298 | `%define api.pure' in a backward-compatible manner here. First, don't | |
299 | complain if %pure-parser is specified multiple times. */ | |
300 | if (!muscle_find_const ("percent_define(api.pure)")) | |
301 | muscle_percent_define_insert ("api.pure", @1, "", | |
302 | MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE); | |
303 | /* In all cases, use api.pure now so that the backend doesn't complain if | |
304 | the skeleton ignores api.pure, but do warn now if there's a previous | |
305 | conflicting definition from an actual %define. */ | |
306 | if (!muscle_percent_define_flag_if ("api.pure")) | |
307 | muscle_percent_define_insert ("api.pure", @1, "", | |
308 | MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE); | |
309 | } | |
310 | | "%require" STRING { version_check (&@2, $2); } | |
311 | | "%skeleton" STRING | |
312 | { | |
313 | char const *skeleton_user = $2; | |
314 | if (mbschr (skeleton_user, '/')) | |
315 | { | |
316 | size_t dir_length = strlen (current_file); | |
317 | char *skeleton_build; | |
318 | while (dir_length && current_file[dir_length - 1] != '/') | |
319 | --dir_length; | |
320 | while (dir_length && current_file[dir_length - 1] == '/') | |
321 | --dir_length; | |
322 | skeleton_build = | |
323 | xmalloc (dir_length + 1 + strlen (skeleton_user) + 1); | |
324 | if (dir_length > 0) | |
325 | { | |
326 | memcpy (skeleton_build, current_file, dir_length); | |
327 | skeleton_build[dir_length++] = '/'; | |
328 | } | |
329 | strcpy (skeleton_build + dir_length, skeleton_user); | |
330 | skeleton_user = uniqstr_new (skeleton_build); | |
331 | free (skeleton_build); | |
332 | } | |
333 | skeleton_arg (skeleton_user, grammar_prio, @1); | |
334 | } | |
335 | | "%token-table" { token_table_flag = true; } | |
336 | | "%verbose" { report_flag |= report_states; } | |
337 | | "%yacc" { yacc_flag = true; } | |
338 | | /*FIXME: Err? What is this horror doing here? */ ";" | |
339 | ; | |
340 | ||
341 | grammar_declaration: | |
342 | precedence_declaration | |
343 | | symbol_declaration | |
344 | | "%start" symbol | |
345 | { | |
346 | grammar_start_symbol_set ($2, @2); | |
347 | } | |
348 | | "%destructor" "{...}" generic_symlist | |
349 | { | |
350 | symbol_list *list; | |
351 | for (list = $3; list; list = list->next) | |
352 | symbol_list_destructor_set (list, $2, @2); | |
353 | symbol_list_free ($3); | |
354 | } | |
355 | | "%printer" "{...}" generic_symlist | |
356 | { | |
357 | symbol_list *list; | |
358 | for (list = $3; list; list = list->next) | |
359 | symbol_list_printer_set (list, $2, @2); | |
360 | symbol_list_free ($3); | |
361 | } | |
362 | | "%default-prec" | |
363 | { | |
364 | default_prec = true; | |
365 | } | |
366 | | "%no-default-prec" | |
367 | { | |
368 | default_prec = false; | |
369 | } | |
370 | | "%code" braceless | |
371 | { | |
372 | /* Do not invoke muscle_percent_code_grow here since it invokes | |
373 | muscle_user_name_list_grow. */ | |
374 | muscle_code_grow ("percent_code()", $2, @2); | |
375 | code_scanner_last_string_free (); | |
376 | } | |
377 | | "%code" ID braceless | |
378 | { | |
379 | muscle_percent_code_grow ($2, @2, $3, @3); | |
380 | code_scanner_last_string_free (); | |
381 | } | |
382 | ; | |
383 | ||
384 | ||
385 | /*----------* | |
386 | | %union. | | |
387 | *----------*/ | |
388 | ||
389 | %token PERCENT_UNION "%union"; | |
390 | ||
391 | union_name: | |
392 | /* Nothing. */ {} | |
393 | | ID { muscle_code_grow ("union_name", $1, @1); } | |
394 | ; | |
395 | ||
396 | grammar_declaration: | |
397 | "%union" union_name braceless | |
398 | { | |
399 | union_seen = true; | |
400 | muscle_code_grow ("stype", $3, @3); | |
401 | code_scanner_last_string_free (); | |
402 | } | |
403 | ; | |
404 | ||
405 | ||
406 | ||
407 | ||
408 | symbol_declaration: | |
409 | "%nterm" { current_class = nterm_sym; } symbol_defs.1 | |
410 | { | |
411 | current_class = unknown_sym; | |
412 | current_type = NULL; | |
413 | } | |
414 | | "%token" { current_class = token_sym; } symbol_defs.1 | |
415 | { | |
416 | current_class = unknown_sym; | |
417 | current_type = NULL; | |
418 | } | |
419 | | "%type" TYPE symbols.1 | |
420 | { | |
421 | symbol_list *list; | |
422 | tag_seen = true; | |
423 | for (list = $3; list; list = list->next) | |
424 | symbol_type_set (list->content.sym, $2, @2); | |
425 | symbol_list_free ($3); | |
426 | } | |
427 | ; | |
428 | ||
429 | precedence_declaration: | |
430 | precedence_declarator type.opt symbols.prec | |
431 | { | |
432 | symbol_list *list; | |
433 | ++current_prec; | |
434 | for (list = $3; list; list = list->next) | |
435 | { | |
436 | symbol_type_set (list->content.sym, current_type, @2); | |
437 | symbol_precedence_set (list->content.sym, current_prec, $1, @1); | |
438 | } | |
439 | symbol_list_free ($3); | |
440 | current_type = NULL; | |
441 | } | |
442 | ; | |
443 | ||
444 | precedence_declarator: | |
445 | "%left" { $$ = left_assoc; } | |
446 | | "%right" { $$ = right_assoc; } | |
447 | | "%nonassoc" { $$ = non_assoc; } | |
448 | ; | |
449 | ||
450 | type.opt: | |
451 | /* Nothing. */ { current_type = NULL; } | |
452 | | TYPE { current_type = $1; tag_seen = true; } | |
453 | ; | |
454 | ||
455 | /* Just like symbols.1 but accept INT for the sake of POSIX. */ | |
456 | symbols.prec: | |
457 | symbol.prec | |
458 | { $$ = symbol_list_sym_new ($1, @1); } | |
459 | | symbols.prec symbol.prec | |
460 | { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); } | |
461 | ; | |
462 | ||
463 | symbol.prec: | |
464 | symbol { $$ = $1; } | |
465 | | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); } | |
466 | ; | |
467 | ||
468 | /* One or more symbols to be %typed. */ | |
469 | symbols.1: | |
470 | symbol | |
471 | { $$ = symbol_list_sym_new ($1, @1); } | |
472 | | symbols.1 symbol | |
473 | { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); } | |
474 | ; | |
475 | ||
476 | generic_symlist: | |
477 | generic_symlist_item { $$ = $1; } | |
478 | | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); } | |
479 | ; | |
480 | ||
481 | generic_symlist_item: | |
482 | symbol { $$ = symbol_list_sym_new ($1, @1); } | |
483 | | TYPE { $$ = symbol_list_type_new ($1, @1); } | |
484 | | "<*>" { $$ = symbol_list_default_tagged_new (@1); } | |
485 | | "<>" { $$ = symbol_list_default_tagless_new (@1); } | |
486 | ; | |
487 | ||
488 | /* One token definition. */ | |
489 | symbol_def: | |
490 | TYPE | |
491 | { | |
492 | current_type = $1; | |
493 | tag_seen = true; | |
494 | } | |
495 | | id | |
496 | { | |
497 | symbol_class_set ($1, current_class, @1, true); | |
498 | symbol_type_set ($1, current_type, @1); | |
499 | } | |
500 | | id INT | |
501 | { | |
502 | symbol_class_set ($1, current_class, @1, true); | |
503 | symbol_type_set ($1, current_type, @1); | |
504 | symbol_user_token_number_set ($1, $2, @2); | |
505 | } | |
506 | | id string_as_id | |
507 | { | |
508 | symbol_class_set ($1, current_class, @1, true); | |
509 | symbol_type_set ($1, current_type, @1); | |
510 | symbol_make_alias ($1, $2, @$); | |
511 | } | |
512 | | id INT string_as_id | |
513 | { | |
514 | symbol_class_set ($1, current_class, @1, true); | |
515 | symbol_type_set ($1, current_type, @1); | |
516 | symbol_user_token_number_set ($1, $2, @2); | |
517 | symbol_make_alias ($1, $3, @$); | |
518 | } | |
519 | ; | |
520 | ||
521 | /* One or more symbol definitions. */ | |
522 | symbol_defs.1: | |
523 | symbol_def | |
524 | | symbol_defs.1 symbol_def | |
525 | ; | |
526 | ||
527 | ||
528 | /*------------------------------------------. | |
529 | | The grammar section: between the two %%. | | |
530 | `------------------------------------------*/ | |
531 | ||
532 | grammar: | |
533 | rules_or_grammar_declaration | |
534 | | grammar rules_or_grammar_declaration | |
535 | ; | |
536 | ||
537 | /* As a Bison extension, one can use the grammar declarations in the | |
538 | body of the grammar. */ | |
539 | rules_or_grammar_declaration: | |
540 | rules | |
541 | | grammar_declaration ";" | |
542 | | error ";" | |
543 | { | |
544 | yyerrok; | |
545 | } | |
546 | ; | |
547 | ||
548 | rules: | |
549 | id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1 | |
550 | { | |
551 | /* Free the current lhs. */ | |
552 | current_lhs (0, @1, 0); | |
553 | } | |
554 | ; | |
555 | ||
556 | rhses.1: | |
557 | rhs { grammar_current_rule_end (@1); } | |
558 | | rhses.1 "|" rhs { grammar_current_rule_end (@3); } | |
559 | | rhses.1 ";" | |
560 | ; | |
561 | ||
562 | rhs: | |
563 | /* Nothing. */ | |
564 | { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location, | |
565 | current_lhs_named_ref); } | |
566 | | rhs symbol named_ref.opt | |
567 | { grammar_current_rule_symbol_append ($2, @2, $3); } | |
568 | | rhs "{...}" named_ref.opt | |
569 | { grammar_current_rule_action_append ($2, @2, $3); } | |
570 | | rhs "%prec" symbol | |
571 | { grammar_current_rule_prec_set ($3, @3); } | |
572 | | rhs "%dprec" INT | |
573 | { grammar_current_rule_dprec_set ($3, @3); } | |
574 | | rhs "%merge" TYPE | |
575 | { grammar_current_rule_merge_set ($3, @3); } | |
576 | ; | |
577 | ||
578 | named_ref.opt: | |
579 | /* Nothing. */ { $$ = 0; } | |
580 | | | |
581 | BRACKETED_ID { $$ = named_ref_new($1, @1); } | |
582 | ; | |
583 | ||
584 | ||
585 | /*----------------------------* | |
586 | | variable and content.opt. | | |
587 | *---------------------------*/ | |
588 | ||
589 | /* The STRING form of variable is deprecated and is not M4-friendly. | |
590 | For example, M4 fails for `%define "[" "value"'. */ | |
591 | variable: | |
592 | ID | |
593 | | STRING { $$ = uniqstr_new ($1); } | |
594 | ; | |
595 | ||
596 | /* Some content or empty by default. */ | |
597 | content.opt: | |
598 | /* Nothing. */ { $$ = ""; } | |
599 | | ID { $$ = $1; } | |
600 | | STRING | |
601 | ; | |
602 | ||
603 | ||
604 | /*-------------* | |
605 | | braceless. | | |
606 | *-------------*/ | |
607 | ||
608 | braceless: | |
609 | "{...}" | |
610 | { | |
611 | code_props plain_code; | |
612 | $1[strlen ($1) - 1] = '\n'; | |
613 | code_props_plain_init (&plain_code, $1+1, @1); | |
614 | code_props_translate_code (&plain_code); | |
615 | gram_scanner_last_string_free (); | |
616 | $$ = plain_code.code; | |
617 | } | |
618 | ; | |
619 | ||
620 | ||
621 | /*---------------* | |
622 | | Identifiers. | | |
623 | *---------------*/ | |
624 | ||
625 | /* Identifiers are returned as uniqstr values by the scanner. | |
626 | Depending on their use, we may need to make them genuine symbols. */ | |
627 | ||
628 | id: | |
629 | ID | |
630 | { $$ = symbol_from_uniqstr ($1, @1); } | |
631 | | CHAR | |
632 | { | |
633 | $$ = symbol_get (char_name ($1), @1); | |
634 | symbol_class_set ($$, token_sym, @1, false); | |
635 | symbol_user_token_number_set ($$, $1, @1); | |
636 | } | |
637 | ; | |
638 | ||
639 | id_colon: | |
640 | ID_COLON { $$ = symbol_from_uniqstr ($1, @1); } | |
641 | ; | |
642 | ||
643 | ||
644 | symbol: | |
645 | id | |
646 | | string_as_id | |
647 | ; | |
648 | ||
649 | /* A string used as an ID: quote it. */ | |
650 | string_as_id: | |
651 | STRING | |
652 | { | |
653 | $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1); | |
654 | symbol_class_set ($$, token_sym, @1, false); | |
655 | } | |
656 | ; | |
657 | ||
658 | epilogue.opt: | |
659 | /* Nothing. */ | |
660 | | "%%" EPILOGUE | |
661 | { | |
662 | code_props plain_code; | |
663 | code_props_plain_init (&plain_code, $2, @2); | |
664 | code_props_translate_code (&plain_code); | |
665 | gram_scanner_last_string_free (); | |
666 | muscle_code_grow ("epilogue", plain_code.code, @2); | |
667 | code_scanner_last_string_free (); | |
668 | } | |
669 | ; | |
670 | ||
671 | %% | |
672 | ||
673 | ||
674 | /* Return the location of the left-hand side of a rule whose | |
675 | right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in | |
676 | the right-hand side, and return an empty location equal to the end | |
677 | boundary of RHS[0] if the right-hand side is empty. */ | |
678 | ||
679 | static YYLTYPE | |
680 | lloc_default (YYLTYPE const *rhs, int n) | |
681 | { | |
682 | int i; | |
683 | YYLTYPE loc; | |
684 | ||
685 | /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;". | |
686 | The bug is fixed in 7.4.2m, but play it safe for now. */ | |
687 | loc.start = rhs[n].end; | |
688 | loc.end = rhs[n].end; | |
689 | ||
690 | /* Ignore empty nonterminals the start of the right-hand side. | |
691 | Do not bother to ignore them at the end of the right-hand side, | |
692 | since empty nonterminals have the same end as their predecessors. */ | |
693 | for (i = 1; i <= n; i++) | |
694 | if (! equal_boundaries (rhs[i].start, rhs[i].end)) | |
695 | { | |
696 | loc.start = rhs[i].start; | |
697 | break; | |
698 | } | |
699 | ||
700 | return loc; | |
701 | } | |
702 | ||
703 | ||
704 | /* Add a lex-param or a parse-param (depending on TYPE) with | |
705 | declaration DECL and location LOC. */ | |
706 | ||
707 | static void | |
708 | add_param (char const *type, char *decl, location loc) | |
709 | { | |
710 | static char const alphanum[26 + 26 + 1 + 10] = | |
711 | "abcdefghijklmnopqrstuvwxyz" | |
712 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
713 | "_" | |
714 | "0123456789"; | |
715 | char const *name_start = NULL; | |
716 | char *p; | |
717 | ||
718 | /* Stop on last actual character. */ | |
719 | for (p = decl; p[1]; p++) | |
720 | if ((p == decl | |
721 | || ! memchr (alphanum, p[-1], sizeof alphanum)) | |
722 | && memchr (alphanum, p[0], sizeof alphanum - 10)) | |
723 | name_start = p; | |
724 | ||
725 | /* Strip the surrounding '{' and '}', and any blanks just inside | |
726 | the braces. */ | |
727 | while (*--p == ' ' || *p == '\t') | |
728 | continue; | |
729 | p[1] = '\0'; | |
730 | while (*++decl == ' ' || *decl == '\t') | |
731 | continue; | |
732 | ||
733 | if (! name_start) | |
734 | complain_at (loc, _("missing identifier in parameter declaration")); | |
735 | else | |
736 | { | |
737 | char *name = xmemdup0 (name_start, strspn (name_start, alphanum)); | |
738 | muscle_pair_list_grow (type, decl, name); | |
739 | free (name); | |
740 | } | |
741 | ||
742 | gram_scanner_last_string_free (); | |
743 | } | |
744 | ||
745 | ||
746 | static void | |
747 | version_check (location const *loc, char const *version) | |
748 | { | |
749 | if (strverscmp (version, PACKAGE_VERSION) > 0) | |
750 | { | |
751 | complain_at (*loc, "require bison %s, but have %s", | |
752 | version, PACKAGE_VERSION); | |
753 | exit (EX_MISMATCH); | |
754 | } | |
755 | } | |
756 | ||
757 | static void | |
758 | gram_error (location const *loc, char const *msg) | |
759 | { | |
760 | complain_at (*loc, "%s", msg); | |
761 | } | |
762 | ||
763 | char const * | |
764 | token_name (int type) | |
765 | { | |
766 | return yytname[YYTRANSLATE (type)]; | |
767 | } | |
768 | ||
769 | static char const * | |
770 | char_name (char c) | |
771 | { | |
772 | if (c == '\'') | |
773 | return "'\\''"; | |
774 | else | |
775 | { | |
776 | char buf[4]; | |
777 | buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0'; | |
778 | return quotearg_style (escape_quoting_style, buf); | |
779 | } | |
780 | } |