C++-代码解析(词法分析、语法分析)

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

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

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

资源描述

C++代码解析(词法分析、语法分析)很久以前就想写个C++的类解析器了,终于花了一晚上时间,写了一个C++头文件和CPP文件的解析。其实跟我上篇“编译原理”的文章说的一样,词法分析和语法分析最基本就是一堆if-else或者switch-case。不要被什么有穷自动机吓倒了。。。下面的代码是一个基本的词法和语法分析,能工作,但现在这个实验品,肯定不会很完善,请各位看官见谅。能解析出来所包含的头文件、类名(输入.h文件可以解析出类名)、函数名等,需要的功能自己再加吧。程序我直接一个main函数写下来的,完全C风格,不用跟我讲什么分函数、分类之类的话,我也知道,后期我在代码上会分好,不过不一定贴上来了,要分你可以自己分一下,如果能共享出来给我学习一下,那更感谢了。我这个程序就是让你看看一个词法分析和一个语法分析的最简单的写法,同时也实现一些有点用的功能。下面就是这个程序代码,能直接拷贝编译运行的。代码也有注释,逻辑比较清楚了。//CodeParser.cpp:Definestheentrypointfortheconsoleapplication.//#includestdafx.h#includestdio.h#includetchar.h#includestdlib.h#includeconio.h#includevector#includestring#includemapusingnamespacestd;//符号表enumeType{Symbol};mapstring,eTypeg_mapSymbol;enumeCharFlag{noFlag,explain};eCharFlagg_charFlag=noFlag;int_tmain(intargc,_TCHAR*argv[]){//输入常用标识符g_mapSymbol[if]=Symbol;//获取输入inti=0,j=0,k=0;charoneChar;charfname[200];memset(fname,0,200);printf(请输入要打开的文件名:\n);scanf(%s,fname);//for测试//strcpy(fname,d:\\1.cpp);//打开文件FILE*fp;if((fp=fopen(fname,r))==NULL){printf(Cannotopeninfile.\n);exit(0);}//int0i;//分析文件oneChar=fgetc(fp);charoneWord[256]={0};intnBuf=0;vectorstringvecWords;while(oneChar!=EOF){if(nBuf==0){if(oneChar==''||oneChar=='\n'||oneChar=='\t'){}elseif(oneChar=='('||oneChar=='{'){oneWord[nBuf++]=oneChar;stringstrWord=oneWord;vecWords.push_back(strWord);nBuf=0;g_charFlag=noFlag;memset(oneWord,0,256);//continue;}elseif(oneChar==')'||oneChar=='}'){oneWord[nBuf++]=oneChar;stringstrWord=oneWord;vecWords.push_back(strWord);nBuf=0;g_charFlag=noFlag;memset(oneWord,0,256);//continue;}elseif(oneChar='0'&&oneChar='9'){/*printf(error,不可以数字打头);return-1;*/oneWord[nBuf++]=oneChar;}elseif(oneChar='a'&&oneChar='z'){oneWord[nBuf++]=oneChar;}elseif(oneChar='A'&&oneChar='Z'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='#'){oneWord[nBuf++]=oneChar;}elseif(oneChar==':'){//界限符号oneWord[nBuf++]=oneChar;}elseif(oneChar=='/'){//注释的开始,找第二根横线//或者是路径描述oneWord[nBuf++]=oneChar;g_charFlag=explain;}elseif(oneChar=='('){//函数的开始,找第二个括号oneWord[nBuf++]=oneChar;}elseif(oneChar==')'){//函数的结束oneWord[nBuf++]=oneChar;}elseif(oneChar=='{'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='}'){oneWord[nBuf++]=oneChar;}elseif(oneChar==';'){//语句结束符oneWord[nBuf++]=oneChar;}elseif(oneChar=='~'){//析构函数符oneWord[nBuf++]=oneChar;}elseif(oneChar=='\'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='='){oneWord[nBuf++]=oneChar;}elseif(oneChar=='_'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='?'){oneWord[nBuf++]=oneChar;}else{//printf(未处理情况:%c,oneChar);oneWord[nBuf++]=oneChar;}}else{if(oneChar==''||oneChar=='\t'||oneChar=='\n'){if(g_charFlag!=explain){stringstrWord=oneWord;vecWords.push_back(strWord);nBuf=0;g_charFlag=noFlag;memset(oneWord,0,256);}elseif(g_charFlag==explain&&oneChar=='\n'){//如果是注释状态,则碰到回车才截断stringstrWord=oneWord;vecWords.push_back(strWord);nBuf=0;g_charFlag=noFlag;memset(oneWord,0,256);}}elseif(oneChar=='('||oneChar=='{'){//oneWord[nBuf++]=oneChar;stringstrWord=oneWord;vecWords.push_back(strWord);nBuf=0;g_charFlag=noFlag;memset(oneWord,0,256);continue;}elseif(oneChar==')'||oneChar=='}'){//oneWord[nBuf++]=oneChar;stringstrWord=oneWord;vecWords.push_back(strWord);nBuf=0;g_charFlag=noFlag;memset(oneWord,0,256);continue;}//elseif(oneChar=='\n')//{////行末,处理一些情况,例如注释等//}elseif(oneChar='0'&&oneChar='9'){oneWord[nBuf++]=oneChar;}elseif(oneChar='a'&&oneChar='z'){oneWord[nBuf++]=oneChar;}elseif(oneChar='A'&&oneChar='Z'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='_'){oneWord[nBuf++]=oneChar;}elseif(oneChar==':'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='('){//函数的开始,找第二个括号oneWord[nBuf++]=oneChar;}elseif(oneChar==')'){//函数的结束oneWord[nBuf++]=oneChar;}elseif(oneChar=='{'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='}'){oneWord[nBuf++]=oneChar;}elseif(oneChar==';'){//语句结束符oneWord[nBuf++]=oneChar;}elseif(oneChar=='~'){//析构函数符oneWord[nBuf++]=oneChar;}elseif(oneChar=='\'){oneWord[nBuf++]=oneChar;}elseif(oneChar=='*'){//定义指针oneWord[nBuf++]=oneChar;}elseif(oneChar=='.'){//.honeWord[nBuf++]=oneChar;}elseif(oneChar=='='){oneWord[nBuf++]=oneChar;}elseif(oneChar=='/'){//如果是注释状态,等待一行结束//if(g_charFlag==explain){//oneWord[nBuf++]=oneChar;//stringstrWord=oneWord;//vecWords.push_back(strWord);//nBuf=0;////g_charFlag=noFlag;//memset(oneWord,0,256);//oneWord[nBuf++]=oneChar;}//else{oneWord[nBuf++]=oneChar;}}elseif(oneChar==','){oneWord[nBuf++]=oneChar;}elseif(oneChar=='?'){oneWord[nBuf++]=oneChar;}else{oneWord[nBuf++]=oneChar;//printf(未处理情况:%c,oneChar);//stringstrWord=未处理情况:%s;//printf(error,未处理情况.);//return-1;}}oneChar=fgetc(fp);}fclose(fp);//写入文件FILE*fpIn;if((fpIn=fopen(d:\\words.txt,w+))==NULL){printf(Cannotopeninfile.\n);exit(0);}for(inti=0;ivecWords.size();i++){charszBuf[300];memset(szBuf,0,300);sprintf(szBuf,%d.%s\n,i,vecWords[i].c_str());fwrite(szBuf,strlen(szBuf),1,fpIn);//printf(%d.%s\n,i,vecWords[i].c_str());}fclose(fpIn);//语法解析//首先要对括号进行匹配,验证语法等vectorstringvecClass;//解析出类vectorstringvecHeader;//解析出头文件typedefstruct{stringfuncName;stringparamList;}stFunc;vectorstFuncvecFunc;//解析出函数for(inti=0;ivecWords.size();i++){if(vecWords[i]==#include){//语法分析会随

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

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

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

×
保存成功