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