def p_struct_declarator_list(p): ''' struct_declarator_list : declarator | struct_declarator_list COMMA declarator ''' success = None if len(p) == 2: if p[1].type != "error": success = sym_table.add_entry(name=p[1].value, type=p[1].type, token_object=p[1].data['token']) if success: p[0] = {p[1].value: p[1].type} else: p[0] = dict({}) else: if p[3].type != "error": success = sym_table.add_entry(name=p[3].value, type=p[3].type, token_object=p[3].data['token']) if success: p[0] = p[1] p[0].update({p[3].value: p[3].type}) else: p[0] = p[1]
def p_init_declarator(p): ''' init_declarator : declarator | declarator ASSIGNMENT initializer ''' if p[1].type == "error": p[0] = [None] return if len(p) == 2: success = sym_table.add_entry(name=p[1].value, type=p[1].type, token_object=p[1].data['token']) p[0] = [None] else: # type checking # check for initliazer init = type_check_init(p[3], p[1].type, p.slice[2]) if init.type == "error": p[0] = [None] return success = sym_table.add_entry(name=p[1].value, type=p[1].type, token_object=p[1].data['token']) if success: p[0] = [ Node(name="initialization", children=[p[1], init], type="ok") ] else: p[0] = [None]
def p_function_definition_1(p): ''' function_definition : type_specifier declarator func_scope parameter_type_list R_PAREN SEMI_COLON pop_sym | type_specifier declarator func_scope R_PAREN SEMI_COLON pop_sym ''' p[0] = [] #make unused sym = True # if len(p) == 8: p[7].unused = True # else: p[6].unused = True success = sym_table.look_up(name=p[2].value, token_object=p[2].data['token'], no_error=True) if success != None: Errors(errorType='DeclarationError', errorText='Function is already declared/defined', token_object=p[2].data['token']) else: if len(p) == 8: sym_table.add_entry(name=p[2].value, type=FunctionType(return_type=p[2].type, param_list=p[4], defined=False, symbol_table=p[7])) else: sym_table.add_entry(name=p[2].value, type=FunctionType(return_type=p[2].type, param_list=[], defined=False, symbol_table=p[6]))
def get_newtmp(type=BasicType("long")): assert isinstance(type, Type), "inconsistent type for newtmp" global temp_cnt name = "tmp@" + str(temp_cnt) temp_cnt += 1 sym_table.add_entry(name=name, type=type) temp_dict[name] = sym_table.curr_symbol_table return name
def p_func_scope(p): ''' func_scope : L_PAREN ''' decl = p.stack[-1].value #getting function name and return type if decl.type.class_type == "PointerType" and len( decl.type.array_size) != 0: Errors(errorType='DeclarationError', errorText='Function cannot have array return type', token_object=p.slice[-1]) sym_table.start_scope(name=decl.value) #starting scope of function sym_table.add_entry(name='return', type=decl.type) #creating entry to check return type p[0] = (decl, FunctionType(return_type=decl.type, symbol_table=sym_table.curr_symbol_table) ) #type of function
def p_parameter_declaration(p): ''' parameter_declaration : type_specifier declarator ''' success = sym_table.add_entry(name=p[2].value, type=p[2].type, token_object=p[2].data['token']) if success: p[0] = [p[2].type] else: p[0] = []