1 %{/* Bison Grammar Parser                             -*- C -*-
 
   3    Copyright (C) 2002-2010 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);
 
  56   static int current_prec = 0;
 
  57   static location current_lhs_location;
 
  58   static named_ref *current_lhs_named_ref;
 
  59   static symbol *current_lhs;
 
  60   static symbol_class current_class = unknown_sym;
 
  61   static uniqstr current_type = NULL;
 
  63   #define YYTYPE_INT16 int_fast16_t
 
  64   #define YYTYPE_INT8 int_fast8_t
 
  65   #define YYTYPE_UINT16 uint_fast16_t
 
  66   #define YYTYPE_UINT8 uint_fast8_t
 
  74 %define parse.error "verbose"
 
  80   /* Bison's grammar can initial empty locations, hence a default
 
  81      location is needed. */
 
  82   boundary_set (&@$.start, current_file, 1, 1);
 
  83   boundary_set (&@$.end, current_file, 1, 1);
 
  96   unsigned char character;
 
  99 /* Define the tokens together with their human representation.  */
 
 100 %token GRAM_EOF 0 "end of file"
 
 101 %token STRING     "string"
 
 104 %token PERCENT_TOKEN       "%token"
 
 105 %token PERCENT_NTERM       "%nterm"
 
 107 %token PERCENT_TYPE        "%type"
 
 108 %token PERCENT_DESTRUCTOR  "%destructor"
 
 109 %token PERCENT_PRINTER     "%printer"
 
 111 %token PERCENT_LEFT        "%left"
 
 112 %token PERCENT_RIGHT       "%right"
 
 113 %token PERCENT_NONASSOC    "%nonassoc"
 
 114 %token PERCENT_PRECEDENCE  "%precedence"
 
 116 %token PERCENT_PREC          "%prec"
 
 117 %token PERCENT_DPREC         "%dprec"
 
 118 %token PERCENT_MERGE         "%merge"
 
 121 /*----------------------.
 
 122 | Global Declarations.  |
 
 123 `----------------------*/
 
 127   PERCENT_DEFAULT_PREC    "%default-prec"
 
 128   PERCENT_DEFINE          "%define"
 
 129   PERCENT_DEFINES         "%defines"
 
 130   PERCENT_ERROR_VERBOSE   "%error-verbose"
 
 131   PERCENT_EXPECT          "%expect"
 
 132   PERCENT_EXPECT_RR       "%expect-rr"
 
 133   PERCENT_FLAG            "%<flag>"
 
 134   PERCENT_FILE_PREFIX     "%file-prefix"
 
 135   PERCENT_GLR_PARSER      "%glr-parser"
 
 136   PERCENT_INITIAL_ACTION  "%initial-action"
 
 137   PERCENT_LANGUAGE        "%language"
 
 138   PERCENT_NAME_PREFIX     "%name-prefix"
 
 139   PERCENT_NO_DEFAULT_PREC "%no-default-prec"
 
 140   PERCENT_NO_LINES        "%no-lines"
 
 141   PERCENT_NONDETERMINISTIC_PARSER
 
 142                           "%nondeterministic-parser"
 
 143   PERCENT_OUTPUT          "%output"
 
 144   PERCENT_REQUIRE         "%require"
 
 145   PERCENT_SKELETON        "%skeleton"
 
 146   PERCENT_START           "%start"
 
 147   PERCENT_TOKEN_TABLE     "%token-table"
 
 148   PERCENT_VERBOSE         "%verbose"
 
 152 %token BRACED_CODE     "{...}"
 
 153 %token BRACKETED_ID    "[identifier]"
 
 155 %token EPILOGUE        "epilogue"
 
 157 %token ID              "identifier"
 
 158 %token ID_COLON        "identifier:"
 
 159 %token PERCENT_PERCENT "%%"
 
 161 %token PROLOGUE        "%{...%}"
 
 167 %type <character> CHAR
 
 168 %printer { fputs (char_name ($$), stderr); } CHAR
 
 170 /* braceless is not to be used for rule or symbol actions, as it
 
 171    calls code_props_plain_init.  */
 
 172 %type <chars> STRING "%{...%}" EPILOGUE braceless content.opt
 
 174 %printer { fputs (quotearg_style (c_quoting_style, $$), stderr); }
 
 176 %printer { fprintf (stderr, "{\n%s\n}", $$); }
 
 177          braceless content.opt "{...}" "%{...%}" EPILOGUE
 
 179 %type <uniqstr> BRACKETED_ID ID ID_COLON PERCENT_FLAG TAG variable
 
 180 %printer { fputs ($$, stderr); } <uniqstr>
 
 181 %printer { fprintf (stderr, "[%s]", $$); } BRACKETED_ID
 
 182 %printer { fprintf (stderr, "%s:", $$); } ID_COLON
 
 183 %printer { fprintf (stderr, "%%%s", $$); } PERCENT_FLAG
 
 184 %printer { fprintf (stderr, "<%s>", $$); } TAG
 
 187 %printer { fprintf (stderr, "%d", $$); } <integer>
 
 189 %type <symbol> id id_colon string_as_id symbol symbol.prec
 
 190 %printer { fprintf (stderr, "%s", $$->tag); } <symbol>
 
 191 %printer { fprintf (stderr, "%s:", $$->tag); } id_colon
 
 193 %type <assoc> precedence_declarator
 
 194 %type <list>  symbols.1 symbols.prec generic_symlist generic_symlist_item
 
 195 %type <named_ref> named_ref.opt
 
 208     param_parse  = 1 << 1,
 
 209     param_both   = param_lex | param_parse
 
 215   /** Add a lex-param and/or a parse-param.
 
 217    * \param type  where to push this formal argument.
 
 218    * \param decl  the formal argument.  Destroyed.
 
 219    * \param loc   the location in the source.
 
 221   static void add_param (param_type type, char *decl, location loc);
 
 222   static param_type current_param = param_none;
 
 228 %token <param> PERCENT_PARAM "%param";
 
 233 #define CASE(In, Out)                                           \
 
 234       case param_ ## In: fputs ("%" #Out, stderr); break
 
 235       CASE(lex,   lex-param);
 
 236       CASE(parse, parse-param);
 
 239       case param_none: aver (false); break;
 
 250   prologue_declarations "%%" grammar epilogue.opt
 
 254         /*------------------------------------.
 
 255         | Declarations: before the first %%.  |
 
 256         `------------------------------------*/
 
 258 prologue_declarations:
 
 260 | prologue_declarations prologue_declaration
 
 263 prologue_declaration:
 
 267       code_props plain_code;
 
 268       code_props_plain_init (&plain_code, $1, @1);
 
 269       code_props_translate_code (&plain_code);
 
 270       gram_scanner_last_string_free ();
 
 271       muscle_code_grow (union_seen ? "post_prologue" : "pre_prologue",
 
 272                         plain_code.code, @1);
 
 273       code_scanner_last_string_free ();
 
 277       muscle_percent_define_ensure ($1, @1, true);
 
 279 | "%define" variable content.opt
 
 281       muscle_percent_define_insert ($2, @2, $3,
 
 282                                     MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE);
 
 284 | "%defines"                       { defines_flag = true; }
 
 288       spec_defines_file = xstrdup ($2);
 
 292       muscle_percent_define_insert ("parse.error", @1, "verbose",
 
 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; }
 
 298 | "%file-prefix" "=" STRING        { spec_file_prefix = $3; } /* deprecated */
 
 301       nondeterministic_parser = true;
 
 304 | "%initial-action" "{...}"
 
 307       code_props_symbol_action_init (&action, $2, @2);
 
 308       code_props_translate_code (&action);
 
 309       gram_scanner_last_string_free ();
 
 310       muscle_code_grow ("initial_action", action.code, @2);
 
 311       code_scanner_last_string_free ();
 
 313 | "%language" STRING            { language_argmatch ($2, grammar_prio, @1); }
 
 314 | "%name-prefix" STRING         { spec_name_prefix = $2; }
 
 315 | "%name-prefix" "=" STRING     { spec_name_prefix = $3; } /* deprecated */
 
 316 | "%no-lines"                   { no_lines_flag = true; }
 
 317 | "%nondeterministic-parser"    { nondeterministic_parser = true; }
 
 318 | "%output" STRING              { spec_outfile = $2; }
 
 319 | "%output" "=" STRING          { spec_outfile = $3; }  /* deprecated */
 
 320 | "%param" { current_param = $1; } params { current_param = param_none; }
 
 321 | "%require" STRING             { version_check (&@2, $2); }
 
 324       char const *skeleton_user = $2;
 
 325       if (strchr (skeleton_user, '/'))
 
 327           size_t dir_length = strlen (current_file);
 
 328           char *skeleton_build;
 
 329           while (dir_length && current_file[dir_length - 1] != '/')
 
 331           while (dir_length && current_file[dir_length - 1] == '/')
 
 334             xmalloc (dir_length + 1 + strlen (skeleton_user) + 1);
 
 337               strncpy (skeleton_build, current_file, dir_length);
 
 338               skeleton_build[dir_length++] = '/';
 
 340           strcpy (skeleton_build + dir_length, skeleton_user);
 
 341           skeleton_user = uniqstr_new (skeleton_build);
 
 342           free (skeleton_build);
 
 344       skeleton_arg (skeleton_user, grammar_prio, @1);
 
 346 | "%token-table"                { token_table_flag = true; }
 
 347 | "%verbose"                    { report_flag |= report_states; }
 
 348 | "%yacc"                       { yacc_flag = true; }
 
 349 | /*FIXME: Err?  What is this horror doing here? */ ";"
 
 353    params "{...}"  { add_param (current_param, $2, @2); }
 
 354 | "{...}"          { add_param (current_param, $1, @1); }
 
 358 /*----------------------.
 
 359 | grammar_declaration.  |
 
 360 `----------------------*/
 
 363   precedence_declaration
 
 367       grammar_start_symbol_set ($2, @2);
 
 369 | "%destructor" "{...}" generic_symlist
 
 372       for (list = $3; list; list = list->next)
 
 373         symbol_list_destructor_set (list, $2, @2);
 
 374       symbol_list_free ($3);
 
 376 | "%printer" "{...}" generic_symlist
 
 379       for (list = $3; list; list = list->next)
 
 380         symbol_list_printer_set (list, $2, @2);
 
 381       symbol_list_free ($3);
 
 389       default_prec = false;
 
 393       /* Do not invoke muscle_percent_code_grow here since it invokes
 
 394          muscle_user_name_list_grow.  */
 
 395       muscle_code_grow ("percent_code()", $2, @2);
 
 396       code_scanner_last_string_free ();
 
 398 | "%code" ID braceless
 
 400       muscle_percent_code_grow ($2, @2, $3, @3);
 
 401       code_scanner_last_string_free ();
 
 410 %token PERCENT_UNION "%union";
 
 414 | ID             { muscle_code_grow ("union_name", $1, @1); }
 
 418   "%union" union_name braceless
 
 421       muscle_code_grow ("stype", $3, @3);
 
 422       code_scanner_last_string_free ();
 
 430   "%nterm" { current_class = nterm_sym; } symbol_defs.1
 
 432       current_class = unknown_sym;
 
 435 | "%token" { current_class = token_sym; } symbol_defs.1
 
 437       current_class = unknown_sym;
 
 440 | "%type" TAG symbols.1
 
 444       for (list = $3; list; list = list->next)
 
 445         symbol_type_set (list->content.sym, $2, @2);
 
 446       symbol_list_free ($3);
 
 450 precedence_declaration:
 
 451   precedence_declarator tag.opt symbols.prec
 
 455       for (list = $3; list; list = list->next)
 
 457           symbol_type_set (list->content.sym, current_type, @2);
 
 458           symbol_precedence_set (list->content.sym, current_prec, $1, @1);
 
 460       symbol_list_free ($3);
 
 465 precedence_declarator:
 
 466   "%left"       { $$ = left_assoc; }
 
 467 | "%right"      { $$ = right_assoc; }
 
 468 | "%nonassoc"   { $$ = non_assoc; }
 
 469 | "%precedence" { $$ = precedence_assoc; }
 
 473   /* Nothing. */ { current_type = NULL; }
 
 474 | TAG            { current_type = $1; tag_seen = true; }
 
 477 /* Just like symbols.1 but accept INT for the sake of POSIX.  */
 
 480     { $$ = symbol_list_sym_new ($1, @1); }
 
 481 | symbols.prec symbol.prec
 
 482     { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
 
 487   | symbol INT { $$ = $1; symbol_user_token_number_set ($1, $2, @2); }
 
 490 /* One or more symbols to be %typed. */
 
 493     { $$ = symbol_list_sym_new ($1, @1); }
 
 495     { $$ = symbol_list_prepend ($1, symbol_list_sym_new ($2, @2)); }
 
 499   generic_symlist_item { $$ = $1; }
 
 500 | generic_symlist generic_symlist_item { $$ = symbol_list_prepend ($1, $2); }
 
 503 generic_symlist_item:
 
 504   symbol    { $$ = symbol_list_sym_new ($1, @1); }
 
 505 | TAG       { $$ = symbol_list_type_new ($1, @1); }
 
 506 | "<*>"     { $$ = symbol_list_default_tagged_new (@1); }
 
 507 | "<>"      { $$ = symbol_list_default_tagless_new (@1); }
 
 510 /* One token definition.  */
 
 519        symbol_class_set ($1, current_class, @1, true);
 
 520        symbol_type_set ($1, current_type, @1);
 
 524       symbol_class_set ($1, current_class, @1, true);
 
 525       symbol_type_set ($1, current_type, @1);
 
 526       symbol_user_token_number_set ($1, $2, @2);
 
 530       symbol_class_set ($1, current_class, @1, true);
 
 531       symbol_type_set ($1, current_type, @1);
 
 532       symbol_make_alias ($1, $2, @$);
 
 534 | id INT string_as_id
 
 536       symbol_class_set ($1, current_class, @1, true);
 
 537       symbol_type_set ($1, current_type, @1);
 
 538       symbol_user_token_number_set ($1, $2, @2);
 
 539       symbol_make_alias ($1, $3, @$);
 
 543 /* One or more symbol definitions. */
 
 546 | symbol_defs.1 symbol_def
 
 550         /*------------------------------------------.
 
 551         | The grammar section: between the two %%.  |
 
 552         `------------------------------------------*/
 
 555   rules_or_grammar_declaration
 
 556 | grammar rules_or_grammar_declaration
 
 559 /* As a Bison extension, one can use the grammar declarations in the
 
 560    body of the grammar.  */
 
 561 rules_or_grammar_declaration:
 
 563 | grammar_declaration ";"
 
 571   id_colon named_ref.opt { current_lhs = $1; current_lhs_location = @1;
 
 572     current_lhs_named_ref = $2; } rhses.1
 
 576   rhs                { grammar_current_rule_end (@1); }
 
 577 | rhses.1 "|" rhs    { grammar_current_rule_end (@3); }
 
 583     { grammar_current_rule_begin (current_lhs, current_lhs_location,
 
 584                                   current_lhs_named_ref); }
 
 585 | rhs symbol named_ref.opt
 
 586     { grammar_current_rule_symbol_append ($2, @2, $3); }
 
 587 | rhs "{...}" named_ref.opt
 
 588     { grammar_current_rule_action_append ($2, @2, $3); }
 
 590     { grammar_current_rule_prec_set ($3, @3); }
 
 592     { grammar_current_rule_dprec_set ($3, @3); }
 
 594     { grammar_current_rule_merge_set ($3, @3); }
 
 598   /* Nothing. */ { $$ = 0; }
 
 600   BRACKETED_ID   { $$ = named_ref_new($1, @1); }
 
 604 /*---------------------------.
 
 605 | variable and content.opt.  |
 
 606 `---------------------------*/
 
 608 /* The STRING form of variable is deprecated and is not M4-friendly.
 
 609    For example, M4 fails for `%define "[" "value"'.  */
 
 612 | STRING { $$ = uniqstr_new ($1); }
 
 615 /* Some content or empty by default. */
 
 617   /* Nothing. */   { $$ = ""; }
 
 630       code_props plain_code;
 
 631       $1[strlen ($1) - 1] = '\n';
 
 632       code_props_plain_init (&plain_code, $1+1, @1);
 
 633       code_props_translate_code (&plain_code);
 
 634       gram_scanner_last_string_free ();
 
 635       $$ = plain_code.code;
 
 644 /* Identifiers are returned as uniqstr values by the scanner.
 
 645    Depending on their use, we may need to make them genuine symbols.  */
 
 649     { $$ = symbol_from_uniqstr ($1, @1); }
 
 652       $$ = symbol_get (char_name ($1), @1);
 
 653       symbol_class_set ($$, token_sym, @1, false);
 
 654       symbol_user_token_number_set ($$, $1, @1);
 
 659   ID_COLON { $$ = symbol_from_uniqstr ($1, @1); }
 
 668 /* A string used as an ID: quote it.  */
 
 672       $$ = symbol_get (quotearg_style (c_quoting_style, $1), @1);
 
 673       symbol_class_set ($$, token_sym, @1, false);
 
 681       code_props plain_code;
 
 682       code_props_plain_init (&plain_code, $2, @2);
 
 683       code_props_translate_code (&plain_code);
 
 684       gram_scanner_last_string_free ();
 
 685       muscle_code_grow ("epilogue", plain_code.code, @2);
 
 686       code_scanner_last_string_free ();
 
 693 /* Return the location of the left-hand side of a rule whose
 
 694    right-hand side is RHS[1] ... RHS[N].  Ignore empty nonterminals in
 
 695    the right-hand side, and return an empty location equal to the end
 
 696    boundary of RHS[0] if the right-hand side is empty.  */
 
 699 lloc_default (YYLTYPE const *rhs, int n)
 
 704   /* SGI MIPSpro 7.4.1m miscompiles "loc.start = loc.end = rhs[n].end;".
 
 705      The bug is fixed in 7.4.2m, but play it safe for now.  */
 
 706   loc.start = rhs[n].end;
 
 707   loc.end = rhs[n].end;
 
 709   /* Ignore empty nonterminals the start of the the right-hand side.
 
 710      Do not bother to ignore them at the end of the right-hand side,
 
 711      since empty nonterminals have the same end as their predecessors.  */
 
 712   for (i = 1; i <= n; i++)
 
 713     if (! equal_boundaries (rhs[i].start, rhs[i].end))
 
 715         loc.start = rhs[i].start;
 
 724 add_param (param_type type, char *decl, location loc)
 
 726   static char const alphanum[26 + 26 + 1 + 10] =
 
 727     "abcdefghijklmnopqrstuvwxyz"
 
 728     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
 732   char const *name_start = NULL;
 
 735     /* Stop on last actual character.  */
 
 736     for (p = decl; p[1]; p++)
 
 738            || ! memchr (alphanum, p[-1], sizeof alphanum))
 
 739           && memchr (alphanum, p[0], sizeof alphanum - 10))
 
 742     /* Strip the surrounding '{' and '}', and any blanks just inside
 
 744     while (*--p == ' ' || *p == '\t')
 
 747     while (*++decl == ' ' || *decl == '\t')
 
 752     complain_at (loc, _("missing identifier in parameter declaration"));
 
 759            memchr (alphanum, name_start[name_len], sizeof alphanum);
 
 763       name = xmalloc (name_len + 1);
 
 764       memcpy (name, name_start, name_len);
 
 765       name[name_len] = '\0';
 
 766       if (type & param_lex)
 
 767         muscle_pair_list_grow ("lex_param", decl, name);
 
 768       if (type & param_parse)
 
 769         muscle_pair_list_grow ("parse_param", decl, name);
 
 773   gram_scanner_last_string_free ();
 
 778 version_check (location const *loc, char const *version)
 
 780   if (strverscmp (version, PACKAGE_VERSION) > 0)
 
 782       complain_at (*loc, "require bison %s, but have %s",
 
 783                    version, PACKAGE_VERSION);
 
 789 gram_error (location const *loc, char const *msg)
 
 791   complain_at (*loc, "%s", msg);
 
 795 token_name (int type)
 
 797   return yytname[YYTRANSLATE (type)];
 
 808       buf[0] = '\''; buf[1] = c; buf[2] = '\''; buf[3] = '\0';
 
 809       return quotearg_style (escape_quoting_style, buf);