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

📄 parseclass.cpp

📁 简单的词法分析(parseclass.h)
💻 CPP
字号:
#include "parseClass.h"
#include <algorithm>
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
#include <utility>
using namespace std;
int Parse::ParseNumber(string &a)
{  
	int m_reduce=0,m_add=0,m_nock=0,m_e=0;//只有一个- ,只有一个 + ,只有一个 e ,只有一个 .
  int i=0;
  int ncounter=0;
  char *l;
  l=new char[60];
  strcpy(l,a.c_str());

  for(i=0;i<a.size();++i)
	{  if(isdigit(l[i])!=0)
		{  ncounter++;
	      continue;
		}
		else
		{
			if(isother(l[i])==1)
			{m_reduce++;}
			if(isother(l[i])==2)
			{m_e++;}
			if(isother(l[i])==3)
			{m_add++;}
			if(isother(l[i])==4)
			{m_nock++;}
		}
		
	}
	
     if(m_reduce<=1&&m_e<=1&&m_add<=1&&m_nock<=1&&(ncounter+m_reduce+m_e+m_add+m_nock)==a.size())
		{    
			 if(m_reduce==1&&m_add==1)
			 {
				 cout<<"it is not digit"<<endl;
				 return 0;
			 }
			 else
				  {
			     if(m_nock==1)
				 {
				 cout<<"it is float digit"<<endl;
				 return 1;
				 }
				 }
		}
	 if(ncounter==a.size())
		{
			cout<<"it is the digit"<<endl;
			return 1;
		}
      else
	{    
		  cout<<"it is not digit"<<endl;
		  return 0;
	}
		
}
vector<string> * Parse::LoadFile(string &b)
{
string getl;
vector<string>::iterator i;
string::size_type pos = 0, prev_pos = 0;
ifstream infile(b.c_str(),ios::in);
if (!infile) 
{
cerr<<"oops! unable to open file "<<b<< " -- bailing out!\n";
exit( -1 );
}
else 
cout <<'\n';
vector<string> *lines_of_text=new vector<string>;
string textline;
typedef pair<string::size_type,int> stats;
stats maxline;
int linenum = 0;
while(getline(infile,textline,'\n')) 
{
cout<<"line read:"<<textline<<'\n';
cout<<linenum;
if (maxline.first<<textline.size()) 
{
maxline.first = textline.size();
maxline.second = linenum;
}
lines_of_text->push_back( textline );
linenum++;
}
	vector<string> words;
	i=lines_of_text->begin();
	while(i!=lines_of_text->end())
	{   getl=*i;
while (( pos = getl.find_first_of( ' ', pos ))!= string::npos )
{
words.push_back( getl.substr(prev_pos, pos-prev_pos));
prev_pos = ++pos;
} 
  
  i++;
  pos=0;
  prev_pos=0;
  
	}
	///////////////////////////////////////////////////////////////////////////////去掉vector中的标点符号
	string filt_elems( "\",;:!?)(\\/" );
	vector<string> *nwords;
	nwords=new vector<string>;
	i=words.begin();
	while(i!=words.end())
	{
         getl=*i;
		 while (( pos = getl.find_first_of( filt_elems, pos ))!= string::npos )
		{
			 getl.erase(pos, 1);
		}
		nwords->push_back(getl);
		i++;
		pos=0;
	}
	/*for(i=nwords->begin();i!=nwords->end();++i)
	{
	   cout<<*i<<endl;
	}*/
	return nwords;
}
int Parse::isother(char &p)
{   
	if(p=='-')
		return 1;
    if(p=='e')
		return 2;
	if(p=='+')
		return 3;
	if(p=='.')
		return 4;
}
int Parse::ischar(string &a)
{    ifstream output,for_get;
    string getval;
	vector<string> reg;
	vector<string> test;
	vector<string>::iterator pos;
	 int i;
	char *l;
	l=new char[30];
	strcpy(l,a.c_str());
    if(isalnum(l[0])!=0)
	{
		for(i=1;i<a.size()-1;i++)
		{
		  if(iscsym(l[i])!=0)
			{
			  continue;
			}
			else
			{
			break;}
		}
		if(i==a.size()-1)
		{
			cout<<"it is the right string"<<endl;
	string c("reg.txt");
	for_get.open(c.c_str(),ios::in);
	while(!for_get.eof())
	{
		for_get>>getval;
		reg.push_back(getval);
	}
	for_get.close();
    pos=find(reg.begin(),reg.end(),a);
	if(pos==reg.end())
	{
		cout<<"it is string"<<endl;
		return 1;
	}
	else
	{
     cout<<a<<"关键字"<<endl;
	 return 0;
	}
		}
	}
	else
	{cout<<"it is not the valible string"<<endl;
	return 0;}
}
int Parse::Is_contrl(string &a)
{   vector<string> reg;
    vector<string>::iterator i;
   string getval;
    ifstream output;
	int Is_cr;
	output.open("contrl.txt",ios::in);
    while(!output.eof())
	{
		output>>getval;
		reg.push_back(getval);
	}
	for(i=reg.begin();i!=reg.end();i++)
	{
		if((*i).compare(a)==0)
		{
			Is_cr=1;
		}
	}
         if(Is_cr==1)
	{
			return 1;
	}
 }

⌨️ 快捷键说明

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