Beispiel #1
0
void doExtDef(TreeNode *p) {
	//printf("doExtDef\n");
	//printf("TreeNode->state:%s\n", p->state);
	//printf("TreeNode->productionRule: %d\n", p->productionRule);
	switch (p->productionRule) {
		case 1:{
			//将doExtDecList()返回的变量链表赋为doSpecifier()返回的Type,插入变量表
			TreeNode *p1 = p->firstChild;
			TreeNode *p2 = p1->rightBrother;
			Type type = doSpecifier(p1);
			varElement *elem = doExtDecList(p2);
			varElement *elemn = NULL;
			while (elem != NULL) {
				Type t = elem->type;
				if (t->kind != BASIC) {
					while (t->kind == ARRAY || t->u.array.elem->kind != BASIC) {	//如果是数组要找到最底部的type节点赋为Specifier传回的Type
						t = t->u.array.elem;				
					}
					//free(t->u.array.elem);
					t->u.array.elem = type;	//在倒数第二个节点处改变Type
				} else {
					//free(elem->type);
					elem->type = type;
					if (search(elem->name) != NULL) {	//查找此层定义不为空,说明变量重复定义
						printf("error type 3 at line %d: variable %s redefined.\n", p->line, elem->name);
						elem = elem->next;
					} else {	
						elemn = elem->next;
						insert(elem);
						elem = elemn;
					}
				}
			}
			break;
			   }
		case 2:	{
			TreeNode *p1 = p->firstChild;
			doSpecifier(p1);
			break;
				}
		case 3:{
			TreeNode *p1 = p->firstChild;
			TreeNode *p2 = p1->rightBrother;
			TreeNode *p3 = p2->rightBrother;
			Type type = doSpecifier(p1);
			doFunDec(type, p2);	//将返回类型传进去,在doFunDec中处理函数表
			doCompSt(p3);
			break;
			   }
	}
}
Beispiel #2
0
varElement* doExtDecList(TreeNode *p) {
	//printf("doExtDecList\n");
	//这个函数会把同一语句中出现的变量串起来
	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;
			varElement *elem1 = doVarDec(p1);
			varElement *elem2 = doExtDecList(p3);
			elem1->next = elem2;
			return elem1;
			break;
			   }
	}
}
Beispiel #3
0
void doExtDef(TreeNode *p) {
	//printf("doExtDef\n");
	//printf("TreeNode->state:%s\n", p->state);
	//printf("TreeNode->productionRule: %d\n", p->productionRule);
	switch (p->productionRule) {
		case 1:{
			//将doExtDefList()返回的变量链表赋为doSpecifier()返回的Type,插入变量表
			TreeNode *p1 = p->firstChild;
			TreeNode *p2 = p1->rightBrother;
		//	printf("here!\n");
			Type type = doSpecifier(p1);
		//	printf("here!\n");
			varElement *elem = doExtDecList(p2);
		//	printf("here!\n");
			varElement *elemn = NULL;
			while (elem != NULL) {
		//		printf("here!\n");
				elem->type = type;	//相当不确定这句指针操作的正确性
				elemn = elem->layer_next;
				insert(elem);
				elem = elemn;
			}
			break;
			   }
		case 2:	{//这条不清楚存在的意义,可以暂不管
			TreeNode *p1 = p->firstChild;
			doSpecifier(p1);
			break;
				}
		case 3:{
			TreeNode *p1 = p->firstChild;
			TreeNode *p2 = p1->rightBrother;
			TreeNode *p3 = p2->rightBrother;
			doSpecifier(p1);
			doFunDec(p2);
			doCompSt(p3);
			break;
			   }
	}
}