Esempio n. 1
0
def parse_dot(queue):
    if len(queue) == 0:
        print("ERROR: Code Not Complete: Expected a method call")
        raise SystemExit

    else:
        value = queue[0][1]

        if value == "length":
            queue.pop(0)
            d = Dot1()
            return d

        elif LexicalAnalyzer.is_id(value):
            i = parse_identifier(queue)
            validate(queue, "(")

            exp_one = parse_exp_only_one(queue)
            exp = parse_exp(queue)

            validate(queue, ")")

            d = Dot2(i, exp_one, exp)
            return d

        else:
            print("ERROR: Code Not Complete: Expected a method call at line", queue[0][0])
            raise SystemExit
Esempio n. 2
0
def parse_new_object(queue):

    if len(queue) == 0:
        print("ERROR: Code Not Complete: Expected an Object")
        raise SystemExit
    
    else:
        value = queue[0][1]

        if value == "int":
            queue.pop(0)
            validate(queue, "[")

            exp = parse_expression(queue)
            throw_exception_expression(exp)

            validate(queue, "]")

            n = NewObject1(exp)
            return n

        elif LexicalAnalyzer.is_id(value):
            i = parse_identifier(queue)

            validate(queue, "(")
            validate(queue, ")")

            n = NewObject2(i)
            return n

        else:
            print("ERROR: Code Not Complete: Expected an Object at line", queue[0][0])
            raise SystemExit
Esempio n. 3
0
def parse_identifier(queue):

    if len(queue) == 0:
        print("ERROR: Expected an identifier")
        raise SystemExit
    
    token = queue.pop(0)

    if not LexicalAnalyzer.is_id(token[1]):
        print("Not a correct identifier at line", token[0])
        raise SystemExit
    
    if token[1] == "public" or token[1] == "static" \
            or token[1] == "void" or token[1] == "main" \
            or token[1] == "int" or token[1] == "float" \
            or token[1] == "if" or token[1] == "else" or token[1] == "while" \
            or token[1] == "String" or token[1] == "char" or token[1] == "boolean" \
            or token[1] == "extends" or token[1] == "return" or token[1] == "new" \
            or token[1] == "this" or token[1] == "true" or token[1] == "false" or token[1] == "class":
                print("Using keyword as an identifier at line", token[0])
                raise SystemExit
    
    return token[1]
Esempio n. 4
0
def parse_expression(queue):
    if len(queue) == 0:
        print("ERROR: Code Not Complete: Expected an expression")
        raise SystemExit
    
    else:
        value = queue[0][1]

        if LexicalAnalyzer.is_integer_literal(value):
            queue.pop(0)
            e_rec = parse_expression_recursion_fix(queue)

            e1 = Expression1(value, e_rec)
            return e1

        elif value == "true":
            queue.pop(0)
            e_rec = parse_expression_recursion_fix(queue)

            e2 = Expression2(e_rec)
            return e2

        elif value == "false":
            queue.pop(0)
            e_rec = parse_expression_recursion_fix(queue)

            e3 = Expression3(e_rec)
            return e3

        elif value == "this":
            queue.pop(0)
            e_rec = parse_expression_recursion_fix(queue)

            e5 = Expression5(e_rec)
            return e5

        elif value == "new":
            queue.pop(0)
            n = parse_new_object(queue)
            e_rec = parse_expression_recursion_fix(queue)

            e6 = Expression6(n, e_rec)
            return e6

        elif value == "!":
            queue.pop(0)
            exp = parse_expression(queue)
            throw_exception_expression(exp)

            e_rec = parse_expression_recursion_fix(queue)

            e7 = Expression7(exp, e_rec)
            return e7

        elif value == "(":
            queue.pop(0)

            exp = parse_expression(queue)
            throw_exception_expression(exp)

            validate(queue, ")")

            e_rec = parse_expression_recursion_fix(queue)

            e8 = Expression8(exp, e_rec)
            return e8

        elif LexicalAnalyzer.is_id(value):
            i = parse_identifier(queue)
            e_rec = parse_expression_recursion_fix(queue)

            e4 = Expression4(i, e_rec)
            return e4

        else:
            e = Epsilon()
            return e
Esempio n. 5
0
def parse_statement(queue):

    if len(queue) == 0:
        print("ERROR: Code Not Complete: Expected a Statement")
        raise SystemExit
    else:
        value = queue[0][1]

        if value == "{":
            queue.pop(0)

            st = parse_st(queue)
            validate(queue, "}")
            s = Statement1(st)
            return s

        elif value == "if":
            queue.pop(0)
            validate(queue, "(")

            exp = parse_expression(queue)
            throw_exception_expression(exp)

            validate(queue, ")")

            s = parse_statement(queue)

            el = parse_else(queue)

            s2 = Statement2(exp, s, el)
            return s2

        elif value == "while":
            queue.pop(0)
            validate(queue, "(")

            exp = parse_expression(queue)
            throw_exception_expression(exp)

            validate(queue, ")")

            s = parse_statement(queue)

            s3 = Statement3(exp, s)
            return s3

        elif value == "System.out.println":
            queue.pop(0)

            validate(queue, "(")

            exp = parse_expression(queue)
            throw_exception_expression(exp)

            validate(queue, ")")
            validate(queue, ";")

            s4 = Statement4(exp)
            throw_exception_expression(exp)

            return s4

        elif LexicalAnalyzer.is_id(value):
            i = parse_identifier(queue)
            si = parse_single_or_array(queue)

            s5 = Statement5(i, si)
            return s5

        else:
            e = Epsilon()
            return e