pl0词法分析器_第1页
pl0词法分析器_第2页
pl0词法分析器_第3页
pl0词法分析器_第4页
pl0词法分析器_第5页
已阅读5页,还剩1页未读 继续免费阅读

下载本文档

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

文档简介

1、#include #include #include char ch;FILE *fp;char Getchar2()ch=fgetc(fp);if(ch= |ch=10|ch=9)ch=fgetc(fp);return ch;char Getchar()ch=fgetc(fp);return ch;bool IsLetter()/判断ch是否为字母if(ch=a | ch=A)return true;elsereturn false;bool IsDigit()/判断ch是否为数字if(ch=0)return true;else return false;void Concat(char s

2、trToken30)/将ch中的字符加入strToken中int i=0;while(strTokeni!=0)i+;strTokeni=ch;strTokeni+1=0;int Reserve(char str)if(strcmp(str,begin)=0)return 1;else if(strcmp(str,call)=0)return 2;else if(strcmp(str,const)=0)return 3;else if(strcmp(str,do)=0)return 4;else if(strcmp(str,end)=0)return 5;else if(strcmp(str,

3、if)=0)return 6;else if(strcmp(str,odd)=0)return 7;else if(strcmp(str,procedure)=0)return 8;else if(strcmp(str,read)=0)return 9;else if(strcmp(str,program)=0)return 10;else if(strcmp(str,var)=0)return 11;else if(strcmp(str,while)=0)return 12;else if(strcmp(str,write)=0)return 13;else if(strcmp(str,th

4、an)=0)return 14;else return 0;int InsertConst(char str)int i,value=0;for(i=0;stri!=0;i+)value=10*value+stri-0;return value;void main()int code , value;char id30=;char strToken30;if(fp=fopen(1.txt,r)=NULL)printf(文件打开失败!n);exit(0);ch=Getchar2();/读取一个非空字符while(ch!=EOF)strcpy(strToken,id);if(IsLetter()w

5、hile( IsLetter() | IsDigit() )Concat(strToken);ch=Getchar();code=Reserve(strToken);/保留字返回它代码,否则返回0值if(code=0)printf(n,strToken);elseswitch(code)case 1:printf(n);break;case 2:printf(n);break;case 3:printf(n);break;case 4:printf(n);break;case 5:printf(n);break;case 6:printf(n);break;case 7:printf(n);b

6、reak;case 8:printf(n);break;case 9:printf(n);break;case 10:printf(n);break;case 11:printf(n);break;case 12:printf(n);break;case 13:printf(n);break;case 14:printf(n);break;else if(IsDigit()while(IsDigit()Concat(strToken);ch=Getchar();value=InsertConst(strToken);printf(n,value);elseif(ch=*)ch=Getchar(

7、);if(ch=*)printf(n);else printf(n);else if(ch=)printf(-n);/ ch=Getchar();else if(ch=)printf(n);/ =ch=Getchar();elseprintf(n);/ )ch=Getchar();if(ch=)printf(=-n);/=ch=Getchar();elseprintf(-n);/else if(ch=:)ch=Getchar();if(ch=)printf(n);/=ch=Getchar();elseprintf(n);/else if(ch=+)printf(n); else if(ch=)printf(n);else if(ch=-)printf(n);else if(ch=/)printf(n);else if(ch=;)printf(n);else if(ch=,)printf(n);else if(ch=.)printf(n);else if(ch=()printf(n);else if(ch=)prin

温馨提示

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

评论

0/150

提交评论