From 8e092082cb318789ab315c4e609d632becf7ba7c Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 28 Nov 2018 05:19:38 +0100 Subject: [PATCH] parser: factor the symbol definition * src/parse-gram.y (int.opt, string_as_id.opt): New. (symbol_def): Use it. --- src/parse-gram.y | 67 +++++++++++++++++++++--------------------------- 1 file changed, 29 insertions(+), 38 deletions(-) diff --git a/src/parse-gram.y b/src/parse-gram.y index a9f38abf..dd15235a 100644 --- a/src/parse-gram.y +++ b/src/parse-gram.y @@ -503,52 +503,37 @@ symbol_def: current_type = $1; tag_seen = true; } -| id +| id int.opt[num] string_as_id.opt[alias] { - symbol_class_set ($1, current_class, @1, true); - symbol_type_set ($1, current_type, @1); - } -| id INT - { - if (current_class != token_sym) + symbol_class_set ($id, current_class, @id, true); + symbol_type_set ($id, current_type, @id); + if (0 <= $num) { - gram_error (&@2, - _("non-terminals cannot be given an explicit number")); - YYERROR; + if (current_class != token_sym) + gram_error (&@num, + _("non-terminals cannot be given an explicit number")); + else + symbol_user_token_number_set ($id, $num, @num); } - symbol_class_set ($1, current_class, @1, true); - 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 ($alias) { - gram_error (&@2, - _("non-terminals cannot be given a string alias")); - YYERROR; + if (current_class != token_sym) + gram_error (&@alias, + _("non-terminals cannot be given a string alias")); + else + symbol_make_alias ($id, $alias, @alias); } - symbol_class_set ($1, current_class, @1, true); - symbol_type_set ($1, current_type, @1); - symbol_make_alias ($1, $2, @2); - } -| id INT string_as_id - { - 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); + if (current_class != token_sym && (0 <= $num || !$alias)) + YYERROR; } ; +%type int.opt; +int.opt: + %empty { $$ = -1; } +| INT +; + /* One or more symbol definitions. */ symbol_defs.1: symbol_def @@ -703,6 +688,12 @@ string_as_id: } ; +%type string_as_id.opt; +string_as_id.opt: + %empty { $$ = NULL; } +| string_as_id +; + epilogue.opt: %empty | "%%" EPILOGUE