bison-patches
[Top][All Lists]
Advanced

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

[PATCH 4/6] %merge: fix compatibility with api.value.type=union


From: Akim Demaille
Subject: [PATCH 4/6] %merge: fix compatibility with api.value.type=union
Date: Thu, 31 Dec 2020 08:14:36 +0100

Reported by Jot Dot.
https://lists.gnu.org/r/help-bison/2020-12/msg00014.html

* data/skeletons/glr.c, data/skeletons/glr2.cc (b4_call_merger): Use
the symbol's slot, not its type.
* examples/c/glr/c++-types.y: Use explicit per-symbol typing together
with api.value.type=union.
(yylex): Use yytoken_kind_t.
---
 TODO                       |  9 +++++++++
 data/skeletons/glr.c       |  2 +-
 data/skeletons/glr2.cc     |  2 +-
 examples/c/glr/c++-types.y | 31 ++++++++++++++++++++-----------
 4 files changed, 31 insertions(+), 13 deletions(-)

diff --git a/TODO b/TODO
index f88a12d4..1b3e008f 100644
--- a/TODO
+++ b/TODO
@@ -2,6 +2,15 @@
 ** glr
 There is no test with "Parse on stack %ld rejected by rule %d" in it.
 
+** %merge
+Tests with typed %merge: 716 717 718 740 741 742 746 747 748
+
+716: Duplicate representation of merged trees: glr.c FAILED 
(glr-regression.at:517)
+740: Leaked semantic values if user action cuts parse: glr.c FAILED 
(glr-regression.at:1230)
+746: Incorrect lookahead during nondeterministic GLR: glr.c FAILED 
(glr-regression.at:1610)
+
+Document typed merges.
+
 ** yyrline etc.
 Clarify that rule numbers in the skeletons are 1-based.
 
diff --git a/data/skeletons/glr.c b/data/skeletons/glr.c
index 08dd7817..33c04bc9 100644
--- a/data/skeletons/glr.c
+++ b/data/skeletons/glr.c
@@ -152,7 +152,7 @@ m4_define([b4_rhs_location],
 m4_define([b4_call_merger],
 [b4_case([$1],
          [    b4_symbol_if([$3], [has_type],
-                           [yy0->b4_symbol($3, type) = $2 (*yy0, *yy1);],
+                           [yy0->b4_symbol($3, slot) = $2 (*yy0, *yy1);],
                            [*yy0 = $2 (*yy0, *yy1);])])])
 
 
diff --git a/data/skeletons/glr2.cc b/data/skeletons/glr2.cc
index 8210c628..796374b3 100644
--- a/data/skeletons/glr2.cc
+++ b/data/skeletons/glr2.cc
@@ -127,7 +127,7 @@ m4_define([b4_rhs_location],
 m4_define([b4_call_merger],
 [b4_case([$1],
          [    b4_symbol_if([$3], [has_type],
-                           [yy0->b4_symbol($3, type) = $2 (*yy0, *yy1);],
+                           [yy0->b4_symbol($3, slot) = $2 (*yy0, *yy1);],
                            [*yy0 = $2 (*yy0, *yy1);])])])
 
 # b4_lex
diff --git a/examples/c/glr/c++-types.y b/examples/c/glr/c++-types.y
index 33bf58a6..a760b600 100644
--- a/examples/c/glr/c++-types.y
+++ b/examples/c/glr/c++-types.y
@@ -31,7 +31,7 @@
   typedef union Node Node;
 }
 
-%define api.value.type {Node *}
+%define api.value.type union
 
 %code
 {
@@ -47,11 +47,11 @@
   static Node *new_term (char *);
   static void free_node (Node *);
   static char *node_to_string (Node *);
-  static YYSTYPE stmtMerge (YYSTYPE x0, YYSTYPE x1);
+  static Node *stmtMerge (YYSTYPE x0, YYSTYPE x1);
 
   static int location_print (FILE *yyo, YYLTYPE const * const yylocp);
   static void yyerror (YYLTYPE const * const llocp, const char *msg);
-  static int yylex (YYSTYPE *lvalp, YYLTYPE *llocp);
+  static yytoken_kind_t yylex (YYSTYPE *lvalp, YYLTYPE *llocp);
 }
 
 %expect-rr 1
@@ -65,7 +65,8 @@
 
 %glr-parser
 
-%destructor { free_node ($$); } stmt expr decl declarator TYPENAME ID
+%type <Node*> stmt expr decl declarator TYPENAME ID
+%destructor { free_node ($$); } <Node*>
 
 %%
 
@@ -152,7 +153,8 @@ void yyerror (YYLTYPE const * const llocp, const char *msg)
   fprintf (stderr, ": %s\n", msg);
 }
 
-int yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
+yytoken_kind_t
+yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
 {
   static int lineNum = 1;
   static int colNum = 0;
@@ -178,7 +180,7 @@ int yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
           break;
         default:
           {
-            int tok;
+            yytoken_kind_t tok;
             llocp->first_line = llocp->last_line = lineNum;
             llocp->first_column = colNum;
             if (isalpha (c))
@@ -197,14 +199,21 @@ int yylex (YYSTYPE *lvalp, YYLTYPE *llocp)
 
                 ungetc (c, stdin);
                 buffer[i++] = 0;
-                tok = isupper ((unsigned char) buffer[0]) ? TYPENAME : ID;
-                *lvalp = new_term (strcpy (malloc (i), buffer));
+                if (isupper ((unsigned char) buffer[0]))
+                  {
+                    tok = TYPENAME;
+                    lvalp->TYPENAME = new_term (strcpy (malloc (i), buffer));
+                  }
+                else
+                  {
+                    tok = ID;
+                    lvalp->ID = new_term (strcpy (malloc (i), buffer));
+                  }
               }
             else
               {
                 colNum += 1;
                 tok = c;
-                *lvalp = NULL;
               }
             llocp->last_column = colNum-1;
             return tok;
@@ -289,8 +298,8 @@ node_to_string (Node *node)
 }
 
 
-static YYSTYPE
+static Node*
 stmtMerge (YYSTYPE x0, YYSTYPE x1)
 {
-  return new_nterm ("<OR>(%s,%s)", x0, x1, NULL);
+  return new_nterm ("<OR>(%s,%s)", x0.stmt, x1.stmt, NULL);
 }
-- 
2.29.2




reply via email to

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