1、词法分析/*cifafenxichengxu*/#includestdio.h#includectype.h#includealloc.h#includestdlib.h#includestring.h#defineNULL0FILE*fp;charcbuffer;char*key[8]={if,else,for,while,do,return,break,continue};char*border[6]={,,;,{,},(,)};char*arithmetic[4]={+,-,*,/};char*relation[6]={,=,=,,=,};char*consts[20];char*label[20];intconstnum=0,labelnum=0;intsearch(charsearchchar[],intwordtype){inti=0;switch(wordtype){case1:for(i=0;i=7;i++){if(strcmp(key[i],searchchar)==0)return(i+1);}case2:{for(i=0;i=5;i++){if(strcmp(border[i],searchchar)==0)return(i+1);}return(0);}case3:{for(i=0;i=3;i++){if(strcmp(arithmetic[i],searchchar)==0){return(i+1);}}return(0);}case4:{for(i=0;i=5;i++){if(strcmp(relation[i],searchchar)==0){return(i+1);}}return(0);}case5:{for(i=0;i=constnum;i++){if(strcmp(consts[i],searchchar)==0){return(i+1);}}consts[i-1]=(char*)malloc(sizeof(searchchar));strcpy(consts[i-1],searchchar);constnum++;return(i);}case6:{for(i=0;i=labelnum;i++){if(strcmp(label[i],searchchar)==0){return(i+1);}}label[i-1]=(char*)malloc(sizeof(searchchar));strcpy(label[i-1],searchchar);labelnum++;return(i);}}}charalphaprocess(charbuffer){intatype;inti=-1;charalphatp[20];while((isalpha(buffer))||(isdigit(buffer))){alphatp[++i]=buffer;buffer=fgetc(fp);}alphatp[i+1]='0';if(atype=search(alphatp,1))printf(%s(1,%d)n,alphatp,atype-1);else{atype=search(alphatp,6);printf(%s(6,%d)n,alphatp,atype-1);}return(buffer);}chardigitprocess(charbuffer){inti=-1;chardigittp[20];intdtype;while((isdigit(buffer))){digittp[++i]=buffer;buffer=fgetc(fp);}digittp[i+1]='0';dtype=search(digittp,5);printf(%s(5,%d)n,digittp,dtype-1);return(buffer);}charotherprocess(charbuffer){inti=-1;charothertp[20];intotype,otypetp;othertp[0]=buffer;othertp[1]='0';if(otype=search(othertp,3)){printf(%s(3,%d)n,othertp,otype-1);buffer=fgetc(fp);gotoout;}if(otype=search(othertp,4)){buffer=fgetc(fp);othertp[1]=buffer;othertp[2]='0';if(otypetp=search(othertp,4)){printf(%s(4,%d)n,othertp,otypetp-1);gotoout;}elseothertp[1]='0';printf(%s(4,%d)n,othertp,otype-1);gotoout;}if(buffer==':'){buffer=fgetc(fp);if(buffer=='=')printf(:=(2,2)n);buffer=fgetc(fp);gotoout;}else{if(otype=search(othertp,2)){printf(%s(2,%d)n,othertp,otype-1);buffer=fgetc(fp);gotoout;}}if((buffer!='n')&&(buffer!=''))printf(%cerror,notawordn,buffer);buffer=fgetc(fp);out:return(buffer);}voidmain(){inti;for(i=0;i=20;i++){label[i]=NULL;consts[i]=NULL;};if((fp=fopen(example.c,r))==NULL)printf(error);else{cbuffer=fgetc(fp);while(cbuffer!=EOF){if(isalpha(cbuffer))cbuffer=alphaprocess(cbuffer);elseif(isdigit(cbuffer))cbuffer=digitprocess(cbuffer);elsecbuffer=otherprocess(cbuffer);}printf(overn);getchar();}}2、LL(1)/*LL(1)分析法源程序,只能在VC++中运行*/#includestdio.h#includestdlib.h#includestring.h#includedos.hcharA[20];/*分析栈*/charB[20];/*剩余串*/charv1[20]={'i','+','*','(',')','#'};/*终结符*/charv2[20]={'E','G','T','S','F'};/*非终结符*/intj=0,b=0,top=0,l;/*L为输入串长度*/typedefstructtype/*产生式类型定义*/{charorigin;/*大写字符*/chararray[5];/*产生式右边字符*/intlength;/*字符个数*/}type;typee,t,g,g1,s,s1,f,f1;/*结构体变量*/typeC[10][10];/*预测分析表*/voidprint()/*输出分析栈*/{inta;/*指针*/for(a=0;a=top+1;a++)printf(%c,A[a]);printf(tt);}/*print*/voidprint1()/*输出剩余串*/{intj;for(j=0;jb;j++)/*输出对齐符*/printf();for(j=b;j=l;j++)printf(%c,B[j]);printf(ttt);}/*print1*/voidmain(){intm,n,k=0,flag=0,finish=0;charch,x;typecha;/*用来接受C[m][n]*//*把文法产生式赋值结构体*/e.origin='E';strcpy(e.array,TG);e.length=2;t.origin='T';strcpy(t.array,FS);t.length=2;g.origin='G';strcpy(g.array,+TG);g.length=3;g1.origin='G';g1.array[0]='^';g1.length=1;s.origin='S';strcpy(s.array,*FS);s.length=3;s1.origin='S';s1.array[0]='^';s1.length=1;f.origin='F';strcpy(f.array,(E));f.length=3;f1.origin='F';f1.array[0]='i';f1.length=1;for(m=0;m=4;m++)/*初始化分析表*/for(n=0;n=5;n++)C[m][n].origin='N';/*全部赋为空*//*填充分析表*/C[0][0]=e;C[0][3]=e;C[1][1]=g;C[1][4]=g1;C[1][5]=g1;C[2][0]=t;C[2][3]=t;C[3][1]=s1;C[3][2]=s;C[3][4]=C[3][5]=s1;C[4][0]=f1;C[4][3]=f;printf(提示:本程序只能对由'i','+','*','(',')'构成的以'#'结束的字符串进行分析,n);printf(请输入要分析的字符串:);do/*读入分析串*/{scanf(%c,&ch);if((ch!='i')&&(ch!='+')&&(ch!='*')&&(ch!='(')&&(ch!=')')&&(ch!='#')){printf(输入串中有非法字符n);exit(1);}B[j]=ch;j++;}while(ch!='#');l=j;/*分析串长度*/ch=B[0];/*当前分析字符*/A[top]='#';A[++top]='E';/*'#','E'进栈*/printf(步骤tt分析栈tt剩余字符tt所用产生式n);do{x=A[top--];/*x为当前栈顶字符*/printf(%d,k++);printf(tt);for(j=0;j=5;j++)/*判断是否为终结符*/if(x==v1[j]){flag=1;break;}if(flag==1)/*如果是终结符*/{if(x=='#'){finish=1;/*结束标记*/printf(acc!n);/*接受*/getchar();getchar();exit(1);}/*if*/if(x==ch){print();print1();printf(%c匹配n,ch);ch=B[++b];/*下一个输入字符*/flag=0;/*恢复标记*/}/*if*/else/*出错处理*/{print();print1();printf(%c出错n,ch);/*输出出错终结符*/exit(1);}/*else*/}/*if*/else/*非终结符处理*/{for(j=0;j=4;j++)if(x==v2[j]){m=j;/*行号*/break;}for(j=0;j=5;j++)if(ch==v1[j]){n=j;/*列号*/break;}cha=C[m][n];if(cha.origin!='N')/*判断是否为空*/{print();print1();printf(%c-,cha.origin);/*输出产生式*/for(j=0;jcha.length;j++)printf(%c,cha.array[j]);printf(n);for(j=(cha.length-1);j=0;j--)/*产生式逆序入栈*/A[++top]=cha.array[j];if(A[top]=='^')/*为空则不进栈*/top--;}/*if*/else/*出错处理*/{print();print1();printf(%c出错n,