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

📄 cpplex.cpp

📁 这是一个很不错的词法语法分析器! 很适合计算机专业的大学生学习参考
💻 CPP
📖 第 1 页 / 共 2 页
字号:
const char RegularExpressionWordHelper::CODE_ANY='a';
const char RegularExpressionWordHelper::CODE_ANY_BUT_0X0D0X0A='x';
const char RegularExpressionWordHelper::CODE_ANY_BUT_DOUBLEQUOTE='q';
const char RegularExpressionWordHelper::CODE_ANY_BUT_SINGLEQUOTE='t';
const char RegularExpressionWordHelper::CODE_1_TO_9='o';
const char RegularExpressionWordHelper::CODE_0_TO_7='z';
const char RegularExpressionWordHelper::CODE_SIGN='g';
const char RegularExpressionWordHelper::CODE_HEX_PREFIX='p';
const char RegularExpressionWordHelper::CODE_HEX_DIGIT='h';

bool RegularExpressionWordHelper::addChar(char ch,Context&)
{
	if(mCurrentOffset>=mRegularExpression.size())
		return false;
	while(mCurrentOffset<mRegularExpression.size())
		switch(mCurrentRepitition)
		{
		case REP_ONE_OR_MORE:
			if(checkChar(ch,mCurrentCode))
			{
				mCurrentRepitition=REP_ZERO_OR_MORE;
				return true;
			}
			return false;
			break;
		case REP_ONE:
			if(checkChar(ch,mCurrentCode))
			{
				mCurrentOffset+=2;
				if(mCurrentOffset<mRegularExpression.size())
				{
					mCurrentCode=mRegularExpression[mCurrentOffset];
					mCurrentRepitition=mRegularExpression[mCurrentOffset+1];
				}
				return true;
			}
			return false;
			break;
		case REP_ZERO_OR_MORE:
			if(checkChar(ch,mCurrentCode))
				return true;
			mCurrentOffset+=2;
			if(mCurrentOffset<mRegularExpression.size())
			{
				mCurrentCode=mRegularExpression[mCurrentOffset];
				mCurrentRepitition=mRegularExpression[mCurrentOffset+1];
			}
			break;
		case REP_ZERO_OR_ONE:
			if(checkChar(ch,mCurrentCode))
			{
				mCurrentOffset+=2;
				if(mCurrentOffset<mRegularExpression.size())
				{
					mCurrentCode=mRegularExpression[mCurrentOffset];
					mCurrentRepitition=mRegularExpression[mCurrentOffset+1];
				}
				return true;
			}
			mCurrentOffset+=2;
			if(mCurrentOffset<mRegularExpression.size())
			{
				mCurrentCode=mRegularExpression[mCurrentOffset];
				mCurrentRepitition=mRegularExpression[mCurrentOffset+1];
			}
			break;
		}
	return false;
}

bool RegularExpressionWordHelper::end(Context&) const
{
	if(mCurrentOffset==mRegularExpression.size())
		return true;
	if(mCurrentRepitition!=REP_ZERO_OR_MORE)
		return false;
	for(string::size_type i=mCurrentOffset+2;i<mRegularExpression.size();i+=2)
		if(mRegularExpression[i+1]!=REP_ZERO_OR_MORE)
			break;
	if(i==mRegularExpression.size())
		return true;
	return false;
}

bool RegularExpressionWordHelper::checkChar(char ch,char code)
{
	switch(code)
	{
	case CODE_WHITE_SPACE:
		return isWhiteSpace(ch);
		break;
	case CODE_IDENTIFIED_START:
		return isIdentifierStart(ch);
		break;
	case CODE_IDENTIFIER:
		return isIdentifier(ch);
		break;
	case CODE_DIGIT:
		return isDigit(ch);
		break;
	case CODE_ANY:
		return true;
		break;
	case CODE_ANY_BUT_0X0D0X0A:
		return ch!=0x0a&&ch!=0x0d;
		break;
	case CODE_ANY_BUT_DOUBLEQUOTE:
		return ch!='\"';
		break;
	case CODE_ANY_BUT_SINGLEQUOTE:
		return ch!='\'';
		break;
	case CODE_1_TO_9:
		return ch>='1'&&ch<='9';
		break;
	case CODE_0_TO_7:
		return ch>='0'&&ch<='7';
		break;
	case CODE_SIGN:
		return ch=='-'||ch=='+';
		break;
	case CODE_HEX_PREFIX:
		return ch=='x'||ch=='X';
		break;
	case CODE_HEX_DIGIT:
		return ch>='0'&&ch<='9'||ch>='a'&&ch<='f'||ch>='A'&&ch<='F';
		break;
	default:
		if(ch==code)
			return true;
		return false;
		break;
	}
}

GroupedRegularExpressionWordHelper::GroupedRegularExpressionWordHelper(Type t,const string& re,bool isIn)
	:RegularExpressionWordHelper(t,re),mLongestValueSize(0),mIsIn(isIn)
{
	clean();
}

void GroupedRegularExpressionWordHelper::clean()
{
	mValue.erase();
	RegularExpressionWordHelper::clean();
}

bool GroupedRegularExpressionWordHelper::addChar(char ch,Context& c)
{
	if(mIsIn)
	{
		string temp=mValue;
		temp+=ch;
		if(isValueValid(temp)&&RegularExpressionWordHelper::addChar(ch,c))
		{
			mValue=temp;
			return true;
		}
		return false;
	}
	if(RegularExpressionWordHelper::addChar(ch,c))
	{
		mValue+=ch;
		return true;
	}
	return false;
}

