Beispiel #1
0
def parse_expression(tokens):
    s = Stack()
    p = {')': '(', ']': '[', '}': '{'}

    for i in tokens:
        try:
            i = BinaryTree(int(i))
        except ValueError:
            pass
        if i not in p:
            s.push(i)
        else:
            try:
                arg2 = s.pop()
                operator = s.pop()
                arg1 = s.pop()
                symbol = s.pop()
                if symbol != p[i]:
                    return
                tree = BinaryTree(operator)
                tree.left_node = arg1
                tree.right_node = arg2
                s.push(tree)
            except EmptyStackError:
                return
    if s.is_empty():
        return
    tree = s.pop()
    if s.is_empty():
        return tree
    else:
        return
def parse_expression(tokens):
    paranthesis = {')': '(', '}': '{', ']': '['}
    stack = Stack()
    for token in tokens:
        try:
            token = BinaryTree(int(token))
        except ValueError:
            pass
        if token not in paranthesis:
            stack.push(token)
        else:
            try:
                arg_2 = stack.pop()
                operator = stack.pop()
                arg_1 = stack.pop()
                opening_paranthesis = stack.pop()
                if opening_paranthesis != paranthesis[token]:
                    return
                #stack.push({'+': add, '-': sub, '*': mul, '/':truediv}[operator](arg_1, arg_2))

                parse_tree = BinaryTree(operator)
                parse_tree.right_node = arg_2
                parse_tree.left_node = arg_1
                stack.push(parse_tree)

            except EmptyStackError:
                return
    if stack.is_empty():
        return
    value = stack.pop()
    if not stack.is_empty():
        return
    return parse_tree
Beispiel #3
0
def parse_expression(tokens):#equal to evaluate_expression(tokens)#below are my code
    parenthese = {')':'(','}':'{',']':'['}
    stack = Stack()
    for token in tokens:
        try:
            token = BinaryTree(int(token))
        except ValueError:
            pass
        if token not in parenthese:
            stack.push(token)
        else:
            try:
                arg_2 = stack.pop()
                operater = stack.pop()
                arg_1 = stack.pop()
                former = stack.pop()
                if parenthese[token] == former:
                    my_tree = BinaryTree(operater)
                    my_tree.left_node = arg_1
                    my_tree.right_node = arg_2
                    stack.push(my_tree)
                else:
                    return
            except EmptyStackError:
                return
    if stack.is_empty():
        return
    my_tree = stack.pop()
    if not stack.is_empty():
        return
    return my_tree
Beispiel #4
0
def parse_expression(tokens):
    parentheses = {')': '(', ']': '[', '}': '{'}
    stack = Stack()
    for token in tokens:
        try:
            token = BinaryTree(int(token))  #***************************
        except ValueError:
            pass
        if token not in parentheses:
            stack.push(token)
        else:
            try:
                arg_2 = stack.pop()
                operator = stack.pop()
                arg_1 = stack.pop()
                opening_group_symbol = stack.pop()
                if parentheses[token] != opening_group_symbol:
                    return
                #******************************************************
                parse_tree = BinaryTree(operator)
                parse_tree.left_node = arg_1
                parse_tree.right_node = arg_2
                stack.push(parse_tree)
                #*****************************************************
            except EmptyStackError:
                return
    if stack.is_empty():
        return
    parse_tree = stack.pop()
    if not stack.is_empty():
        return
    return parse_tree
def parse_expression(tokens):
    parentheses = {')': '(', ']': '[', '}': '{'}
    stack = Stack()
    # tree = BinaryTree()
    for token in tokens:
        try:
            token = BinaryTree(int(token))
        except ValueError:
            pass
        if token not in parentheses:
            stack.push(token)
        else:
            try:
                arg_2 = stack.pop()
                operator = stack.pop()
                arg_1 = stack.pop()
                symbol = stack.pop()
                if symbol != parentheses[token]:
                    return
                tree = BinaryTree(operator)
                tree.left_node = arg_1
                tree.right_node = arg_2
                stack.push(tree)
                # stack.push({'+': add, '-': sub, '*': mul, '/': truediv}[operator](arg_1, arg_2))
            except EmptyStackError:
                return
    if stack.is_empty():
        return
    tree = stack.pop()
    if not stack.is_empty():
        return
    return tree
def parse_tree_for_expression_or_term(operators, tokens):
    parse_tree_function = (partial(parse_tree_for_expression_or_term, '*/')
                           if '+' in operators else parse_tree_for_factor)
    tree = parse_tree_function(tokens)
    if tree is None:
        return
    parse_tree = None
    while len(tokens) and tokens[-1] in operators:
        operator = tokens.pop()
        other_tree = parse_tree_function(tokens)
        if other_tree is None:
            return
        parse_tree = BinaryTree(operator)
        parse_tree.left_node = tree
        parse_tree.right_node = other_tree
        tree = parse_tree
    if not parse_tree:
        return tree
    return parse_tree
def parse_tree_for_factor(tokens):
    try:
        token = tokens.pop()
        return BinaryTree(int(token))
    # No token was left
    except IndexError:
        return
    except ValueError:
        if token != '(':
            return
        parse_tree = parse_tree_for_expression_or_term('+-', tokens)
        if len(tokens) and tokens.pop() == ')':
            return parse_tree
        return
Beispiel #8
0
def parse_expression(tokens):
    # Replace pass above with your code, modified from the exercise:
    # Evaluate fully parenthetised expressions with a stack.
    stack = Stack()
    oppo_brace = {')': '(', ']': '[', '}': '{'}
    for token in tokens:
        if not (token == ')' or token == ']' or token == '}'):
            if not (token == '(' or token == '[' or token == '{' \
                    or token == '+' or token == '-' or token == '*' \
                    or token == '/'):
                token = BinaryTree(int(token))
                stack.push(token)
            else:
                stack.push(token)
        else:
            oppo = oppo_brace[token]
            try:
                arg_3 = stack.pop()
                arg_2 = stack.pop()
                arg_1 = stack.pop()
                arg = stack.pop()
                if arg != oppo:
                    return
            except EmptyStackError:
                return

            #stack.push({'+': add, '-': sub, '*': mul, '/': truediv}[arg_2](arg_1, arg_3))
            parse_tree = BinaryTree(arg_2)
            parse_tree.left_node = arg_1
            parse_tree.right_node = arg_3
            stack.push(parse_tree)
    if stack.is_empty():
        return
    parse_tree = stack.pop()
    if not stack.is_empty():
        return
    return parse_tree