def bgf2dot(g): cg = metrics.getCallGraph(g) s = 'digraph generated{\n' for a in cg.keys(): for b in cg[a]: s += a+' -> '+b+';\n' #s += '}\n' print metrics.calculateLevels(cg) cg = metrics.makeOneStep(cg) #s = 'digraph generated{\n' for a in cg.keys(): s += a.replace('-','_')+'_[label="'+a+'"];\n' for b in cg[a]: s += a.replace('-','_')+'_ -> '+b+'_;\n' print metrics.calculateLevels(cg) cg = metrics.makeOneStep(cg) for a in cg.keys(): s += a.replace('-','_')+'__[label="'+a+'"];\n' for b in cg[a]: s += a.replace('-','_')+'__ -> '+b+'__;\n' print metrics.calculateLevels(cg) s += '}' print metrics.calculateLevels(metrics.getClosure(cg)) return s
def bgf2dot(g): cg = metrics.getCallGraph(g) s = 'digraph generated{\n' for a in cg.keys(): for b in cg[a]: s += a + ' -> ' + b + ';\n' #s += '}\n' print metrics.calculateLevels(cg) cg = metrics.makeOneStep(cg) #s = 'digraph generated{\n' for a in cg.keys(): s += a.replace('-', '_') + '_[label="' + a + '"];\n' for b in cg[a]: s += a.replace('-', '_') + '_ -> ' + b + '_;\n' print metrics.calculateLevels(cg) cg = metrics.makeOneStep(cg) for a in cg.keys(): s += a.replace('-', '_') + '__[label="' + a + '"];\n' for b in cg[a]: s += a.replace('-', '_') + '__ -> ' + b + '__;\n' print metrics.calculateLevels(cg) s += '}' print metrics.calculateLevels(metrics.getClosure(cg)) return s
def addProductionsOf(r,cg,src,tgt): if r in processed: return for p in src.prods: if p.nt == r: tgt.addProd(p) processed.append(r) for n in cg[r]: addProductionsOf(n,cg,src,tgt) return if __name__ == "__main__": if len(sys.argv) < 4: print 'This tool extracts a portion of a grammar that starts at a given root nonterminal and includes all referenced nonterminals as well.' print 'Usage:' print ' subgrammar <bgf-input> <new-root> [<new-root>...] <bgf-output>' sys.exit(1) bgf = BGF.Grammar() newBgf = BGF.Grammar() bgf.parse(sys.argv[1]) roots = sys.argv[2:-1] print 'Setting root(s) to',roots for r in roots: newBgf.addRoot(r) cg = metrics.getCallGraph(bgf) for r in roots: addProductionsOf(r,cg,bgf,newBgf) ET.ElementTree(newBgf.getXml()).write(sys.argv[-1]) sys.exit(0)
def addProductionsOf(r, cg, src, tgt): if r in processed: return for p in src.prods: if p.nt == r: tgt.addProd(p) processed.append(r) for n in cg[r]: addProductionsOf(n, cg, src, tgt) return if __name__ == "__main__": if len(sys.argv) < 4: print 'This tool extracts a portion of a grammar that starts at a given root nonterminal and includes all referenced nonterminals as well.' print 'Usage:' print ' subgrammar <bgf-input> <new-root> [<new-root>...] <bgf-output>' sys.exit(1) bgf = BGF.Grammar() newBgf = BGF.Grammar() bgf.parse(sys.argv[1]) roots = sys.argv[2:-1] print 'Setting root(s) to', roots for r in roots: newBgf.addRoot(r) cg = metrics.getCallGraph(bgf) for r in roots: addProductionsOf(r, cg, bgf, newBgf) ET.ElementTree(newBgf.getXml()).write(sys.argv[-1]) sys.exit(0)
def _getCallGraph(self): if not self.cache.cg: self.cache.cg = metrics.getCallGraph(self.grammar) return self.cache.cg