예제 #1
0
def p_goal(p):
    """
    start : main_class class_s
          | main_class
    """
    if len(p) == 2:
        p[0] = ast.Program(p[1], None, ast.Position(*get_pos(p)))
    else:
        p[0] = ast.Program(p[1], p[2], ast.Position(*get_pos(p)))
    return p[0]
예제 #2
0
def p_exp(p):
    """
    exp : exp L_SQUARE exp R_SQUARE
        | exp DOT LENGTH                                               
        | exp DOT id L_ROUND R_ROUND                                   
        | exp DOT id L_ROUND exp_s R_ROUND
    """
    if len(p) == 5:
        p[0] = ast.RandomAccessExpr(p[1], p[3], ast.Position(*get_pos(p)))
    elif len(p) == 4:
        p[0] = ast.LengthExpr(p[1], ast.Position(*get_pos(p)))
    elif len(p) == 6:
        p[0] = ast.CallMethodExpr(p[1], p[3], None, ast.Position(*get_pos(p)))
    else:
        p[0] = ast.CallMethodExpr(p[1], p[3], p[5], ast.Position(*get_pos(p)))
예제 #3
0
def p_method(p):
    """
    method : modifier type id L_ROUND arg_s R_ROUND L_BRACKET var_s statement_s stm_ret R_BRACKET
           | modifier type id L_ROUND arg_s R_ROUND L_BRACKET var_s stm_ret R_BRACKET
           | modifier type id L_ROUND arg_s R_ROUND L_BRACKET statement_s stm_ret R_BRACKET
           | modifier type id L_ROUND arg_s R_ROUND L_BRACKET stm_ret R_BRACKET
    """
    if isinstance(p[8], ast.ReturnStatement):
        p[0] = ast.MethodDecl(p[1], p[2], p[3], p[5], None, None, p[8],
                              ast.Position(*get_pos(p)))
    elif len(p) == 12:
        p[0] = ast.MethodDecl(p[1], p[2], p[3], p[5], p[8], p[9], p[10],
                              ast.Position(*get_pos(p)))
    elif isinstance(p[8], ast.VarDeclList):
        p[0] = ast.MethodDecl(p[1], p[2], p[3], p[5], p[8], None, p[9],
                              ast.Position(*get_pos(p)))
    else:
        p[0] = ast.MethodDecl(p[1], p[2], p[3], p[5], None, p[8], p[9],
                              ast.Position(*get_pos(p)))
예제 #4
0
def p_exp_binary_operation(p):
    """
    exp : exp AND exp
        | exp LESS exp
        | exp PLUS exp
        | exp MINUS exp
        | exp STAR exp
        | exp PERCENT exp
        | exp OR exp
    """
    p[0] = ast.BinaryExpr(p[1], p[2], p[3], ast.Position(*get_pos(p)))
예제 #5
0
def p_exp_vars(p):
    """
    exp : INTEGER
        | TRUE
        | FALSE
        | id
        | THIS
        | NEW INT L_SQUARE exp R_SQUARE
        | NEW id L_ROUND R_ROUND
        | BANG exp
        | L_ROUND exp R_ROUND
    """
    if p[1] == 'true' or p[1] == 'false':
        p[0] = ast.ValueExpr(ast.ValueEnum.BOOLEAN, p[1],
                             ast.Position(*get_pos(p)))
    elif p[1] == 'this':
        p[0] = ast.ThisExpr(ast.Position(*get_pos(p)))
    elif len(p) == 6:
        p[0] = ast.NewIntArrExpr(p[4], ast.Position(*get_pos(p)))
    elif len(p) == 5:
        p[0] = ast.NewObjectExpr(p[2], ast.Position(*get_pos(p)))
    elif len(p) == 3:
        p[0] = ast.NotExpr(p[2], ast.Position(*get_pos(p)))
    elif len(p) == 4:
        p[0] = p[2]
    elif isinstance(p[1], int):
        p[0] = ast.ValueExpr(ast.ValueEnum.INTEGER, p[1],
                             ast.Position(*get_pos(p)))
    else:
        p[0] = p[1]
