Fix bug with reference offset for mid-rule actions, ensure all symbols typed
authorNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 02:19:24 +0000 (12:19 +1000)
committerNick Downing <downing.nick@gmail.com>
Sun, 29 Jul 2018 02:19:24 +0000 (12:19 +1000)
ast.py
tests/cal.l
tests/cal.y

diff --git a/ast.py b/ast.py
index 40155bf..7f78cfd 100644 (file)
--- a/ast.py
+++ b/ast.py
@@ -399,7 +399,6 @@ class PYACC(element.Element):
         self.repr_serialize(params)
         return 'ast.PYACC.Production.Action({0:s})'.format(', '.join(params))
       # GENERATE END
-
       def post_process(
         self,
         pyacc,
@@ -414,7 +413,6 @@ class PYACC(element.Element):
         pyacc.n_productions += int(last_action) # midrule action production
         production.n_symbols += int(last_action) # midrule action symbol
         return True
-
       def add_to_symbols(
         self,
         pyacc,
@@ -441,7 +439,7 @@ class PYACC(element.Element):
           )
         for i in self[0]:
           if isinstance(i, PYACC.Text.StackLocation):
-            i.offset = -production.n_symbols
+            i.offset = -len(symbols)
           elif isinstance(i, PYACC.Text.StackReference):
             if (
               len(i.tag_name) == 0 and
@@ -449,7 +447,7 @@ class PYACC(element.Element):
               i.index <= len(tag_names)
             ):
               i.tag_name = tag_names[i.index - 1]
-            i.offset = -production.n_symbols
+            i.offset = -len(symbols)
         return self[0]
 
     class DPrec(Item):
@@ -3385,9 +3383,14 @@ class PYACC(element.Element):
     # fill in token numbers that are not characters or overridden by user
     token = 0x100
     for i in self.symbols:
-      if i._type == PYACC.Symbol.TYPE_TERMINAL and len(i.character_set) == 0:
-        i.character_set = [token, token + 1]
-        token += 1
+      if i._type == PYACC.Symbol.TYPE_TERMINAL:
+        if len(i.character_set) == 0:
+          i.character_set = [token, token + 1]
+          token += 1
+      elif i._type == PYACC.Symbol.TYPE_NONTERMINAL:
+        pass
+      else:
+        assert False
 
   def to_lr1(self):
     _lr1 = lr1.LR1(
index bbeb981..1895c9c 100644 (file)
@@ -12,7 +12,7 @@ DIGIT [0-9]+\.?|[0-9]*\.[0-9]+
 
 [ ]
 {DIGIT}        { yylval = atof(yytext); return NUM; }
-\n|.   { return yytext[0]; }
+\n|.   { yylval = 1000 + yytext[0]; return yytext[0]; }
 
 %%
 
index 771fa19..d0e0c33 100644 (file)
@@ -22,7 +22,7 @@ E : E '+' E { $$ = $1 + $3; }
   | E '/' E { $$ = $1 / $3; }
   | '(' E ')' { $$ = $2; }
   /*| '-' E %prec UMINUS { $$ = -$2; }*/
-  | '-' { printf("unary minus\n"); } E %prec UMINUS { $$ = -$3; }
+  | '-' { printf("unary minus %f\n", $1); } E %prec UMINUS { $$ = -$3; }
   | NUM
   ;
 %%