Ejemplo n.º 1
0
 def computeLookaheads(self):
     D = Symbol("#", self.grammar)
     for kSet in self.kernelTable:
         # Find the items
         I = LA0.closure(kSet)
         # Dictionary of symbol and kernelEntry
         gotoTable = {}
         for kernel in kSet:
             for item in closure(Item(kernel.production, kernel.position, D)):
                 a = item.nextSymbol()
                 if a == None:
                     continue
                 if not gotoTable.has_key(a):
                     SearchFor = ()
                     for it in LA0.kernelGoto(I, a).kernels():
                         SearchFor += (it,)
                         # rSet is the KernelEntry where items == SearchFor
                     rSet = None
                     for entry in self.kernelTable:
                         if entry.match(SearchFor):
                             rSet = entry
                             break
                     assert rSet != None, "rSet must not be None!!"
                     gotoTable[a] = rSet
                 else:
                     rSet = gotoTable[a]
                     # rItem is the LA0 item where searching for...
                 rItem = item.toLA0()
                 rItem.position += 1  # Move to the other side of X
                 rItem.rehash()
                 if item.lookahead != D:
                     rSet.addLookahead(rItem, item.lookahead)
                 else:
                     to = rSet.idOf(rItem)
                     kSet.addPropagation(kernel, to)
Ejemplo n.º 2
0
 def __init__(self, grammar):
     self.grammar = grammar
     self.kernelTable = ()  # Tuple of kernelItemSets
     for itSet in LA0.items(self.grammar):
         t = KernelItemSet()
         for k in itSet.kernels():
             t.addKernel(k)
         self.kernelTable += (t,)
         # Add the EOF symbol as lookahead for root production
     rItem = LA0.Item(self.grammar.rootProduction, 0)
     for kset in self.kernelTable:
         if rItem in kset:
             kset.addLookahead(rItem, self.grammar.EOF)