예제 #6
0
def p_statement(p):
    """
    statement : L_BRACKET statement_s R_BRACKET
              | IF L_ROUND exp R_ROUND statement ELSE statement
              | WHILE L_ROUND exp R_ROUND statement
              | SYSTEM_OUT_PRINTLN L_ROUND exp R_ROUND SEMICOLON
              | id EQUALS exp SEMICOLON
              | id L_SQUARE exp R_SQUARE EQUALS exp SEMICOLON
    """
    if p[1] == 'if':
        p[0] = ast.IfStatement(p[3], p[5], p[7], ast.Position(*get_pos(p)))
    elif p[1] == 'while':
        p[0] = ast.WhileStatement(p[3], p[5], ast.Position(*get_pos(p)))
    elif p[1] == 'System.out.println':
        p[0] = ast.PrintLineStatement(p[3], ast.Position(*get_pos(p)))
    elif len(p) == 4:
        p[0] = ast.Statements(p[2])
    elif len(p) == 5:
        p[0] = ast.AssignStatement(p[1], p[3], ast.Position(*get_pos(p)))
    else:
        p[0] = ast.RandomAccessAssignStatement(p[1], p[3], p[6],
                                               ast.Position(*get_pos(p)))
예제 #7
0
def p_class(p):
    """
    class : CLASS id L_BRACKET R_BRACKET
          | CLASS id L_BRACKET var_s R_BRACKET
          | CLASS id L_BRACKET method_s R_BRACKET
          | CLASS id L_BRACKET var_s method_s R_BRACKET
          | CLASS id EXTENDS id L_BRACKET R_BRACKET
          | CLASS id EXTENDS id L_BRACKET var_s R_BRACKET
          | CLASS id EXTENDS id L_BRACKET method_s R_BRACKET
          | CLASS id EXTENDS id L_BRACKET var_s method_s R_BRACKET
    """
    if p[3] != 'extends':
        if isinstance(p[4], str):
            p[0] = ast.ClassDecl(p[2], None, None, None,
                                 ast.Position(*get_pos(p)))
        elif len(p) == 7:
            p[0] = ast.ClassDecl(p[2], None, p[4], p[5],
                                 ast.Position(*get_pos(p)))
        elif isinstance(p[4], ast.VarDeclList):
            p[0] = ast.ClassDecl(p[2], None, p[4], None,
                                 ast.Position(*get_pos(p)))
        elif isinstance(p[4], ast.MethodDeclList):
            p[0] = ast.ClassDecl(p[2], None, None, p[4],
                                 ast.Position(*get_pos(p)))
    else:
        if isinstance(p[6], str):
            p[0] = ast.ClassDecl(p[2], p[4], None, None,
                                 ast.Position(*get_pos(p)))
        elif len(p) == 9:
            p[0] = ast.ClassDecl(p[2], p[4], p[6], p[7],
                                 ast.Position(*get_pos(p)))
        elif isinstance(p[6], ast.VarDeclList):
            p[0] = ast.ClassDecl(p[2], p[4], p[6], None,
                                 ast.Position(*get_pos(p)))
        elif isinstance(p[6], ast.MethodDeclList):
            p[0] = ast.ClassDecl(p[2], p[4], None, p[6],
                                 ast.Position(*get_pos(p)))
예제 #8
0
def p_main_class(p):
    """
    main_class : CLASS id L_BRACKET PUBLIC STATIC_VOID_MAIN L_ROUND STRING L_SQUARE R_SQUARE id R_ROUND L_BRACKET statement_s R_BRACKET R_BRACKET
    """
    p[0] = ast.MainClass(p[2], p[10], p[13], ast.Position(*get_pos(p)))
예제 #9
0
def p_id(p):
    """
    id : ID
    """
    p[0] = ast.Id(p[1], ast.Position(*get_pos(p)))
예제 #10
0
def p_arg(p):
    """
    arg : type id
    """
    p[0] = ast.ArgDecl(p[1], p[2], ast.Position(*get_pos(p)))
예제 #11
0
def p_stm_ret(p):
    """
    stm_ret : RETURN exp SEMICOLON
    """
    p[0] = ast.ReturnStatement(p[2], ast.Position(*get_pos(p)))
예제 #12
0
def p_var(p):
    """
    var : type id SEMICOLON
    """
    p[0] = ast.VarDecl(p[1], p[2], ast.Position(*get_pos(p)))