def p_unary_exp(p): '''unary_expr : "+" "+" is_array_term %prec PRE | "-" "-" is_array_term %prec PRE | is_array_term "+" "+" %prec POST | is_array_term "-" "-" %prec POST ''' r_leaf = leaf("1", "INT") global sym_table new_branch = branch() if p[1] == '+': l_leaf = p[3] new_branch.setType("ADD") sym_table.appendGrammar(12, 'unary_expr -> ++ is_array_term') elif p[1] == '-': l_leaf = p[3] new_branch.setType("SUB") sym_table.appendGrammar(12, 'unary_expr -> -- is_array_term') # TODO create an special node type that indicates ADD and SUB will occur in the next block elif p[2] == '+': l_leaf = p[1] new_branch.setType("ADD") sym_table.appendGrammar(12, 'unary_expr -> is_array_term ++') elif p[2] == '-': l_leaf = p[1] new_branch.setType("SUB") sym_table.appendGrammar(12, 'unary_expr -> is_array_term --') new_branch.add(l_leaf) new_branch.add(r_leaf) new_branch2 = branch() new_branch2.add(l_leaf) new_branch2.add(new_branch) new_branch2.setType("ASSIGN") p[0] = new_branch2
def p_is_array_term(p): '''is_array_term : is_array_term '[' expression ']' | is_array_term '.' ID | ID ''' if len(p) > 4: if type(p[1]) == leaf: new_branch = branch() new_branch.setType("ACCESS") new_branch.add(p[1]) new_branch.add(p[3]) p[0] = new_branch else: new_branch = p[1] new_branch.add(p[3]) p[0] = new_branch elif len(p) > 2: if type(p[1]) == leaf: new_branch = branch() new_branch.setType("ACCESS") new_branch.add(p[1]) new_branch.add(leaf(p[3], "STRING")) p[0] = new_branch else: new_branch = p[1] new_branch.add(leaf(p[3], "STRING")) p[0] = new_branch else: p[0] = leaf(p[1], "ID")
def p_code(p): '''code : code LABEL ":" list | code LABEL ":" | MAIN ":" | MAIN ":" list ''' global __ast global sym_table if len(p) == 4: if p[3] != ':': p[3].setType("LABEL") p[3].setValue("MAIN") __ast.add(p[3]) sym_table.appendGrammar(1, 'code -> MAIN : list') else: new_branch = branch() new_branch.setType("LABEL") new_branch.setValue(str(p[2])) __ast.add(new_branch) sym_table.appendGrammar(2.1, 'code -> code LABEL :') elif len(p) == 3: new_branch = branch() new_branch.setType("LABEL") new_branch.setValue("MAIN") __ast.add(new_branch) sym_table.appendGrammar(1.1, 'code -> MAIN :') else: p[4].setType("LABEL") p[4].setValue(str(p[2])) __ast.add(p[4]) sym_table.appendGrammar(2, 'code -> code LABEL : list') p[0] = __ast
def p_iteration_statement(p): '''iteration_statement : WHILE "(" expression ")" "{" compound_statement | DO "{" compound_statement WHILE "(" expression ")" ";" | FOR "(" declaration ";" expression ";" unary_expr ")" "{" compound_statement | FOR "(" sub_decl ";" expression ";" unary_expr ")" "{" compound_statement ''' new_branch = branch() if p[1] == "while": new_branch.setType("WHILE") new_branch.add(p[3]) if p[6] != None: new_branch.add(p[6]) elif p[1] == "do": new_branch.setType("WHILE") new_branch.add(p[6]) if p[3] != None: new_branch.add(p[3]) else: new_branch.setType("FOR") new_branch.add(p[3]) new_branch.add(p[5]) new_branch.add(p[7]) if p[10] != None: new_branch.add(p[10]) p[0] = new_branch
def p_statement_group(p): '''statement : IF '(' expression ')' GOTO LABEL | UNSET '(' term ')' | GOTO LABEL | EXIT ''' global sym_table new_branch = branch() if len(p) > 5: new_branch.add(p[3]) r_leaf = leaf(p[6], "LABEL") new_branch.add(r_leaf) new_branch.setType("IF") sym_table.appendGrammar( 7, 'statement -> IF ( expression ) GOTO LABEL ') elif len(p) > 3: new_branch.add(p[3]) new_branch.setType("UNSET") sym_table.appendGrammar(8, 'statement -> UNSET ( term ) ') elif len(p) > 2: r_leaf = leaf(p[2], "LABEL") new_branch.add(r_leaf) new_branch.setType("GOTO") sym_table.appendGrammar(9, 'statement -> GOTO LABEL ') else: new_branch.setType("EXIT") sym_table.appendGrammar(10, 'statement -> EXIT ') p[0] = new_branch
def p_assigment(p): 'assigment : is_array_term assign_op expression %prec ASSIGN' new_branch = branch() new_branch.add(p[1]) if p[2] != '=': assign_branch = branch() assign_branch.add(p[1]) assign_branch.add(p[3]) assign_branch.setType(p[2]) new_branch.add(assign_branch) else: new_branch.setType("ASSIGN") new_branch.add(p[3]) p[0] = new_branch
def p_struct(p): 'struct : STRUCT ID "{" assigment_list "}" ";" ' new_branch = branch() new_branch.setType("STRUCT") new_branch.add(leaf(p[2], "ID")) new_branch.add(p[4]) p[0] = new_branch
def input(self, text): global __ast global __text global sym_table __ast = branch() __text = text sym_table = table() result = parser.parse(text, lexer=lexer) return result
def p_term_group(p): '''expression : '(' INT ')' factor | '(' FLOAT ')' factor | '(' CHAR ')' factor | '(' factor ')' | '~' factor | '!' factor | '&' VAR | ABS '(' factor ')' | ARRAY '(' ')' | READ '(' ')' ''' global sym_table new_branch = branch() if p[1] == '(': l_leaf = p[4] new_branch.add(l_leaf) if p[2] == 'int': new_branch.setType("TOINT") sym_table.appendGrammar(32, 'expression -> ( INT ) factor') elif p[2] == 'float': new_branch.setType("TOFLOAT") sym_table.appendGrammar(33, 'expression -> ( FLOAT ) factor') elif p[2] == 'char': new_branch.setType("TOCHAR") sym_table.appendGrammar(34, 'expression -> ( CHAR ) factor') else: new_branch = p[2] sym_table.appendGrammar(35, 'expression -> ( factor )') elif p[1] == '~': l_leaf = p[2] new_branch.add(l_leaf) new_branch.setType("BNOT") sym_table.appendGrammar(36, 'expression -> ~ factor') elif p[1] == '!': l_leaf = p[2] new_branch.add(l_leaf) new_branch.setType("NOT") sym_table.appendGrammar(37, 'expression -> ! factor') elif p[1] == '&': l_leaf = leaf(p[2], "ID") new_branch.add(l_leaf) new_branch.setType("POINT") sym_table.appendGrammar(38, 'expression -> & factor') elif p[1] == 'abs': l_leaf = p[3] new_branch.add(l_leaf) new_branch.setType("ABS") sym_table.appendGrammar(39, 'expression -> ABS ( factor )') elif p[1] == 'array': new_branch.setType("ARRAY") sym_table.appendGrammar(40, 'expression -> ARRAY ( )') elif p[1] == 'read': new_branch.setType("READ") sym_table.appendGrammar(41, 'expression -> READ ( )') p[0] = new_branch
def p_term_group(p): '''expression : '(' INT ')' expression %prec CAST | '(' FLOAT ')' expression %prec CAST | '(' CHAR ')' expression %prec CAST | '(' ID ')' expression %prec CAST | '(' expression ')' | '~' expression | '!' expression %prec NOT_PRE | '&' ID %prec POINT | expression '?' expression ':' expression | function_call ''' global sym_table new_branch = branch() if p[1] == '(': if len(p) > 4: l_leaf = p[4] new_branch.add(l_leaf) if p[2] == 'int': new_branch.setType("TOINT") sym_table.appendGrammar( 32, 'expression -> ( INT ) expression') elif p[2] == 'float': new_branch.setType("TOFLOAT") sym_table.appendGrammar( 33, 'expression -> ( FLOAT ) expression') elif p[2] == 'char': new_branch.setType("TOCHAR") sym_table.appendGrammar( 34, 'expression -> ( CHAR ) expression') else: new_branch.setType("TOID") sym_table.appendGrammar(34, 'expression -> ( ID ) expression') else: new_branch = p[2] sym_table.appendGrammar(35, 'expression -> ( expression )') elif p[1] == '~': l_leaf = p[2] new_branch.add(l_leaf) new_branch.setType("BNOT") sym_table.appendGrammar(36, 'expression -> ~ expression') elif p[1] == '!': l_leaf = p[2] new_branch.add(l_leaf) new_branch.setType("NOT") sym_table.appendGrammar(37, 'expression -> ! expression') elif p[1] == '&': l_leaf = leaf(p[2], "ID") new_branch.add(l_leaf) new_branch.setType("POINT") sym_table.appendGrammar(38, 'expression -> & ID') else: new_branch = p[1] sym_table.appendGrammar(38.1, 'expression -> function_call') p[0] = new_branch
def p_selection_if(p): 'selection_if : IF "(" expression ")" "{" compound_statement' new_branch = branch() new_branch.setType("IF") new_branch.add(p[3]) new_branch.add(p[6]) p[0] = new_branch
def p_function_call(p): 'function_call : ID "(" parentheses_expression' new_branch = branch() new_branch.setType("CALL") new_branch.add(leaf(p[1], "ID")) if p[3] != None: new_branch.add(p[3]) p[0] = new_branch
def p_statement_expr(p): 'statement : PRINT "(" term ")" ' global sym_table leaf = p[3] new_branch = branch() new_branch.add(leaf) new_branch.setType("PRINT") p[0] = new_branch sym_table.appendGrammar(6, 'statement -> PRINT ( term ) ')
def p_statement_list(p): '''list : list statement ";" | statement ";" ''' global sym_table if len(p) == 3: new_branch = branch() new_branch.add(p[1]) p[0] = new_branch sym_table.appendGrammar(3, 'list -> statement ;') else: if p[1] != None: p[1].add(p[2]) p[0] = p[1] sym_table.appendGrammar(4, 'list -> list statement ;') else: new_branch = branch() new_branch.add(p[2]) p[0] = new_branch sym_table.appendGrammar(3, 'list -> statement ;')
def p_expression_binop(p): '''expression : term '+' term | term '-' term | term '*' term | term '/' term | term '%' term | term '&' term | term '|' term | term '^' term | term '<' term | term '>' term | term XOR term ''' l_leaf = p[1] r_leaf = p[3] global sym_table new_branch = branch() new_branch.add(l_leaf) new_branch.add(r_leaf) if p[2] == '+': new_branch.setType("ADD") sym_table.appendGrammar(11, 'expression -> term + term') elif p[2] == '-': new_branch.setType("SUB") sym_table.appendGrammar(12, 'expression -> term - term') elif p[2] == '*': new_branch.setType("MUL") sym_table.appendGrammar(13, 'expression -> term * term') elif p[2] == '/': new_branch.setType("DIV") sym_table.appendGrammar(14, 'expression -> term / term') elif p[2] == '%': new_branch.setType("MOD") sym_table.appendGrammar(15, 'expression -> term % term') elif p[2] == '&': new_branch.setType("BAND") sym_table.appendGrammar(16, 'expression -> term & term') elif p[2] == '|': new_branch.setType("BOR") sym_table.appendGrammar(17, 'expression -> term | term') elif p[2] == '^': new_branch.setType("BXOR") sym_table.appendGrammar(18, 'expression -> term ^ term') elif p[2] == '<': new_branch.setType("LTHAN") sym_table.appendGrammar(19, 'expression -> term < term') elif p[2] == '>': new_branch.setType("GTHAN") sym_table.appendGrammar(20, 'expression -> term > term') else: new_branch.setType("XOR") sym_table.appendGrammar(21, 'expression -> term XOR term') p[0] = new_branch
def p_expression_uminus(p): "expression : '-' expression %prec UMINUS" l_leaf = p[2] r_leaf = leaf(-1, "INT") global sym_table new_branch = branch() new_branch.add(l_leaf) new_branch.add(r_leaf) new_branch.setType("MUL") sym_table.appendGrammar(31, 'term -> - expression') p[0] = new_branch
def p_jump_stament(p): '''jump_statement : CONTINUE | BREAK | RETURN expression | RETURN ''' new_branch = branch() if len(p) > 2: new_branch.add(p[2]) new_branch.setType(p[1].upper()) p[0] = new_branch
def p_term_uminus(p): "term : '-' factor %prec UMINUS" l_leaf = p[2] r_leaf = leaf(-1, "NUM") global sym_table new_branch = branch() new_branch.add(l_leaf) new_branch.add(r_leaf) new_branch.setType("MUL") sym_table.appendGrammar(31, 'term -> - factor') p[0] = new_branch
def p_compound_stament(p): '''compound_statement : statement_list "}" | "}" ''' new_branch = None if len(p) > 2: new_branch = p[1] else: new_branch = branch() new_branch.setType("SCOPE") new_branch.setValue("S") p[0] = new_branch
def p_statement_assign(p): 'statement : is_array_term "=" expression' global sym_table l_leaf = p[1] r_leaf = p[3] new_branch = branch() new_branch.add(l_leaf) new_branch.add(r_leaf) new_branch.setType("ASSIGN") p[0] = new_branch sym_table.appendGrammar(5, 'statement -> is_array_term = expression')
def p_declaration_statement(p): '''declaration : declaration "," sub_decl | type sub_decl ''' if len(p) == 3: new_branch = branch() new_branch.add(leaf(p[1], p[1])) new_branch.add(p[2]) new_branch.setType("DECLARE") p[0] = new_branch else: new_branch = p[1] new_branch.add(p[3]) p[0] = new_branch
def p_expression_list(p): '''expression_list : expression_list "," expression | expression ''' if len(p) == 2: new_branch = branch() new_branch.add(p[1]) new_branch.setType("SCOPE") new_branch.setValue("ARGS") p[0] = new_branch else: new_branch = p[1] new_branch.add(p[3]) p[0] = new_branch
def p_selection_stament(p): '''selection_statement : labeled_statement DEFAULT ':' statement_list "}" | selection_if_has_more ELSE "{" compound_statement | selection_if_has_more ''' if len(p) > 5: pass elif len(p) > 2: else_branch = branch() else_branch.setType("ELSE") else_branch.add(p[4]) p[1].add(else_branch) p[0] = p[1]
def p_expression_binop2(p): '''expression : expression '&' '&' expression %prec AND | expression '|' '|' expression %prec OR | expression '<' '<' expression | expression '>' '>' expression | expression '!' '=' expression | expression '=' '=' expression | expression '<' '=' expression %prec LESSEQ | expression '>' '=' expression %prec GREATEREQ ''' l_leaf = p[1] r_leaf = p[4] global sym_table new_branch = branch() new_branch.add(l_leaf) new_branch.add(r_leaf) if p[2] == '&': new_branch.setType("AND") sym_table.appendGrammar(22, 'expression -> expression && expression') elif p[2] == '|': new_branch.setType("OR") sym_table.appendGrammar(23, 'expression -> expression || expression') elif p[2] == '<' and p[3] == '<': new_branch.setType("SLEFT") sym_table.appendGrammar(24, 'expression -> expression << expression') elif p[2] == '>' and p[3] == '>': new_branch.setType("SRIGHT") sym_table.appendGrammar(25, 'expression -> expression >> expression') elif p[2] == '!': new_branch.setType("NOEQUAL") sym_table.appendGrammar(26, 'expression -> expression != expression') elif p[2] == '=': new_branch.setType("EQUAL") sym_table.appendGrammar(27, 'expression -> expression == expression') elif p[2] == '<' and p[3] == '=': new_branch.setType("LE_OP") sym_table.appendGrammar(28, 'expression -> expression <= expression') elif p[2] == '>' and p[3] == '=': new_branch.setType("GE_OP") sym_table.appendGrammar(29, 'expression -> expression >= expression') p[0] = new_branch
def p_code(p): '''code : code block | block ''' global sym_table if len(p) > 2: p[1].add(p[2]) p[0] = p[1] else: newbranch = branch() newbranch.add(p[1]) newbranch.setType("SCOPE") newbranch.setValue("S") p[0] = newbranch
def p_sub_decl(p): '''sub_decl : ID "=" expression | ID "=" READ "(" ")" | decl_array "=" "{" expression_list "}" | decl_index "=" "{" expression_list "}" | decl_index "=" expression | decl_index "=" READ "(" ")" | decl_index | decl_array | ID ''' global sym_table new_branch = branch() if len(p) == 4: if type(p[1]) == branch: #TODO assign to index of array pass else: l_leaf = leaf(p[1], "ID") r_leaf = p[3] new_branch.add(l_leaf) new_branch.add(r_leaf) new_branch.setType("ASSIGN") sym_table.appendGrammar( 5, 'statement -> is_array_term = expression') elif len(p) > 3: if p[4] == '(': l_leaf = leaf(p[1], "ID") r_leaf = leaf("read()", "READ") new_branch.add(l_leaf) new_branch.add(r_leaf) new_branch.setType("ASSIGN") else: l_leaf = p[1] r_leaf = p[4] r_leaf.setType("VARRAY") new_branch.add(l_leaf) new_branch.add(r_leaf) new_branch.setType("ASSIGN") else: if type(p[1]) == branch: #TODO assign to index of array pass else: new_branch = leaf(p[1], "ID") p[0] = new_branch
def p_arg(p): ''' arg : type ID | type "&" ID ''' new_branch = branch() new_branch.add(leaf(p[1], p[1])) if len(p) == 3: new_branch.add(leaf(p[2], "ID")) else: new_branch.add(leaf(p[3], "ID")) new_branch.add(leaf("&", "POINT")) new_branch.setType("DECLARE") p[0] = new_branch
def p_assigment_list(p): ''' assigment_list : assigment_list declaration ";" | declaration ";" ''' new_branch = None if len(p) > 3: new_branch = p[1] new_branch.add(p[2]) else: new_branch = branch() new_branch.setType("SCOPE") new_branch.setValue("ATTRIBS") new_branch.add(p[1]) p[0] = new_branch
def p_statement_list(p): ''' statement_list : statement_list statement | statement ''' new_branch = None if len(p) > 2: new_branch = p[1] new_branch.add(p[2]) else: new_branch = branch() new_branch.setType("SCOPE") new_branch.setValue("S") new_branch.add(p[1]) p[0] = new_branch
def p_arguments(p): '''arguments : arguments "," arg | arg ''' new_branch = None if len(p) > 2: new_branch = p[1] new_branch.add(p[3]) else: new_branch = branch() new_branch.setType("SCOPE") new_branch.setValue("ARGUMENTS") new_branch.add(p[1]) p[0] = new_branch