diff python/pyyacc.py @ 195:37ac6c016e0f

Expanded asm subsystem
author Windel Bouwman
date Fri, 31 May 2013 21:06:44 +0200
parents b01429a5d695
children 494828a7adf1
line wrap: on
line diff
--- a/python/pyyacc.py	Wed May 29 22:36:37 2013 +0200
+++ b/python/pyyacc.py	Fri May 31 21:06:44 2013 +0200
@@ -209,7 +209,7 @@
                 if item.IsReduce:
                     if item.production.name == self.start_symbol and item.look_ahead == EOF:
                         # Rule 3: accept:
-                        setAction(states.index(state), item.look_ahead, (ACCEPT, None))
+                        setAction(states.index(state), item.look_ahead, (ACCEPT, item.production))
                     else:
                         # Rule 2, reduce item:
                         setAction(states.index(state), item.look_ahead, (REDUCE, item.production))
@@ -218,7 +218,7 @@
                 if key in transitions:
                     goto_table[key] = transitions[key]
 
-        return LRParser(action_table, goto_table)
+        return LRParser(action_table, goto_table, self.start_symbol)
 
 
 class Production:
@@ -299,45 +299,65 @@
 
 class LRParser:
     """ LR parser """
-    def __init__(self, action_table, goto_table):
+    def __init__(self, action_table, goto_table, start_symbol):
         self.action_table = action_table
         self.goto_table = goto_table
+        self.start_symbol = start_symbol
 
     def parse(self, toks):
         """ Parse an iterable with tokens """
         assert hasattr(toks, '__iter__'), '{0} not iter type'.format(type(toks))
         stack = [0]
+        r_data_stack = []
         try:
             look_ahead = toks.__next__()
         except StopIteration:
             look_ahead = Token(EOF, EOF)
         assert type(look_ahead) is Token
-        while True:
+        # TODO: exit on this condition:
+        while stack != [0, self.start_symbol, 2222]:
+            #print(stack)
             state = stack[-1]   # top of stack
             key = (state, look_ahead.typ)
             if not key in self.action_table:
                 raise ParserException('Error parsing at character {0}'.format(look_ahead))
             action, param = self.action_table[key]
             if action == REDUCE:
-                #print('reduce', param)
                 f_args = []
                 for s in param.symbols:
                     stack.pop()
                     stack.pop()
-                    f_args.append(0)
+                    f_args.append(r_data_stack.pop())
+                f_args.reverse()
+                r_data = None
                 if param.f:
-                    param.f(*f_args)
+                    r_data = param.f(*f_args)
                 state = stack[-1]
                 stack.append(param.name)
                 stack.append(self.goto_table[(state, param.name)])
+                r_data_stack.append(r_data)
             elif action == SHIFT:
                 stack.append(look_ahead.typ)
                 stack.append(param)
+                r_data_stack.append(look_ahead.val)
                 try:
                     look_ahead = toks.__next__()
                 except StopIteration:
                     look_ahead = Token(EOF, EOF)
                 assert type(look_ahead) is Token
             elif action == ACCEPT:
+                # Pop last rule data off the stack:
+                f_args = []
+                for s in param.symbols:
+                    stack.pop()
+                    stack.pop()
+                    f_args.append(r_data_stack.pop())
+                f_args.reverse()
+                if param.f:
+                    param.f(*f_args)
+                # Break out!
                 break
+        # At exit, the stack must be 1 long
+        # TODO: fix that this holds:
+        #assert len(stack) == 1, 'stack {0} not totally reduce'.format(stack)