def p_extdef_funcDec(p): 'ExtDef : Specifier FunDec SEMI' specifier = SemAnalysis.procSpecifier(p[1]) funcdec = SemAnalysis.procFunDec(p[2]) if SemAnalysis.checkFuncRedec(specifier, funcdec): print('Error type 19 at Line %d: Inconsistent declaration of function \"%s\"' %(p[1].lineno, funcdec[0])) else: SemAnalysis.addInSymbolTable(['func-dec', specifier, funcdec, p[1].lineno])
def p_extdef_1(p): # structure 'ExtDef : Specifier SEMI' p[0] = Node('ExtDef', p, [p[1], 'SEMI'], p[0]) struct_info = SemAnalysis.procSpecifier(p[1]) # check if there have duplicated struct names for item in SemAnalysis.getSymbolTable(): if item[0] == 'global struct' and item[1][0] == struct_info[0]: print('Error type 16 at Line %d: Duplicated name \"%s\"' %(p[1].lineno, struct_info[0].split(' ')[1])) if item[0] == 'global struct': # nested struct can also cause duplicated name !! for var_definiton in item[1][1]: if type(var_definiton[0]) == tuple and var_definiton[0][0] == struct_info[0]: print('Error type 16 at Line %d: Duplicated name \"%s\"' %(p[1].lineno, struct_info[0].split(' ')[1])) SemAnalysis.addInSymbolTable(['global struct', struct_info])
def p_extdef_2(p): # function 'ExtDef : Specifier FunDec CompSt' p[0] = Node('ExtDef', p, [p[1], p[2], p[3]], p[0]) fundec = SemAnalysis.procFunDec(p[2]) # [func_name, var_list] func_name = fundec[0] func_var_list = fundec[1] return_type = SemAnalysis.procSpecifier(p[1], 'func') # check if there has redefined function if func_name in SemAnalysis.getFuncName(): print('Error type 4 at Line %d: Redefined function \"%s\"' %(p[1].lineno, func_name)) compst = SemAnalysis.procCompSt(p[3], func_var_list, func_name, return_type) SemAnalysis.addInSymbolTable(['func', return_type, SemAnalysis.procFunDec(p[2]), compst])
def p_extdef_3( p): # global variable, it may also has a definition of a structure 'ExtDef : Specifier ExtDecList SEMI' p[0] = Node('ExtDef', p, [p[1], p[2], 'SEMI'], p[0]) Specifier = SemAnalysis.procSpecifier(p[1]) extdeclist = SemAnalysis.procExtDecList(p[2], []) structType = None # use this variable to allow add one struct type in a declist for item in extdeclist: specifier = Specifier if '[' in item: # it's an array, need to reshape it item = item.replace(']', '').split('[') if type(specifier) == str: # basic type for dim in item[1:]: specifier += '-' + dim item = item[0] SemAnalysis.addInSymbolTable( ['global variable', specifier, item]) elif 'struct' in specifier[0]: # struct type if specifier[ 0] != 'struct UNknownType' and structType == None: # it's a definition of a struct as well SemAnalysis.addInSymbolTable(['global struct', specifier]) structType = specifier[0] var_list = specifier[1] specifier = specifier[0] for dim in item[1:]: specifier += '-' + dim item = item[0] SemAnalysis.addInSymbolTable( ['global variable', (specifier, var_list), item]) else: # add it to SymbolTable directly if type(specifier) == tuple and specifier[ 0] != 'struct UNknownType': # it also has a definition of a structure SemAnalysis.addInSymbolTable(['global struct', specifier]) SemAnalysis.addInSymbolTable(['global variable', specifier, item])