1 /* Bison Grammar Parser                             -*- C -*-
 
   3    Copyright (C) 2002, 2003 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 2 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, write to the Free Software
 
  19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 
  36 #include "conflicts.h"
 
  40 #include "muscle_tab.h"
 
  45 /* Produce verbose syntax errors.  */
 
  46 #define YYERROR_VERBOSE 1
 
  48 #define YYLLOC_DEFAULT(Current, Rhs, N)  (Current) = lloc_default (Rhs, N)
 
  49 static YYLTYPE lloc_default (YYLTYPE const *, int);
 
  51 /* Request detailed syntax error messages, and pass them to GRAM_ERROR.
 
  52    FIXME: depends on the undocumented availability of YYLLOC.  */
 
  54 #define yyerror(Msg) \
 
  55         gram_error (&yylloc, Msg)
 
  56 static void gram_error (location const *, char const *);
 
  58 #define YYPRINT(File, Type, Value) \
 
  59         print_token_value (File, Type, &Value)
 
  60 static void print_token_value (FILE *, int, YYSTYPE const *);
 
  62 static void add_param (char const *, char *, location);
 
  64 symbol_class current_class = unknown_sym;
 
  65 uniqstr current_type = 0;
 
  67 location current_lhs_location;
 
  73 /* Only NUMBERS have a value.  */
 
  84 /* Define the tokens together with their human representation.  */
 
  85 %token GRAM_EOF 0 "end of file"
 
  86 %token STRING     "string"
 
  89 %token PERCENT_TOKEN       "%token"
 
  90 %token PERCENT_NTERM       "%nterm"
 
  92 %token PERCENT_TYPE        "%type"
 
  93 %token PERCENT_DESTRUCTOR  "%destructor {...}"
 
  94 %token PERCENT_PRINTER     "%printer {...}"
 
  96 %token PERCENT_UNION       "%union {...}"
 
  98 %token PERCENT_LEFT        "%left"
 
  99 %token PERCENT_RIGHT       "%right"
 
 100 %token PERCENT_NONASSOC    "%nonassoc"
 
 102 %token PERCENT_PREC          "%prec"
 
 103 %token PERCENT_DPREC         "%dprec"
 
 104 %token PERCENT_MERGE         "%merge"
 
 107 /*----------------------.
 
 108 | Global Declarations.  |
 
 109 `----------------------*/
 
 112   PERCENT_DEBUG         "%debug"
 
 113   PERCENT_DEFINE        "%define"
 
 114   PERCENT_DEFINES       "%defines"
 
 115   PERCENT_ERROR_VERBOSE "%error-verbose"
 
 116   PERCENT_EXPECT        "%expect"
 
 117   PERCENT_FILE_PREFIX   "%file-prefix"
 
 118   PERCENT_GLR_PARSER    "%glr-parser"
 
 119   PERCENT_LEX_PARAM     "%lex-param {...}"
 
 120   PERCENT_LOCATIONS     "%locations"
 
 121   PERCENT_NAME_PREFIX   "%name-prefix"
 
 122   PERCENT_NO_LINES      "%no-lines"
 
 123   PERCENT_NONDETERMINISTIC_PARSER    "%nondeterministic-parser"
 
 124   PERCENT_OUTPUT        "%output"
 
 125   PERCENT_PARSE_PARAM   "%parse-param {...}"
 
 126   PERCENT_PURE_PARSER   "%pure-parser"
 
 127   PERCENT_SKELETON      "%skeleton"
 
 128   PERCENT_START         "%start"
 
 129   PERCENT_TOKEN_TABLE   "%token-table"
 
 130   PERCENT_VERBOSE       "%verbose"
 
 138 %token ID              "identifier"
 
 139 %token ID_COLON        "identifier:"
 
 140 %token PERCENT_PERCENT "%%"
 
 141 %token PROLOGUE        "%{...%}"
 
 142 %token EPILOGUE        "epilogue"
 
 143 %token BRACED_CODE     "{...}"
 
 146 %type <chars> STRING string_content
 
 156 %type <symbol> ID ID_COLON symbol string_as_id
 
 157 %type <assoc> precedence_declarator
 
 158 %type <list>  symbols.1
 
 162   declarations "%%" grammar epilogue.opt
 
 166         /*------------------------------------.
 
 167         | Declarations: before the first %%.  |
 
 168         `------------------------------------*/
 
 172 | declarations declaration
 
 177 | PROLOGUE                                 { prologue_augment ($1, @1); }
 
 178 | "%debug"                                 { debug_flag = true; }
 
 179 | "%define" string_content string_content  { muscle_insert ($2, $3); }
 
 180 | "%defines"                               { defines_flag = true; }
 
 181 | "%error-verbose"                         { error_verbose = true; }
 
 182 | "%expect" INT                            { expected_conflicts = $2; }
 
 183 | "%file-prefix" "=" string_content        { spec_file_prefix = $3; }
 
 184 | "%glr-parser"                            { nondeterministic_parser = true;
 
 186 | "%lex-param {...}"                       { add_param ("lex_param", $1, @1); }
 
 187 | "%locations"                             { locations_flag = true; }
 
 188 | "%name-prefix" "=" string_content        { spec_name_prefix = $3; }
 
 189 | "%no-lines"                              { no_lines_flag = true; }
 
 190 | "%nondeterministic-parser"               { nondeterministic_parser = true; }
 
 191 | "%output" "=" string_content             { spec_outfile = $3; }
 
 192 | "%parse-param {...}"                   { add_param ("parse_param", $1, @1); }
 
 193 | "%pure-parser"                           { pure_parser = true; }
 
 194 | "%skeleton" string_content               { skeleton = $2; }
 
 195 | "%token-table"                           { token_table_flag = true; }
 
 196 | "%verbose"                               { report_flag = report_states; }
 
 197 | "%yacc"                                  { yacc_flag = true; }
 
 202   precedence_declaration
 
 206       grammar_start_symbol_set ($2, @2);
 
 211       MUSCLE_INSERT_INT ("stype_line", @1.start.line);
 
 212       muscle_insert ("stype", $1);
 
 214 | "%destructor {...}" symbols.1
 
 217       for (list = $2; list; list = list->next)
 
 218         symbol_destructor_set (list->sym, $1, @1);
 
 219       symbol_list_free ($2);
 
 221 | "%printer {...}" symbols.1
 
 224       for (list = $2; list; list = list->next)
 
 225         symbol_printer_set (list->sym, $1, list->location);
 
 226       symbol_list_free ($2);
 
 231   "%nterm" { current_class = nterm_sym; } symbol_defs.1
 
 233       current_class = unknown_sym;
 
 236 | "%token" { current_class = token_sym; } symbol_defs.1
 
 238       current_class = unknown_sym;
 
 241 | "%type" TYPE symbols.1
 
 244       for (list = $3; list; list = list->next)
 
 245         symbol_type_set (list->sym, $2, @2);
 
 246       symbol_list_free ($3);
 
 250 precedence_declaration:
 
 251   precedence_declarator type.opt symbols.1
 
 255       for (list = $3; list; list = list->next)
 
 257           symbol_type_set (list->sym, current_type, @2);
 
 258           symbol_precedence_set (list->sym, current_prec, $1, @1);
 
 260       symbol_list_free ($3);
 
 265 precedence_declarator:
 
 266   "%left"     { $$ = left_assoc; }
 
 267 | "%right"    { $$ = right_assoc; }
 
 268 | "%nonassoc" { $$ = non_assoc; }
 
 272   /* Nothing. */ { current_type = NULL; }
 
 273 | TYPE           { current_type = $1; }
 
 276 /* One or more nonterminals to be %typed. */
 
 279   symbol            { $$ = symbol_list_new ($1, @1); }
 
 280 | symbols.1 symbol  { $$ = symbol_list_prepend ($1, $2, @2); }
 
 283 /* One token definition.  */
 
 291        symbol_class_set ($1, current_class, @1);
 
 292        symbol_type_set ($1, current_type, @1);
 
 296       symbol_class_set ($1, current_class, @1);
 
 297       symbol_type_set ($1, current_type, @1);
 
 298       symbol_user_token_number_set ($1, $2, @2);
 
 302       symbol_class_set ($1, current_class, @1);
 
 303       symbol_type_set ($1, current_type, @1);
 
 304       symbol_make_alias ($1, $2, @$);
 
 306 | ID INT string_as_id
 
 308       symbol_class_set ($1, current_class, @1);
 
 309       symbol_type_set ($1, current_type, @1);
 
 310       symbol_user_token_number_set ($1, $2, @2);
 
 311       symbol_make_alias ($1, $3, @$);
 
 315 /* One or more symbol definitions. */
 
 318 | symbol_defs.1 symbol_def
 
 322         /*------------------------------------------.
 
 323         | The grammar section: between the two %%.  |
 
 324         `------------------------------------------*/
 
 327   rules_or_grammar_declaration
 
 328 | grammar rules_or_grammar_declaration
 
 331 /* As a Bison extension, one can use the grammar declarations in the
 
 332    body of the grammar.  */
 
 333 rules_or_grammar_declaration:
 
 335 | grammar_declaration ";"
 
 338         complain_at (@$, _("POSIX forbids declarations in the grammar"));
 
 347   ID_COLON { current_lhs = $1; current_lhs_location = @1; } rhses.1
 
 351   rhs                { grammar_rule_end (@1); }
 
 352 | rhses.1 "|" rhs    { grammar_rule_end (@3); }
 
 358     { grammar_rule_begin (current_lhs, current_lhs_location); }
 
 360     { grammar_current_rule_symbol_append ($2, @2); }
 
 362     { grammar_current_rule_action_append ($2, @2); }
 
 364     { grammar_current_rule_prec_set ($3, @3); }
 
 366     { grammar_current_rule_dprec_set ($3, @3); }
 
 368     { grammar_current_rule_merge_set ($3, @3); }
 
 373 | string_as_id    { $$ = $1; }
 
 381 /* A string used as an ID: we have to keep the quotes. */
 
 385       $$ = symbol_get ($1, @1);
 
 386       symbol_class_set ($$, token_sym, @1);
 
 390 /* A string used for its contents.  Strip the quotes. */
 
 395       $$[strlen ($$) - 1] = '\0';
 
 403       epilogue_augment ($2, @2);
 
 404       scanner_last_string_free ();
 
 411 /* Return the location of the left-hand side of a rule whose
 
 412    right-hand side is RHS[1] ... RHS[N].  Ignore empty nonterminals in
 
 413    the right-hand side, and return an empty location equal to the end
 
 414    boundary of RHS[0] if the right-hand side is empty.  */
 
 417 lloc_default (YYLTYPE const *rhs, int n)
 
 421   loc.start = loc.end = rhs[n].end;
 
 423   /* Ignore empty nonterminals the start of the the right-hand side.
 
 424      Do not bother to ignore them at the end of the right-hand side,
 
 425      since empty nonterminals have the same end as their predecessors.  */
 
 426   for (i = 1; i <= n; i++)
 
 427     if (! equal_boundaries (rhs[i].start, rhs[i].end))
 
 429         loc.start = rhs[i].start;
 
 437 /* Add a lex-param or a parse-param (depending on TYPE) with
 
 438    declaration DECL and location LOC.  */
 
 441 add_param (char const *type, char *decl, location loc)
 
 443   static char const alphanum[] =
 
 445     "abcdefghijklmnopqrstuvwxyz"
 
 446     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
 448   char const *alpha = alphanum + 10;
 
 449   char const *name_start = NULL;
 
 452   for (p = decl; *p; p++)
 
 453     if ((p == decl || ! strchr (alphanum, p[-1])) && strchr (alpha, p[0]))
 
 456   /* Strip the surrounding '{' and '}'.  */
 
 461     complain_at (loc, _("missing identifier in parameter declaration"));
 
 468            name_start[name_len] && strchr (alphanum, name_start[name_len]);
 
 472       name = xmalloc (name_len + 1);
 
 473       memcpy (name, name_start, name_len);
 
 474       name[name_len] = '\0';
 
 475       muscle_pair_list_grow (type, decl, name);
 
 479   scanner_last_string_free ();
 
 482 /*----------------------------------------------------.
 
 483 | When debugging the parser, display tokens' values.  |
 
 484 `----------------------------------------------------*/
 
 487 print_token_value (FILE *file, int type, YYSTYPE const *value)
 
 493       fprintf (file, " = %s", value->symbol->tag);
 
 497       fprintf (file, " = %d", value->integer);
 
 501       fprintf (file, " = \"%s\"", value->chars);
 
 505       fprintf (file, " = <%s>", value->uniqstr);
 
 509     case PERCENT_DESTRUCTOR:
 
 510     case PERCENT_LEX_PARAM:
 
 511     case PERCENT_PARSE_PARAM:
 
 512     case PERCENT_PRINTER:
 
 516       fprintf (file, " = {{ %s }}", value->chars);
 
 520       fprintf (file, "unknown token type");
 
 526 gram_error (location const *loc, char const *msg)
 
 528   complain_at (*loc, "%s", msg);
 
 532 token_name (int type)
 
 534   return yytname[type];