Esempio n. 1
0
cls_yacc_product::cls_yacc_product(string a_exp,cls_yacc_info* a_info,int a_pos):info(a_info)
{
	oper = -1;
	order = a_pos;
	str_exp = a_exp;
	analyze_exp();
}
Esempio n. 2
0
static int analyze_args_exp(NODE* expp , FieldList * arg){
	NODE* exp = expp->child_head;
	int errno = 0;
	while(arg != NULL){
		if(exp != NULL && exp->name == nonterminal_name[Exp]){
			Type* ttype ;
			ttype = analyze_exp(exp) ;
			if(ttype->kind != arg->type->kind){
				error("unmatched argument type",9,exp->line);
				errno = 1;
			}else{
				if(ttype->kind == BASIC_TYPE){
					if(ttype->u.basic != ttype->u.basic){
						error("unmatched argument type:one is INT the other is FLOAT",9,exp->line);
						errno = 1 ;
					}
				}else if(ttype->kind == ARRAY_TYPE){
					errno = error_argument_array_struct(cmp_array(arg->type,ttype),exp->line) ;
				}else if(ttype->kind == STRUCTURE_TYPE){
					errno = error_argument_array_struct(cmp_array(arg->type,ttype),exp->line) ;
				}
			}
		}else{
			error("too few arguments in \'(\' \')\'",9,expp->line);
			errno = 1;
			break ;
		}
		if(arg = arg->tail ) {
			if((exp = exp->next_sister) && (exp = exp->next_sister) && (exp = exp->child_head)){
				continue ;
			}
			exp = NULL ;
		}
	}
	if(exp && (exp = exp->next_sister)&&(exp = exp->next_sister)&&(exp = exp->child_head)){
		error("too many arguments",9,exp->line);
	}
	return errno ;
}
Esempio n. 3
0
static Type* analyze_exp(NODE* exp){
/*child_head*/
	if(exp->attr == NULL){
		NODE * temp = exp->child_head ;
		if(temp->name == terminal_name[ID - WHILE]){
			NODE* ntemp = temp->next_sister ;
			IDTEM* tid = temp->value.type_p ;
			tid = tid->cur;
			temp->value.type_p = tid ;
			temp->parent->attr = tid->u.t;//Exp get attr from ID
			temp->value.type_p = tid ;
			if(ntemp == NULL){
				if(tid->kind != VARIABLE_KIND){
					error("Undefined variable",1,temp->line);
					temp->parent->attr = create_type();
				}else{
					if(temp->parent->attr){}else{
						temp->parent->attr = create_type();
					}
				}
			}else if(ntemp->name == terminal_name[LP - WHILE]){
				if(tid->kind == UNSPECIFIED ){
					error("Undefined function",2,temp->line);
					temp->parent->attr = create_type();
				}else if(tid->kind == FUNCTION_KIND){
					FieldList * arg = tid->u.funtype.varhead ;
					NODE * tmp = temp->next_sister->next_sister ;
					analyze_args_exp(tmp , arg) ;
				}else if(tid->kind == STRUCTURE_KIND){
					error("not a function before \'(\':it\'s a struct name",11,ntemp->line);
					temp->parent->attr = create_type();
				}else{
					error("not a function before \'(\':it\'s a variable",11,ntemp->line);
					temp->parent->attr = create_type();
				}
			}
		}else if(temp->name == terminal_name[FLOAT - WHILE]){
			(temp->parent->attr = create_type_kind(BASIC_TYPE,float_type)) ;//Exp get attr from FLOAT 
		}else if(temp->name == terminal_name[INT - WHILE]){
			(temp->parent->attr = create_type_kind(BASIC_TYPE,int_type)) ;//Exp get attr from INT
		}else if(temp->name == terminal_name[MINUS - WHILE]){//int or float
			Type * ttype = temp->parent->attr = analyze_exp(temp->next_sister) ;//Exp get attr from  Exp after MINUS
			if(ttype->kind == BASIC_TYPE){
			}else {
				error("illegal arithmetic operation",7,temp->line);
			}
			(temp->parent->attr = ttype) ;
		}else if(temp->name == terminal_name[LP - WHILE]){
			(temp->parent->attr = analyze_exp(temp->next_sister)) ;//Exp get attr from Exp after LP
		}else if(temp->name == terminal_name[NOT - WHILE]){// only int 
			Type * ttype = temp->parent->attr = analyze_exp(temp->next_sister) ;//Exp get attr from  Exp after MINUS
			if(ttype->kind == BASIC_TYPE && ttype->u.basic == int_type){
			}else {
				error("illegal NOT logic operation",7,temp->line);
			}
			(temp->parent->attr = ttype) ;
		}else if(temp->name == nonterminal_name[Exp]){
			NODE* ntemp = temp->next_sister; 
			if(ntemp->name == terminal_name[AND - WHILE] || ntemp->name == terminal_name[OR - WHILE]){//only int
				Type * ttype1 = analyze_exp(temp) ;
				Type * ttype2 = analyze_exp(ntemp->next_sister) ;
				if(ttype1->kind == ttype2->kind && ttype1->kind == BASIC_TYPE && ttype1->u.basic == ttype2->u.basic && ttype1->u.basic == int_type){
					(temp->parent->attr = ttype1) ;
				}else{
					error("illegal logic operation ",7,ntemp->line);
					(temp->parent->attr = create_type_kind(BASIC_TYPE,int_type));
				}
			}else if(ntemp->name == terminal_name[PLUS - WHILE]||
				ntemp->name == terminal_name[MINUS - WHILE]||
				ntemp->name == terminal_name[STAR - WHILE]||
				ntemp->name == terminal_name[DIV - WHILE]  ){//int or float
				Type * ttype1 = analyze_exp(temp) ;
				Type * ttype2 = analyze_exp(ntemp->next_sister) ;
				if(ttype1->kind == ttype2->kind && ttype1->kind == BASIC_TYPE && ttype1->u.basic == ttype2->u.basic){
				}else {
					error("illegal arithmetic operation" , 7 , ntemp->line);
				}
				(temp->parent->attr = ttype1) ;
			}else if(ntemp->name == terminal_name[RELOP - WHILE]){//int or float
				Type * ttype1 = analyze_exp(temp) ;
				Type * ttype2 = analyze_exp(ntemp->next_sister) ;
				if(ttype1->kind == ttype2->kind && ttype1->kind == BASIC_TYPE && ttype1->u.basic == ttype2->u.basic){
				}else {
					error("illegal relation operation",7,ntemp->line);
				}
				(temp->parent->attr = create_type_kind(BASIC_TYPE,int_type));
			}else if(ntemp->name == terminal_name[LB - WHILE]){
				Type * ttype1 = analyze_exp(temp) ;
				Type * ttype2 = analyze_exp(ntemp->next_sister) ;
				if(ttype2->kind == BASIC_TYPE && ttype2->u.basic == int_type){
				}else{
					error("noninteger in bracket",12,ntemp->next_sister->line);
				}
				if(ttype1->kind == ARRAY_TYPE){
					(temp->parent->attr = ttype1->u.array.elem );
				}else {
					error("not an array before \'[\'",10,ntemp->line);
					(temp->parent->attr = create_type());
				}
			}else if(ntemp->name == terminal_name[ASSIGNOP - WHILE]){
				Type * ttype1 = analyze_exp(temp) ;
				Type * ttype2 = analyze_exp(ntemp->next_sister) ;
				if((temp->child_head->name == terminal_name[ID - WHILE] && temp->child_head->next_sister == NULL)||
					(temp->child_head->name == nonterminal_name[Exp] && (
					temp->child_head->next_sister->name == terminal_name[LB - WHILE] || 
					temp->child_head->next_sister->name == terminal_name[DOT - WHILE] ))){// temp->child_head->next_sister may not be null pointer
					if(ttype1->kind == ttype2->kind && ttype1->kind == BASIC_TYPE && ttype1->u.basic == ttype2->u.basic){	
					}else if(ttype1->kind == ttype2->kind && ttype1->kind == STRUCTURE_TYPE && ttype1->u.structure.name == ttype2->u.structure.name){
					}else {
						if(ttype1->kind != UNSPECIFIED && ttype2->kind != UNSPECIFIED)
                                        		error("unmatched type",5,ntemp->line);
                                        }
					(temp->parent->attr = ttype1) ;
				}else{
					error("lvalue expression expected",6,ntemp->line);
					(temp->parent->attr = ttype2) ;
				}
			}else if(ntemp->name == terminal_name[DOT - WHILE]){
				Type * ttype1 = analyze_exp(temp) ;
				IDTEM * tid = ntemp->next_sister->value.type_p ;
				tid = tid->cur ;
				const char * ctemp = tid->name ;
				if(ttype1->kind == STRUCTURE_TYPE){
					FieldList * field = ttype1->u.structure.field;
					while(field){
						if(strcmp(field->name,ctemp) == 0){
							break;
						}
						field = field->tail ;
					}
					if(field == NULL){
						error("undefined field" , 14, ntemp->next_sister->line);
						(temp->parent->attr = create_type());
					}else{
						(temp->parent->attr = field->type);
					}
				}else{
					error("not a structure before \'.\'",13,ntemp->line);
					(temp->parent->attr = create_type());
				}
				
			}
		}
	}
	{
		NODE * temp = exp->previous_sister ;
		if(temp && temp->name == terminal_name[RETURN - WHILE]){
			NODE * tmp = temp;
			Type * ttype1 ;
			Type * ttype2 = exp->attr ;
			while((tmp = tmp->parent)->name != nonterminal_name[CompSt]) {}//tmp->name ==CompSt
			tmp = tmp->previous_sister ;//tmp->name == FunDec
			ttype1 = tmp->attr ;
			if(ttype1->kind == ttype2->kind && ttype1->kind == BASIC_TYPE && ttype1->u.basic == ttype2->u.basic){
			}else if(ttype1->kind == ttype2->kind && ttype1->kind == STRUCTURE_TYPE && cmp_structure_name(ttype1,ttype2)==0){
			}else {
				error("function return unmatched type",8,temp->line);
			}
		}else if(temp && temp->name == terminal_name[ASSIGNOP - WHILE] 
			&& temp->previous_sister && temp->previous_sister->name == nonterminal_name[VarDec]
			&& temp->next_sister && temp->next_sister->name == nonterminal_name[Exp]){
			NODE * tmp1 = temp->previous_sister ;
			NODE * tmp2 = temp->next_sister ;
			Type* ttype1 = tmp1->attr ;
			Type* ttype2 = tmp2->attr ;
			if(ttype1->kind == ttype2->kind && ttype1->kind == BASIC_TYPE && ttype1->u.basic == ttype2->u.basic){
			}else if(ttype1->kind == ttype2->kind && ttype1->kind == STRUCTURE_TYPE && cmp_structure_name(ttype1,ttype2)==0){
			}else {
				error("unmatched type before and after \'=\'",5,temp->line);
			}
			
		}
	}
	return exp->attr;
}
Esempio n. 4
0
int analyze(NODE* head){
	const char * ctemp ;
	int depth ;
	if((ctemp = head->name) == terminal_name[ID-WHILE]){
#ifndef SUBMIT
		printf("%s: %s\n",head->name , ((IDTEM*)(head->value).type_p)->name);
#endif
	}else if(ctemp == terminal_name[TYPE-WHILE]){
#ifndef SUBMIT
		printf("%s: %s\n",head->name,(char *)(head->value).type_p) ;
#endif
	}else if(ctemp == terminal_name[FLOAT-WHILE]){
#ifndef SUBMIT
		printf("%s: %f\n",head->name,(head->value).type_float) ;
#endif
	}else if(ctemp == terminal_name[INT-WHILE]){
#ifndef SUBMIT
		printf("%s: %d\n",head->name,(head->value).type_int);
#endif
	}else if(ctemp == nonterminal_name[Exp]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		analyze_exp(head);
	}else if(ctemp == terminal_name[LC - WHILE]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		if(head->parent->previous_sister != NULL){
		}else{
			allowoverlap(idtable_head);
		}
	}else if(ctemp == terminal_name[RC - WHILE]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		disallowoverlap(idtable_head);
	}else if(ctemp == nonterminal_name[ExtDef]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		analyze_def(head,ExtDef);
	}else if(ctemp == nonterminal_name[Def]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		analyze_def(head,Def);
	}else if(ctemp == nonterminal_name[DecList] || ctemp == nonterminal_name[ExtDecList]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		analyze_dec_extdeclist(head);
	}else if(ctemp == terminal_name[LP - WHILE]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		if(head->parent->name == nonterminal_name[FunDec]){
			allowoverlap(idtable_head);
		}
	}else if(ctemp == nonterminal_name[VarDec]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		analyze_vardec(head);
	}else if(ctemp == nonterminal_name[ParamDec]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		analyze_def(head,ParamDec);
	}else if(ctemp == nonterminal_name[VarList]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
		analyze_varlist(head);
	}else if(ctemp == terminal_name[RETURN - WHILE]){
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
	}else{
#ifndef SUBMIT
		printf("%s (%ld)\n",head->name,head->line);
#endif
	}
	return 0;
}