Add %initial-action, and add some forgotten location handling code in skeleton
authorNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 22:35:30 +0000 (08:35 +1000)
committerNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 22:35:30 +0000 (08:35 +1000)
ast.py
bison_lr1dfa.py
skel/skel.y
skel/y.tab.c.patch

diff --git a/ast.py b/ast.py
index 9fd42b1..3f0f119 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -1830,8 +1830,7 @@ class PYACC(element.Element):
         string_to_symbol,
         name_to_tag
       ):
-        assert pyacc.initial_action_code is None
-        pyacc.initial_action_code = self[0]
+        pyacc.initial_action_code.append(self[0])
 
     class Language(Item):
       # GENERATE ELEMENT() BEGIN
@@ -3246,7 +3245,7 @@ class PYACC(element.Element):
     ):
       pass
 
-  # GENERATE ELEMENT(list(ref) top_code, list(ref) before_union_code, list(ref) requires_code, str union_name, list(ref) union_code, list(ref) after_union_code, ref initial_action_code, list(ref) tags, list(ref) symbols, int n_productions, list(ref) productions, int first_nonterminal, int start_nonterminal, list(int) associativities) BEGIN
+  # GENERATE ELEMENT(list(ref) top_code, list(ref) before_union_code, list(ref) requires_code, str union_name, list(ref) union_code, list(ref) after_union_code, list(ref) initial_action_code, list(ref) tags, list(ref) symbols, int n_productions, list(ref) productions, int first_nonterminal, int start_nonterminal, list(int) associativities) BEGIN
   def __init__(
     self,
     tag = 'PYACC',
@@ -3259,7 +3258,7 @@ class PYACC(element.Element):
     union_name = '',
     union_code = [],
     after_union_code = [],
-    initial_action_code = None,
+    initial_action_code = [],
     tags = [],
     symbols = [],
     n_productions = -1,
@@ -3328,7 +3327,10 @@ class PYACC(element.Element):
       'after_union_code',
       ' '.join([element.serialize_ref(i, ref_list) for i in self.after_union_code])
     )
-    self.set('initial_action_code', element.serialize_ref(self.initial_action_code, ref_list))
+    self.set(
+      'initial_action_code',
+      ' '.join([element.serialize_ref(i, ref_list) for i in self.initial_action_code])
+    )
     self.set(
       'tags',
       ' '.join([element.serialize_ref(i, ref_list) for i in self.tags])
@@ -3371,7 +3373,10 @@ class PYACC(element.Element):
       element.deserialize_ref(i, ref_list)
       for i in self.get('after_union_code', '').split()
     ]
-    self.initial_action_code = element.deserialize_ref(self.get('initial_action_code', '-1'), ref_list)
+    self.initial_action_code = [
+      element.deserialize_ref(i, ref_list)
+      for i in self.get('initial_action_code', '').split()
+    ]
     self.tags = [
       element.deserialize_ref(i, ref_list)
       for i in self.get('tags', '').split()
@@ -3447,9 +3452,11 @@ class PYACC(element.Element):
           ', '.join([repr(i) for i in self.after_union_code])
         )
       )
-    if self.initial_action_code != None:
+    if len(self.initial_action_code):
       params.append(
-        'initial_action_code = {0:s}'.format(repr(self.initial_action_code))
+        'initial_action_code = [{0:s}]'.format(
+          ', '.join([repr(i) for i in self.initial_action_code])
+        )
       )
     if len(self.tags):
       params.append(
@@ -3501,7 +3508,7 @@ class PYACC(element.Element):
     self.union_name = ''
     self.union_code = []
     self.after_union_code = []
-    self.initial_action_code = None
+    self.initial_action_code = []
     self.precedences = 0
     self.symbols = [
       PYACC.Symbol(
index 1f98d6a..f52421a 100644 (file)
@@ -929,12 +929,17 @@ static const yytype_int16 yyr2[] =
                 )
               ).replace('YYDEBUG', type_prefix + 'DEBUG') # hack
             )
