⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parse.c

📁 编译原理LL1语法分析的实验程序
💻 C
字号:
#include"global.h"
void push(int i){
	struct node*temp;
	temp=stack;
	stack=(struct node*)malloc(sizeof(struct node));
	stack->element=i;
	stack->next=temp;
}
int pop(void){
	struct node*temp;
	int i;
	if(stack==NULL)
		return -1;
	temp=stack;
	stack=stack->next;
	i=temp->element;
	free(temp);
	return i;
}

void usep(i){
	int *t,n;
	print_pro(i);
	t=pro_l[i].p;
	if(stack->element!=t[0])
		printf("error use of production\n");
	pop();
	for(n=max_right_d;n>0;n--){
		if(t[n]==0)
			continue;
		if(t[n]==bound_v-1)
			break;
		push(t[n]);
	}
}
int find_pro(int interm,int term){
	struct analy*temp_a;
	struct analy_node*temp_b;
	temp_a=find_analy(interm);
	//printf("find %s,%s\n", ass_sym(interm) , ass_sym(term) );
	for(temp_b=temp_a->row;temp_b!=NULL;temp_b=temp_b->next){
		if(temp_b->terminal==term)
			break;
	}
	if(temp_b==NULL)
		return -1;
	return temp_b->production;
}
int parse(){
	int a,t,i;
	while(1){
		a=lexer();
AAA:	t=stack->element;
		if(a==t){
			pop();
			if(t==0){
				printf("parse:done!\n");
				return 1;
			}
			continue;
		}
		if(t<bound_v){
			printf("parse:unexperted token %s\n",ass_sym(a));
			return -1; 
		}
		i=find_pro(t,a);
		if(i==-1){
			printf("parse:can't parse token %s\n",ass_sym(a));
			return -1;
		}
		usep(i);
		goto AAA;
	}
}


int main(int argc,char*argv[]){
	FILE*gfile;
	gfile=fopen(argv[1],"r");
	if(gfile==NULL)
		return -1;
	load_grammer(gfile);
	sin=fopen(argv[2],"r");
	if(sin==NULL)
		return -2;
	analysis();
	parse();
	fclose(sin);
	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -