w.setExpr(BGF.Expression(nt)) return BGF.Expression(w) if __name__ == "__main__": g = BGF.Grammar() prod = 400 term = 150 for i in range(0, prod): p = BGF.Production() p.setNT('nt' + str(i)) p.setExpr(BGF.Expression(singles[i % 3])) g.addProd(p) p = BGF.Production() p.setNT('nt' + str(i)) a = ntd('nt' + str((i + 1) % prod), BGF.Plus) b = ntd('nt' + str((i + 2) % prod), BGF.Star) c = ntd('nt' + str((i + 3) % prod), BGF.Optional) bc = BGF.Choice() bc.add(b) bc.add(c) abc = BGF.Sequence() abc.add(a) abc.add(BGF.Expression(bc)) d = BGF.Terminal() d.setName('t' + str(i % term)) abc.add(BGF.Expression(d)) p.setExpr(BGF.Expression(abc)) g.addProd(p) ET.ElementTree(g.getXml()).write('monster.bgf')
# if there is whitespace between the label and the : prod.setLabel(alt[0]) alt = alt[2:] if alt and len(alt) > 1 and alt[0][:2] == '/*': if alt[0].find('*/') > 0: alt[0] = alt[0][:alt[0]. index('/*')] + alt[0][alt[0].index('*/') + 2:] if alt[0] == '': alt = alt[1:] # had to do it the second time - bad sign if alt and len(alt) > 0 and alt[0][-1] == ':': prod.setLabel(alt[0][:-1]) alt = alt[1:] cx = 0 seq = BGF.Sequence() sym = None #print '['+str(cx)+']',alt while cx < len(alt): if alt[cx][-1] == ';': alt[cx] = alt[cx][:-1] print '[' + str(cx) + ']', '@', alt if curly > 0: if alt[cx] == '{': curly += 1 #print 'MORE CURLY' elif alt[cx] == '}': curly -= 1 #print 'LESS CURLY' #print 'Skipped over',alt[cx] cx += 1