1 %{/* Bison Grammar Parser -*- C -*-
3 Copyright (C) 2002-2013 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/>. */
25 #include "conflicts.h"
29 #include "named-ref.h"
34 #include "scan-gram.h"
35 #include "scan-code.h"
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;
48 /** Set the new current left-hand side symbol, possibly common
49 * to several right-hand side parts of rule.
51 static void current_lhs (symbol *sym, location loc, named_ref *ref);
53 #define YYLLOC_DEFAULT(Current, Rhs, N) \
54 (Current) = lloc_default (Rhs, N)
55 static YYLTYPE lloc_default (YYLTYPE const *, int);
57 #define YY_LOCATION_PRINT(File, Loc) \
58 location_print (Loc, File)
60 static void version_check (location const *loc, char const *version);
62 static void gram_error (location const *, char const *);
64 /* A string that describes a char (e.g., 'a' -> "'a'"). */
65 static char const *char_name (char);
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
73 %define api.prefix {gram_}
76 %define parse.error verbose
77 %define parse.lac full
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);
91 /* Define the tokens together with their human representation. */
92 %token GRAM_EOF 0 "end of file"
93 %token STRING "string"
95 %token PERCENT_TOKEN "%token"
96 %token PERCENT_NTERM "%nterm"
98 %token PERCENT_TYPE "%type"
99 %token PERCENT_DESTRUCTOR "%destructor"
100 %token PERCENT_PRINTER "%printer"
102 %token PERCENT_LEFT "%left"
103 %token PERCENT_RIGHT "%right"
104 %token PERCENT_NONASSOC "%nonassoc"
105 %token PERCENT_PRECEDENCE "%precedence"
107 %token PERCENT_PREC "%prec"
108 %token PERCENT_DPREC "%dprec"
109 %token PERCENT_MERGE "%merge"
111 /*----------------------.
112 | Global Declarations. |
113 `----------------------*/
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"
142 %token BRACED_CODE "{...}"
143 %token BRACED_PREDICATE "%?{...}"
144 %token BRACKETED_ID "[identifier]"
146 %token EPILOGUE "epilogue"
148 %token ID "identifier"
149 %token ID_COLON "identifier:"
150 %token PERCENT_PERCENT "%%"
152 %token PROLOGUE "%{...%}"
158 %union {unsigned char character;}
159 %type <character> CHAR
160 %printer { fputs (char_name ($$), yyo); } CHAR
162 /* braceless is not to be used for rule or symbol actions, as it
163 calls code_props_plain_init. */
169 %type <chars> STRING "%{...%}" EPILOGUE braceless
170 %type <code> "{...}" "%?{...}"
171 %printer { fputs (quotearg_style (c_quoting_style, $$), yyo); }
173 %printer { fprintf (yyo, "{\n%s\n}", $$); }
174 braceless "{...}" "%{...%}" EPILOGUE
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
184 %union {int integer;};
185 %token <integer> INT "integer"
186 %printer { fprintf (yyo, "%d", $$); } <integer>
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
193 %union {assoc assoc;};
194 %type <assoc> precedence_declarator
196 %union {symbol_list *list;}
197 %type <list> symbols.1 symbols.prec generic_symlist generic_symlist_item
199 %union {named_ref *named_ref;}
200 %type <named_ref> named_ref.opt
211 param_parse = 1 << 1,
212 param_both = param_lex | param_parse
217 /** Add a lex-param and/or a parse-param.
219 * \param type where to push this formal argument.
220 * \param decl the formal argument. Destroyed.
221 * \param loc the location in the source.
223 static void add_param (param_type type, char *decl, location loc);
224 static param_type current_param = param_none;
226 %union {param_type param;}
227 %token <param> PERCENT_PARAM "%param";
232 #define CASE(In, Out) \
233 case param_ ## In: fputs ("%" #Out, yyo); break
234 CASE (lex, lex-param);
235 CASE (parse, parse-param);
238 case param_none: aver (false); break;
249 prologue_declarations "%%" grammar epilogue.opt
253 /*------------------------------------.
254 | Declarations: before the first %%. |
255 `------------------------------------*/
257 prologue_declarations:
259 | prologue_declarations prologue_declaration
262 prologue_declaration:
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 ();
276 muscle_percent_define_ensure ($1, @1, true);
278 | "%define" variable value
280 muscle_percent_define_insert ($2, @2, $3.kind, $3.chars,
281 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
283 | "%defines" { defines_flag = true; }
287 spec_defines_file = xstrdup ($2);
291 muscle_percent_define_insert ("parse.error", @1, muscle_keyword,
293 MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
295 | "%expect" INT { expected_sr_conflicts = $2; }
296 | "%expect-rr" INT { expected_rr_conflicts = $2; }
297 | "%file-prefix" STRING { spec_file_prefix = $2; }
300 nondeterministic_parser = true;
303 | "%initial-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 ();
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); }
321 char const *skeleton_user = $2;
322 if (strchr (skeleton_user, '/'))
324 size_t dir_length = strlen (current_file);
325 char *skeleton_build;
326 while (dir_length && current_file[dir_length - 1] != '/')
328 while (dir_length && current_file[dir_length - 1] == '/')
331 xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
334 memcpy (skeleton_build, current_file, dir_length);
335 skeleton_build[dir_length++] = '/';
337 strcpy (skeleton_build + dir_length, skeleton_user);
338 skeleton_user = uniqstr_new (skeleton_build);
339 free (skeleton_build);
341 skeleton_arg (skeleton_user, grammar_prio, @1);
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? */ ";"
350 params "{...}" { add_param (current_param, $2, @2); }
351 | "{...}" { add_param (current_param, $1, @1); }
355 /*----------------------.
356 | grammar_declaration. |
357 `----------------------*/
360 precedence_declaration
364 grammar_start_symbol_set ($2, @2);
366 | code_props_type "{...}" generic_symlist
369 code_props_symbol_action_init (&code, $2, @2);
370 code_props_translate_code (&code);
373 for (list = $3; list; list = list->next)
374 symbol_list_code_props_set (list, $1, &code);
375 symbol_list_free ($3);
384 default_prec = false;
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 ();
393 | "%code" ID braceless
395 muscle_percent_code_grow ($2, @2, $3, @3);
396 code_scanner_last_string_free ();
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>;
404 "%destructor" { $$ = destructor; }
405 | "%printer" { $$ = printer; }
412 %token PERCENT_UNION "%union";
416 | ID { muscle_code_grow ("union_name", $1, @1); }
420 "%union" union_name braceless
423 muscle_code_grow ("union_members", $3, @3);
424 code_scanner_last_string_free ();
432 "%nterm" { current_class = nterm_sym; } symbol_defs.1
434 current_class = unknown_sym;
437 | "%token" { current_class = token_sym; } symbol_defs.1
439 current_class = unknown_sym;
442 | "%type" TAG symbols.1
446 for (list = $3; list; list = list->next)
447 symbol_type_set (list->content.sym, $2, @2);
448 symbol_list_free ($3);
452 precedence_declaration:
453 precedence_declarator tag.opt symbols.prec
457 for (list = $3; list; list = list->next)
459 symbol_type_set (list->content.sym, current_type, @2);
460 symbol_precedence_set (list->content.sym, current_prec, $1, @1);
462 symbol_list_free ($3);
467 precedence_declarator:
468 "%left" { $$ = left_assoc; }
469 | "%right" { $$ = right_assoc; }
470 | "%nonassoc" { $$ = non_assoc; }
471 | "%precedence" { $$ = precedence_assoc; }
475 %empty { current_type = NULL; }
476 | TAG { current_type = $1; tag_seen = true; }
479 /* Just like symbols.1 but accept INT for the sake of POSIX. */
482 { $$ = symbol_list_sym_new ($1, @1); }
483 | symbols.prec symbol.prec
484 { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
491 symbol_class_set ($1, token_sym, @1, false);
496 symbol_user_token_number_set ($1, $2, @2);
497 symbol_class_set ($1, token_sym, @1, false);
501 /* One or more symbols to be %typed. */
504 { $$ = symbol_list_sym_new ($1, @1); }
506 { $$ = symbol_list_append ($1, symbol_list_sym_new ($2, @2)); }
510 generic_symlist_item { $$ = $1; }
511 | generic_symlist generic_symlist_item { $$ = symbol_list_append ($1, $2); }
514 generic_symlist_item:
515 symbol { $$ = symbol_list_sym_new ($1, @1); }
516 | tag { $$ = symbol_list_type_new ($1, @1); }
521 | "<*>" { $$ = uniqstr_new ("*"); }
522 | "<>" { $$ = uniqstr_new (""); }
525 /* One token definition. */
534 symbol_class_set ($1, current_class, @1, true);
535 symbol_type_set ($1, current_type, @1);
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);
545 symbol_class_set ($1, current_class, @1, true);
546 symbol_type_set ($1, current_type, @1);
547 symbol_make_alias ($1, $2, @$);
549 | id INT string_as_id
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, @$);
558 /* One or more symbol definitions. */
561 | symbol_defs.1 symbol_def
565 /*------------------------------------------.
566 | The grammar section: between the two %%. |
567 `------------------------------------------*/
570 rules_or_grammar_declaration
571 | grammar rules_or_grammar_declaration
574 /* As a Bison extension, one can use the grammar declarations in the
575 body of the grammar. */
576 rules_or_grammar_declaration:
578 | grammar_declaration ";"
586 id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
588 /* Free the current lhs. */
589 current_lhs (0, @1, 0);
594 rhs { grammar_current_rule_end (@1); }
595 | rhses.1 "|" rhs { grammar_current_rule_end (@3); }
599 %token PERCENT_EMPTY "%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); }
609 { grammar_current_rule_action_append ($2, @2, NULL, true); }
611 { grammar_current_rule_empty_set (@2); }
613 { grammar_current_rule_prec_set ($3, @3); }
615 { grammar_current_rule_dprec_set ($3, @3); }
617 { grammar_current_rule_merge_set ($3, @3); }
622 | BRACKETED_ID { $$ = named_ref_new($1, @1); }
625 /*---------------------.
626 | variable and value. |
627 `---------------------*/
629 /* The STRING form of variable is deprecated and is not M4-friendly.
630 For example, M4 fails for '%define "[" "value"'. */
633 | STRING { $$ = uniqstr_new ($1); }
636 /* Some content or empty by default. */
637 %code requires {#include "muscle-tab.h"};
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;
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; }
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;
686 /* Identifiers are returned as uniqstr values by the scanner.
687 Depending on their use, we may need to make them genuine symbols. */
691 { $$ = symbol_from_uniqstr ($1, @1); }
694 $$ = symbol_get (char_name ($1), @1);
695 symbol_class_set ($$, token_sym, @1, false);
696 symbol_user_token_number_set ($$, $1, @1);
701 ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
710 /* A string used as an ID: quote it. */
714 $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
715 symbol_class_set ($$, token_sym, @1, false);
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 ();
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. */
740 lloc_default (YYLTYPE const *rhs, int n)
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;
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))
756 loc.start = rhs[i].start;
765 add_param (param_type type, char *decl, location loc)
767 static char const alphanum[26 + 26 + 1 + 10] =
768 "abcdefghijklmnopqrstuvwxyz"
769 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
773 char const *name_start = NULL;
776 /* Stop on last actual character. */
777 for (p = decl; p[1]; p++)
779 || ! memchr (alphanum, p[-1], sizeof alphanum))
780 && memchr (alphanum, p[0], sizeof alphanum - 10))
783 /* Strip the surrounding '{' and '}', and any blanks just inside
786 while (c_isspace ((unsigned char) *p))
790 while (c_isspace ((unsigned char) *decl))
795 complain (&loc, complaint, _("missing identifier in parameter declaration"));
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);
806 gram_scanner_last_string_free ();
811 version_check (location const *loc, char const *version)
813 if (strverscmp (version, PACKAGE_VERSION) > 0)
815 complain (loc, complaint, "require bison %s, but have %s",
816 version, PACKAGE_VERSION);
822 gram_error (location const *loc, char const *msg)
824 complain (loc, complaint, "%s", msg);
828 token_name (int type)
830 return yytname[YYTRANSLATE (type)];
841 buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
842 return quotearg_style (escape_quoting_style, buf);
848 current_lhs (symbol *sym, location loc, named_ref *ref)
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;