def view_parsing_graph(filename: str): """ :param filename: JSON format file """ import graphviz_artist as ga import graphviz_artist.attr as a import json with open(filename) as f: graph = json.load(f) starts = {v: k for k, v in graph['starts'].items()} g = ga.Graph(directed=True) nodes = {int(k): v for k, v in graph['nodes'].items()} gnodes = {} for i, node in nodes.items(): kind = node['kind'] ctor = kind['ctor'] if ctor == "NEntity": val = kind['val'] ctor = val['ctor'] if ctor == "ETerm": label = repr(val['val']) elif ctor == "ENonTerm": label = val['name'] else: label = ctor + " ..." elif ctor == "NReturn": label = "return" else: label = ctor + " ..." if label == 'Start ...': label = "Start " + str(starts[i]) label += " %d" % i gnodes[i] = g.new(a.Label(label), a.Shape.box) for i, node in nodes.items(): gnode = gnodes[i] for each in node['followed']: _ = gnode > gnodes[each] g.view()
import graphviz_artist as ga import graphviz_artist.attr as a g = ga.Graph(directed=True) g.update(attrs=(a.HorizontalGraph, )) math = g.new(a.Label("math : Python的一个library")) pi = g.new(a.Label("pi : math.pi")) r = g.new(a.Label("r : 1")) h = g.new(a.Label("h : 1")) S = g.new(a.Label("S : 2 * S_top + S_side")) S_top = g.new(a.Label("S_top : pi * r ** 2")) S_side = g.new(a.Label("S_side : C * h ")) C = g.new(a.Label("C : 2 * pi * r")) _ = math > pi > r > h > S_top > C > S_side > S g.view()
import graphviz_artist as ga # use attr module to see which Graphviz Attributes # could be auto-completed. import graphviz_artist.attr as attr # for some reason, the name of keyword arg has no actual semantics, # but the value does: # ga.Graph(kw=attr.XXX) == ga.Graph(_=XXX) == ga.Graph(x=XXX) g = ga.Graph(attr.HorizontalGraph) # `attr.Shape("<name>")` to specify the shape of nodes. n1 = g.new(attr.Label('hey'), attr.Shape.diamond) n2 = g.new(attr.Label('hey'), attr.Shape.hexagon) n3 = g.new(attr.Label('you'), attr.Shape.star) # `attr.Directed()` makes a directed edge. directed = attr.Directed() # `attr.Label` to specify the text that edges display edge_label = attr.Label("passed_here") # `attr.Penwidth` to decide the width of edge glyph. edge_size = attr.Penwidth(2.) # in `a < b[b_to_c_attrs...] > c`, the edge `b -> c` will have attribute `b_to_c_attrs`. _ = n3[directed, edge_label, edge_size] > n1[directed] == n2 > n3 g.save() assert """digraph {
import graphviz_artist as ga import graphviz_artist.attr as attr g = ga.Graph() n1 = g.new() n2 = g.new() n1.update(attr.Label("Hey")) n1.update(attr.Label("Holla")) _ = n1[attr.Label("ok")] == n2 g.save() assert """digraph { 0 1 0 [label=Hey] 0 [label=Holla] 0 -> 1 [label=ok dir=none] 1 -> 0 [label=ok dir=none] }""" == str(g.g)
import graphviz_artist as ga import graphviz_artist.attr as attr g = ga.Graph() n1 = g.new() n2 = g.new() n3 = g.new() _ = n1 > n2[attr.Label("woundn't work")] _ = n1 < n2[attr.Label("woundn't work")] _ = n3 == n2[attr.Label("woundn't work")] g.update(attrs=(attr.HorizontalGraph, )) g.save() assert """digraph { graph [rankdir=LR] 0 1 2 0 -> 1 [dir=none] 1 -> 0 [dir=none] 2 -> 1 [dir=none] 1 -> 2 [dir=none] }""" == str(g.g)
g = ga.Graph(directed=True) nodes = {int(k): v for k, v in graph['nodes'].items()} gnodes = {} for i, node in nodes.items(): kind = node['kind'] ctor = kind['ctor'] if ctor == "NEntity": val = kind['val'] ctor = val['ctor'] if ctor == "ETerm": label = repr(val['val']) elif ctor == "ENonTerm": label = val['name'] else: label = ctor + " ..." elif ctor == "NReturn": label = "return" else: label = ctor + " ..." if label == 'Start ...': label = "Start " + str(starts[i]) label += " %d" % i gnodes[i] = g.new(a.Label(label), a.Shape.box) for i, node in nodes.items(): gnode = gnodes[i] for each in node['followed']: gnode > gnodes[each] g.view()
import graphviz_artist as ga import graphviz_artist.attr as attr # make a graph g = ga.Graph(directed=True) new = g.new # decl nodes false = new(attr.Label("False")) true = new(attr.Label("True")) not_ = new(attr.Label("not")) and_ = new(attr.Label("and")) unary1 = new(attr.Label("unary")) unary2 = new(attr.Label("unary")) binary = new(attr.Label("binary"), attr.Width(2), attr.Shape.box) expr = new(attr.Label("expr")) # build graph _ = false > unary1 < not_ _ = true > unary2 _ = and_[attr.Label('Op')] > binary # XLabel: For edges, the label will be placed near the center of the edge. _ = unary1[attr.XLabel("Left operand")] > binary _ = unary2[attr.XLabel('Right operand')] > binary _ = binary > expr g.save()
import graphviz_artist as ga import graphviz_artist.attr as a """ A -> b c A -> A d """ g = ga.Graph(directed=True) g.update(attrs=(a.HorizontalGraph, )) A = g.new(a.Label("start of A")) b = g.new(a.Label("b")) c = g.new(a.Label("c")) d = g.new(a.Label("d")) B = g.new(a.Label("push scope of B")) B_ = g.new(a.Label("pop scope of B")) A_ = g.new(a.Label("end of A")) _ = A > b > c > A_ _ = A_[a.Label("left recur")] > B > d > B_[a.Label( "left recursion reduction")] > A g.view()