-          elif line == '/* GENERATE SECTION2INITIAL */\n':
+          elif line == '/* GENERATE INITIALACTION */\n':
             fout.write(
-              '''/* GENERATE SECTION2INITIAL BEGIN */
-{0:s}/* GENERATE SECTION2INITIAL END */
+              '''/* GENERATE INITIALACTION BEGIN */
+{0:s}/* GENERATE INITIALACTION END */
 '''.format(
-                '' #.join([i.get_text() for i in pyacc[1].code_blocks])
+                ''.join(
+                  [
+                    '{0:s}\n'.format(i.get_text())
+                    for i in pyacc.initial_action_code
+                  ]
+                )
               )
             )
           elif line == '/* GENERATE SECTION2 */\n':
index 24b6c6a..2353f09 100644 (file)
@@ -1,3 +1,4 @@
+
 %token TOKEN
 %start start
 %union { /* something */ }
index d3600dc..002b101 100644 (file)
@@ -1,5 +1,5 @@
---- y.tab.c.orig       2018-07-29 21:31:56.731898662 +1000
-+++ y.tab.c    2018-07-29 21:34:16.699902356 +1000
+--- y.tab.c.orig       2018-07-30 07:49:37.272876744 +1000
++++ y.tab.c    2018-07-30 08:33:25.532946107 +1000
 @@ -50,7 +50,7 @@
  #define YYSKELETON_NAME "yacc.c"
  
 -union YYSTYPE
 -{
 - /* something */ 
--
--};
 +/* GENERATE SECTION1REQUIRES */
  
+-};
+-
 -typedef union YYSTYPE YYSTYPE;
 -# define YYSTYPE_IS_TRIVIAL 1
 -# define YYSTYPE_IS_DECLARED 1
 -#endif
 +# if (defined YYLTYPE || defined YYLTYPE_IS_DECLARED) \
 +     && defined YYLTYPE_IS_TRIVIAL && YYLTYPE_IS_TRIVIAL
-+
 +/* Print *YYLOCP on YYO.  Private, do not rely on its existence. */
-+
+-# define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
 +YY_ATTRIBUTE_UNUSED
 +static unsigned
 +yy_location_print_ (FILE *yyo, YYLTYPE const * const yylocp)
 +    }
 +  return res;
 + }
++
 +#  define YY_LOCATION_PRINT(File, Loc)          \
 +  yy_location_print_ (File, &(Loc))
--# define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
++
 +# else
 +#  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
 +# endif
    yystacksize = YYINITDEPTH;
  
    YYDPRINTF ((stderr, "Starting parse\n"));
-@@ -1075,6 +1153,9 @@
+@@ -1009,6 +1087,13 @@
+   yyerrstatus = 0;
+   yynerrs = 0;
+   yychar = YYEMPTY; /* Cause a token to be read.  */
++
++/* User initialization code.  */
++/* GENERATE INITIALACTION */
++
++#if defined YYLTYPE || defined YYLTYPE_IS_DECLARED
++  yylsp[0] = yylloc;
++#endif
+   goto yysetstate;
+ /*------------------------------------------------------------.
+@@ -1034,16 +1119,29 @@
+            memory.  */
+         YYSTYPE *yyvs1 = yyvs;
+         yytype_int16 *yyss1 = yyss;
++#if defined YYLTYPE || defined YYLTYPE_IS_DECLARED
++        YYLTYPE *yyls1 = yyls;
++#endif
+         /* Each stack pointer address is followed by the size of the
+            data in use in that stack, in bytes.  This used to be a
+            conditional around just the two extra args, but that might
+            be undefined if yyoverflow is a macro.  */
++#if defined YYLTYPE || defined YYLTYPE_IS_DECLARED
+         yyoverflow (YY_("memory exhausted"),
+                     &yyss1, yysize * sizeof (*yyssp),
+                     &yyvs1, yysize * sizeof (*yyvsp),
++                    &yyls1, yysize * sizeof (*yylsp),
+                     &yystacksize);
++        yyls = yyls1;
++#else
++        yyoverflow (YY_("memory exhausted"),
++                    &yyss1, yysize * sizeof (*yyssp),
++                    &yyvs1, yysize * sizeof (*yyvsp),
++                    &yystacksize);
++
++#endif
+         yyss = yyss1;
+         yyvs = yyvs1;
+       }
+@@ -1066,6 +1164,9 @@
+           goto yyexhaustedlab;
+         YYSTACK_RELOCATE (yyss_alloc, yyss);
+         YYSTACK_RELOCATE (yyvs_alloc, yyvs);
++#if defined YYLTYPE || defined YYLTYPE_IS_DECLARED
++        YYSTACK_RELOCATE (yyls_alloc, yyls);
++#endif
+ #  undef YYSTACK_RELOCATE
+         if (yyss1 != yyssa)
+           YYSTACK_FREE (yyss1);
+@@ -1075,6 +1176,9 @@
  
        yyssp = yyss + yysize - 1;
        yyvsp = yyvs + yysize - 1;
  
        YYDPRINTF ((stderr, "Stack size increased to %lu\n",
                    (unsigned long int) yystacksize));
-@@ -1109,7 +1190,15 @@
+@@ -1109,7 +1213,15 @@
    if (yychar == YYEMPTY)
      {
        YYDPRINTF ((stderr, "Reading a token: "));
      }
  
    if (yychar <= YYEOF)
-@@ -1152,7 +1241,9 @@
+@@ -1152,7 +1264,9 @@
    YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
    *++yyvsp = yylval;
    YY_IGNORE_MAYBE_UNINITIALIZED_END
    goto yynewstate;
  
  
-@@ -1183,18 +1274,14 @@
+@@ -1183,18 +1297,14 @@
       GCC warning that YYVAL may be used uninitialized.  */
    yyval = yyvsp[1-yylen];
  
  
        default: break;
      }
-@@ -1216,6 +1303,9 @@
+@@ -1216,6 +1326,9 @@
    YY_STACK_PRINT (yyss, yyssp);
  
    *++yyvsp = yyval;
  
    /* Now 'shift' the result of the reduction.  Determine what state
       that goes to, based on the state we popped back to and the rule
-@@ -1245,7 +1335,11 @@
+@@ -1245,7 +1358,11 @@
      {
        ++yynerrs;
  #if ! YYERROR_VERBOSE
  #else
  # define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
                                          yyssp, yytoken)
-@@ -1272,7 +1366,11 @@
+@@ -1272,7 +1389,11 @@
                  yymsgp = yymsg;
                }
            }
          if (yysyntax_error_status == 2)
            goto yyexhaustedlab;
        }
-@@ -1280,7 +1378,9 @@
+@@ -1280,7 +1401,9 @@
  #endif
      }
  
  
    if (yyerrstatus == 3)
      {
-@@ -1295,8 +1395,13 @@
+@@ -1295,8 +1418,13 @@
          }
        else
          {
            yychar = YYEMPTY;
          }
      }
-@@ -1317,6 +1422,9 @@
+@@ -1317,6 +1445,9 @@
    if (/*CONSTCOND*/ 0)
       goto yyerrorlab;
  
    /* Do not reclaim the symbols of the rule whose action triggered
       this YYERROR.  */
    YYPOPSTACK (yylen);
-@@ -1350,9 +1458,14 @@
+@@ -1350,9 +1481,14 @@
        if (yyssp == yyss)
          YYABORT;
  
        YYPOPSTACK (1);
        yystate = *yyssp;
        YY_STACK_PRINT (yyss, yyssp);
-@@ -1362,6 +1475,13 @@
+@@ -1362,6 +1498,13 @@
    *++yyvsp = yylval;
    YY_IGNORE_MAYBE_UNINITIALIZED_END
  
  
    /* Shift the error token.  */
    YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
-@@ -1389,7 +1509,11 @@
+@@ -1389,7 +1532,11 @@
  | yyexhaustedlab -- memory exhaustion comes here.  |
  `-------------------------------------------------*/
  yyexhaustedlab:
    yyresult = 2;
    /* Fall through.  */
  #endif
-@@ -1400,8 +1524,13 @@
+@@ -1400,8 +1547,13 @@
        /* Make sure we have latest lookahead translation.  See comments at
           user semantic actions for why this is necessary.  */
        yytoken = YYTRANSLATE (yychar);
      }
    /* Do not reclaim the symbols of the rule whose action triggered
       this YYABORT or YYACCEPT.  */
-@@ -1409,8 +1538,13 @@
+@@ -1409,8 +1561,13 @@
    YY_STACK_PRINT (yyss, yyssp);
    while (yyssp != yyss)
      {
        YYPOPSTACK (1);
      }
  #ifndef yyoverflow
-@@ -1423,3 +1557,5 @@
+@@ -1423,3 +1580,5 @@
  #endif
    return yyresult;
  }