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