词法分析器的设计与实现

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

《编译原理》课程实验报告实验题目:某种简单程序语言的词法分析器的设计与实现专业:计算机科学与技术班级:11060341学号:11060341姓名:实验目的:设计一个词法分析程序,理解词法分析器实现的原理,掌握程序设计语言中的各类单词的词法分析方法,加深对词法分析原理的理解。实验任务:词法分析是从左向右扫描每行源程序的符号,拼成单词,换成统一的二元式(单词种别,单词符号的属性值)表示。对给定的程序通过词法分析器识别一个个单词符号,并以二元式(单词种别,单词符号的属性值)显示,本程序则是通过对给定程序段分析后以单词符号和文字提示显示)实验流程:程序清单:#includeiostream#includecstdio#includecstringusingnamespacestd;intk=0;structword{charname[10];intkind;}word[1000];charkey[35][10]={scanf,short,int,long,float,double,char,struct,union,printf,typedef,const,unsigned,signed,extern,register,static,volatile,void,if,else,switch,case,for,do,while,goto,continue,break,default,sizeof,return,include,bool};boolcmp(chara[]){inti;for(intk=0;k35;k++){if(strcmp(a,key[k])==0)return1;}return0;}intmain(){#ifdefLOCALfreopen(in.txt,r,stdin);freopen(out.txt,w,stdout);#endifintp,q,flag;chara[1000],b[10],ch;while(gets(a)){p=0;intlen=strlen(a);while(plen){ch=a[p];memset(b,0,sizeof(b));while(ch==''){p++;ch=a[p];}if((ch='a'&&ch='z')||(ch='A'&&ch='Z')||ch=='_'){flag=0;q=0;while((ch='a'&&ch='z')||(ch='A'&&ch='Z')||ch=='_'||(ch='0'&&ch='9')){if((ch='0'&&ch='9')||ch=='_')flag=1;b[q++]=ch;p++;ch=a[p];}if(flag==1){strcpy(word[k].name,b);word[k++].kind=1;}elseif(flag==0){if(ch=='\''||ch==''){strcpy(word[k].name,b);word[k++].kind=2;}elseif(cmp(b)==1){strcpy(word[k].name,b);word[k++].kind=3;}else{strcpy(word[k].name,b);word[k++].kind=1;}}}elseif((ch='0'&&ch='9')||ch=='-'){intt=p-1;if(a[t]='0'&&a[t]='9'||a[t]='a'&&a[t]='z'||a[t]='A'&&a[t]='Z'){p++;ch=a[p];if(ch=='-'||ch=='='){b[0]='-';b[1]=ch;strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='-';strcpy(word[k].name,b);word[k++].kind=5;}}else{q=0;b[q++]=ch;p++;ch=a[p];while((ch='0'&&ch='9')||ch=='.'){b[q++]=ch;p++;ch=a[p];}strcpy(word[k].name,b);word[k++].kind=2;}}elseif(ch=='('||ch==')'||ch=='['||ch==']'||ch=='{'||ch=='}'||ch==','||ch==';'||ch==':'||ch=='\''||ch=='')//ch=='('||ch==')'||ch=='['||ch==']'||ch=='{'||ch=='}'||{b[0]=ch;strcpy(word[k].name,b);word[k++].kind=4;ch=a[++p];}elseif(ch=='%'||ch=='^'){b[0]=ch;strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}elseif(ch=='+'){p++;ch=a[p];if(ch=='+'||ch=='='){b[0]='+';b[1]=ch;strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='+';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch=='*'){p++;ch=a[p];if(ch=='*'||ch=='='){b[0]='*';b[1]=ch;strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='*';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch=='/'){p++;ch=a[p];if(ch=='/'||ch=='='){b[0]='/';b[1]=ch;strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='/';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch=='='){p++;ch=a[p];if(ch=='='){b[0]=b[1]='=';strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='=';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch==''){p++;ch=a[p];if(ch==''||ch=='='){b[0]='';b[1]=ch;strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch==''){p++;ch=a[p];if(ch==''||ch=='='){b[0]='';b[1]=ch;strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch=='!'){p++;ch=a[p];if(ch=='='){b[0]='!';b[1]='=';strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='!';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch=='&'){p++;ch=a[p];if(ch=='&'){b[0]=b[1]='&';strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='&';strcpy(word[k].name,b);word[k++].kind=5;}}elseif(ch=='|'){p++;ch=a[p];if(ch=='|'){b[0]=b[1]='|';strcpy(word[k].name,b);word[k++].kind=5;ch=a[++p];}else{b[0]='|';strcpy(word[k].name,b);word[k++].kind=5;}}}}for(inti=0;ik;i++){switch(word[i].kind){case1:{printf((标识符,);break;}case2:{printf((常量,);break;}case3:{printf((关键字,);break;}case4:{printf((界符,);break;}case5:{printf((运算符,);break;}}printf(%s)\n,word[i].name);}inta1=0,a2=0,a3=0,a4=0,a5=0;for(inti=0;ik;i++){if(word[i].kind==1)a1++;elseif(word[i].kind==2)a2++;elseif(word[i].kind==3)a3++;elseif(word[i].kind==4)a4++;elseif(word[i].kind==5)a5++;}printf(标识符:%d\n常量:%d\n关键字:%d\n界符:%d\n运算符:%d\n,a1,a2,a3,a4,a5);return0;}运行结果:

1 / 12
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功