bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

parser: factor the symbol definition


From: Akim Demaille
Subject: parser: factor the symbol definition
Date: Thu, 29 Nov 2018 06:09:58 +0100

I'm not completely done with Rici's comments about the
way we handle symbols.

(http://lists.gnu.org/archive/html/bug-bison/2018-10/msg00000.html).


commit a5fb74e558525b2c5a78a8a287dc317a4158b316
Author: Akim Demaille <address@hidden>
Date:   Wed Nov 28 05:19:38 2018 +0100

    parser: factor the symbol definition
    
    * src/parse-gram.y (int.opt, string_as_id.opt): New.
    (symbol_def): Use it.

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;
-        }
-      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)
-        {
-          gram_error (&@2,
-                      _("non-terminals cannot be given a string alias"));
-          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_make_alias ($1, $2, @2);
-    }
-| id INT string_as_id
-    {
-      if (current_class != token_sym)
+      if ($alias)
         {
-          gram_error (&@2,
-                      _("non-terminals cannot be given an explicit number"));
-          gram_error (&@3,
-                      _("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_user_token_number_set ($1, $2, @2);
-      symbol_make_alias ($1, $3, @3);
+      if (current_class != token_sym && (0 <= $num || !$alias))
+        YYERROR;
     }
 ;
 
+%type <int> 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 <symbol*> string_as_id.opt;
+string_as_id.opt:
+  %empty             { $$ = NULL; }
+| string_as_id
+;
+
 epilogue.opt:
   %empty
 | "%%" EPILOGUE




reply via email to

[Prev in Thread] Current Thread [Next in Thread]