华中师范大学CENTRALCHINANORMALUNIVERSITY题目:语法分析院系:计算机科学与技术系班级:0701姓名:王辉学号:2007210514指导老师:王明安一.实验题目语法分析二.实验目的编制一个递归下降分析程序,实现对词法分析程序所提供的单词序列的语法检查和结构分析。三.实验内容用C语言编制递归下降分析程,并对简单语言进行语法分析。四.实验步骤1.设计语法分析程序的算法。2.画流程图。3.编写程序。4.测试并调试程序。5.写实验报告。五.实验设计思想是否标识符?调用scaner是否:=?调用scaner调用expression函数出错处理否否是是调用term函数是否+,--?调用scaner调用term函数出错处理否是expression表达式分析函数示意图statement语句分析函数示意图是否begin?调用scaner调用语句串分析函数是否end?Syn=0&&kk=0?调用scaner是是否是否否调用statement函数是否;?调用scaner调用statement函数出错处理是否是打印分析成功出错处理递归下降分析程序示意图语句串分析示意图六.实现代码#includestdio.h#includestdlib.h#includestring.hcharstr[50][10];charword[6][10]={begin,if,then,while,do,end};charpro[80],token[10];intsyn,num,kk;voidscaner();voidlrparser();voidyuxu();voidstatement();voidexpression();voidterm();voidfactor();voidreadfile();voidreadbroad();voidmain(){charch;intTag=1;while(Tag){num=0;printf(1键盘读取2文件读取:);scanf(%c,&ch);if(ch=='1')readbroad();if(ch=='2'){readfile();}printf(\n是否继续输入1:);scanf(%d,&Tag);getchar();}}voidreadfile()//从文件中读取{FILE*fp;charch;inti=0;fp=fopen(date.txt,rt);if(fp==NULL){printf(thememoryisnotenough);exit(0);}while(!feof(fp)){memset(pro,NULL,80);num=0;do{fscanf(fp,%c,&ch);pro[num++]=ch;}while((ch!='\n')&&(!feof(fp)));printf(\n%d.文件中的符号为:%s,++i,pro);num=0;kk=0;scaner();lrparser();}fclose(fp);}voidreadbroad()//从键盘中读取{FILE*fp;charch;memset(pro,NULL,80);ch=getchar();printf(inputthestring:);do{ch=getchar();pro[num++]=ch;}while(ch!='#');fp=fopen(date.txt,a++);if(fp==NULL){printf(thememoryisnotenough);exit(0);}num=0;do{ch=pro[num++];fprintf(fp,%c,ch);}while(ch!='#');fprintf(fp,\n);fclose(fp);num=0;kk=0;scaner();lrparser();}voidlrparser(){if(syn==1){{scaner();yuxu();}if(syn==6){scaner();if(syn==0&&(kk==0))printf(success!);else{printf(语句串错误!);kk=1;}}elseif((kk!=1)){printf(lackend);kk=1;}}else{printf(lackbegin!);}return;}voidyuxu(){statement();while(syn==26){scaner();statement();}return;}voidstatement(){if(syn==10){scaner();if(syn==18){scaner();expression();}else{printf(赋值错误);kk=1;}}else{printf(语句错误!);kk=1;}}voidexpression(){term();while(syn==13||syn==14){scaner();term();}return;}voidterm(){factor();while(syn==15||syn==16){scaner();factor();}return;}voidfactor(){if(syn==10||syn==11){scaner();}elseif(syn==27){scaner();expression();if(syn==28)scaner();else{printf(错误')');kk=1;}}else{printf(表达式错误1);kk=1;}return;}voidscaner(){inti=0;charch;for(i=0;i10;i++)token[i]=NULL;ch=pro[num++];while(ch==''){ch=pro[num++];}i=0;if('a'=ch&&ch='z'){while('a'=ch&&ch='z'||'0'=ch&&ch='9'){token[i++]=ch;ch=pro[num++];}token[i++]='\0';num--;syn=10;for(i=0;i10;i++)if(strcmp(token,word[i])==0){syn=i+1;break;}}elseif('0'=ch&&ch='9'){while('0'=ch&&ch='9'){token[i++]=ch;ch=pro[num++];}token[i]='\0';num--;syn=11;}elseswitch(ch){case'':i=0;token[i++]=ch;ch=pro[num++];if(ch==''){token[i++]=ch;syn=21;}elseif(ch=='='){token[i++]=ch;syn=22;}else{syn=20;num--;syn=20;}break;case'':i=0;token[i++]=ch;ch=pro[num++];if(ch=='='){token[i++]=ch;syn=24;}else{num--;syn=23;}break;case':':token[i++]=ch;ch=pro[num++];if(ch=='='){token[i++]=ch;syn=18;}else{num--;syn=17;}break;case'+':token[i++]=ch;syn=13;break;case';':token[i++]=ch;syn=26;break;case'-':token[i++]=ch;syn=14;break;case'*':token[i++]=ch;syn=15;break;case'/':token[i++]=ch;syn=16;break;case'(':token[i++]=ch;syn=27;break;case')':token[i++]=ch;syn=28;break;case'#':token[i++]=ch;syn=0;break;default:syn=-1;break;}}程序运行结果:七.小结八.参考文献编译原理教材后面的程序模板