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

📄 wa.cpp

📁 一个词法分析程序
💻 CPP
字号:
#include<ctype.h>
#include<string.h>
#include<iostream>
#include<iomanip>
#include <string>
#include<fstream>
using namespace std;
int main(int argc, char* argv[])
{
	string str;
	if (argc == 1)
	{
		cout << "Please input the file name: ";
		getline(cin, str);
	}
	else
		str = argv[1];
    //fstream constructor opens file
   ifstream  fcin(str.c_str());
   if(fcin.fail())
   {
	  //file not found
	   cout<<"the  could not be opened"<<endl;
	  return 1;
   }
    
   else  //filter poiter at the first position
	  	fcin.seekg(0);   
	    while(!fcin.eof())   
		{
			char data[256] = "";
	        char mid;
	        int i=0;
			
            fcin.get(mid);//read the char
			//if space and tab  delet
			if(isspace(mid))
				;
			//if they are const 
		    else if (isdigit(mid))
			{ 	  
			    while( isdigit(mid) )//A big program
				{
				data[i]=mid;  //put the biaoshifu to array
			    i++;
				fcin.get(mid);
				}
                cout<<"data: "<<data<<endl;  
				fcin.seekg(-1,ios::cur); //back a byte
			}
			 
			//if they are biaoshifu
		    else if(isalpha(mid))
			{
                //fcin.seekg(-1,ios::cur); //back a byte
				char Biaoshifu[256] = "";
				int i=0;
				
				// Reversword array
				char *Reversword[35]={"program","begin",
					"end","and","array","case","const",
					"goto","div","do","downto","else",
					"file","for","funcation","if","in",
					"label","mod","nil","not","of",
					"or","packed","procedure","record",
					"repeat","set","then","to","type",
					"until","var","while","with"};
				//opertor array
				char *opertor[20]={"pred","succ",
					"abs","sqr","trunc","round",
					"ord","sin","cos","arctan",
					"ln","exp","sqrt","chr","odd",
					"div","mod","not","and","or"};
	 while(isdigit(mid) || isalpha(mid) || mid=='_')
				{ 
				//put the biaoshifu to array
		        Biaoshifu[i]=mid;  
			    i++;
				fcin.get(mid);
				}
				bool  rb=false;
				bool  rp=false;
				//judge reversword or not
                for(int j=0;j<=34;j++)
				{
                  if(_stricmp(Reversword[j],Biaoshifu)==0)
				  {
				   rb=true;
				   break;
				  }
				}
				  //judge opertor or not
                for(int m=0;m<=19;m++)
				{
                  if(_stricmp(opertor[m],Biaoshifu)==0)
				  {
				   rp=true;
				   break;
				  }
				}
				if(rb)
				{ 
					cout<<"Reversword:"<<Biaoshifu<<endl;
                    fcin.seekg(-1,ios::cur);
			    	continue;
				}
				if(rp)
				{ 
					cout<<"opertor: "<<Biaoshifu<<endl;
					fcin.seekg(-1,ios::cur);
					continue;
				}
				
			    cout<<"Biaoshifu:"<<Biaoshifu<<endl;  
				fcin.seekg(-1,ios::cur); //back a byte
				
			}
		
			// "<","<=","<<","<>"
			else if(mid=='<')
			{
			    char xiaoyu[1] = "";
				xiaoyu[0]=mid;
				fcin.get(mid);
				if(mid=='=')
				{
					xiaoyu[1]=mid;
cout<<"smaller or equal:"<<xiaoyu[0]<<xiaoyu[1]<<endl;
					continue;
				}
				else if(mid=='<')
				{
				    xiaoyu[1]=mid;
	cout<<"far smaller:"<<xiaoyu[0]<<xiaoyu[1]<<endl;
					continue;
				}
				else if(mid=='>')
				{
				  xiaoyu[1]=mid;
	 cout<<"not equal:"<<xiaoyu[0]<<xiaoyu[1]<<endl;
				  continue;
				}
				else 
				{
					
					cout<<"small: "<<xiaoyu[0]<<endl;
				}
				
				
			fcin.seekg(-1,ios::cur);	 //back a byte
			}
			// ">",">=",">>"
			else if(mid=='>')
			{
			    char dayu[2] = "";
				dayu[0]=mid;
				fcin.get(mid);
				if(mid=='=')
				{
					dayu[1]=mid;
	cout<<"larger or equal:"<<dayu[0]<<dayu[1]<<endl;
				}
				else if(mid=='>')
				{
				  dayu[1]=mid;
	cout<<"far larger: "<<dayu[0]<<dayu[1]<<endl;
				}
				else
					cout<<"larger: "<<mid<<endl;
                    fcin.seekg(-1,ios::cur);
			}
			
			//delete the interrapts "{}"
			else if(mid=='{')
			{
			  while(fcin.get(mid)!="}")
			  { 
				 return 0;
			  }
			}
			//the char "()"
			else if(mid=='(')
			{
cout<<"the left parenthesis:"<<mid<<endl;
        	}
			else if(mid==')')
cout<<"the right parenthesis:"<<mid<<endl;
			//the char "[]"
			else if(mid=='[')
cout<<"the left squre brackets:"<<mid<<endl;
			else if(mid==']')
cout<<"the right squre brackets:"<<mid<<endl;
			//the char "< >"
			else if(mid=='<')
cout<<"the left brackets:"<<mid<<endl;
			else if(mid=='>')
cout<<"the right brackets:"<<mid<<endl;
			else if(mid=='`')
cout<<"zuo fenhao: "<<mid<<endl;
			//":",":="
			else if(mid==':')
			{
			    char maohao[2] = "";
				maohao[0]=mid;
				fcin.get(mid);
				if(mid=='=')
				{
					maohao[1]=mid;
cout<<"assign:"<<maohao[0]<<maohao[1]<<endl;
					continue;
				}
				
				else
cout<<"colo: "<<maohao[0]<<endl;
				fcin.seekg(-1,ios::cur);
			}
			//"&&"
			else if(mid=='&')
			{
			  char dizhi[2] = "";
				dizhi[0]=mid;
				fcin.get(mid);
				if(mid=='&')
				{
					dizhi[1]=mid;
cout<<"and:"<<dizhi[0]<<dizhi[1]<<endl;
				}
				
				else
			cout<<"invaild symtax: "<<dizhi[0]<<endl;
			}
			//'||'
            else if(mid=='|')
			{
			  char shuxian[2] = "";
				shuxian[0]=mid;
				fcin.get(mid);
				if(mid=='|')
				{
					shuxian[1]=mid;
			cout<<"or:"<<shuxian[0]<<shuxian[1]<<endl;
				}
				
				else
			cout<<"invaild symtax: "<<shuxian[0]<<endl;
			}
			else if(mid==',')
			{
				cout<<"Cmma:"<<mid<<endl;
				continue;
			}
			else if(mid==';')
			{
				cout<<"semicolon: "<<mid<<endl;
				continue;
			}
			else if(mid=='^')
			{
				cout<<"pointer: "<<mid<<endl;
				continue;
			}
			else if(mid=='!')
			{
				cout<<"tanhao: "<<mid<<endl;
				continue;
			}
			else if(mid=='=')
			{
				cout<<"assign:"<<mid<<endl;
			    continue;
			}
			else if(mid=='+')
			{
				cout<<"plus:"<<mid<<endl;
			    continue;
			}
			else if(mid=='-')
			{
				cout<<"minus:"<<mid<<endl;
			    continue;
			}
			else if(mid=='*')
			{
				cout<<"multiply:"<<mid<<endl;
				continue;
			}
			else if(mid=='/')
			{
				cout<<"div:"<<mid<<endl;
				continue;
			}
			else if(mid=='#')
			{
				cout<<"jinghao: "<<mid<<endl;
                continue;
			}		
			else if(mid=='.')
			{
				cout<<"period: "<<mid<<endl;
				break;			
			}
			else  if(isspace(mid))
			{continue;}
		   	//the error in the program
			else 
				cout<<"error: "<<mid<<endl;
	}
return 0;
}

⌨️ 快捷键说明

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