Implement %define api.pure (sets #define YYPURE to 0=false, 1=true, 2=full)
authorNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 06:46:40 +0000 (16:46 +1000)
committerNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 07:19:28 +0000 (17:19 +1000)
ast.py
bison_lr1dfa.py
skel/y.tab.c.patch

diff --git a/ast.py b/ast.py
index a60be6b..b4303ce 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -1476,17 +1476,32 @@ class PYACC(element.Element):
         if name == 'api.prefix':
           section.api_prefix = value
         elif name == 'api.pure':
-          section.api_pure = value
+          if value == 'full':
+            section.api_pure = 2
+          elif value == '' or value == 'true':
+            section.api_pure = 1
+          elif value == 'false':
+            section.api_pure = 0
+          else:
+            assert False
         elif name == 'locations':
-          assert value == '' or value == 'true' or value == 'false'
-          section.locations = value != 'false'
+          if value == '' or value == 'true':
+            section.locations = True
+          elif value == 'false':
+            section.locations = False
+          else:
+            assert False
         elif name == 'parse.error':
           section.parse_error = value
         elif name == 'parse.lac':
           section.parse_lac = value
         elif name == 'parse.trace':
-          assert value == '' or value == 'true' or value == 'false'
-          section.parse_trace = value != 'false'
+          if value == '' or value == 'true':
+            section.parse_trace = True
+          elif value == 'false':
+            section.parse_trace = False
+          else:
+            assert False
         else:
           assert False
 
@@ -2144,7 +2159,7 @@ class PYACC(element.Element):
         return 'ast.PYACC.Section1.YACC({0:s})'.format(', '.join(params))
       # GENERATE END
 
-    # GENERATE ELEMENT(str api_prefix, str api_pure, bool locations, str parse_error, str parse_lac, bool parse_trace, bool defines, int expect, bool verbose) BEGIN
+    # GENERATE ELEMENT(str api_prefix, int api_pure, bool locations, str parse_error, str parse_lac, bool parse_trace, bool defines, int expect, bool verbose) BEGIN
     def __init__(
       self,
       tag = 'PYACC_Section1',
@@ -2152,7 +2167,7 @@ class PYACC(element.Element):
       text = '',
       children = [],
       api_prefix = '',
-      api_pure = '',
+      api_pure = -1,
       locations = False,
       parse_error = '',
       parse_lac = '',
@@ -2169,7 +2184,11 @@ class PYACC(element.Element):
         children
       )
       self.api_prefix = api_prefix
-      self.api_pure = api_pure
+      self.api_pure = (
+        element.deserialize_int(api_pure)
+      if isinstance(api_pure, str) else
+        api_pure
+      )
       self.locations = (
         element.deserialize_bool(locations)
       if isinstance(locations, str) else
@@ -2200,7 +2219,7 @@ class PYACC(element.Element):
     def serialize(self, ref_list):
       PYACC.Section1Or2.serialize(self, ref_list)
       self.set('api_prefix', element.serialize_str(self.api_prefix))
-      self.set('api_pure', element.serialize_str(self.api_pure))
+      self.set('api_pure', element.serialize_int(self.api_pure))
       self.set('locations', element.serialize_bool(self.locations))
       self.set('parse_error', element.serialize_str(self.parse_error))
       self.set('parse_lac', element.serialize_str(self.parse_lac))
@@ -2211,7 +2230,7 @@ class PYACC(element.Element):
     def deserialize(self, ref_list):
       PYACC.Section1Or2.deserialize(self, ref_list)
       self.api_prefix = element.deserialize_str(self.get('api_prefix', ''))
-      self.api_pure = element.deserialize_str(self.get('api_pure', ''))
+      self.api_pure = element.deserialize_int(self.get('api_pure', '-1'))
       self.locations = element.deserialize_bool(self.get('locations', 'false'))
       self.parse_error = element.deserialize_str(self.get('parse_error', ''))
       self.parse_lac = element.deserialize_str(self.get('parse_lac', ''))
@@ -2240,7 +2259,7 @@ class PYACC(element.Element):
         params.append(
           'api_prefix = {0:s}'.format(repr(self.api_prefix))
         )
-      if self.api_pure != '':
+      if self.api_pure != -1:
         params.append(
           'api_pure = {0:s}'.format(repr(self.api_pure))
         )
@@ -2286,7 +2305,7 @@ class PYACC(element.Element):
       name_to_tag
     ):
       self.api_prefix = ''
-      self.api_pure = ''
+      self.api_pure = 0
       self.locations = False
       self.parse_error = ''
       self.parse_lac = ''
@@ -2304,8 +2323,8 @@ class PYACC(element.Element):
       )
       if len(self.api_prefix):
         sys.stderr.write('warning: ignoring %define api.prefix\n')
