parser: factor the symbol definition

* src/parse-gram.y (int.opt, string_as_id.opt): New.
(symbol_def): Use it.
This commit is contained in:
Akim Demaille
2018-11-28 05:19:38 +01:00
parent 2c5e933672
commit 8e092082cb

View File

@@ -503,50 +503,35 @@ symbol_def:
current_type = $1; current_type = $1;
tag_seen = true; tag_seen = true;
} }
| id | id int.opt[num] string_as_id.opt[alias]
{ {
symbol_class_set ($1, current_class, @1, true); symbol_class_set ($id, current_class, @id, true);
symbol_type_set ($1, current_type, @1); symbol_type_set ($id, current_type, @id);
} if (0 <= $num)
| id INT
{ {
if (current_class != token_sym) if (current_class != token_sym)
{ gram_error (&@num,
gram_error (&@2,
_("non-terminals cannot be given an explicit number")); _("non-terminals cannot be given an explicit number"));
YYERROR; else
symbol_user_token_number_set ($id, $num, @num);
} }
symbol_class_set ($1, current_class, @1, true); if ($alias)
symbol_type_set ($1, current_type, @1);
symbol_user_token_number_set ($1, $2, @2);
}
| id string_as_id
{ {
if (current_class != token_sym) if (current_class != token_sym)
{ gram_error (&@alias,
gram_error (&@2,
_("non-terminals cannot be given a string alias")); _("non-terminals cannot be given a string alias"));
else
symbol_make_alias ($id, $alias, @alias);
}
if (current_class != token_sym && (0 <= $num || !$alias))
YYERROR; YYERROR;
} }
symbol_class_set ($1, current_class, @1, true); ;
symbol_type_set ($1, current_type, @1);
symbol_make_alias ($1, $2, @2); %type <int> int.opt;
} int.opt:
| id INT string_as_id %empty { $$ = -1; }
{ | INT
if (current_class != token_sym)
{
gram_error (&@2,
_("non-terminals cannot be given an explicit number"));
gram_error (&@3,
_("non-terminals cannot be given a string alias"));
YYERROR;
}
symbol_class_set ($1, current_class, @1, true);
symbol_type_set ($1, current_type, @1);
symbol_user_token_number_set ($1, $2, @2);
symbol_make_alias ($1, $3, @3);
}
; ;
/* One or more symbol definitions. */ /* One or more symbol definitions. */
@@ -703,6 +688,12 @@ string_as_id:
} }
; ;
%type <symbol*> string_as_id.opt;
string_as_id.opt:
%empty { $$ = NULL; }
| string_as_id
;
epilogue.opt: epilogue.opt:
%empty %empty
| "%%" EPILOGUE | "%%" EPILOGUE