6.9.1.4.6. LALR Closure

Compute the LALR Closure. An 'item' is logically a pair consisting of a production and an indictor of a position in the RHS. It is represented here as a pair of integers: an index into the sequence of productions, and an integer between 0 and the length of the RHS of the production, inclusive.
Start python section to interscript/parsers/lalr1.py[10 /24 ] Next Prev Last
   217: #line 360 "lalr1_parser.ipk"
   218:   def closure(self, items):
   219:       res = items[:]
   220:       todo = items[:]
   221:       while 1:
   222:           more = []
   223:           for (prodind, rhsind), term in todo:
   224:               if rhsind >= len(self.productions[prodind].RHS):
   225:                   continue
   226:               for p in self.lhsprods.get(self.productions[prodind].RHS[rhsind], []):
   227:                   try:
   228:                       newpart = self.productions[prodind].RHS[rhsind + 1]
   229:                   except IndexError:
   230:                       newpart = EPS
   231:                   stringofsyms = [newpart, term]
   232:                   for t in self.firstofstring(stringofsyms):
   233:                       if ((self.productions.index(p), 0), t) not in res:
   234:                           more.append(((self.productions.index(p), 0), t))
   235:                   if term == EOF and newpart == EPS:
   236:                       if ((self.productions.index(p), 0), EOF) not in res:
   237:                           more.append(((self.productions.index(p), 0), EOF))
   238:           if more:
   239:               res = res + more
   240:               todo = more
   241:           else:
   242:               break
   243:       return res
   244: 
   245: 
   246: #  def goto(self, items, sym):
   247: #      itemset = []
   248: #      for (prodind, rhsind), term in items:
   249: #          try:
   250: #              if self.productions[prodind].RHS[rhsind] == sym and ((prodind, rhsind+1), term) not in itemset:
   251: #                  itemset.append( ((prodind, rhsind +1), term))
   252: #          except IndexError:
   253: #              pass
   254: #      return self.closure(itemset)
   255: 
End python section to interscript/parsers/lalr1.py[10]