6.9.1.4.5. Find FOLLOW sets

Calculcate FOLLOW(sym) for each symbol in the grammar, including epsilon,
Start python section to interscript/parsers/lalr1.py[9 /24 ] Next Prev Last
   167: #line 304 "lalr1_parser.ipk"
   168:   def calc_followmap(self):
   169:       eof = EOF
   170:       follow = {}
   171:       startsym = self.productions[0].LHS
   172:       follow[startsym] = set(eof)
   173:       nts = self.nonterms
   174:       for p in self.productions:
   175:           cutoff = range(len(p.RHS))
   176:           cutoff.reverse()
   177:           for c in cutoff[:-1]:  # all but the first of the RHS elements
   178:               f = self.firstmap[p.RHS[c]].copy()
   179:               f.excise(EPS)
   180:               if follow.has_key(p.RHS[c - 1]):
   181:                   if p.RHS[c -1] in nts:
   182:                       follow[p.RHS[c -1]] = follow[p.RHS[c - 1]] + f[:]
   183:               else:
   184:                   if p.RHS[c -1] in nts:
   185:                       follow[p.RHS[c - 1]] = f[:]
   186:       for p in self.productions:
   187:           if not p.RHS: continue
   188:           cutoff = range(len(p.RHS))
   189:           cutoff.reverse()
   190:           if p.RHS[-1] in nts:
   191:               if follow.has_key(p.LHS):
   192:                   add = follow[p.LHS]
   193:               else:
   194:                   add = []
   195: 
   196:               if follow.has_key(p.RHS[-1]):
   197:                   follow[p.RHS[-1]] = follow[p.RHS[-1]] + add
   198:               else:
   199:                   follow[p.RHS[-1]] = add
   200:           for c in cutoff[:-1]:
   201:               f = self.firstmap[p.RHS[c]].copy()
   202:               if EPS in f:
   203:                   if follow.has_key(p.LHS):
   204:                       add = follow[p.LHS]
   205:                   else:
   206:                       add = set()
   207:                   if follow.has_key(p.RHS[c-1]):
   208:                       follow[p.RHS[c-1]] = follow[p.RHS[c-1]] + add
   209:                   elif add:
   210:                       follow[p.RHS[c - 1]] = add
   211:       for k in follow.keys():
   212:           d = set()
   213:           for i in follow[k]: d.insert(i)
   214:           follow[k] = d
   215:       self.followmap = follow
   216: 
End python section to interscript/parsers/lalr1.py[9]