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

📄 grammer_l.c

📁 编译原理LL1语法分析的实验程序
💻 C
字号:
#include"global.h"
//add a entry to the table,string s must be in memory
struct assign_node*add_assign_l(char*s,int assign){
	struct assign_node*temp_a;
	struct link_node*temp_l;
	int m,l;
	m=str_h(s)%assign_l_max;
	l=assign%assign_l_max;
	//
	temp_a=&(assign_list[m]);
	while(temp_a->next!=NULL)
		temp_a=temp_a->next;
	temp_a->next=(struct assign_node*)malloc(sizeof(struct assign_node));
	temp_a=temp_a->next;
	strcpy(temp_a->idf,s);
	temp_a->assign=assign;
	temp_a->next=NULL;
	//
	temp_l=&(link_list[l]);
	while(temp_l->next!=NULL)
		temp_l=temp_l->next;
	temp_l->next=(struct link_node*)malloc(sizeof(struct link_node));
	temp_l=temp_l->next;
	temp_l->assign=assign;
	temp_l->point=temp_a;
	temp_l->next=NULL;
	return temp_a;
}
//find the assign of a symbol
int sym_ass(char*s){
	struct assign_node *temp;
	int i;
	i=str_h(s)%assign_l_max;
	temp=assign_list[i].next;
	while(temp!=NULL){
		if(str_cmp(s,temp->idf))
			return temp->assign;
		temp=temp->next;
	}
	printf("can't find grammer symbol %s\n",s);
	return 0;
}
//find the symbol of a assign
char*ass_sym(int as){
	struct link_node* temp;
	int i;
	i=as%assign_l_max;
	temp=link_list[i].next;
	while(temp!=NULL){
		if(temp->assign==as)
			return temp->point->idf;
		temp=temp->next;
	}
	return NULL;
}
//initializ the list
void a_init(void){
	int i,j;
	for(i=0;i<assign_l_max;i++){
		memset(assign_list[i].idf,'\0',max_symbol_len);
		assign_list[i].assign=0;
		assign_list[i].next=NULL;
		link_list[i].assign=0;
		link_list[i].point=NULL;
		link_list[i].next=NULL;
	}
	for(i=0;i<max_production;i++)
		for(j=0;j<max_right_d+1;j++)
			pro_l[i].p[j]=0;
	add_assign_l("$",0);
	add_assign_l("null",bound_v-1);
	add_assign_l("start",bound_v);
	ff=(struct analy*)malloc(sizeof(struct analy));
	init_analy(ff,bound_v);
	push(0);
	push(bound_v);
}
//load grammer and construct a interminal symbol list
int load_grammer(FILE*ginput){
	int nextass=bound_v;
	int temp,i,j;
	struct analy *temp_a;
	char s[max_symbol_len];
	a_init();
	//load terminals
	printf("start load grammer\n");
	while(fscanf(ginput,"%s",s)==1){
		if(str_cmp(s,"%%"))
			break;
		fscanf(ginput," %d",&temp);
		add_assign_l(s,temp);
	}
	//load symbol
	temp_a=ff;
	while(fscanf(ginput,"%s",s)==1){
		if(str_cmp(s,"%%"))
			break;
		add_assign_l(s,++nextass);
		temp_a->next=(struct analy*)malloc(sizeof(struct analy));
		temp_a=temp_a->next;
		init_analy(temp_a,nextass);
	}
	//load production
	i=0;
	j=0;
	while(fscanf(ginput,"%s",s)==1){
		if(str_cmp(s,"%%"))
			break;
		if(str_cmp(s,"!!")){
			i++;
			j=0;
			continue;
		}
		pro_l[i].p[j]=sym_ass(s);
		j++;
	}
	printf("end loading grammers\n");
	return nextass-bound_v+1;
}
//print a production
int print_pro(int i){
	int j;
	for(j=0;j<max_right_d+1;j++){
		if(pro_l[i].p[j]==0)
			break;
		if(j==1)
			printf("->");
		printf("%s",ass_sym(pro_l[i].p[j]));
	}
	printf("\n");
	return j;
}
/*
//test
int main(void){
	FILE*in;
	int i;
	in=fopen("g.txt","r");
	i=load_grammer(in);
	fclose(in);
	printf("%d production\n" ,i);
	for(i=0;i<max_production;i++){
		if(pro_l[i].p[0]==0)
			break;
		print_pro(i);
	}
	return i;
}
*/

⌨️ 快捷键说明

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