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