欢迎来到天天文库
浏览记录
ID:11187270
大小:104.00 KB
页数:17页
时间:2018-07-10
《语法分析器源代码》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、#include#include#include#defineHIGHER1#defineLOWER-1#defineEQUAL0#defineTRUE1#defineFALSE0#defineOPER_NUM50//默认算符数目#defineVN_NUM50//默认非终结符数目#defineMAX_BUFFER128//每行输入行最大长度#defineMAX_GRA_NUM20//最大文法数目#defineEMPTY-2//算符优先表初始值,表示这对算符没有优先关系#defineSTACK_SIZE64
2、typedefstruct{charc;//非终极符符号intfirstvt[OPER_NUM];//firstvt集,保存算符在oper_list中的下标intfir_n,last_n;intlastvt[OPER_NUM];}vn_t;intprior_table[OPER_NUM][OPER_NUM];charoper_list[OPER_NUM];intoper_num=0;vn_tvn_list[VN_NUM];intvn_num=0;char*grammar[MAX_GRA_NUM][2];intgra_num=0;charstart_vn;char
3、stack[STACK_SIZE];inttop=0;voidget_input(char*buf);intbuf_deal(char*buf);voidget_FIRVT_LASTVT(intvn_n);intcreate_table();voidinit_table();intanalyze(char*sentence);voiddisplay_table();voiddisplay_fir_lastvt();intget_oper(charc);//得到算符c的数组下标没有返回-1intget_vn(charc);//得到非终结符c的数组下标,没有返回-1
4、intis_vn(chara){return(('A'<=a&&a<='Z')?1:0);}intget_oper(charc){inti;for(i=0;i5、,ti,cn=0;intsame;tar=(char*)malloc(sizeof(char)*MAX_BUFFER);if(!tar){printf("Allocationfails.");exit(-1);}for(i=start;i<=end;++i)//将此最左素短语的终结符存入tar字符串{if(!is_vn(stack[i]))tar[j++]=stack[i];}tar[j++]=' ';for(i=0;i6、];while(t1!=' '){if(t2==' '&&!is_vn(t1)){same=FALSE;break;}if(!is_vn(t1)){if(t1!=t2){same=FALSE;break;}t2=tar[++ti];same=TRUE;}t1=g[++gi];}if(same&&t2==' ')break;}if(i==gra_num){printf("无法找到相应文法!");returnFALSE;}left=grammar[i][0][0];returnvn_list[get_vn(left)].c;}intanalyze(char7、*sentence){charr,c,new_vn;inti=0,k=0,j,pi,printi=1,cou=1;//i是sentence[]和stack[]的索引intr_index,s_index,pre_index;printf("规约过程如下表所示:");printf("--------------------------------------");stack[top++]='#';printf("序号t符号栈t最左素短语t规约t");do{r=sentence[i++];if((r_index=get_oper(r))==8、-1){printf("
5、,ti,cn=0;intsame;tar=(char*)malloc(sizeof(char)*MAX_BUFFER);if(!tar){printf("Allocationfails.");exit(-1);}for(i=start;i<=end;++i)//将此最左素短语的终结符存入tar字符串{if(!is_vn(stack[i]))tar[j++]=stack[i];}tar[j++]=' ';for(i=0;i6、];while(t1!=' '){if(t2==' '&&!is_vn(t1)){same=FALSE;break;}if(!is_vn(t1)){if(t1!=t2){same=FALSE;break;}t2=tar[++ti];same=TRUE;}t1=g[++gi];}if(same&&t2==' ')break;}if(i==gra_num){printf("无法找到相应文法!");returnFALSE;}left=grammar[i][0][0];returnvn_list[get_vn(left)].c;}intanalyze(char7、*sentence){charr,c,new_vn;inti=0,k=0,j,pi,printi=1,cou=1;//i是sentence[]和stack[]的索引intr_index,s_index,pre_index;printf("规约过程如下表所示:");printf("--------------------------------------");stack[top++]='#';printf("序号t符号栈t最左素短语t规约t");do{r=sentence[i++];if((r_index=get_oper(r))==8、-1){printf("
6、];while(t1!=' '){if(t2==' '&&!is_vn(t1)){same=FALSE;break;}if(!is_vn(t1)){if(t1!=t2){same=FALSE;break;}t2=tar[++ti];same=TRUE;}t1=g[++gi];}if(same&&t2==' ')break;}if(i==gra_num){printf("无法找到相应文法!");returnFALSE;}left=grammar[i][0][0];returnvn_list[get_vn(left)].c;}intanalyze(char
7、*sentence){charr,c,new_vn;inti=0,k=0,j,pi,printi=1,cou=1;//i是sentence[]和stack[]的索引intr_index,s_index,pre_index;printf("规约过程如下表所示:");printf("--------------------------------------");stack[top++]='#';printf("序号t符号栈t最左素短语t规约t");do{r=sentence[i++];if((r_index=get_oper(r))==
8、-1){printf("
此文档下载收益归作者所有