-      if len(self.api_pure):
-        sys.stderr.write('warning: ignoring %define api.pure\n')
+      #if self.api_pure != 0:
+      #  sys.stderr.write('warning: ignoring %define api.pure\n')
       if self.locations:
         sys.stderr.write('warning: ignoring %define locations\n')
       if len(self.parse_error):
index 2f3520d..0c01235 100644 (file)
@@ -337,7 +337,16 @@ def generate(pyacc, skel_file, out_file, defines_file = None):
       with open(out_file, 'w+') as fout:
         line = fin.readline()
         while len(line):
-          if line == '/* GENERATE SECTION1TOP */\n':
+          if line == '/* GENERATE YYPURE */\n':
+            fout.write(
+              '''/* GENERATE YYPURE BEGIN */
+#define YYPURE {0:d}
+/* GENERATE YYPURE END */
+'''.format(
+                pyacc[0].api_pure
+              )
+            )
+          elif line == '/* GENERATE SECTION1TOP */\n':
             fout.write(
               '''/* GENERATE SECTION1TOP BEGIN */
 {0:s}/* GENERATE SECTION1TOP END */
index 957dcee..8c424f8 100644 (file)
@@ -1,5 +1,14 @@
---- y.tab.c.orig       2018-07-26 21:54:50.154480530 +1000
-+++ y.tab.c    2018-07-26 22:00:46.558496008 +1000
+--- y.tab.c.orig       2018-07-29 17:18:39.855497595 +1000
++++ y.tab.c    2018-07-29 17:18:53.715497961 +1000
+@@ -50,7 +50,7 @@
+ #define YYSKELETON_NAME "yacc.c"
+ /* Pure parsers.  */
+-#define YYPURE 0
++/* GENERATE YYPURE */
+ /* Push parsers.  */
+ #define YYPUSH 0
 @@ -58,11 +58,11 @@
  /* Pull parsers.  */
  #define YYPULL 1
@@ -14,7 +23,7 @@
  
  # ifndef YY_NULLPTR
  #  if defined __cplusplus && 201103L <= __cplusplus
-@@ -97,22 +97,17 @@
+@@ -97,28 +97,24 @@
  # define YYTOKENTYPE
    enum yytokentype
    {
  # define YYSTYPE_IS_TRIVIAL 1
  # define YYSTYPE_IS_DECLARED 1
  #endif
-@@ -126,6 +121,7 @@
+-
++#if !YYPURE
+ extern YYSTYPE yylval;
++#endif
+ int yyparse (void);
+@@ -126,6 +122,7 @@
  
  /* Copy the second part of user declarations.  */
  
@@ -50,7 +67,7 @@
  
  #ifdef short
  # undef short
-@@ -364,155 +360,7 @@
+@@ -364,155 +361,7 @@
  # endif
  #endif /* !YYCOPY_NEEDED */
  
  
  #define yyerrok         (yyerrstatus = 0)
  #define yyclearin       (yychar = YYEMPTY)
-@@ -1187,14 +1035,7 @@
+@@ -936,9 +785,7 @@
+   YY_IGNORE_MAYBE_UNINITIALIZED_END
+ }
+-
+-
+-
++#if !YYPURE
+ /* The lookahead symbol.  */
+ int yychar;
+@@ -946,7 +793,7 @@
+ YYSTYPE yylval;
+ /* Number of syntax errors so far.  */
+ int yynerrs;
+-
++#endif
+ /*----------.
+ | yyparse.  |
+@@ -955,6 +802,20 @@
+ int
+ yyparse (void)
+ {
++#if YYPURE
++    /* The lookahead symbol.  */
++    int yychar;
++
++    /* The semantic value of the lookahead symbol.  */
++    /* Default value used for initialization, for pacifying older GCCs
++       or non-GCC compilers.  */
++    YY_INITIAL_VALUE (static YYSTYPE yyval_default;)
++    YYSTYPE yylval YY_INITIAL_VALUE (= yyval_default);
++
++    /* Number of syntax errors so far.  */
++    int yynerrs;
++#endif
++
+     int yystate;
+     /* Number of tokens to shift before error messages enabled.  */
+     int yyerrstatus;
+@@ -1109,7 +970,11 @@
+   if (yychar == YYEMPTY)
+     {
+       YYDPRINTF ((stderr, "Reading a token: "));
++#if YYPURE
++      yychar = yylex (&yylval);
++#else
+       yychar = yylex ();
++#endif
+     }
+   if (yychar <= YYEOF)
+@@ -1187,14 +1052,7 @@
    YY_REDUCE_PRINT (yyn);
    switch (yyn)
      {
  
        default: break;
      }
-@@ -1423,3 +1264,5 @@
+@@ -1423,3 +1281,5 @@
  #endif
    return yyresult;
  }