1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
| int Semantic_Analysis(struct node* T,int type,int level,char flag,int command) { int type1,type2; if(T){ switch(T->kind){ case EXT_DEF_LIST: Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case EXT_VAR_DEF: type=Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case ARRAY_DEF: type = Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case ARRAY_DEC: flag = 'A'; strcpy(new_table.symbols[new_table.index].name,T->type_id); new_table.symbols[new_table.index].level=level; new_table.symbols[new_table.index].type=type; new_table.symbols[new_table.index].flag=flag; new_table.index++; break; case TYPE: return T->type; case EXT_DEC_LIST: flag='V'; Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case ID: i=0; while(new_table.symbols[i].level!=level&&i<new_table.index) i++; if(command==0){ while(i<new_table.index){ if(strcmp(new_table.symbols[i].name,T->type_id)==0 && new_table.symbols[i].flag==flag){ if(flag=='V') printf("ERROR!第%d行:全局变量中出现相同变量名%s\n",T->pos,T->type_id); else if(flag=='F') printf("ERROR!第%d行:函数定义中出现了相同的函数名%s\n",T->pos,T->type_id); else if(flag=='T') printf("ERROR!第%d行:局部变量中出现了相同的变量名%s\n",T->pos,T->type_id); else printf("ERROR!第%d行:函数参数中中出现了相同的变量名%s\n",T->pos,T->type_id); return 0; } i++; } strcpy(new_table.symbols[new_table.index].name,T->type_id); new_table.symbols[new_table.index].level=level; new_table.symbols[new_table.index].type=type; new_table.symbols[new_table.index].flag=flag; new_table.index++; return new_table.symbols[i].type; } else{ i=new_table.index-1; while(i>=0){ if(strcmp(new_table.symbols[i].name,T->type_id)==0&&(new_table.symbols[i].flag=='V'||new_table.symbols[i].flag=='T'||new_table.symbols[i].flag=='P')){ return new_table.symbols[i].type; } i--; } if(i<0){ printf("ERROR!第%d行:变量名%s未定义\n",T->pos,T->type_id); } } break; case FUNC_DEF: type=Semantic_Analysis(T->ptr[0],type,level+1,flag,command); Semantic_Analysis(T->ptr[1],type,1,flag,command); Semantic_Analysis(T->ptr[2],type,1,flag,command); break; case FUNC_DEC: strcpy(new_table.symbols[new_table.index].name,T->type_id); new_table.symbols[new_table.index].level=0; new_table.symbols[new_table.index].type=type; new_table.symbols[new_table.index].flag='F'; new_table.index++; counter=0; Semantic_Analysis(T->ptr[0],type,level,flag,command); new_table.symbols[new_table.index - counter - 1].paramnum=counter; break; case PARAM_LIST: counter++; Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case PARAM_DEC: flag='P'; type=Semantic_Analysis(T->ptr[0],type,level+1,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case COMP_STM: flag='T'; command=0; new_scope.TX[new_scope.top]=new_table.index; new_scope.top++; Semantic_Analysis(T->ptr[0],type,level,flag,command); command=1; Semantic_Analysis(T->ptr[1],type,level+1,flag,command); DisplaySymbolTable(); new_table.index=new_scope.TX[new_scope.top-1]; new_scope.top--; if (new_scope.top == 0) DisplaySymbolTable(); break; case DEF_LIST: Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case VAR_DEF: type=Semantic_Analysis(T->ptr[0],type,level+1,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case DEC_LIST: Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case STM_LIST: Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case EXP_STMT: Semantic_Analysis(T->ptr[0],type,level,flag,command); break; case RETURN: Semantic_Analysis(T->ptr[0],type,level,flag,command); break; case IF_THEN: case WHILE: case FOR: Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); break; case IF_THEN_ELSE: Semantic_Analysis(T->ptr[0],type,level,flag,command); Semantic_Analysis(T->ptr[1],type,level,flag,command); Semantic_Analysis(T->ptr[2],type,level,flag,command); break; case ASSIGNOP: case OR: case AND: case RELOP: case PLUS: case MINUS: case STAR: case DIV: case COMADD: case COMSUB: type1=Semantic_Analysis(T->ptr[0],type,level,flag,command); type2=Semantic_Analysis(T->ptr[1],type,level,flag,command); if(type1==type2) return type1; else printf("ERROR!第%d行:赋值类型不匹配\n",T->pos); break; case AUTOADD_L: case AUTOSUB_L: case AUTOADD_R: case AUTOSUB_R: Semantic_Analysis(T->ptr[0],type,level,flag,command); break; case INT: return INT; case FLOAT: return FLOAT; case CHAR: return CHAR; case FUNC_CALL: j=0; while(new_table.symbols[j].level==0&&j<new_table.index){ if(strcmp(new_table.symbols[j].name,T->type_id)==0){ if(new_table.symbols[j].flag!='F') printf("ERROR!第%d行:函数名%s在符号表中定义为变量\n",T->pos,T->type_id); break; } j++; } if(new_table.symbols[j].level==1||j==new_table.index){ printf("ERROR!第%d行:函数%s未定义\n",T->pos,T->type_id); break; } type=new_table.symbols[j+1].type; counter=0; Semantic_Analysis(T->ptr[0],type,level,flag,command); if(new_table.symbols[j].paramnum!=counter) printf("ERROR!第%d行:函数调用%s参数个数不匹配\n",T->pos,T->type_id); return new_table.symbols[j].type; break; case ARGS: counter++; t=Semantic_Analysis(T->ptr[0],type,level,flag,command); if(type!=t) printf("ERROR!第%d行:函数调用的第%d个参数类型不匹配\n",T->pos,counter); type=new_table.symbols[j+counter+1].type; Semantic_Analysis(T->ptr[1],type,level,flag,command); break; } } return 0; }
|