⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpl.cpp

📁 利用C++语言实现DOS界面下C++源程序的编译分析功能.
💻 CPP
字号:
#include <fstream>
#include <string>
#include <iostream>
#include <iomanip>
using namespace std;
int isKey(char *str);
int KeyNum=77;
int StrNum=55;
	ifstream inf;
    ofstream outf;
	char *Buffer=new char[10000];//存放一行代码
/****************************保留字集合***************************/
char* keyword[]={"_asm", "auto", "bad_cast", "bad_typeid","bool",
                "break" ,"case" ,"catch","char","class",
                "const","const_cast","continue" ,"default" ,"delete" ,
                 "do","double", "dynamic_cast", "else", "enum" ,
				 "except" ,"explicit" ,"extern" ,"false","finally", 
				 "float", "for", "friend","goto", "if", 
				 "include","inline", "int","long", "mutable",
				 "namespace", "new","operator", "private", "protected",
				 "public","register", "reinterpret_cast", "return", "short",
				 "signed", "sizeof", "static", "static_cast","struct",
				 "switch", "template", "this","throw","true", 
				 "try", "type_info","typedef", "typeid", "typename", 
				 "union","unsigned", "using", "virtual","void",
				 "volatile", "while","main","iostream",
				 "outf","cin","cout","open","close","ifstream",
				 "ofstream","iostream.h"
};				 
char*Str[]={
"->", "::","++", "--", "!", "~",  "-", "+", "*", "&", "*", "/",
"%", "+",  "-", "<<", ">>", "<<=", ">>=","==",  "!=", "&", "|", "&&", 
"||",  "?:","=","+=","-=","*=","/=", "%=", "&=", "^=","|=",
"(",")","[","]","`","~","@","#","$","%","^",":",";","'","<",
">", ",", "?","{","}"
};//55个

/*****************************是不是标志符*******************************/
int identf(char *str)
{
 int flag=0;
 char *p=str;
 /*判断第一个字符是否符合*/
 if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_')
 {
  flag=1;
  str++;
 }
 /*从第二个字符开始遍历判断*/
 while(*str && flag)
 {
  if((*str>='a' && *str<='z') || (*str>='A' && *str<='Z') || *str=='_'
   || (*str>='0' && *str<='9'))
  {
   flag=1;
   str++;
  }
  else
   flag=0;
 }
 
 /*判断是不是关键字*/
 if(flag)
  flag=isKey(p);
 return flag;
}

/*****************************是不是数字字*******************************/
int isNum(char *str)
{
 int flag=0;
 char *p=str;
 /*判断第一个字符是否符合*/
 if(*str>='0' && *str<='9')
 {
  flag=1;
  str++;
 }
 /*从第二个字符开始遍历判断*/
 while(*str && flag)
 {
  if( *str=='.'|| (*str>='0' && *str<='9'))
  {
   flag=1;
   str++;
  }
  else
   flag=0;
 }
 
 /*判断是不是关键字*/
 if(flag)
  flag=isKey(p);
 return flag;
}

/*****************************是不是关键字*******************************/
int isKey(char *str)
{
 int i, flag=1;

 for(i=0; i<KeyNum; i++)
 {
  if(strcmp(str, keyword[i])==0)
  {
   flag=0;
   break;
  }
 }

 return flag;
}

/*****************************是不是运算符*******************************/
int isMath(char *str)
{
 int i, flag=1;

 for(i=0; i<StrNum; i++)
 {
  if(strcmp(str, Str[i])==0)
  {
   flag=0;
   break;
  }
 }
 return flag;
}

/*****************************主函数*******************************/


