您好,欢迎访问三七文档
[实验任务]完成以下正则文法所描述的Pascal语言子集单词符号的词法分析程序。标识符→字母︱标识符字母︱标识符数字无符号整数→数字︱无符号整数数字单字符分界符→+︱-︱*︱;︱(︱)双字符分界符→大于=︱小于=︱小于︱冒号=︱斜竖*小于→等于→=大于→冒号→:斜竖→/该语言的保留字:beginendifthenelsefordowhileandornot说明:1该语言大小写不敏感。2字母为a-zA-Z,数字为0-9。3可以对上述文法进行扩充和改造。4‘/*……*/’为程序的注释部分。[设计要求]1、给出各单词符号的类别编码。2、词法分析程序应能发现输入串中的错误。3、词法分析作为单独一遍编写,词法分析结果为二元式序列组成的中间文件。4、设计两个测试用例(尽可能完备),并给出测试结果。demo.cpp#includestdio.h#includectype.h#includestring.h#includedemo.hchartoken[20];intlookup(char*token){for(inti=0;i11;i++){if(strcmp(token,KEY_WORDS[i])==0){returni+1;}}return0;}chargetletter(FILE*fp){returntolower(fgetc(fp));}voidout(FILE*fp,intc,char*value){fprintf(fp,%d,%s\n,c,value);}voidreport_error(FILE*fp,charch){fprintf(fp,Theremustbesomeerror.\n);fprintf(fp,%c...\n,ch);fprintf(fp,^\n);}voidscanner(FILE*infile,FILE*outfile){charch;inti,c;do{do{ch=getletter(infile);}while(isspace(ch));if(isalpha(ch)){token[0]=ch;ch=getletter(infile);i=1;while(isalnum(ch)){token[i]=ch;i++;ch=getletter(infile);}token[i]='\0';fseek(infile,-1,1);c=lookup(token);if(c==0){out(outfile,ID,token);}else{out(outfile,c,);}}else{if(isdigit(ch)){token[0]=ch;ch=getletter(infile);i=1;while(isdigit(ch)){token[i]=ch;i++;ch=getletter(infile);}token[i]='\0';fseek(infile,-1,1);out(outfile,INT,token);}else{switch(ch){case'':{ch=getletter(infile);if(ch=='='){out(outfile,LE,);}elseif(ch==''){out(outfile,NE,);}else{fseek(infile,-1,1);out(outfile,LT,);}break;}case'=':{out(outfile,EQ,);break;}case'':{ch=getletter(infile);if(ch=='='){out(outfile,GE,);}else{fseek(infile,-1,1);out(outfile,GT,);}break;}case':':{ch=getletter(infile);if(ch=='='){out(outfile,FU,);}else{fseek(infile,-1,1);out(outfile,MAO,);}break;}case'/':{ch=getletter(infile);if(ch=='*'){out(outfile,ZHU,);}else{fseek(infile,-1,1);out(outfile,XIE,);}break;}case'+':{out(outfile,JIA,);break;}case'-':{out(outfile,JIAN,);break;}case'*':{out(outfile,CHEN,);break;}case';':{out(outfile,FEN,);break;}case'(':{out(outfile,ZUO,);break;}case')':{out(outfile,YOU,);break;}default:{if(ch!=EOF){report_error(outfile,ch);}break;}}}}}while(ch!=EOF);return;}voidmain(){FILE*in,*out;in=fopen(pascal.txt,r);out=fopen(result.txt,w);scanner(in,out);fprintf(out,0,#);fclose(in);fclose(out);printf(Finished!\n);}本文来自CSDN博客,转载请标明出处:[实验任务]1、实现LL(1)分析中控制程序(表驱动程序);2、完成以下描述算术表达式的LL(1)文法的LL(1)分析程序(LL(1)分析表见教材)。G[E]:E→TE′E′→ATE′|εT→FT′T′→MFT′|εF→(E)|iA→+|-M→*|/说明:终结符号i为用户定义的简单变量,即标识符的定义。[设计要求]1、输入串应是词法分析的输出二元式序列,即某算术表达式“实验项目一”的输出结果。输出为输入串是否为该文法定义的算术表达式的判断结果。2、LL(1)分析过程应能发现输入串出错。3、设计两个测试用例(尽可能完备,正确和出错),并给出测试结果。demo.cpp#includestdio.h#includestring.h#includestdlib.h#includedemo.h#defineSIZE100charstack[SIZE];intbottom=0,top=0;voidpush(charch){if(top!=SIZE){stack[top]=ch;top++;}}charpop(){if(top!=bottom){top--;stack[top]='\0';returnstack[top];}return-2;}intget_id(FILE*fp){chartemp[100];intid;fscanf(fp,%d,&id);fgets(temp,100,fp);returnid;}voidtranslate(intid,char*a){switch(id){case0:*a='#';break;case12:*a='i';break;case22:*a='+';break;case23:*a='-';break;case24:*a='*';break;case21:*a='/';break;}}boolis_terminate(charch){if(ch=='i'||ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='#')returntrue;returnfalse;}voidtrans(charch,chara,int*i,int*j){switch(ch){case'E':*i=0;break;case'e':*i=1;break;case'T':*i=2;break;case't':*i=3;break;case'F':*i=4;break;case'A':*i=5;break;case'M':*i=6;break;}switch(a){case'i':*j=0;break;case'+':*j=1;break;case'-':*j=2;break;case'*':*j=3;break;case'/':*j=4;break;case'(':*j=5;break;case')':*j=6;break;case'#':*j=7;break;}}voidsearch_table(charch,chara){inti,j;trans(ch,a,&i,&j);chartemp[20]={'\0'};strcpy(temp,TABLE[i][j]);if(strcmp(temp,~)==0){pop();return;}elseif(strcmp(temp,)!=0){pop();for(unsignedii=0;iistrlen(temp);ii++){push(temp[ii]);}return;}else{printf(Error!\n);exit(0);}}voidmain(){FILE*in;intid=0;chara=0;in=fopen(result.txt,r);push('#');push('E');id=get_id(in);translate(id,&a);while(true){if(is_terminate(stack[top-1])){if(stack[top-1]==a&&a=='#'){printf(success.\n);return;}elseif(stack[top-1]==a){pop();id=get_id(in);translate(id,&a);}else{printf(Error!\n);return;}}elseif(!is_terminate(stack[top-1])){search_table(stack[top-1],a);}}fclose(in);}本文来自CSDN博客,转载请标明出处:[实验任务]完成以下描述算术表达式的LL(1)文法的递归下降分析程序G[E]:E→TE′E′→ATE′|εT→FT′T′→MFT′|εF→(E)|iA→+|-M→*|/说明:终结符号i为用户定义的简单变量,即标识符的定义。[设计要求]1、输入串应是词法分析的输出二元式序列,即某算术表达式“实验项目一”的输出结果。输出为输入串是否为该文法定义的算术表达式的判断结果。2、递归下降分析程序应能发现输入串出错。3、设计两个测试用例(尽可能完备,正确和出错),并给出测试结果。demo.cppboolE(){if(T()){if(E_prime()){returntrue;}}returnfalse;}boolE_prime(){if(A()){if(T()){if(E_prime()){returntrue;}elsereturnfalse;}elsereturnfalse;}returntrue;}boolT(){if(F()){if(T_prime()){returntrue;}}returnfalse;}boolT_prime(){if(M()){if(F()){if(T_prime()){returntrue;}elsereturnfalse;}elsereturnfalse;}returntrue;}boolF(){if(a=='('){advance();if(E()){if(a==')'){advance();returntrue;}}}elseif(a=='i'){advance();returntrue;}returnfalse;}boolA(){if(a=='+'){advance();returntrue;}elseif(a=='-'){advance();retu
本文标题:编译原理实验代码
链接地址:https://www.777doc.com/doc-4254593 .html