def p_identifier(p): '''identifier : ID''' symbol_table = main_table.get_table(main_table.inScope-1) if(dec==1): ret = symbol_table.check_existing(p[1]) if ret ==-1: print("Redeclaration of variable:",p[1]) sys.exit() elif(p[-1] is None): p[0] = AST.Identifier(p[1],idtype = p[-2].type) symbol_table.add_type(p[-2].type,p[0]) else: p[0] = AST.Identifier(p[1],idtype = p[-2].type+p[-1]['type']) symbol_table.add_type(p[-2].type+p[-1]['type'],p[0]) else: #return sym table entry symbol_table.check_existing(p[1]) node = get_node(symbol_table,p[1]) if node != -1: p[0] = node else: print("Variable undeclared or outOfScope!") sys.exit() if(threeAC.switch_cond==1): threeAC.AddToTable(p[1],'',"id") threeAC.switch_cond=0
def p_directDec(p): '''directDec : identifier | identifier arrayDec''' #| LEFTCURLYBRACKET declarator RIGHTCURLYBRACKET #| directDec LEFTCURLYBRACKET idList RIGHTCURLYBRACKET #| directDec LPAREN parTypeList RPAREN if(len(p)==3): symbol_table = main_table.get_table(main_table.inScope-1) p[1].changeToArray(p[2]['val']) symbol_table.change_array(p[2]['val']) p[0]=p[1]
def get_node(symtab,val): while(True): indicies = [index for index, value in enumerate(symtab.variables) if value == val] if len(indicies)>0: for x in indicies: if symtab.symtab[indicies[0]][0][1]!=None: return symtab.symtab[indicies[0]][1] if symtab.outScope > 0: symtab = main_table.get_table(symtab.outScope-1) else: return -1