Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

You can write your own parse tree class. It should be possible to plot the tree as graph.

class Tree(object):

    def __init__(self,item,left=None,right=None):
        self.item = item
        self.type = type
        self.left = left
        self.right = right

    def show(self):       
        if self.left: self.left.show()
        print self.item, 
        if self.right: self.right.show()

    def evaluate(self):
        """ 
        if self.item is an operator, it is a callable function,
        so you can call ist with left and right sub-tree as arguments
        """
        if self.left==None and self.right==None:  # self.item is a 'leaf'
            return self.item
        else:                # self.item is an operator 
            return self.item(self.left.evaluate(),self.right.evaluate())


def create_tree(expr):
    """
    expr is an expression  or(!)  a tuple containing an operation 
                                  and its list of operands
    """
    if type(expr)==tuple:
        op = expr[0]
        operands = expr[1]
    else:
        try:
           op = expr.operator()
           operands = expr.operands()
        except:
            return Tree(expr)
    if not op:  
       return Tree(expr)
    else:
        if len(operands) > 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree((op,operands[1:])))
        elif len(operands) == 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree(operands[1])) 
        else:    
            # unary operation , not testet
            return Tree(op,right=create_tree(operands[0]))


T=create_tree(5+x+x^2)
#T.show()
T.evaluate()

You can write your own parse tree class. It should be possible to plot the tree as graph.

class Tree(object):

    def __init__(self,item,left=None,right=None):
        self.item = item
        self.type = type
        self.left = left
        self.right = right

    def show(self):       
        if self.left: self.left.show()
        print self.item, 
        if self.right: self.right.show()

    def evaluate(self):
        """ 
        if self.item is an operator, it is a callable function,
        so you can call ist with left and right sub-tree as arguments
        """
        if self.left==None and self.right==None:  # self.item is a 'leaf'
            return self.item
        else:                # self.item is an operator 
            return self.item(self.left.evaluate(),self.right.evaluate())


def create_tree(expr):
    """
    expr is an expression  or(!)  a tuple containing an operation 
                                  and its list of operands
    """
    if type(expr)==tuple:
        op = expr[0]
        operands = expr[1]
    else:
        try:
           op = expr.operator()
           operands = expr.operands()
        except:
            return Tree(expr)
    if not op:  
       return Tree(expr)
    else:
        if len(operands) > 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree((op,operands[1:])))
        elif len(operands) == 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree(operands[1])) 
        else:    
            # unary operation , not testet
            return Tree(op,right=create_tree(operands[0]))


T=create_tree(5+x+x^2)
#T.show()
T.evaluate()

You can write your own parse tree class. It should be possible to plot the tree as graph.

class Tree(object):

    def __init__(self,item,left=None,right=None):
        self.item = item
        self.left = left
        self.right = right

    def show(self):       
        if self.left: self.left.show()
        print self.item, 
        if self.right: self.right.show()

    def evaluate(self):
        """ 
        if self.item is an operator, it is a callable function,
        so you can call ist it with left and right sub-tree as arguments
        """
        if self.left==None and self.right==None:  # self.item is a 'leaf'
            return self.item
        else:                # self.item is an operator 
            return self.item(self.left.evaluate(),self.right.evaluate())


def create_tree(expr):
    """
    expr is an expression  or(!)  a tuple containing an operation 
                                  and its list of operands
    """
    if type(expr)==tuple:
        op = expr[0]
        operands = expr[1]
    else:
        try:
           op = expr.operator()
           operands = expr.operands()
        except:
            return Tree(expr)
    if not op:  
       return Tree(expr)
    else:
        if len(operands) > 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree((op,operands[1:])))
        elif len(operands) == 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree(operands[1])) 
        else:    
            # unary operation , not testet
            return Tree(op,right=create_tree(operands[0]))


T=create_tree(5+x+x^2)
#T.show()
T.evaluate()

You can write your own parse tree class. It should be possible to plot the tree as graph.

class Tree(object):

    def __init__(self,item,left=None,right=None):
        self.item = item
        self.left = left
        self.right = right

    def show(self):       
        if self.left: self.left.show()
        print self.item, 
        if self.right: self.right.show()

    def evaluate(self):
        """ 
        if self.item is an operator, it is a callable function,
        so you can call it with left and right sub-tree as arguments
        """
        if self.left==None and self.right==None:  # self.item is a 'leaf'
            return self.item
        elif self.left==None:  # unary operation
            return self.item(self.right.evaluate())
        else:                 # self.item is an operator binary operation 
            return self.item(self.left.evaluate(),self.right.evaluate())


def create_tree(expr):
    """
    expr is an expression  or(!)  a tuple containing an operation 
                                  and its list of operands
    """
    if type(expr)==tuple:
        op = expr[0]
        operands = expr[1]
    else:
        try:
           op = expr.operator()
           operands = expr.operands()
        except:
            return Tree(expr)
    if not op:  
       return Tree(expr)
    else:
        if len(operands) > 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree((op,operands[1:])))
        elif len(operands) == 2:
            return Tree(op,left=create_tree(operands[0]),
                           right=create_tree(operands[1])) 
        else:    
            # unary operation , not testet
            return Tree(op,right=create_tree(operands[0]))


T=create_tree(5+x+x^2)
#T.show()
T.evaluate()