Exemplo n.º 1
0
def buildParseTree(fpexp):
    fplist = fpexp.split()
    pStack = Stack()
    eTree = BinaryTree('')
    pStack.push(eTree)

    for i in fplist:
        if i == '(':
            # 如果当前符号是“(”,添加一个新节点作为当前节点的左子节点,并且下降到左子节点
            eTree.insertLeft('')
            pStack.push(eTree)
            eTree = eTree.getLeftChild()
        elif i not in ['+', '-', '/', '*', ')']:
            # 如果当前符号是数字,将当前节点的根值设置为该数字并返回父节点
            eTree.setRootVal(int(i))
            parent = pStack.pop()
            eTree = parent
        elif i in ['+', '-', '/', '*']:
            eTree.setRootVal(i)
            eTree.insertRight('')
            pStack.push(eTree)
            eTree = eTree.getRightChild()
        elif i == ')':
            eTree = pStack.pop()
        else:
            raise ValueError

    return eTree
Exemplo n.º 2
0
def build_tree_condition(elements):
    if len(elements) == 1:
        return elements[0]
    tree = BinaryTree('')
    for i in range(len(elements)):
        if type(elements[i]) is BinaryTree:
            if tree.getRootVal() == '':
                tree.insertLeft(elements[i])
            else:
                tree.insertRight(elements[i])
        elif tree.getRootVal() in ['AND', 'OR', 'or', 'and']:
            new_tree = BinaryTree('')
            new_tree.setRootVal(elements[i])
            new_tree.insertLeft(tree)
            tree = new_tree
        else:
            tree.setRootVal(elements[i])
    return tree
Exemplo n.º 3
0
def build_parse_tree(fpexp):
    fplist = fpexp.split()
    for i in fplist:
        if i.__contains__('(') and i != '(':
            index = fplist.index(i)
            i_l = i.split('(')
            fplist[index] = i_l[-1]
            for j in range(len(i_l)-1):
                fplist.insert(index, '(')
        if i.__contains__(')') and i != ')':
            index = fplist.index(i)
            i_l = i.split(')')
            fplist[index] = i.split(')')[0]
            for j in range(len(i_l)-1):
                fplist.insert(index+1, ')')
    pstack = Stack()
    current_tree = BinaryTree('')
    current_tree.insertLeft('')
    pstack.push(current_tree)
    current_tree = current_tree.getLeftChild()
    for i in fplist:
        if i == '(':
            current_tree.insertLeft('')
            pstack.push(current_tree)
            current_tree = current_tree.getLeftChild()
        elif i not in ['and', 'or', ')']:
            current_tree.setRootVal(i)
            parent = pstack.pop()
            current_tree = parent

        elif i in ['and', 'or']:
            if current_tree.getRootVal() != "":
                pstack.push(current_tree)
                current_tree = BinaryTree('')
                current_tree.leftChild = pstack.pop()
            current_tree.setRootVal(i)
            current_tree.insertRight('')
            pstack.push(current_tree)
            current_tree = current_tree.getRightChild()
        elif i == ')':
            current_tree = pstack.pop()
        else:
            raise ValueError
    return current_tree
