Minor tweaks for building bison (now works, but don't have %define api.XXX yet)
authorNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 06:19:06 +0000 (16:19 +1000)
committerNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 06:19:06 +0000 (16:19 +1000)
ast.py
bison_lr1dfa.py

diff --git a/ast.py b/ast.py
index 7f78cfd..a60be6b 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -2605,6 +2605,20 @@ class PYACC(element.Element):
         name = self[0].get_text()
         if name in name_to_symbol:
           symbol = name_to_symbol[name]
+          assert self.user_token != 0
+        elif self.user_token == 0:
+          # stupid exception following what bison does, because bison
+          # adds the $end token at the end if not already defined by
+          # user, but we need to define the $end token upfront to make
+          # it have internal token number 0 (and apparently they don't)
+          symbol = 0
+          name_to_symbol[name] = symbol
+          assert pyacc.symbols[symbol].name == '$end'
+          pyacc.symbols[symbol] = PYACC.Symbol(
+            name = name,
+            code_props = [None, None],
+            character_set = []
+          )
         else:
           symbol = len(pyacc.symbols)
           name_to_symbol[name] = symbol
@@ -2786,6 +2800,8 @@ class PYACC(element.Element):
         self.repr_serialize(params)
         return 'ast.PYACC.Text.StackLocation({0:s})'.format(', '.join(params))
       # GENERATE END
+      def get_text(self):
+        return '(yylsp[{0:d}])'.format(self.offset + self.index)
 
     class StackReference(Item):
       # GENERATE ELEMENT(str tag_name, int offset, int index) BEGIN
@@ -2894,6 +2910,8 @@ class PYACC(element.Element):
         self.repr_serialize(params)
         return 'ast.PYACC.Text.ValueLocation({0:s})'.format(', '.join(params))
       # GENERATE END
+      def get_text(self):
+        return '(yyloc)'
 
     class ValueReference(Item):
       # GENERATE ELEMENT(str tag_name) BEGIN
@@ -3332,7 +3350,7 @@ class PYACC(element.Element):
     self.precedences = 0
     self.symbols = [
       PYACC.Symbol(
-        name = '$eof',
+        name = '$end',
         code_props = [None, None],
         _type = PYACC.Symbol.TYPE_TERMINAL,
         character_set = [0, 1]
index 90ded8a..2f3520d 100644 (file)
@@ -12,6 +12,10 @@ class BisonLR1DFA:
     n_nonterminals,
     translate_nonterminals
   ):
+    #numpy.set_printoptions(threshold = numpy.nan)
+    #print(translate_terminals)
+    #print(translate_nonterminals)
     # translate is yytranslate
     self.translate = translate_terminals
 
@@ -60,6 +64,10 @@ class BisonLR1DFA:
         goto_table[nonterminal0:nonterminal1, i] = gotos[j]
         nonterminal0 = nonterminal1
       assert nonterminal0 == len(lr1dfa.productions)
+    assert numpy.all(action_table != 0)
+    assert numpy.all(goto_table != 0)
+    #print(action_table)
+    #print(goto_table)
  
     # permute and combine columns/rows on the basis of the translate vectors
     new_action_table = numpy.zeros(