1 %{/* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002-2012 Free Software Foundation, Inc.
5 This file is part of Bison, the GNU Compiler Compiler.
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.
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.
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/>. */
24 #include "conflicts.h"
28 #include "muscle-tab.h"
29 #include "named-ref.h"
33 #include "scan-gram.h"
34 #include "scan-code.h"
37 #define YYLLOC_DEFAULT(Current, Rhs, N) (Current) = lloc_default (Rhs, N)
38 static YYLTYPE lloc_default (YYLTYPE const *, int);
40 #define YY_LOCATION_PRINT(File, Loc) \
41 location_print (File, Loc)
43 static void version_check (location const *loc, char const *version);
45 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
46 FIXME: depends on the undocumented availability of YYLLOC. */
48 #define yyerror(Msg) \
49 gram_error (&yylloc, Msg)
50 static void gram_error (location const *, char const *);
52 static char const *char_name (char);
57 static int current_prec = 0;
58 static location current_lhs_location;
59 static named_ref *current_lhs_named_ref;
60 static symbol *current_lhs_symbol;
61 static symbol_class current_class = unknown_sym;
62 static uniqstr current_type = NULL;
64 /** Set the new current left-hand side symbol, possibly common
65 * to several right-hand side parts of rule.
69 current_lhs(symbol *sym, location loc, named_ref *ref)
71 current_lhs_symbol = sym;
72 current_lhs_location = loc;
73 /* In order to simplify memory management, named references for lhs
74 are always assigned by deep copy into the current symbol_list
75 node. This is because a single named-ref in the grammar may
76 result in several uses when the user factors lhs between several
77 rules using "|". Therefore free the parser's original copy. */
78 free (current_lhs_named_ref);
79 current_lhs_named_ref = ref;
82 #define YYTYPE_INT16 int_fast16_t
83 #define YYTYPE_INT8 int_fast8_t
84 #define YYTYPE_UINT16 uint_fast16_t
85 #define YYTYPE_UINT8 uint_fast8_t
93 %define parse.error "verbose"
94 %define parse.lac full
100 /* Bison's grammar can initial empty locations, hence a default
101 location is needed. */
102 boundary_set (&@$.start, current_file, 1, 1);
103 boundary_set (&@$.end, current_file, 1, 1);
112 named_ref *named_ref;
116 unsigned char character;
119 /* Define the tokens together with their human representation. */
120 %token GRAM_EOF 0 "end of file"
121 %token STRING "string"
124 %token PERCENT_TOKEN "%token"
125 %token PERCENT_NTERM "%nterm"
127 %token PERCENT_TYPE "%type"
128 %token PERCENT_DESTRUCTOR "%destructor"
129 %token PERCENT_PRINTER "%printer"
131 %token PERCENT_LEFT "%left"
132 %token PERCENT_RIGHT "%right"
133 %token PERCENT_NONASSOC "%nonassoc"
134 %token PERCENT_PRECEDENCE "%precedence"
136 %token PERCENT_PREC "%prec"
137 %token PERCENT_DPREC "%dprec"
138 %token PERCENT_MERGE "%merge"
140 /*----------------------.
141 | Global Declarations. |
142 `----------------------*/
146 PERCENT_DEFAULT_PREC "%default-prec"
147 PERCENT_DEFINE "%define"
148 PERCENT_DEFINES "%defines"
149 PERCENT_ERROR_VERBOSE "%error-verbose"
150 PERCENT_EXPECT "%expect"
151 PERCENT_EXPECT_RR "%expect-rr"
152 PERCENT_FLAG "%<flag>"
153 PERCENT_FILE_PREFIX "%file-prefix"
154 PERCENT_GLR_PARSER "%glr-parser"
155 PERCENT_INITIAL_ACTION "%initial-action"
156 PERCENT_LANGUAGE "%language"
157 PERCENT_NAME_PREFIX "%name-prefix"
158 PERCENT_NO_DEFAULT_PREC "%no-default-prec"
159 PERCENT_NO_LINES "%no-lines"
160 PERCENT_NONDETERMINISTIC_PARSER
161 "%nondeterministic-parser"
162 PERCENT_OUTPUT "%output"
163 PERCENT_REQUIRE "%require"
164 PERCENT_SKELETON "%skeleton"
165 PERCENT_START "%start"
166 PERCENT_TOKEN_TABLE "%token-table"
167 PERCENT_VERBOSE "%verbose"
171 %token BRACED_CODE "{...}"
172 %token BRACED_PREDICATE "%?{...}"
173 %token BRACKETED_ID "[identifier]"
175 %token EPILOGUE "epilogue"
177 %token ID "identifier"
178 %token ID_COLON "identifier:"
179 %token PERCENT_PERCENT "%%"
181 %token PROLOGUE "%{...%}"
187 %type <character> CHAR
188 %printer { fputs (char_name ($$), stderr); } CHAR
190 /* braceless is not to be used for rule or symbol actions, as it
191 calls code_props_plain_init. */
192 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
193 %type <code> "{...}" "%?{...}"
194 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
196 %printer { fprintf (stderr, "{\n%s\n}", $$); }
197 braceless content.opt "{...}" "%{...%}" EPILOGUE
199 %type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG variable
200 %printer { fputs ($$, stderr); } <uniqstr>
201 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
202 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
203 %printer { fprintf (stderr, "%%%s", $$); } PERCENT_FLAG
204 %printer { fprintf (stderr, "<%s>", $$); } TAG
207 %printer { fprintf (stderr, "%d", $$); } <integer>
209 %type <symbol> id id_colon string_as_id symbol symbol.prec
210 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
211 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
213 %type <assoc> precedence_declarator
214 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
215 %type <named_ref> named_ref.opt
228 param_parse = 1 << 1,
229 param_both = param_lex | param_parse
235 /** Add a lex-param and/or a parse-param.
237 * \param type where to push this formal argument.
238 * \param decl the formal argument. Destroyed.
239 * \param loc the location in the source.
241 static void add_param (param_type type, char *decl, location loc);
242 static param_type current_param = param_none;
248 %token <param> PERCENT_PARAM "%param";
253 #define CASE(In, Out) \
254 case param_ ## In: fputs ("%" #Out, stderr); break
255 CASE(lex, lex-param);
256 CASE(parse, parse-param);
259 case param_none: aver (false); break;
270 prologue_declarations "%%" grammar epilogue.opt
274 /*------------------------------------.
275 | Declarations: before the first %%. |
276 `------------------------------------*/
278 prologue_declarations:
280 | prologue_declarations prologue_declaration
283 prologue_declaration:
287 code_props plain_code;
288 code_props_plain_init (&plain_code, $1, @1);
289 code_props_translate_code (&plain_code);
290 gram_scanner_last_string_free ();
291 muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
292 plain_code.code, @1);
293 code_scanner_last_string_free ();
297 muscle_percent_define_ensure ($1, @1, true);
299 | "%define" variable content.opt
301 muscle_percent_define_insert ($2, @2, $3,
302 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
304 | "%defines" { defines_flag = true; }
308 spec_defines_file = xstrdup ($2);
312 muscle_percent_define_insert ("parse.error", @1, "verbose",
313 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
315 | "%expect" INT { expected_sr_conflicts = $2; }
316 | "%expect-rr" INT { expected_rr_conflicts = $2; }
317 | "%file-prefix" STRING { spec_file_prefix = $2; }
318 | "%file-prefix" "=" STRING { spec_file_prefix = $3; } /* deprecated */
321 nondeterministic_parser = true;
324 | "%initial-action" "{...}"
327 code_props_symbol_action_init (&action, $2, @2);
328 code_props_translate_code (&action);
329 gram_scanner_last_string_free ();
330 muscle_code_grow ("initial_action", action.code, @2);
331 code_scanner_last_string_free ();
333 | "%language" STRING { language_argmatch ($2, grammar_prio, @1); }
334 | "%name-prefix" STRING { spec_name_prefix = $2; }
335 | "%name-prefix" "=" STRING { spec_name_prefix = $3; } /* deprecated */
336 | "%no-lines" { no_lines_flag = true; }
337 | "%nondeterministic-parser" { nondeterministic_parser = true; }
338 | "%output" STRING { spec_outfile = $2; }
339 | "%output" "=" STRING { spec_outfile = $3; } /* deprecated */
340 | "%param" { current_param = $1; } params { current_param = param_none; }
341 | "%require" STRING { version_check (&@2, $2); }
344 char const *skeleton_user = $2;
345 if (strchr (skeleton_user, '/'))
347 size_t dir_length = strlen (current_file);
348 char *skeleton_build;
349 while (dir_length && current_file[dir_length - 1] != '/')
351 while (dir_length && current_file[dir_length - 1] == '/')
354 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
357 memcpy (skeleton_build, current_file, dir_length);
358 skeleton_build[dir_length++] = '/';
360 strcpy (skeleton_build + dir_length, skeleton_user);
361 skeleton_user = uniqstr_new (skeleton_build);
362 free (skeleton_build);
364 skeleton_arg (skeleton_user, grammar_prio, @1);
366 | "%token-table" { token_table_flag = true; }
367 | "%verbose" { report_flag |= report_states; }
368 | "%yacc" { yacc_flag = true; }
369 | /*FIXME: Err? What is this horror doing here? */ ";"
373 params "{...}" { add_param (current_param, $2, @2); }
374 | "{...}" { add_param (current_param, $1, @1); }
378 /*----------------------.
379 | grammar_declaration. |
380 `----------------------*/
383 precedence_declaration
387 grammar_start_symbol_set ($2, @2);
389 | "%destructor" "{...}" generic_symlist
392 for (list = $3; list; list = list->next)
393 symbol_list_code_props_set (list, destructor, @2, $2);
394 symbol_list_free ($3);
396 | "%printer" "{...}" generic_symlist
399 for (list = $3; list; list = list->next)
400 symbol_list_code_props_set (list, printer, @2, $2);
401 symbol_list_free ($3);
409 default_prec = false;
413 /* Do not invoke muscle_percent_code_grow here since it invokes
414 muscle_user_name_list_grow. */
415 muscle_code_grow ("percent_code()", $2, @2);
416 code_scanner_last_string_free ();
418 | "%code" ID braceless
420 muscle_percent_code_grow ($2, @2, $3, @3);
421 code_scanner_last_string_free ();
430 %token PERCENT_UNION "%union";
434 | ID { muscle_code_grow ("union_name", $1, @1); }
438 "%union" union_name braceless
441 muscle_code_grow ("stype", $3, @3);
442 code_scanner_last_string_free ();
450 "%nterm" { current_class = nterm_sym; } symbol_defs.1
452 current_class = unknown_sym;
455 | "%token" { current_class = token_sym; } symbol_defs.1
457 current_class = unknown_sym;
460 | "%type" TAG symbols.1
464 for (list = $3; list; list = list->next)
465 symbol_type_set (list->content.sym, $2, @2);
466 symbol_list_free ($3);
470 precedence_declaration:
471 precedence_declarator tag.opt symbols.prec
475 for (list = $3; list; list = list->next)
477 symbol_type_set (list->content.sym, current_type, @2);
478 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
480 symbol_list_free ($3);
485 precedence_declarator:
486 "%left" { $$ = left_assoc; }
487 | "%right" { $$ = right_assoc; }
488 | "%nonassoc" { $$ = non_assoc; }
489 | "%precedence" { $$ = precedence_assoc; }
493 /* Nothing. */ { current_type = NULL; }
494 | TAG { current_type = $1; tag_seen = true; }
497 /* Just like symbols.1 but accept INT for the sake of POSIX. */
500 { $$ = symbol_list_sym_new ($1, @1); }
501 | symbols.prec symbol.prec
502 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
507 | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
510 /* One or more symbols to be %typed. */
513 { $$ = symbol_list_sym_new ($1, @1); }
515 { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
519 generic_symlist_item { $$ = $1; }
520 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
523 generic_symlist_item:
524 symbol { $$ = symbol_list_sym_new ($1, @1); }
525 | TAG { $$ = symbol_list_type_new ($1, @1); }
526 | "<*>" { $$ = symbol_list_default_tagged_new (@1); }
527 | "<>" { $$ = symbol_list_default_tagless_new (@1); }
530 /* One token definition. */
539 symbol_class_set ($1, current_class, @1, true);
540 symbol_type_set ($1, current_type, @1);
544 symbol_class_set ($1, current_class, @1, true);
545 symbol_type_set ($1, current_type, @1);
546 symbol_user_token_number_set ($1, $2, @2);
550 symbol_class_set ($1, current_class, @1, true);
551 symbol_type_set ($1, current_type, @1);
552 symbol_make_alias ($1, $2, @$);
554 | id INT string_as_id
556 symbol_class_set ($1, current_class, @1, true);
557 symbol_type_set ($1, current_type, @1);
558 symbol_user_token_number_set ($1, $2, @2);
559 symbol_make_alias ($1, $3, @$);
563 /* One or more symbol definitions. */
566 | symbol_defs.1 symbol_def
570 /*------------------------------------------.
571 | The grammar section: between the two %%. |
572 `------------------------------------------*/
575 rules_or_grammar_declaration
576 | grammar rules_or_grammar_declaration
579 /* As a Bison extension, one can use the grammar declarations in the
580 body of the grammar. */
581 rules_or_grammar_declaration:
583 | grammar_declaration ";"
591 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
593 /* Free the current lhs. */
594 current_lhs (0, @1, 0);
599 rhs { grammar_current_rule_end (@1); }
600 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
606 { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
607 current_lhs_named_ref); }
608 | rhs symbol named_ref.opt
609 { grammar_current_rule_symbol_append ($2, @2, $3); }
610 | rhs "{...}" named_ref.opt
611 { grammar_current_rule_action_append ($2, @2, $3, false); }
613 { grammar_current_rule_action_append ($2, @2, NULL, true); }
615 { grammar_current_rule_prec_set ($3, @3); }
617 { grammar_current_rule_dprec_set ($3, @3); }
619 { grammar_current_rule_merge_set ($3, @3); }
623 /* Nothing. */ { $$ = 0; }
625 BRACKETED_ID { $$ = named_ref_new($1, @1); }
628 /*---------------------------.
629 | variable and content.opt. |
630 `---------------------------*/
632 /* The STRING form of variable is deprecated and is not M4-friendly.
633 For example, M4 fails for `%define "[" "value"'. */
636 | STRING { $$ = uniqstr_new ($1); }
639 /* Some content or empty by default. */
641 /* Nothing. */ { $$ = ""; }
654 code_props plain_code;
655 $1[strlen ($1) - 1] = '\n';
656 code_props_plain_init (&plain_code, $1+1, @1);
657 code_props_translate_code (&plain_code);
658 gram_scanner_last_string_free ();
659 $$ = plain_code.code;
668 /* Identifiers are returned as uniqstr values by the scanner.
669 Depending on their use, we may need to make them genuine symbols. */
673 { $$ = symbol_from_uniqstr ($1, @1); }
676 $$ = symbol_get (char_name ($1), @1);
677 symbol_class_set ($$, token_sym, @1, false);
678 symbol_user_token_number_set ($$, $1, @1);
683 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
692 /* A string used as an ID: quote it. */
696 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
697 symbol_class_set ($$, token_sym, @1, false);
705 code_props plain_code;
706 code_props_plain_init (&plain_code, $2, @2);
707 code_props_translate_code (&plain_code);
708 gram_scanner_last_string_free ();
709 muscle_code_grow ("epilogue", plain_code.code, @2);
710 code_scanner_last_string_free ();
717 /* Return the location of the left-hand side of a rule whose
718 right-hand side is RHS[1] ... RHS[N]. Ignore empty nonterminals in
719 the right-hand side, and return an empty location equal to the end
720 boundary of RHS[0] if the right-hand side is empty. */
723 lloc_default (YYLTYPE const *rhs, int n)
728 /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
729 The bug is fixed in 7.4.2m, but play it safe for now. */
730 loc.start = rhs[n].end;
731 loc.end = rhs[n].end;
733 /* Ignore empty nonterminals the start of the right-hand side.
734 Do not bother to ignore them at the end of the right-hand side,
735 since empty nonterminals have the same end as their predecessors. */
736 for (i = 1; i <= n; i++)
737 if (! equal_boundaries (rhs[i].start, rhs[i].end))
739 loc.start = rhs[i].start;
748 add_param (param_type type, char *decl, location loc)
750 static char const alphanum[26 + 26 + 1 + 10] =
751 "abcdefghijklmnopqrstuvwxyz"
752 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
756 char const *name_start = NULL;
759 /* Stop on last actual character. */
760 for (p = decl; p[1]; p++)
762 || ! memchr (alphanum, p[-1], sizeof alphanum))
763 && memchr (alphanum, p[0], sizeof alphanum - 10))
766 /* Strip the surrounding '{' and '}', and any blanks just inside
768 while (*--p == ' ' || *p == '\t')
771 while (*++decl == ' ' || *decl == '\t')
776 complain_at (loc, complaint,
777 _("missing identifier in parameter declaration"));
780 char *name = xmemdup0 (name_start, strspn (name_start, alphanum));
781 if (type & param_lex)
782 muscle_pair_list_grow ("lex_param", decl, name);
783 if (type & param_parse)
784 muscle_pair_list_grow ("parse_param", decl, name);
788 gram_scanner_last_string_free ();
793 version_check (location const *loc, char const *version)
795 if (strverscmp (version, PACKAGE_VERSION) > 0)
797 complain_at (*loc, complaint, "require bison %s, but have %s",
798 version, PACKAGE_VERSION);
804 gram_error (location const *loc, char const *msg)
806 complain_at (*loc, complaint, "%s", msg);
810 token_name (int type)
812 return yytname[YYTRANSLATE (type)];
823 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
824 return quotearg_style (escape_quoting_style, buf);