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