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"
 
  36 #define YYLLOC_DEFAULT(Current, Rhs, N)  (Current) = lloc_default (Rhs, N)
 
  37 static YYLTYPE lloc_default (YYLTYPE const *, int);
 
  39 #define YY_LOCATION_PRINT(File, Loc) \
 
  40           location_print (File, Loc)
 
  42 static void version_check (location const *loc, char const *version);
 
  44 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
 
  45    FIXME: depends on the undocumented availability of YYLLOC.  */
 
  47 #define yyerror(Msg) \
 
  48         gram_error (&yylloc, Msg)
 
  49 static void gram_error (location const *, char const *);
 
  51 static char const *char_name (char);
 
  53 /** Add a lex-param or a parse-param.
 
  55  * \param type  \a lex_param or \a parse_param
 
  56  * \param decl  the formal argument
 
  57  * \param loc   the location in the source.
 
  59 static void add_param (char const *type, char *decl, location loc);
 
  62 static symbol_class current_class = unknown_sym;
 
  63 static uniqstr current_type = NULL;
 
  64 static symbol *current_lhs_symbol;
 
  65 static location current_lhs_location;
 
  66 static named_ref *current_lhs_named_ref;
 
  67 static int current_prec = 0;
 
  69 /** Set the new current left-hand side symbol, possibly common
 
  70  * to several right-hand side parts of rule.
 
  74 current_lhs(symbol *sym, location loc, named_ref *ref)
 
  76   current_lhs_symbol = sym;
 
  77   current_lhs_location = loc;
 
  78   /* In order to simplify memory management, named references for lhs
 
  79      are always assigned by deep copy into the current symbol_list
 
  80      node.  This is because a single named-ref in the grammar may
 
  81      result in several uses when the user factors lhs between several
 
  82      rules using "|".  Therefore free the parser's original copy.  */
 
  83   free (current_lhs_named_ref);
 
  84   current_lhs_named_ref = ref;
 
  88 #define YYTYPE_INT16 int_fast16_t
 
  89 #define YYTYPE_INT8 int_fast8_t
 
  90 #define YYTYPE_UINT16 uint_fast16_t
 
  91 #define YYTYPE_UINT8 uint_fast8_t
 
 100 %define parse.lac full
 
 106   /* Bison's grammar can initial empty locations, hence a default
 
 107      location is needed. */
 
 108   boundary_set (&@$.start, current_file, 1, 1);
 
 109   boundary_set (&@$.end, current_file, 1, 1);
 
 121   unsigned char character;
 
 122   named_ref *named_ref;
 
 125 /* Define the tokens together with their human representation.  */
 
 126 %token GRAM_EOF 0 "end of file"
 
 127 %token STRING     "string"
 
 130 %token PERCENT_TOKEN       "%token"
 
 131 %token PERCENT_NTERM       "%nterm"
 
 133 %token PERCENT_TYPE        "%type"
 
 134 %token PERCENT_DESTRUCTOR  "%destructor"
 
 135 %token PERCENT_PRINTER     "%printer"
 
 137 %token PERCENT_LEFT        "%left"
 
 138 %token PERCENT_RIGHT       "%right"
 
 139 %token PERCENT_NONASSOC    "%nonassoc"
 
 141 %token PERCENT_PREC          "%prec"
 
 142 %token PERCENT_DPREC         "%dprec"
 
 143 %token PERCENT_MERGE         "%merge"
 
 146 /*----------------------.
 
 147 | Global Declarations.  |
 
 148 `----------------------*/
 
 152   PERCENT_DEBUG           "%debug"
 
 153   PERCENT_DEFAULT_PREC    "%default-prec"
 
 154   PERCENT_DEFINE          "%define"
 
 155   PERCENT_DEFINES         "%defines"
 
 156   PERCENT_ERROR_VERBOSE   "%error-verbose"
 
 157   PERCENT_EXPECT          "%expect"
 
 158   PERCENT_EXPECT_RR       "%expect-rr"
 
 159   PERCENT_FILE_PREFIX     "%file-prefix"
 
 160   PERCENT_GLR_PARSER      "%glr-parser"
 
 161   PERCENT_INITIAL_ACTION  "%initial-action"
 
 162   PERCENT_LANGUAGE        "%language"
 
 163   PERCENT_LEX_PARAM       "%lex-param"
 
 164   PERCENT_LOCATIONS       "%locations"
 
 165   PERCENT_NAME_PREFIX     "%name-prefix"
 
 166   PERCENT_NO_DEFAULT_PREC "%no-default-prec"
 
 167   PERCENT_NO_LINES        "%no-lines"
 
 168   PERCENT_NONDETERMINISTIC_PARSER
 
 169                           "%nondeterministic-parser"
 
 170   PERCENT_OUTPUT          "%output"
 
 171   PERCENT_PARSE_PARAM     "%parse-param"
 
 172   PERCENT_PURE_PARSER     "%pure-parser"
 
 173   PERCENT_REQUIRE         "%require"
 
 174   PERCENT_SKELETON        "%skeleton"
 
 175   PERCENT_START           "%start"
 
 176   PERCENT_TOKEN_TABLE     "%token-table"
 
 177   PERCENT_VERBOSE         "%verbose"
 
 181 %token BRACED_CODE     "{...}"
 
 182 %token BRACKETED_ID    "[identifier]"
 
 184 %token EPILOGUE        "epilogue"
 
 186 %token ID              "identifier"
 
 187 %token ID_COLON        "identifier:"
 
 188 %token PERCENT_PERCENT "%%"
 
 190 %token PROLOGUE        "%{...%}"
 
 193 %token TYPE_TAG_ANY    "<*>"
 
 194 %token TYPE_TAG_NONE   "<>"
 
 196 %type <character> CHAR
 
 197 %printer { fputs (char_name ($$), stderr); } CHAR
 
 199 /* braceless is not to be used for rule or symbol actions, as it
 
 200    calls code_props_plain_init.  */
 
 201 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
 
 203 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
 
 205 %printer { fprintf (stderr, "{\n%s\n}", $$); }
 
 206          braceless content.opt "{...}" "%{...%}" EPILOGUE
 
 208 %type <uniqstr> BRACKETED_ID ID ID_COLON TYPE variable
 
 209 %printer { fputs ($$, stderr); } <uniqstr>
 
 210 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
 
 211 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
 
 212 %printer { fprintf (stderr, "<%s>", $$); } TYPE
 
 215 %printer { fprintf (stderr, "%d", $$); } <integer>
 
 217 %type <symbol> id id_colon string_as_id symbol symbol.prec
 
 218 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
 
 219 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
 
 221 %type <assoc> precedence_declarator
 
 222 %type <list>  symbols.1 symbols.prec generic_symlist generic_symlist_item
 
 223 %type <named_ref> named_ref.opt
 
 228   prologue_declarations "%%" grammar epilogue.opt
 
 232         /*------------------------------------.
 
 233         | Declarations: before the first %%.  |
 
 234         `------------------------------------*/
 
 236 prologue_declarations:
 
 238 | prologue_declarations prologue_declaration
 
 241 prologue_declaration:
 
 245       code_props plain_code;
 
 246       code_props_plain_init (&plain_code, $1, @1);
 
 247       code_props_translate_code (&plain_code);
 
 248       gram_scanner_last_string_free ();
 
 249       muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
 
 250                         plain_code.code, @1);
 
 251       code_scanner_last_string_free ();
 
 253 | "%debug"                         { debug_flag = true; }
 
 254 | "%define" variable content.opt
 
 256       muscle_percent_define_insert ($2, @2, $3,
 
 257                                     MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
 
 259 | "%defines"                       { defines_flag = true; }
 
 263       spec_defines_file = xstrdup ($2);
 
 265 | "%error-verbose"                 { error_verbose = true; }
 
 266 | "%expect" INT                    { expected_sr_conflicts = $2; }
 
 267 | "%expect-rr" INT                 { expected_rr_conflicts = $2; }
 
 268 | "%file-prefix" STRING            { spec_file_prefix = $2; }
 
 269 | "%file-prefix" "=" STRING        { spec_file_prefix = $3; } /* deprecated */
 
 272       nondeterministic_parser = true;
 
 275 | "%initial-action" "{...}"
 
 278       code_props_symbol_action_init (&action, $2, @2);
 
 279       code_props_translate_code (&action);
 
 280       gram_scanner_last_string_free ();
 
 281       muscle_code_grow ("initial_action", action.code, @2);
 
 282       code_scanner_last_string_free ();
 
 284 | "%language" STRING            { language_argmatch ($2, grammar_prio, @1); }
 
 285 | "%lex-param" "{...}"          { add_param ("lex_param", $2, @2); }
 
 286 | "%locations"                  { locations_flag = true; }
 
 287 | "%name-prefix" STRING         { spec_name_prefix = $2; }
 
 288 | "%name-prefix" "=" STRING     { spec_name_prefix = $3; } /* deprecated */
 
 289 | "%no-lines"                   { no_lines_flag = true; }
 
 290 | "%nondeterministic-parser"    { nondeterministic_parser = true; }
 
 291 | "%output" STRING              { spec_outfile = $2; }
 
 292 | "%output" "=" STRING          { spec_outfile = $3; }  /* deprecated */
 
 293 | "%parse-param" "{...}"        { add_param ("parse_param", $2, @2); }
 
 296       /* %pure-parser is deprecated in favor of `%define api.pure', so use
 
 297          `%define api.pure' in a backward-compatible manner here.  First, don't
 
 298          complain if %pure-parser is specified multiple times.  */
 
 299       if (!muscle_find_const ("percent_define(api.pure)"))
 
 300         muscle_percent_define_insert ("api.pure", @1, "",
 
 301                                       MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
 
 302       /* In all cases, use api.pure now so that the backend doesn't complain if
 
 303          the skeleton ignores api.pure, but do warn now if there's a previous
 
 304          conflicting definition from an actual %define.  */
 
 305       if (!muscle_percent_define_flag_if ("api.pure"))
 
 306         muscle_percent_define_insert ("api.pure", @1, "",
 
 307                                       MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
 
 309 | "%require" STRING             { version_check (&@2, $2); }
 
 312       char const *skeleton_user = $2;
 
 313       if (mbschr (skeleton_user, '/'))
 
 315           size_t dir_length = strlen (current_file);
 
 316           char *skeleton_build;
 
 317           while (dir_length && current_file[dir_length - 1] != '/')
 
 319           while (dir_length && current_file[dir_length - 1] == '/')
 
 322             xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
 
 325               strncpy (skeleton_build, current_file, dir_length);
 
 326               skeleton_build[dir_length++] = '/';
 
 328           strcpy (skeleton_build + dir_length, skeleton_user);
 
 329           skeleton_user = uniqstr_new (skeleton_build);
 
 330           free (skeleton_build);
 
 332       skeleton_arg (skeleton_user, grammar_prio, @1);
 
 334 | "%token-table"                { token_table_flag = true; }
 
 335 | "%verbose"                    { report_flag |= report_states; }
 
 336 | "%yacc"                       { yacc_flag = true; }
 
 337 | /*FIXME: Err?  What is this horror doing here? */ ";"
 
 341   precedence_declaration
 
 345       grammar_start_symbol_set ($2, @2);
 
 347 | "%destructor" "{...}" generic_symlist
 
 350       for (list = $3; list; list = list->next)
 
 351         symbol_list_destructor_set (list, $2, @2);
 
 352       symbol_list_free ($3);
 
 354 | "%printer" "{...}" generic_symlist
 
 357       for (list = $3; list; list = list->next)
 
 358         symbol_list_printer_set (list, $2, @2);
 
 359       symbol_list_free ($3);
 
 367       default_prec = false;
 
 371       /* Do not invoke muscle_percent_code_grow here since it invokes
 
 372          muscle_user_name_list_grow.  */
 
 373       muscle_code_grow ("percent_code()", $2, @2);
 
 374       code_scanner_last_string_free ();
 
 376 | "%code" ID braceless
 
 378       muscle_percent_code_grow ($2, @2, $3, @3);
 
 379       code_scanner_last_string_free ();
 
 388 %token PERCENT_UNION "%union";
 
 392 | ID             { muscle_code_grow ("union_name", $1, @1); }
 
 396   "%union" union_name braceless
 
 399       muscle_code_grow ("stype", $3, @3);
 
 400       code_scanner_last_string_free ();
 
 408   "%nterm" { current_class = nterm_sym; } symbol_defs.1
 
 410       current_class = unknown_sym;
 
 413 | "%token" { current_class = token_sym; } symbol_defs.1
 
 415       current_class = unknown_sym;
 
 418 | "%type" TYPE symbols.1
 
 422       for (list = $3; list; list = list->next)
 
 423         symbol_type_set (list->content.sym, $2, @2);
 
 424       symbol_list_free ($3);
 
 428 precedence_declaration:
 
 429   precedence_declarator type.opt symbols.prec
 
 433       for (list = $3; list; list = list->next)
 
 435           symbol_type_set (list->content.sym, current_type, @2);
 
 436           symbol_precedence_set (list->content.sym, current_prec, $1, @1);
 
 438       symbol_list_free ($3);
 
 443 precedence_declarator:
 
 444   "%left"     { $$ = left_assoc; }
 
 445 | "%right"    { $$ = right_assoc; }
 
 446 | "%nonassoc" { $$ = non_assoc; }
 
 450   /* Nothing. */ { current_type = NULL; }
 
 451 | TYPE           { current_type = $1; tag_seen = true; }
 
 454 /* Just like symbols.1 but accept INT for the sake of POSIX.  */
 
 457     { $$ = symbol_list_sym_new ($1, @1); }
 
 458 | symbols.prec symbol.prec
 
 459     { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
 
 464   | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
 
 467 /* One or more symbols to be %typed. */
 
 470     { $$ = symbol_list_sym_new ($1, @1); }
 
 472     { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
 
 476   generic_symlist_item { $$ = $1; }
 
 477 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
 
 480 generic_symlist_item:
 
 481   symbol            { $$ = symbol_list_sym_new ($1, @1); }
 
 482 | TYPE              { $$ = symbol_list_type_new ($1, @1); }
 
 483 | "<*>"             { $$ = symbol_list_default_tagged_new (@1); }
 
 484 | "<>"             { $$ = symbol_list_default_tagless_new (@1); }
 
 487 /* One token definition.  */
 
 496        symbol_class_set ($1, current_class, @1, true);
 
 497        symbol_type_set ($1, current_type, @1);
 
 501       symbol_class_set ($1, current_class, @1, true);
 
 502       symbol_type_set ($1, current_type, @1);
 
 503       symbol_user_token_number_set ($1, $2, @2);
 
 507       symbol_class_set ($1, current_class, @1, true);
 
 508       symbol_type_set ($1, current_type, @1);
 
 509       symbol_make_alias ($1, $2, @$);
 
 511 | id INT string_as_id
 
 513       symbol_class_set ($1, current_class, @1, true);
 
 514       symbol_type_set ($1, current_type, @1);
 
 515       symbol_user_token_number_set ($1, $2, @2);
 
 516       symbol_make_alias ($1, $3, @$);
 
 520 /* One or more symbol definitions. */
 
 523 | symbol_defs.1 symbol_def
 
 527         /*------------------------------------------.
 
 528         | The grammar section: between the two %%.  |
 
 529         `------------------------------------------*/
 
 532   rules_or_grammar_declaration
 
 533 | grammar rules_or_grammar_declaration
 
 536 /* As a Bison extension, one can use the grammar declarations in the
 
 537    body of the grammar.  */
 
 538 rules_or_grammar_declaration:
 
 540 | grammar_declaration ";"
 
 548   id_colon named_ref.opt { current_lhs ($1, @1, $2); } rhses.1
 
 550     /* Free the current lhs. */
 
 551     current_lhs (0, @1, 0);
 
 556   rhs                { grammar_current_rule_end (@1); }
 
 557 | rhses.1 "|" rhs    { grammar_current_rule_end (@3); }
 
 563     { grammar_current_rule_begin (current_lhs_symbol, current_lhs_location,
 
 564                                   current_lhs_named_ref); }
 
 565 | rhs symbol named_ref.opt
 
 566     { grammar_current_rule_symbol_append ($2, @2, $3); }
 
 567 | rhs "{...}" named_ref.opt
 
 568     { grammar_current_rule_action_append ($2, @2, $3); }
 
 570     { grammar_current_rule_prec_set ($3, @3); }
 
 572     { grammar_current_rule_dprec_set ($3, @3); }
 
 574     { grammar_current_rule_merge_set ($3, @3); }
 
 578   /* Nothing. */ { $$ = 0; }
 
 580   BRACKETED_ID   { $$ = named_ref_new($1, @1); }
 
 584 /*----------------------------*
 
 585  | variable and content.opt.  |
 
 586  *---------------------------*/
 
 588 /* The STRING form of variable is deprecated and is not M4-friendly.
 
 589    For example, M4 fails for `%define "[" "value"'.  */
 
 592 | STRING { $$ = uniqstr_new ($1); }
 
 595 /* Some content or empty by default. */
 
 597   /* Nothing. */   { $$ = ""; }
 
 610       code_props plain_code;
 
 611       $1[strlen ($1) - 1] = '\n';
 
 612       code_props_plain_init (&plain_code, $1+1, @1);
 
 613       code_props_translate_code (&plain_code);
 
 614       gram_scanner_last_string_free ();
 
 615       $$ = plain_code.code;
 
 624 /* Identifiers are returned as uniqstr values by the scanner.
 
 625    Depending on their use, we may need to make them genuine symbols.  */
 
 629     { $$ = symbol_from_uniqstr ($1, @1); }
 
 632       $$ = symbol_get (char_name ($1), @1);
 
 633       symbol_class_set ($$, token_sym, @1, false);
 
 634       symbol_user_token_number_set ($$, $1, @1);
 
 639   ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
 
 648 /* A string used as an ID: quote it.  */
 
 652       $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
 
 653       symbol_class_set ($$, token_sym, @1, false);
 
 661       code_props plain_code;
 
 662       code_props_plain_init (&plain_code, $2, @2);
 
 663       code_props_translate_code (&plain_code);
 
 664       gram_scanner_last_string_free ();
 
 665       muscle_code_grow ("epilogue", plain_code.code, @2);
 
 666       code_scanner_last_string_free ();
 
 673 /* Return the location of the left-hand side of a rule whose
 
 674    right-hand side is RHS[1] ... RHS[N].  Ignore empty nonterminals in
 
 675    the right-hand side, and return an empty location equal to the end
 
 676    boundary of RHS[0] if the right-hand side is empty.  */
 
 679 lloc_default (YYLTYPE const *rhs, int n)
 
 684   /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
 
 685      The bug is fixed in 7.4.2m, but play it safe for now.  */
 
 686   loc.start = rhs[n].end;
 
 687   loc.end = rhs[n].end;
 
 689   /* Ignore empty nonterminals the start of the right-hand side.
 
 690      Do not bother to ignore them at the end of the right-hand side,
 
 691      since empty nonterminals have the same end as their predecessors.  */
 
 692   for (i = 1; i <= n; i++)
 
 693     if (! equal_boundaries (rhs[i].start, rhs[i].end))
 
 695         loc.start = rhs[i].start;
 
 703 /* Add a lex-param or a parse-param (depending on TYPE) with
 
 704    declaration DECL and location LOC.  */
 
 707 add_param (char const *type, char *decl, location loc)
 
 709   static char const alphanum[26 + 26 + 1 + 10] =
 
 710     "abcdefghijklmnopqrstuvwxyz"
 
 711     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
 714   char const *name_start = NULL;
 
 717   /* Stop on last actual character.  */
 
 718   for (p = decl; p[1]; p++)
 
 720          || ! memchr (alphanum, p[-1], sizeof alphanum))
 
 721         && memchr (alphanum, p[0], sizeof alphanum - 10))
 
 724   /* Strip the surrounding '{' and '}', and any blanks just inside
 
 726   while (*--p == ' ' || *p == '\t')
 
 729   while (*++decl == ' ' || *decl == '\t')
 
 733     complain_at (loc, _("missing identifier in parameter declaration"));
 
 740            memchr (alphanum, name_start[name_len], sizeof alphanum);
 
 744       name = xmalloc (name_len + 1);
 
 745       memcpy (name, name_start, name_len);
 
 746       name[name_len] = '\0';
 
 747       muscle_pair_list_grow (type, decl, name);
 
 751   gram_scanner_last_string_free ();
 
 756 version_check (location const *loc, char const *version)
 
 758   if (strverscmp (version, PACKAGE_VERSION) > 0)
 
 760       complain_at (*loc, "require bison %s, but have %s",
 
 761                    version, PACKAGE_VERSION);
 
 767 gram_error (location const *loc, char const *msg)
 
 769   complain_at (*loc, "%s", msg);
 
 773 token_name (int type)
 
 775   return yytname[YYTRANSLATE (type)];
 
 786       buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
 
 787       return quotearg_style (escape_quoting_style, buf);