编译原理词法分析程序.doc_第1页
编译原理词法分析程序.doc_第2页
编译原理词法分析程序.doc_第3页
编译原理词法分析程序.doc_第4页
编译原理词法分析程序.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

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

文档简介

#include #include #include #include #include using namespace std;ifstream fp(source.txt,ios:in);char cbuffer;char *key13=if,else,for,while,do,return,break,continue,int,void,main,const,printf; /关键字char *border7= , , ; , , , ( , ) ,/; /分界符char *arithmetic6=+ , - , * , / , + , -; /运算符char *relation7= , , = , = ,!=; /关系运算符 char *lableconst80; /标识符int constnum=40;int lableconstnum=0; int linenum=1; /统计常数和标识符数量 int search(char searchchar,int wordtype) int i=0,t=0; switch (wordtype) case 1: for (i=0;i=12;i+) /关键字 if (strcmp(keyi,searchchar)=0) return(i+1); return(0); case 2: for (i=0;i=6;i+) /分界符 if (strcmp(borderi,searchchar)=0) return(i+1); return(0); case 3: for (i=0;i=5;i+) /运算符 if (strcmp(arithmetici,searchchar)=0) return(i+1); return(0); case 4: for (i=0;i=6;i+) /关系运算符 if (strcmp(relationi,searchchar)=0) return(i+1); return(0); case 5: for (t=40;t=constnum;t+) /常数 if (strcmp(searchchar,lableconstt)=0)/判断该常数是否已出现过 return(t+1); lableconstt-1=(char *)malloc(sizeof(searchchar);/为新的元素分配内存空间 strcpy(lableconstt-1,searchchar);/为数组赋值lableconst指针数组名 constnum+; /常数个数自加 return(t); case 6: for (i=0;i=lableconstnum;i+) if (strcmp(searchchar,lableconsti)=0) /判断标识符是否已出现过 return(i+1); lableconsti-1=(char *)malloc(sizeof(searchchar); strcpy(lableconsti-1,searchchar); lableconstnum+; /标识符个数自加 return(i); default:cout错误!; char alphaprocess(char buffer) /字符处理过程 int atype; int i=-1; char alphatp20; while (isalpha(buffer)|(isdigit(buffer) /这两个函数分别是判字符和判数字函数位于ctype.h中 alphatp+i=buffer; fp.get(buffer); alphatpi+1=0;/在末尾添加字符串结束标志 if (atype=search(alphatp,1) coutlinenum: linenum String= alphatpttt关键字endl; else atype=search(alphatp,6); /标识符 coutlinenum: linenum String= alphatpttt标识符endl; return(buffer);char digitprocess(char buffer) /数字处理过程 int i=-1; char digittp20; int dtype; while (isdigit(buffer) digittp+i=buffer; fp.get(buffer); digittpi+1=0; dtype=search(digittp,5); coutlinenum: linenum String= digittpttt数据endl; return(buffer);char otherprocess(char buffer) /分界符、运算符、逻辑运算符、等 int i=-1; char othertp20; int otype,otypetp; othertp0=buffer; othertp1=0; if (otype=search(othertp,3) fp.get(buffer); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,3) /判断该运算符是否是 /由连续的两个字符组成的 coutlinenum: linenum String= othertpttt运算符endl; fp.get(buffer); goto out; else /单字符逻辑运算符 othertp1=0; coutlinenum: linenum String= othertpttt逻辑运算符endl; goto out; if (otype=search(othertp,4) /关系运算符 fp.get(buffer); othertp1=buffer; othertp2=0; if (otypetp=search(othertp,4) /判断该关系运算符是否是 /由连续的两个字符组成的 coutlinenum: linenum String= othertpttt关系运算符endl; fp.get(buffer); goto out; else /单字符逻辑运算符 othertp1=0; coutlinenum: linenum String= othertpttt逻辑运算endl; goto out; if (buffer=!) /=的判断 fp.get(buffer); if (buffer=) /cout!= (2,2)n; fp.get(buffer); goto out; else if (otype=search(othertp,2) /分界符 coutlinenum: linenum String= othertpttt分界符endl; fp.get(buffer); goto out; if (buffer!=n)&(buffer!= ) cout错误!,字符非法tttbufferendl; fp.get(buffer); out: return(buffer); void main() int i; for (i=0;i=50;i+) lableconsti= ;/用于保存标识符 if (!fp) cout文件打开错误!endl; else fp.get (cbuffer); while (!fp.eof() if(cbuffer=n) linenum+; fp.get(cbuffer); else if (isalpha(cbuffer) cbuffer=alphaprocess(cbuffer); else if (isdigit(cbuffer) cbuffer=digi

温馨提示

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

评论

0/150

提交评论