Beispiel #1
0
def prefix_parser_recursive(tokenizer):
    math_tokens = ["+", "-", "/", "*"]
    token = tokenizer.get_next_token()
    if token in math_tokens:
        left = prefix_parser_recursive(tokenizer)
        right = prefix_parser_recursive(tokenizer)

        if token == "+":
            return left + right

        if token == "-":
            return left - right

        if token == "/":
            if right == 0:
                raise DivisionByZero()
            else:
                return left / right

        if token == "*":
            return left * right

    if token.isdigit:
        return int(token)

    #print(token) # debug line
    return 0
Beispiel #2
0
    def build_tree_recursive(self, tokenizer):
        token = tokenizer.get_next_token()

        if token == "+" or token == "-" or token == "*" or token == "/":
            return PrefixNode(token, self.build_tree_recursive(tokenizer), self.build_tree_recursive(tokenizer))
        elif token.isdigit():
            return PrefixNode(int(token), None, None, TokenType.NUMBER)
        else:
            return PrefixNode(token, None, None, TokenType.UNKNOWN)
def prefix_parser_recursive(tokenizer):
    token = tokenizer.get_next_token()
    if token in {"*", "/", "+", "-"}:
        a_str = str(prefix_parser_recursive(tokenizer)) + token
        a_str += str(prefix_parser_recursive(tokenizer)) 
        try:
            return eval(a_str)
        except ZeroDivisionError:
            raise DivisionByZero()
    else:
        return token
Beispiel #4
0
def prefix_parser_recursive(tokenizer):
    token = tokenizer.get_next_token()
    #print(token) # debug line
    if token.isnumeric():
        return int(token)
    if token == "+":
        return prefix_parser_recursive(tokenizer) + prefix_parser_recursive(tokenizer)
    elif token == "-":
        return prefix_parser_recursive(tokenizer) - prefix_parser_recursive(tokenizer)
    elif token == "*":
        return prefix_parser_recursive(tokenizer) * prefix_parser_recursive(tokenizer)
    elif token == "/":
        a = prefix_parser_recursive(tokenizer) 
        b = prefix_parser_recursive(tokenizer)

        if b == 0:
            raise DivisionByZero
        else:
            return a/b
Beispiel #5
0
def prefix_parser_recursive(tokenizer):
    token = tokenizer.get_next_token()
    # print(token) # debug line
    if token.isdigit():
        return int(token)
    elif token == "+":
        return prefix_parser_recursive(tokenizer) + prefix_parser_recursive(
            tokenizer)
    elif token == '-':
        return prefix_parser_recursive(tokenizer) - prefix_parser_recursive(
            tokenizer)
    elif token == '/':
        try:
            return prefix_parser_recursive(
                tokenizer) / prefix_parser_recursive(tokenizer)
        except:
            raise DivisionByZero()
    elif token == '*':
        return prefix_parser_recursive(tokenizer) * prefix_parser_recursive(
            tokenizer)
    return 0
def prefix_parser_recursive(tokenizer):
    token = tokenizer.get_next_token()
    # print(token) # debug line
    if token.isdigit():
        return int(token)
    elif token == "+":
        return prefix_parser_recursive(tokenizer) + prefix_parser_recursive(
            tokenizer)
    elif token == "-":
        return prefix_parser_recursive(tokenizer) - prefix_parser_recursive(
            tokenizer)
    elif token == "*":
        return prefix_parser_recursive(tokenizer) * prefix_parser_recursive(
            tokenizer)
    elif token == "/":
        val1 = prefix_parser_recursive(tokenizer)
        val2 = prefix_parser_recursive(tokenizer)
        if (val2 == 0):
            raise DivisionByZero
        return val1 / val2
    return 0
Beispiel #7
0
def prefix_parser_recursive(tokenizer):
    token = tokenizer.get_next_token()

    if token.isdigit():
        return int(token)

    if token == '+':
        return prefix_parser_recursive(tokenizer) + prefix_parser_recursive(
            tokenizer)
    elif token == '-':
        return prefix_parser_recursive(tokenizer) - prefix_parser_recursive(
            tokenizer)
    elif token == '*':
        return prefix_parser_recursive(tokenizer) * prefix_parser_recursive(
            tokenizer)
    elif token == '/':
        a = prefix_parser_recursive(tokenizer)
        b = prefix_parser_recursive(tokenizer)

        if b == 0:
            raise DivisionByZero()

        return a / b
Beispiel #8
0
def prefix_parser_recursive(tokenizer):
    token = tokenizer.get_next_token()
    print(token)  # debug line
    print(type(token))
    return 0