Exemplo n.º 4
0
def build_tree_expression(elements):
    stack = Stack()
    tree = BinaryTree('')
    if len(elements) == 1:
        tree.setRootVal(elements[0])
        return tree
    stack.push(tree)
    for i in range(len(elements)):
        if (elements[i] == '(') or (i == 0):
            tree.insertLeft('')
            if elements[i] == '(':
                stack.push("(")
            stack.push(tree)
            tree = tree.getLeftChild()
            if elements[i] != '(':
                tree.setRootVal(elements[i])
                parent = stack.pop()
                tree = parent
        elif elements[i] not in ['+', '-', '*', '/', ')']:
            tree.setRootVal(elements[i])
            parent = stack.pop()
            tree = parent
        elif elements[i] in ['+', '-', '*', '/']:
            sign = elements[i]
            if tree.getRootVal() in ['+', '-', '*', '/']:
                if sign in ['+', '-'] or elements[i - 1] == ')':
                    temp = BinaryTree('')
                    parent = stack.pop()
                    if stack.size() == 0:
                        temp.insertLeft(parent)
                        parent = temp
                        tree = parent
                        stack.push(parent)
                    elif parent == "(":
                        temp.insertLeft(tree)
                        parent = stack.pop()
                        parent.insertRight(temp)
                        stack.push(parent)
                        stack.push("(")
                        tree = parent.getRightChild()
                    else:
                        temp.insertLeft(tree)
                        parent.insertRight(temp)
                        stack.push(parent)
                        tree = parent.getRightChild()
                elif sign in ['*', '/']:
                    rChild = tree.getRightChild()
                    rChild.insertLeft(rChild.getRootVal())
                    tree = rChild
            tree.setRootVal(elements[i])
            tree.insertRight('')
            stack.push(tree)
            tree = tree.getRightChild()
        elif elements[i] == ')':
            parent = ""
            while parent != "(":
                parent = stack.pop()
                if parent == "(":
                    tree = stack.pop()
            stack.push(tree)
        if i + 1 == len(elements):
            for j in range(stack.size()):
                parent = stack.pop()
                tree = parent
    return tree
Exemplo n.º 5
0
def p_tree_selects(p):
    '''tree_selects :   LBRACKET tree_selects RBRACKET union LBRACKET tree_selects RBRACKET
                    |   LBRACKET tree_selects RBRACKET intersect LBRACKET tree_selects RBRACKET
                    |   LBRACKET tree_selects RBRACKET join LBRACKET tree_selects RBRACKET join_condition
                    |   LBRACKET tree_selects RBRACKET join LBRACKET tree_selects RBRACKET
                    |   LBRACKET tree_selects RBRACKET union select
                    |   LBRACKET tree_selects RBRACKET intersect select
                    |   LBRACKET tree_selects RBRACKET join select join_condition
                    |   LBRACKET tree_selects RBRACKET join select
                    |   LBRACKET tree_selects RBRACKET join name_table join_condition
                    |   LBRACKET tree_selects RBRACKET join name_table
                    |   select union LBRACKET tree_selects RBRACKET
                    |   select intersect LBRACKET tree_selects RBRACKET
                    |   select join LBRACKET tree_selects RBRACKET AS name_table join_condition
                    |   select join LBRACKET tree_selects RBRACKET AS name_table
                    |   select union select
                    |   select intersect select
                    |   select join select join_condition
                    |   select join select
                    |   select join name_table join_condition
                    |   select join name_table
                    |   select'''

    tree = BinaryTree('')

    if len(p) == 2:
        tree.setRootVal(p[1])
        p[0] = tree
    elif len(p) == 4:
        tree.insertLeft(p[1])
        tree.setRootVal(p[2])
        tree.insertRight(p[3])
    elif len(p) == 5:
        tree.insertLeft(p[1])
        temp = p[2]
        temp.join_condition = p[4]
        tree.setRootVal(temp)
        tree.insertRight(p[3])
    elif len(p) == 6:
        if p[1] == "(":
            tree.insertLeft(p[2])
            tree.setRootVal(p[4])
            tree.insertRight(p[5])
        else:
            tree.insertLeft(p[1])
            tree.setRootVal(p[2])
            tree.insertRight(p[4])
    elif len(p) == 7:
        tree.insertLeft(p[2])
        temp = p[4]
        temp.join_condition = p[6]
        tree.setRootVal(temp)
        tree.insertRight(p[5])
    elif len(p) == 8:
        if p[5] == "(":
            tree.insertLeft(p[2])
            tree.setRootVal(p[4])
            tree.insertRight(p[6])
        else:
            tree.insertLeft(p[1])
            tree.setRootVal(p[2])
            tree.insertRight(PNamedTree(p[4], p[7]))
    elif len(p) == 9:
        if p[5] == "(":
            tree.insertLeft(p[2])
            temp = p[4]
            temp.join_condition = p[8]
            tree.setRootVal(temp)
            tree.insertRight(p[6])
        else:
            tree.insertLeft(p[1])
            temp = p[2]
            temp.join_condition = p[8]
            tree.setRootVal(temp)
            tree.insertRight(PNamedTree(p[4], p[7]))
    p[0] = PTreeSelects(tree)