int main (int)
{   
    char c;
	int j;
    char *buf=new char[256];
    int i,n,nCount;
	inf.open("in.txt");
	outf.open("out.txt");
	inf >> noskipws; 
	i=0;
	
	/************************将每个;之间的代码保存下来****************/
    while(inf >>c)
    {
		Buffer[i++]=c;
		}
	n=i;
	for(i=0;i<n;i++)
		cout<<Buffer[i];
	cout<<endl<<endl;
	
	i=0;
	nCount=0;
		while(i<n)
		{

			 /******************************判断字符串**********************/
    if(Buffer[i]=='\"')
		  {
		 //j=0;
	         cout<<Buffer[i];
			 outf<<Buffer[i];
			while(Buffer[++i]!='\"')
			{
				if(i>n)break;
				else{
		        outf<<Buffer[i];
				cout<<Buffer[i];
			}
			}
		if(i<n)	
		{  
		   	outf<<Buffer[i];
		    cout<<Buffer[i];
		  // buf[++j]='\0';
			  
		   cout<<"  "<<"字符串"<<endl;
		   outf<<"  "<<"字符串"<<endl;
		}
		   }
	  /****************************空格处理**********************/
			if(Buffer[i]==' ')
			{
			i++;
			continue;
			}

	    /****************************换行处理**********************/
		 if(Buffer[i]=='\n')
			{
			i++;
			continue;
			}


			

     /******************************排出头文件情况**********************/
      	if(Buffer[i]=='.' && Buffer[i+1]=='h')
				{
				i=i+2;
				continue;
				}

			/******************************判断两种注释方式**********************/
			 if(Buffer[i]=='/' && Buffer[i+1]=='/')
			{
			while(Buffer[i]!='\n')
			i++;
			continue;
			}//单行情况

		else if(Buffer[i]=='/' && Buffer[i+1]=='*')
			{
			i=i+2;
			while(!(Buffer[i]=='*' && Buffer[i+1]=='/'))
				i++;
			i++;
			continue;
			}//多行情况
       else  
	   {     
  /******************************判断运算符号**********************/
           j=0;
		   int h=i;
			while(Buffer[i]=='!'
			||	(Buffer[i]>='#' &&  Buffer[i]<='/')  
			||  (Buffer[i]>=':' && Buffer[i]<='@') 
	    	||  (Buffer[i]>='[' && Buffer[i]<='`')  
			||  (Buffer[i]>='{' && Buffer[i]<='~') )
		{
			
				buf[j++]=Buffer[i++];
			} 
			
			buf[j]='\0';
	  if(!isMath(buf))
 {

	 for(int k=0;k<j;k++)
	 {
		 cout << buf[k];
		 outf<< buf[k];
	 }
	cout<<"  特殊符号"<<endl;
	outf<<"  特殊符号"<<endl;

		continue;
 }
	else if(isMath(buf))
	{
	for(int k=0;k<j;k++)
	 {
		if (buf[k]=='/' && buf[k+1]=='/')
		{
			i=i+k-j-1;//返回//之前的指针 
			break;
		}
		if(buf[k]!='.'
			|| j!=1 )
		{
	   	 cout << buf[k];
		 outf<< buf[k];	
		 cout<<"  特殊符号"<<endl;
	     outf<<"  特殊符号"<<endl;
		}
	 }
			}
	
/*****************************标志符处理**********************/
       j=0;
       while((Buffer[i]>='a' && Buffer[i]<='z')
			|| (Buffer[i]>='A' && Buffer[i]<='Z') 
			|| Buffer[i]=='_'
			|| Buffer[i]=='.'
            || (Buffer[i]>='0' && Buffer[i]<='9'))
 { 
             buf[j++]=Buffer[i++];
 }


/*****************************关键字处理**********************/
	  
      buf[j]='\0';
	 if(!isKey(buf))
 {
	 for(int k=0;k<j;k++)
	 {
		 cout << buf[k];
		 outf<< buf[k];
	 }
		
	    cout<<"  关键字"<<endl;
		outf<<"  关键字"<<endl;
 }


/****************************标志符处理**********************/
 else if(identf(buf))
 {
    for(int k=0;k<j;k++)
	{
		cout << buf[k];
		outf<< buf[k];
	}
        cout<<"  标识符"<<endl;  
		outf<<"  标识符"<<endl;
	
	}// else if

 /****************************数字处理**********************/
 else if(isNum(buf))
 {
    for(int k=0;k<j;k++)
	{
		cout << buf[k];
		outf<< buf[k];
	}
        cout<<"  数值"<<endl;  
		outf<<"  数值"<<endl;
	
	}// else if
  else{
	   i++;
	   }
 }//else

		}//while
		
    inf.close();
    outf.close();
    return 0;
}//main

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -