108: #line 240 "lalr1_parser.ipk" 109: def calc_firstmap(self): 110: res = {} 111: for sym in self.terms + [EPS, EOF, Grammar.DummyLA]: 112: res[sym] = set(sym) 113: while 1: 114: added = 0 115: for nt in self.nonterms: 116: firsts = res.get(nt, set()) 117: for p in self.lhsprods[nt]: 118: if not p.RHS: 119: if not firsts.contains(EPS): 120: added = 1 121: firsts.insert(EPS) 122: continue 123: i = 0 124: while i < len(p.RHS): 125: f = res.get(p.RHS[i], set()) 126: for t in f: 127: if not firsts.contains(t): 128: added = 1 129: firsts.insert(t) 130: if self.lhsdereps.contains(p.RHS[i]): 131: i = i + 1 132: else: break 133: res[nt] = firsts 134: if not added: 135: break 136: self.firstmap = res 137: 138: 139: # 140: # these function are used as the grammar produces the tables (or writes them 141: # to a file) 142: # 143: def firstofstring(self, gs_list): 144: tmpres = {} 145: allhaveeps = 1 146: for x in range(len(gs_list)): 147: tmp = self.firstmap[gs_list[x]] 148: for s in tmp: tmpres[s] = 1 149: if EPS in tmp: del tmpres[EPS] 150: else: 151: allhaveeps = 0 152: break 153: if allhaveeps: tmpres[EPS] = 1 154: return tmpres.keys() 155: 156: