def graph(expr):
 nodes = range(len(expr))
 edges = list()
 labels = dict()
 stack = []
 for (i, node) in enumerate(expr):
  if stack:
   edges.append((stack[(-1)][0], i))
   stack[(-1)][1] -= 1
  labels[i] = (node.name if isinstance(node, Primitive) else node.value)
  stack.append([i, node.arity])
  while (stack and (stack[(-1)][1] == 0)):
   stack.pop()
 return (nodes, edges, labels)

def graph ( expr ) : nodes = range ( len ( expr ) ) edges = list ( ) labels = dict ( ) stack = [ ] for ( i , node ) in enumerate ( expr ) : if stack : edges . append ( ( stack [ ( - 1 ) ] [ 0 ] , i ) ) stack [ ( - 1 ) ] [ 1 ] -= 1 labels [ i ] = ( node . name if isinstance ( node , Primitive ) else node . value ) stack . append ( [ i , node . arity ] ) while ( stack and ( stack [ ( - 1 ) ] [ 1 ] == 0 ) ) : stack . pop ( ) return ( nodes , edges , labels )
construct the graph of a tree expression .