argElement *doArgs(TreeNode *p) { //printf("doArgs\n"); //printf("TreeNode->state:%s\n", p->state); switch (p->productionRule) { case 1: { TreeNode *p1 = p->firstChild; TreeNode *p3 = p1->rightBrother->rightBrother; argElement *arg1 = (argElement *)malloc(sizeof(argElement)); arg1->type = doExp(p1); argElement *arg2 = doArgs(p3); arg1->next = arg2; return arg1; break; } case 2: { TreeNode *p1 = p->firstChild; argElement *arg1 = (argElement *)malloc(sizeof(argElement)); arg1->type = doExp(p1); arg1->next = NULL; //if (arg1 == NULL) printf("wow...\n"); return arg1; break; } } }
varElement* doDec(TreeNode *p) { //printf("doDec\n"); //printf("TreeNode->production rule: %d\n", p->productionRule); switch (p->productionRule) { case 1:{ TreeNode *p1 = p->firstChild; //printf("before doDec1\n"); varElement *elem1 = doVarDec(p1); //printf("after doDec1\n"); return elem1; break; } case 2: { TreeNode *p1 = p->firstChild; TreeNode *p3 = p1->rightBrother->rightBrother; //printf("p1->state:%s\n", p1->state); //printf("p3->state:%s\n", p3->state); //printf("before doDec\n"); varElement *elem1 = doVarDec(p1);//产生一个结点 //判断初始化时类型是否匹配 Type t3 = doExp(p3); elem1->initType = t3; //将初始化时等号后面的变量类型赋给这个结点的initType; fprintf(out_fp, "\tlw, $t0, 0($sp)\n"); fprintf(out_fp, "\tsw, $t0, %d($sp)\n", elem1->addr_sp - ADDR); return elem1; break; } } return NULL; }
varElement* doDec(TreeNode *p) { //printf("doDec\n"); //printf("TreeNode->state:%s\n", p->state); switch (p->productionRule) { case 1:{ TreeNode *p1 = p->firstChild; varElement *elem1 = doVarDec(p1); return elem1; break; } case 2: {//先没有处理初始化 TreeNode *p1 = p->firstChild; TreeNode *p3 = p1->rightBrother->rightBrother; //printf("p1->state:%s\n", p1->state); //printf("p3->state:%s\n", p3->state); varElement *elem1 = doVarDec(p1);//产生一个结点 //判断初始化时类型是否匹配 Type t3 = doExp(p3); elem1->type = t3; //将初始化时等号后面的变量类型赋给这个新结点 return elem1; break; } } return NULL; }
void doStmt(TreeNode *p) { //printf("doStmt\n"); //printf("TreeNode->state:%s\n", p->state); //printf("TreeNode->productionRule: %d\n", p->productionRule); switch (p->productionRule) { case 1:{ TreeNode *p1 = p->firstChild; doExp(p1); break; } case 2:{ TreeNode *p1 = p->firstChild; doCompSt(p1); break; } case 3: {//这块之后要做函数返回类型检查 TreeNode *p2 = p->firstChild->rightBrother; doExp(p2); break; } case 4: { //这块可以判断一下p3代表的exp是不是int。不过实验讲义假设一定是 //后两个同理 TreeNode *p3 = p->firstChild->rightBrother->rightBrother; TreeNode *p5 = p3->rightBrother->rightBrother; doExp(p3); doStmt(p5); break; } case 5:{ TreeNode *p3 = p->firstChild->rightBrother->rightBrother; TreeNode *p5 = p3->rightBrother->rightBrother; TreeNode *p7 = p5->rightBrother->rightBrother; doExp(p3); doStmt(p5); doStmt(p7); break; } case 6:{ TreeNode *p3 = p->firstChild->rightBrother->rightBrother; TreeNode *p5 = p3->rightBrother->rightBrother; doExp(p3); doStmt(p5); break; } } }
Type doOptExp(TreeNode *p) { //printf("doOptExp\n"); switch(p->productionRule) { case 1: { TreeNode *p1 = p->firstChild; return(doExp(p1)); } case 2: { break; return NULL; //还不确定返回NULL正不正确 } } }
void Canonize(vector<INode*>& trees, vector<IStm*>& canonized_trees){ canonized_trees.clear(); CCanonizer canonizer; for ( int i = 0; i < trees.size(); ++i ) { trees[i]->accept(&canonizer); INode* root = canonizer.current_node; IExp* exp = dynamic_cast<IExp*>(root); IStm* stm = dynamic_cast<IStm*>(root); IStm* result = 0; if (exp != 0) { IExp* res = doExp(exp); ESEQ* eseq = dynamic_cast<ESEQ*>(res); result = dynamic_cast<SEQ*>(eseq->stm); } if (stm !=0 ) { result = doStm(stm); } if (result != 0) { canonized_trees.push_back(result); } } }
Type doExp(TreeNode *p) { // printf("doExp\n"); // printf("TreeNode->state:%s\n", p->state); // printf("TreeNode->productionRule: %d\n", p->productionRule); if(strcmp(p->state, "Exp") == 0) { TreeNode *tempNode = p->firstChild;//产生式右边第一个符号 switch (p->productionRule) { case 1:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; //Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if (tempNode->productionRule > 16 || tempNode->productionRule < 14) { //不符合唯一的三个能当右值的产生式 printf("Error type 6 at line %d: left expression illegal for assign\n", p->line); } if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { // printf("Exp ASSIGNOP Exp\n"); // printf("tempNode->firstChild->addr_sp: %d\n", tempNode->firstChild->addr_sp); fprintf(out_fp, "\tlw $t0, %d($sp)\n", temp2Node->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tsw $t0, %d($sp)\n", tempNode->firstChild->addr_sp - ADDR); return t1;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d '=' type mismatch\n", p->line);//类型不匹配 OUT = false;//表示语义分析错误,删除已生成的目标文件 } } break; //exp = exp } case 2:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; ADDR -= 4; p->firstChild->addr_sp = ADDR;//将表达式结果存在该地址处 fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tlw $t0, %d($sp)\n", tempNode->firstChild->addr_sp); fprintf(out_fp, "\tlw $t1, %d($sp)\n", temp2Node->firstChild->addr_sp); fprintf(out_fp, "\tand $t0, $t0, $t1\n"); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d '&&' type mismatch\n", p->line);//类型不匹配 OUT = false; } } break; //Exp AND Exp } case 3:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; ADDR -= 4; p->firstChild->addr_sp = ADDR;//将表达式结果存在该地址处 fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tlw $t0, %d($sp)\n", tempNode->firstChild->addr_sp); fprintf(out_fp, "\tlw $t1, %d($sp)\n", temp2Node->firstChild->addr_sp); fprintf(out_fp, "\tor $t0, $t0, $t1\n"); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d '||' type mismatch\n", p->line);//类型不匹配 OUT = false; } } break; //Exp OR Exp } case 4:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d 'RELOP' type mismatch\n", p->line);//类型不匹配 } } break; //Exp RELOP Exp } case 5:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; // printf("t1->u.basic: %d\n", t1->u.basic); ADDR -= 4; p->firstChild->addr_sp = ADDR; fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tlw $t0, %d($sp)\n", tempNode->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tlw $t1, %d($sp)\n", temp2Node->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tadd $t0, $t0, $t1\n"); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d '+' type mismatch\n", p->line);//类型不匹配 OUT = false; } } break; //Exp PLUS Exp } case 6:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; ADDR -= 4; p->firstChild->addr_sp = ADDR; fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tlw $t0, %d($sp)\n", tempNode->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tlw $t1, %d($sp)\n", temp2Node->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tsub $t0, $t0, $t1\n"); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d '-' type mismatch\n", p->line);//类型不匹配 } } break; //Exp MINUS Exp } case 7:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; ADDR -= 4; p->firstChild->addr_sp = ADDR; fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tlw $t0, %d($sp)\n", tempNode->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tlw $t1, %d($sp)\n", temp2Node->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tmul $t0, $t0, $t1\n"); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d '*' type mismatch\n", p->line);//类型不匹配 } } break; //Exp STAR Exp } case 8:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; ADDR -= 4; p->firstChild->addr_sp = ADDR; fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tlw $t0, %d($sp)\n", tempNode->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tlw $t1, %d($sp)\n", temp2Node->firstChild->addr_sp - ADDR); fprintf(out_fp, "\tdiv $t0, $t0, $t1\n"); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 7 at line %d '/' type mismatch\n", p->line);//类型不匹配 } } break; //Exp DIV Exp } case 9:return doExp(tempNode->rightBrother); //LP Exp RP case 10:return doExp(tempNode->rightBrother); //MINUS Exp case 11:return doExp(tempNode->rightBrother); //NOT Exp case 12:{funcTableElement *func = searchFunc(tempNode->value.idValue); if(func == NULL) { varElement *var = searchAll(tempNode->value.idValue); if (var != NULL) { printf("Error type 11 at line %d: normal variable %s uses '()'\n", p->line, tempNode->value.idValue); } else printf("Error type 4 at line %d: undefined function %s\n", p->line, tempNode->value.idValue); return NULL; } TreeNode *argNode = tempNode->rightBrother->rightBrother; argElement *args = doArgs(argNode); if (compareArgs(func->argListHeader, args) == 0) { //参数匹配有问题 printf("Error type 9 at line %d: function var list not matched %s\n", p->line, tempNode->value.idValue); return func->type; } break; //ID LP Args RP } case 13:{funcTableElement *func = searchFunc(tempNode->value.idValue); if(func == NULL) { varElement *var = searchAll(tempNode->value.idValue); if (var != NULL) { printf("Error type 11 at line %d: normal variable %s uses '()'\n", p->line, tempNode->value.idValue); OUT = false; } else { printf("Error type 4 at line %d:undefined function %s\n", p->line, tempNode->value.idValue); OUT = false; } return NULL; } else if (func->argListHeader != NULL) { //函数定义中有参数而调用没有 printf("Error type 9 at line %d:function var list not matched %s\n", p->line, tempNode->value.idValue); OUT = false; return func->type; } break; //ID LP RP } case 14:{Type type = (Type)malloc(sizeof(struct Type_)); TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(tempNode != NULL && temp2Node != NULL) { if(t1->kind != ARRAY) { printf("Error type 10 at line %d: normal variable uses '[]'\n", p->line); OUT = false; } else if(t2->u.basic != 0) { printf("Error type 12 at line %d:int required\n", p->line); OUT = false; } else { type = doExp(tempNode)->u.array.elem; // printf("tempNode->firstChild->addr_sp: %d\n", tempNode->firstChild->addr_sp); tempNode->addr_sp = tempNode->firstChild->addr_sp - temp2Node->firstChild->value.intValue * 4; return type; } } break; //Exp LB Exp RB } case 15:{ TreeNode *p3 = tempNode->rightBrother->rightBrother; Type t = doExp(tempNode); if(t != NULL) { if(t->kind != STRUCTURE) { printf("Error type 13 at line %d: error use of operator '.'\n", p->line); } else { //showStructMember(t); ; varElement *field = t->u.var; while(field != NULL) { if(strcmp(field->name, p3->value.idValue) == 0) { return field->type; } field = field->next; } printf("Error type 14 at line %d:struct member %s undefined\n", p->line, tempNode->rightBrother->rightBrother->value.idValue); //结构体成员未定义 } } break; //Exp DOT ID } case 16:{Type type = (Type)malloc(sizeof(struct Type_)); varElement *id = searchAll(tempNode->value.idValue); //在符号表中寻找是否定义 if(id == NULL) { printf("Error type 1 at line %d:undefined variable %s\n", p->line, tempNode->value.idValue); OUT = false; } else { type = id->type; tempNode->addr_sp = id->addr_sp; printf("tempNode->addr_sp: %d\n", id->addr_sp); return type; } break; //ID } case 17:{Type type = (Type)malloc(sizeof(struct Type_)); type->kind = BASIC; type->u.basic = 0; ADDR -= 4; tempNode->addr_sp = ADDR; fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tli $t0, %d\n", tempNode->value.intValue); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type; //INT } case 18:{Type type = (Type)malloc(sizeof(struct Type_)); type->kind = BASIC; type->u.basic = 1; ADDR -= 4; tempNode->addr_sp = ADDR; fprintf(out_fp, "\taddi $sp, $sp, -4\n"); fprintf(out_fp, "\tli $t0, %f\n", tempNode->value.floatValue); fprintf(out_fp, "\tsw $t0, 0($sp)\n"); return type; //FLOAT } } } return NULL; }
void doStmt(TreeNode *p) { //printf("doStmt\n"); //printf("TreeNode->state:%s\n", p->state); //printf("TreeNode->productionRule: %d\n", p->productionRule); switch (p->productionRule) { case 1:{ TreeNode *p1 = p->firstChild; doExp(p1); break; } case 2:{ TreeNode *p1 = p->firstChild; doCompSt(p1); break; } case 3: { //printf("in return\n"); TreeNode *p2 = p->firstChild->rightBrother; Type type = doExp(p2); if (!type_equal(type, funcTableHeader->type)) { //直接和函数表中第一项比较,一定是最近的函数 printf("Error 8 at line %d: function return unexpected type\n", p->line); } //printf("out return\n"); break; } case 4: { //这块可以判断一下p3代表的exp是不是int。不过实验讲义假设一定是 //后两个同理 TreeNode *p3 = p->firstChild->rightBrother->rightBrother; TreeNode *p5 = p3->rightBrother->rightBrother; doExp(p3); doStmt(p5); break; } case 5:{ TreeNode *p3 = p->firstChild->rightBrother->rightBrother; TreeNode *p5 = p3->rightBrother->rightBrother; TreeNode *p7 = p5->rightBrother->rightBrother; doExp(p3); doStmt(p5); doStmt(p7); break; } case 6:{ TreeNode *p3 = p->firstChild->rightBrother->rightBrother; TreeNode *p5 = p3->rightBrother->rightBrother; loopNum++; doExp(p3); doStmt(p5); loopNum--; break; } case 7: { if (loopNum == 0) printf("Error 18 at line %d: use 'break' out of a loop\n", p->line); break; } case 8: { if (loopNum == 0) printf("Error 19 at line %d: use 'continue' out of a loop\n", p->line); break; } case 9:{ //the for loop,待完善,应该是中间那个要做个type检查。 TreeNode *p3 = p->firstChild->rightBrother->rightBrother; TreeNode *p5 = p3->rightBrother->rightBrother; TreeNode *p7 = p5->rightBrother->rightBrother; TreeNode *p9 = p7->rightBrother->rightBrother; loopNum++; doOptExp(p3); doExp(p5); doOptExp(p7); doStmt(p9); loopNum--; } } }
Type doExp(TreeNode *p) { //printf("doExp\n"); //printf("TreeNode->state:%s\n", p->state); //printf("TreeNode->productionRule: %d\n", p->productionRule); if(strcmp(p->state, "Exp") == 0) { TreeNode *tempNode = p->firstChild;//产生式右边第一个符号 switch (p->productionRule) { case 1:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1;//?这里不是只赋了地址吗 return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d '=' type mismatch\n", p->line);//类型不匹配 } } break; //exp = exp } case 2:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d '&&' type mismatch\n", p->line);//类型不匹配 } } break; //Exp AND Exp } case 3:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d '||' type mismatch\n", p->line);//类型不匹配 } } break; //Exp OR Exp } case 4:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d 'RELOP' type mismatch\n", p->line);//类型不匹配 } } break; //Exp RELOP Exp } case 5:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d '+' type mismatch\n", p->line);//类型不匹配 } } break; //Exp PLUS Exp } case 6:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d '-' type mismatch\n", p->line);//类型不匹配 } } break; //Exp MINUS Exp } case 7:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d '*' type mismatch\n", p->line);//类型不匹配 } } break; //Exp STAR Exp } case 8:{TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type type = (Type)malloc(sizeof(struct Type_)); Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(t1 != NULL && t2 != NULL) { if(type_equal(t1, t2)) { type = t1; return type;//类型匹配 } else if(!type_equal(t1, t2)) { printf("Error type 3 at line %d '/' type mismatch\n", p->line);//类型不匹配 } } break; //Exp DIV Exp } case 9:return doExp(tempNode->rightBrother); //LP Exp RP case 10:return doExp(tempNode->rightBrother); //MINUS Exp case 11:return doExp(tempNode->rightBrother); //NOT Exp case 12:{Type type = (Type)malloc(sizeof(struct Type_)); varElement *id = searchAll(tempNode->value.idValue); if(id == NULL) { printf("Error type 4 at line %d:undefined function %s\n", p->line, tempNode->value.idValue); } else if(id->type->kind == FUNCTION) {//FUNCTION printf("Error type 4 at line %d:undefined function %s\n", p->line, tempNode->value.idValue); } //else if(){}//函数参数类型不匹配 else { type = id->type; return type; } break; //ID LP Args RP } case 13:{Type type = (Type)malloc(sizeof(struct Type_)); varElement *id =searchAll(tempNode->value.idValue); //在符号表中寻找 if(id == NULL) { printf("Error type 4 at line %d:undefined function %s\n", p->line, tempNode->value.idValue); } else if(id->type->kind == FUNCTION) {//FUNCTION printf("Error type 4 at line %d:undefined function %s\n", p->line, tempNode->value.idValue); } //else if(){}//函数参数数量类型不匹配 else { type = id->type; return type; } break; //ID LP RP } case 14:{Type type = (Type)malloc(sizeof(struct Type_)); TreeNode *temp2Node = tempNode->rightBrother->rightBrother; Type t1 = doExp(tempNode); Type t2 = doExp(temp2Node); if(tempNode != NULL && temp2Node != NULL) { if(t1->kind != ARRAY) printf("Error type 5 at line %d:'[]' unexcepted\n", p->line); else if(t2->u.basic != 0) printf("Error type 6 at line %d:int required\n", p->line); else { type = doExp(tempNode)->u.array.elem; return type; } } break; //Exp LB Exp RB } case 15:{Type type = (Type)malloc(sizeof(struct Type_)); Type t = doExp(tempNode); FieldList field = t->u.structure; if(t != NULL) { if(t->kind != STRUCTURE) { printf("Error type 7 at line %d:'.'before %s unexpected\n", p->line, tempNode->value.idValue); } else { while(field != NULL) { if(strcmp(field->name, tempNode->rightBrother->rightBrother->value.idValue)) { type = field->type; return type; } field = field->tail; } printf("Error type 8 at line %d:struct item %s undefined\n", p->line, tempNode->rightBrother->rightBrother->value.idValue); //结构体成员未定义 } } break; //Exp DOT ID } case 16:{Type type = (Type)malloc(sizeof(struct Type_)); varElement *id =searchAll(tempNode->value.idValue); //在符号表中寻找是否定义 if(id == NULL) { printf("Error type 4 at line %d:undefined variable %s\n", p->line, tempNode->value.idValue); } else if(id->type->kind == FUNCTION) {//FUNCTION printf("Error type 4 at line %d:undefined variable %s\n", p->line, tempNode->value.idValue); }//若为函数名则也错 else { type = id->type; return type; } break; //ID } case 17:{Type type = (Type)malloc(sizeof(struct Type_)); type->kind = BASIC; type->u.basic = 0; return type; //INT } case 18:{Type type = (Type)malloc(sizeof(struct Type_)); type->kind = BASIC; type->u.basic = 1; return type; //FLOAT } } } return NULL; }