Tidy up Python skeleton stack usage so there is only one dummy element, at the start...
authorNick Downing <nick@ndcode.org>
Sun, 3 Feb 2019 07:03:35 +0000 (18:03 +1100)
committerNick Downing <nick@ndcode.org>
Sun, 3 Feb 2019 07:03:35 +0000 (18:03 +1100)
piyacc.t
skel/skel_py.py
skel/skel_py_element.py
tests_ast/cal_py.y

index f2ade59..f20b710 100644 (file)
--- a/piyacc.t
+++ b/piyacc.t
@@ -1301,7 +1301,7 @@ def get_text(self, python = False):
 @method(AST.Text.StackLocation)
 def get_text(self, python = False):
   return (
-    '(yystack[{0:d}][2])'.format(self.offset + self.index - 2)
+    '(yystack[{0:d}][2])'.format(self.offset + self.index - 1)
   if python else
     '(yylsp[{0:d}])'.format(self.offset + self.index)
   )
@@ -1310,7 +1310,7 @@ def get_text(self, python = False):
   return (
     (
       '(yystack[{0:d}][1].{1:s})'.format(
-        self.offset + self.index - 2,
+        self.offset + self.index - 1,
        self.tag_name
       )
     if python else
@@ -1322,7 +1322,7 @@ def get_text(self, python = False):
   if len(self.tag_name) else
     (
       '(yystack[{0:d}][1])'.format(
-        self.offset + self.index - 2,
+        self.offset + self.index - 1,
       )
     if python else
       '(yyvsp[{0:d}])'.format(
index e2dfbed..9883bcf 100644 (file)
@@ -78,10 +78,10 @@ def yyparse():
 
   # GENERATE INITIALACTION
 
-  state = 0
-  yystack = [(-1, yylval, yylloc.copy())] # kludge for bison compatibility
+  yystack = [(0, yylval, yylloc.copy())]
   yychar = -1
   while True:
+    state = yystack[-1][0]
     #print('state', state, 'yystack', yystack)
     reduce = yy_lr1dfa_states[state][4]
     if reduce == -1:
@@ -94,27 +94,30 @@ def yyparse():
       if action == -1:
         raise Exception('syntax error')
       if (action & 1) == 0:
-        yystack.append((state, yylval, yylloc.copy()))
-        state = action >> 1
-        #print('shift', state)
+        yystack.append((action >> 1, yylval, yylloc.copy()))
+        #print('shift', yystack[-1][0])
         yychar = -1
         continue
       reduce = action >> 1
     #print('reduce', reduce)
     len_symbols, ref_data = yy_lr1dfa_productions[reduce]
+    yyval = yystack[-len_symbols][1] if len_symbols else None
     # GENERATE YYLLOC_DEFAULT
-    base = len(yystack) - len_symbols
-    yystack.append((state, None, None)) # only has effect if len_symbols == 0
-    state, yyval, _ = yystack[base]
     ref_data()
-    del yystack[base:]
+    del yystack[len(yystack) - len_symbols:]
     if reduce == 0:
-      assert base == 1
+      assert len(yystack) == 1
       break
-    yystack.append((state, yyval, yyloc.copy()))
-    state = yy_lr1dfa_states[state][3][
-      bisect.bisect_right(yy_lr1dfa_states[state][2], reduce)
-    ]
-    assert state != -1
+    state = yystack[-1][0]
+    yystack.append(
+      (
+        yy_lr1dfa_states[state][3][
+          bisect.bisect_right(yy_lr1dfa_states[state][2], reduce)
+        ],
+        yyval,
+        yyloc.copy()
+      )
+    )
+    assert yystack[-1][0] != -1
 
 # GENERATE SECTION3
index 1ef590f..5cf5674 100644 (file)
@@ -82,11 +82,11 @@ def yyparse(factory, *args, **kwargs):
 
   # GENERATE INITIALACTION
 
-  state = 0
-  yystack = [(-1, yylval, yylloc.copy())] # kludge for bison compatibility
+  yystack = [(0, yylval, yylloc.copy())]
   yychar = -1
   yy_element_stack = []
   while True:
+    state = yystack[-1][0]
     #print('state', state, 'yystack', yystack)
     reduce = yy_lr1dfa_states[state][4]
     if reduce == -1:
@@ -103,33 +103,29 @@ def yyparse(factory, *args, **kwargs):
       if action == -1:
         raise Exception('syntax error')
       if (action & 1) == 0:
-        yystack.append((state, yylval, yylloc.copy()))
-
         # push space then AST element contiguously onto yy_element_stack
         # even numbered elements are spaces, odd numbered elements are AST
         yy_element_stack.extend(
           [lex_yy.yy_element_space, lex_yy.yy_element_token]
         )
 
-        state = action >> 1
-        #print('shift', state)
+        yystack.append((action >> 1, yylval, yylloc.copy()))
+        #print('shift', yystack[-1][0])
         yychar = -1
         continue
       reduce = action >> 1
     #print('reduce', reduce)
     len_symbols, ref_data = yy_lr1dfa_productions[reduce]
+    yyval = yystack[-len_symbols][1] if len_symbols else None
     # GENERATE YYLLOC_DEFAULT
-    base = len(yystack) - len_symbols
-    yystack.append((state, None, None)) # only has effect if len_symbols == 0
-    state, yyval, _ = yystack[base]
     ref_data()
-    del yystack[base:]
+    del yystack[len(yystack) - len_symbols:]
     if reduce == 0:
-      assert base == 1
+      assert len(yystack) == 1
       break
-    yystack.append((state, yyval, yyloc.copy()))
 
     # action creates empty space in yy_element_stack[base * 2] if needed
+    base = len(yystack) - len_symbols
     assert len(yy_element_stack) >= base * 2 - 1
 
     # concatenate yy_element_stack[base * 2 - 1:] to a single AST element
@@ -140,10 +136,17 @@ def yyparse(factory, *args, **kwargs):
       )
     ]
 
-    state = yy_lr1dfa_states[state][3][
-      bisect.bisect_right(yy_lr1dfa_states[state][2], reduce)
-    ]
-    assert state != -1
+    state = yystack[-1][0]
+    yystack.append(
+      (
+        yy_lr1dfa_states[state][3][
+          bisect.bisect_right(yy_lr1dfa_states[state][2], reduce)
+        ],
+        yyval,
+        yyloc.copy()
+      )
+    )
+    assert yystack[-1][0] != -1
 
   # return space then AST then space in the user's choice of element type
   yy_element_stack.append(lex_yy.yy_element_space)
index c5fbe45..4ec2399 100644 (file)
@@ -23,7 +23,6 @@ import sys
 
 %locations
 
-%blah
 %token NUM
 
 %left '+' '-'