bool GroupedRegularExpressionWordHelper::end(Context& c) const
{
	if(!RegularExpressionWordHelper::end(c))
		return false;
	if(mIsIn&&isValueIn(mValue))
		return true;
	if(!mIsIn&&!isValueIn(mValue))
		return true;
	return false;
}

void GroupedRegularExpressionWordHelper::addValue(const string& v)
{
	if(!v.size())
		return;
	mValues.insert(v);
	mValidValues.insert(v);
	for(string::const_iterator iter=v.begin()+1;iter!=v.end();++iter)
		mValidValues.insert(string(v.begin(),iter));
	if(v.size()>mLongestValueSize)
		mLongestValueSize=v.size();
}

void GroupedRegularExpressionWordHelper::removeValue(const string& v)
{
	mValues.erase(v);
	if(v.size()==mLongestValueSize)
	{
		for(set<string>::iterator iter=mValues.begin();iter!=mValues.end();++iter)
			if(iter->size()==mLongestValueSize)
				break;
		if(iter==mValues.end())
			--mLongestValueSize;
	}
	mValidValues.erase(mValidValues.begin(),mValidValues.end());
	for(set<string>::iterator iter=mValues.begin();iter!=mValues.end();++iter)
	{
		mValidValues.insert(*iter);
		for(string::const_iterator siter=iter->begin()+1;siter<iter->end();++siter)
			mValidValues.insert(string(iter->begin(),siter));
	}
}

bool GroupedRegularExpressionWordHelper::isValueIn(const string& v) const
{
	return mValues.find(v)!=mValues.end();
}

bool GroupedRegularExpressionWordHelper::isValueValid(const string& v) const
{
	return mValidValues.find(v)!=mValidValues.end();
}

PPKeyword::PPKeyword():RegularExpressionWordHelper(PPKEYWORD,"#$x*")
{
}

Keyword::Keyword():GroupedRegularExpressionWordHelper(KEYWORD,"s*",true)
{
	for(int i=0;i<sizeof(KEYWORDS)/sizeof(*KEYWORDS);++i)
		addValue(KEYWORDS[i]);
}

Identifier::Identifier():GroupedRegularExpressionWordHelper(IDENTIFIER,"s$i*",false)
{
	for(int i=0;i<sizeof(KEYWORDS)/sizeof(*KEYWORDS);++i)
		addValue(KEYWORDS[i]);
}

WhiteSpace::WhiteSpace():RegularExpressionWordHelper(WHITESPACE,"w+")
{
}

LineFeed::LineFeed():RegularExpressionWordHelper(LINEFEED,"\x0d-\x0a$")
{
}

SemicolonDelimiter::SemicolonDelimiter():RegularExpressionWordHelper(DELIMITER,";$")
{
}

LeftBracketDelimiter::LeftBracketDelimiter():RegularExpressionWordHelper(DELIMITER,"{$")
{
}

RightBracketDelimiter::RightBracketDelimiter():RegularExpressionWordHelper(DELIMITER,"}$")
{
}

Operator::Operator():GroupedRegularExpressionWordHelper(OPERATOR,"a+",true)
{
	for(int i=0;i<sizeof(OPERATORS)/sizeof(*OPERATORS);++i)
		addValue(OPERATORS[i]);
}

SingleLineComment::SingleLineComment():RegularExpressionWordHelper(SINGLELINECOMMENT,"/$/$x*")
{
}

StringLiteral::StringLiteral():RegularExpressionWordHelper(STRINGLITERAL,"\"$q*\"$")
{
}

CharLiteral::CharLiteral():RegularExpressionWordHelper(CHARLITERAL,"\'$t-t-t-t-\'$")
{
}

DecimalLiteral::DecimalLiteral():RegularExpressionWordHelper(DECNUMBERLITERAL,"o$d*")
{
}

OctLiteral::OctLiteral():RegularExpressionWordHelper(OCTNUMBERLITERAL,"0$z*")
{
}

HexLiteral::HexLiteral():RegularExpressionWordHelper(HEXNUMBERLITERAL,"0$p$h+")
{
}

IntegralFloatLiteral::IntegralFloatLiteral():RegularExpressionWordHelper(FLOATNUMBERLITERAL,"d+.$")
{
}

NonIntegralFloatLiteral::NonIntegralFloatLiteral():RegularExpressionWordHelper(FLOATNUMBERLITERAL,"d*.$d+")
{
}

ExpIntegralFloatLiteral::ExpIntegralFloatLiteral():RegularExpressionWordHelper(FLOATNUMBERLITERAL,"d+.-e$+-d+")
{
}

ExpNonIntegralFloatLiteral::ExpNonIntegralFloatLiteral():RegularExpressionWordHelper(FLOATNUMBERLITERAL,"d*.$d+e$+-d+")
{
}

BlockComment::BlockComment()
{
	clean();
}

void BlockComment::clean()
{
	mStatus=INITIAL;
}

bool BlockComment::addChar(char ch,Context&)
{
	switch(mStatus)
	{
	case INITIAL:
		if(ch=='/')
		{
			mStatus=FIRSTSLASH;
			return true;
		}
		return false;
		break;
	case FIRSTSLASH:
		if(ch=='*')
		{
			mStatus=FIRSTSTAR;
			return true;
		}
		return false;
		break;
	case FIRSTSTAR:
		if(ch=='*')
			mStatus=SECONDSTAR;
		return true;
		break;
	case SECONDSTAR:
		if(ch=='/')
			mStatus=FINISHED;
		else
			mStatus=FIRSTSTAR;
		return true;
		break;
	case FINISHED:
		return false;
		break;
	}
	return false;
}

bool BlockComment::end(Context&) const
{
	return mStatus==FINISHED;
}

Type BlockComment::getType() const
{
	return BLOCKCOMMENT;
}

}

⌨️ 快捷键说明

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