资源描述:
《c语言词法分析研究器》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、个人收集整理仅供参考学习实验任务6:编写词法分析程序1.实验目地:体会词法分析程序地工作原理和功能2.实验内容:编写一个词法分析程序,实现地功能是:输入一个C语言程序,经该词法分析程序处理后,输出单词记号序列.b5E2RGbCAP3.实验要求:提交程序清单(包含注释),提交输入一个C语言程序用词法分析程序处理后地运行结果截图以及运行环境配置说明.p1EanqFDPw4.验收时间:第12周(1)main主程序图处理每个字符开始结束是否结束判断并输出每个单词类型属性.Scan()函数,分析并返回每个单词地属性Run=1?初始化数组Program[],
2、words[]提示输入程序字符串输入并读取程序字符初始化是DXDiTa9E3d(2)Scan()函数流程图9/9个人收集整理仅供参考学习初始化数组,标志符判断并标志是否为关键字判断并标志为运算符或标点符号判断并标志为其它类型Return返回sign数值RTCrpUDGiT9/9个人收集整理仅供参考学习五,原程序代码#include#includeinti,j,k,sign,number,flag,run,sum,autuor;/*flagwhichisusetojudgethestringiskeywords
3、ornot!*/5PCzVD7HxAcharch;charwords[10]={""};charprogram[500];intScan(charprogram[]){char*keywords[39]={"auto","default","do","extern","goto","int","short","static","struct","switch","typedef","void","break","case","char","const","continue","double","else","enum","float","for"
4、,"if","long","register","return","signed","zhouhong","sizeof","union","unsigned","volatile","while","main","printf","scanf","include","define","string"};number=0;flag=0;j=0;ch=program[i++];/*Tohandlethelettlespaceandstab*/while((ch=='')
5、
6、(ch=='t')
7、
8、(ch=='r')
9、
10、(ch==''))jLB
11、HrnAILgch=program[i++];/*跳过不理睬*/xHAQX74J0X/*handleletters*/if((ch>='a')&&(ch<='z')){while((ch>='a')&&(ch<='z')){words[j++]=ch;ch=program[i++];}i--;words[j++]=' ';for(k=0;k<39;k++)if(strcmp(words,keywords[k])==0){sign=k+1;flag=1;if(k==27)autuor=1;break;}if(flag==0){sign=100;/
12、*为关键字*/}}/*if是关键字*//*handledigits*/elseif((ch>='0')&&(ch<='9')){number=0;while((ch>='0')&&(ch<='9')){number=number*10+(ch-'0');ch=program[i++];}sign=200;/*为数字*/i--;}/*opereationandedgehandle*/elseswitch(ch){case'=':{if(ch=='=')words[j++]=ch;words[j]=' ';ch=program[i++];if(ch
13、=='='){words[j++]=ch;words[j]=' ';sign=401;/*==*/}else{i--;/*退回一个字符*/sign=402;/*=*/}break;}case'>':{if(ch=='>')words[j++]=ch;words[j]=' ';ch=program[i++];if(ch=='='){words[j++]=ch;words[j]=' ';sign=403;/*>=*/}elseif(ch=='>')9/9个人收集整理仅供参考学习{words[j++]=ch;words[j]=' ';sign=
14、419;/*>>*/}else{i--;sign=404;/*>*/}break;}case'<':{if(ch=='<')words[