编译原理作业.doc_第1页
编译原理作业.doc_第2页
编译原理作业.doc_第3页
编译原理作业.doc_第4页
编译原理作业.doc_第5页
已阅读5页,还剩9页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

/*识别 分界符,运算符,赋值运算符,增减运算符,逻辑与逻辑或,关系运算符,科学计数法,实型常数,整形常数,有符号数,字符串常量,关键字,标示符,输入输出流符号*/#include#include#include #includeusing namespace std;string keyword15=int,include,using,namespace,return,void,char,float,double,unsigned,string,break,continue,do,while;/*关键字标示符*/string instruction3=/,/*,*/;/*注释行符号*/分界符 char boundary9=,;,(,),#;/运算符char operation5=+,-,*,/,%;/赋值运算符char equals=; /增减运算符 string in_decrease2=+,-; /逻辑与逻辑或/string and_or2=&,|;/关系运算符 /string relation6=,=,=,=,!=;int i_point=1;/void analy_boundary(char A);void analy_operation(char A);void analy_string(char answer);void analy_keyword(char answer);void enter_p(int i_pt);void analy(FILE *fp);/void main()/主函数FILE *fp0;fp0=fopen(f:/program.txt,r+);if(fp0=NULL)printf(打开文件错误!n); exit(0);analy(fp0);return;/void analy_boundary(char A)/分析分界符的函数if(A!=EOF)if(A=,|A=;|A=|A=|A=|A=|A=(|A=)|A=#)for(int b=0;b9;b+)/char boundary11=,;,(,),#;if(A=boundaryb)cout【 分界符 A】endl; break;/void analy_operation(char A)/分析运算符的函数int d=0;if(A!=EOF)if(A=+|A=-|A=*|A=/|A=%)/char operation5=+,-,*,/,%while(d5)/char operation5=+,-,*,/,%;if(A=operationd)cout【 运算符 A】endl; break;d+;/void analy_string(char answer)/识别字符串常量cout【 符号串常量 answer】endl;/void analy_keyword(char answer)/识别关键字和标示符for(int l=0;l15;l+)if(answer=keywordl)cout【 关键字 answer】endl;break; for(int m=0;m15;m+)if(answer!=keywordl)cout【 标示符 answer】endl;break;/void enter_p(int i_pt)/分屏显示操作if(i_pt % 21 = 0) cout,=,=,=,!=;char answer_2100;/识别注释行char ch,ch0,ch1,ch2,ch3,ch4,ch5,ch6;int k=0,k1=0,k2=0,k3=0,k4=0,k5=0,k6=0;ch=fgetc(fp);/fgetc()读字符函数,如果遇到文件结束符号,函数返回文件结束标志EOFwhile(ch!=EOF)if(ch=|ch=!)/关系运算符couti_point+ : ;ch3=ch;ch=fgetc(fp); if(ch=)/判断 = , = ,!= 符号ch4=ch; cout【 关系运算符 ch3ch4】endl;ch3=ch4=0;ch=fgetc(fp);enter_p(i_point);continue;else if(ch=ch3)cout【 输入输出流符 ch3ch】 , 符号cout【 关系运算符 ch3】endl;ch3=ch4=0; enter_p(i_point);continue;if(ch=)/判断 = 和 = 符号couti_point+ : ;ch5=ch;ch=fgetc(fp);if(ch=ch5)cout【 关系运算符 ch5ch】endl;ch5=0;ch=fgetc(fp);enter_p(i_point);continue;elsecout【 赋值运算符 ch5】endl;ch5=0;enter_p(i_point);continue;if(ch=,|ch=;|ch=|ch=|ch=|ch=|ch=(|ch=)|ch=#)/判断分界符couti_point+ : ;analy_boundary(ch);enter_p(i_point); if(ch=*|ch=/|ch=%)/ ch=+|ch=-|/判断算术运算符ch6=ch;ch=fgetc(fp);if(ch6=/)if(ch=/)/判断 / 注释couti_point+ : ;answer_2k5+=ch6;answer_2k5+=ch;ch=fgetc(fp);while(ch!=n)answer_2k5+=ch;ch=fgetc(fp);answer_2k5=0;cout【 注释语句 , answer_2】endl;k5=0;ch6=0;enter_p(i_point);continue;else if(ch=*)/判断 /*.*/ 注释couti_point+ : ;answer_2k5+=ch6;answer_2k5+=ch;ch=fgetc(fp);while(ch!=/)answer_2k5+=ch;ch=fgetc(fp);answer_2k5+=ch;answer_2k5=0; cout【 注释语句 , answer_2】endl;k5=0;ch6=0;enter_p(i_point);continue; elsecouti_point+ : ; analy_operation(ch6); enter_p(i_point);ch6=0;continue;if(ch=+|ch=-)/判断算术运算符或增减运算符couti_point+=0&ch=0&ch=9|ch=.|ch=-|ch=e|ch= );numberk6=0;cout【 有符号数 ch0number】endl;enter_p(i_point);ch0=0;k6=0;continue;elseanaly_operation(ch0);ch0=0;k6=0;enter_p(i_point);continue;elsecout【 增减运算符 ch0ch】endl;ch0=0; ch=fgetc(fp);enter_p(i_point);continue;if(ch=&|ch=|)/判断逻辑与逻辑或couti_point+ : ; ch1=ch;ch=fgetc(fp);ch2=ch;cout【 逻辑与逻辑或 ch1ch2】=0&ch=9)numberk+=ch;ch=fgetc(fp);numberk=0;k=0;cout【 实型常数 number】=0&ch=9)/|ch=./判断整形常数和实型常数couti_point+=0&ch=0&ch=9)numberk+=ch;ch=fgetc(fp);numberk=0;k=0;cout【 实型常数 number】=0& ch=9|ch=e|ch=E|ch=-|ch= );numberk=0;k=0;cout【 科学计数法 number】endl;enter_p(i_point);continue;else/整型常数/整型常数numberk=0; k=0; cout【 整形常数 number】endl;enter_p(i_point); continue;if(ch=)/识别字符串couti_point+=a&ch=A&ch=Z)|ch=_)/判断关键字和标示符couti_point+=a&ch=A&ch=0&ch,=,=,=,!=,&,|,!,|,&,=,+=,-=,*=,/=,%=,=,.,(,),:,#,;,;char ch;int i,k;char buff20;char buff11;void output(int ,char*);void zhuyao(FILE *fp)int j;ch=fgetc(fp);while(ch= |ch=n|ch=t)ch=fgetc(fp);if(ch=a&ch=A&ch=a&ch=A&ch=0&ch=9)buffi=ch;i+;ch=fgetc(fp); /从流中读取字符 buffi=0; /*查找保留字*/fseek(fp,-1,1);j=0;while(j=keyword)output(2,buff);elseoutput(1,buff);else if(ch=0&ch=0&ch=0&ch=0&ch0) /*分隔符号*/buff0=ch; ch=fgetc(fp); buff1=0; fseek(fp,-1,1); k=0; if(k0) /*运算符号*/buff0=ch;ch=fgetc(fp); if(buff0=/&ch=*)while(ch!=/)ch=fgetc(fp);else if(buff0=/&ch=/)while(ch!=n)ch=fgetc(fp); elsei=1;while(strchr(yun,ch)0)buffi=ch;i+;ch=fgetc(fp);buffi=0;fseek(fp,-1,1);k=0;while(kfuhao)&strcmp(buff,bk)k+;if(k=0;-i)data+=(ai-0)*t;t*=10;if(data=0)cout0=0;-i)coutci;coutendl;*/void output(int n,char *m)switch(n)case 1:cout关键字:mendl;break;case 2:cout标识符:mendl;break;case 3:cout分界符:mendl;break;case 4:cout运算符:mendl;break;case 5:cout常数:mendl;break;default:cout错误:mendl;break;void main() /*主函数*/FILE *fp; char filename20,sh;printf(Type the file name which you want to open:);scanf(%s,&filename);fp=fopen(filename,r); /*以r-只读方式打开

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论