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

📄 cpplex.h

📁 这是一个很不错的词法语法分析器! 很适合计算机专业的大学生学习参考
💻 H
字号:
#ifndef __D22E6047944D6CFD4F8B7D8CF361D3DB_B20098E03CA1__
#define __D22E6047944D6CFD4F8B7D8CF361D3DB_B20098E03CA1__

#pragma warning(disable:4786)

#include <string>
#include <fstream>
#include <vector>
#include <set>

namespace CppParser
{

using namespace std;

enum Type
{
	INVALID=0,PPKEYWORD,KEYWORD,IDENTIFIER,WHITESPACE,LINEFEED,DELIMITER,OPERATOR,SINGLELINECOMMENT,
	STRINGLITERAL,CHARLITERAL,DECNUMBERLITERAL,HEXNUMBERLITERAL,OCTNUMBERLITERAL,FLOATNUMBERLITERAL,
	BLOCKCOMMENT,NUMOFTYPES
};

struct Info
{
	virtual string getName() const=0;
};

struct Context;

struct Parser
{
	virtual void parse(Type,const string&,const Context&)=0;
};

struct Context
{
	virtual void run()=0;
	virtual const Info& getInfo() const=0;
};

struct Word
{
	virtual void clean()=0;
	virtual bool addChar(char,Context&)=0;
	virtual bool end(Context&) const=0;
	virtual Type getType() const=0;
};

class WordBase:public Word
{
	Type mType;
public:
	WordBase(Type t);
	virtual Type getType() const;
	static bool isWhiteSpace(char ch);
	static bool isIdentifierStart(char ch);
	static bool isIdentifier(char ch);
	static bool isDigit(char ch);
};

class RegularExpressionWordHelper:public WordBase
{
	string mRegularExpression;
public:
	virtual void clean();
	virtual bool addChar(char,Context&);
	virtual bool end(Context&) const;
protected:
	RegularExpressionWordHelper(Type t,const string& re);
	static const char REP_ONE_OR_MORE;
	static const char REP_ONE;
	static const char REP_ZERO_OR_MORE;
	static const char REP_ZERO_OR_ONE;
	static const char CODE_WHITE_SPACE;
	static const char CODE_IDENTIFIED_START;
	static const char CODE_IDENTIFIER;
	static const char CODE_DIGIT;
	static const char CODE_ANY;
	static const char CODE_ANY_BUT_0X0D0X0A;
	static const char CODE_ANY_BUT_DOUBLEQUOTE;
	static const char CODE_ANY_BUT_SINGLEQUOTE;
	static const char CODE_1_TO_9;
	static const char CODE_0_TO_7;
	static const char CODE_SIGN;
	static const char CODE_HEX_PREFIX;
	static const char CODE_HEX_DIGIT;
private:
	char mCurrentCode;
	char mCurrentRepitition;
	std::string::size_type mCurrentOffset;
	static bool checkChar(char ch,char code);
};

class GroupedRegularExpressionWordHelper:public RegularExpressionWordHelper
{
	set<string> mValues;
	set<string> mValidValues;
	string::size_type mLongestValueSize;
	bool mIsIn;
	string mValue;
protected:
	GroupedRegularExpressionWordHelper(Type t,const string& re,bool isIn);
	virtual void clean();
	virtual bool addChar(char,Context&);
	virtual bool end(Context&) const;
	void addValue(const string& v);
	void removeValue(const string& v);
	bool isValueIn(const string& v) const;
	bool isValueValid(const string& v) const;
};

class PPKeyword:public RegularExpressionWordHelper
{
public:
	PPKeyword();
};

class Keyword:public GroupedRegularExpressionWordHelper
{
public:
	Keyword();
};

class Identifier:public GroupedRegularExpressionWordHelper
{
public:
	Identifier();
};

class WhiteSpace:public RegularExpressionWordHelper
{
public:
	WhiteSpace();
};

class LineFeed:public RegularExpressionWordHelper
{
public:
	LineFeed();
};

class SemicolonDelimiter:public RegularExpressionWordHelper
{
public:
	SemicolonDelimiter();
};

class LeftBracketDelimiter:public RegularExpressionWordHelper
{
public:
	LeftBracketDelimiter();
};

class RightBracketDelimiter:public RegularExpressionWordHelper
{
public:
	RightBracketDelimiter();
};

class Operator:public GroupedRegularExpressionWordHelper
{
public:
	Operator();
};

class SingleLineComment:public RegularExpressionWordHelper
{
public:
	SingleLineComment();
};

class StringLiteral:public RegularExpressionWordHelper
{
public:
	StringLiteral();
};

class CharLiteral:public RegularExpressionWordHelper
{
public:
	CharLiteral();
};

class DecimalLiteral:public RegularExpressionWordHelper
{
public:
	DecimalLiteral();
};

class OctLiteral:public RegularExpressionWordHelper
{
public:
	OctLiteral();
};

class HexLiteral:public RegularExpressionWordHelper
{
public:
	HexLiteral();
};

class IntegralFloatLiteral:public RegularExpressionWordHelper
{
public:
	IntegralFloatLiteral();
};

class NonIntegralFloatLiteral:public RegularExpressionWordHelper
{
public:
	NonIntegralFloatLiteral();
};

class ExpIntegralFloatLiteral:public RegularExpressionWordHelper
{
public:
	ExpIntegralFloatLiteral();
};

class ExpNonIntegralFloatLiteral:public RegularExpressionWordHelper
{
public:
	ExpNonIntegralFloatLiteral();
};

class BlockComment:public Word
{
	enum Status
	{
		INITIAL=0,
		FIRSTSLASH,
		FIRSTSTAR,
		SECONDSTAR,
		FINISHED
	};
	Status mStatus;
public:
	BlockComment();
	virtual void clean();
	virtual bool addChar(char,Context&);
	virtual bool end(Context&) const;
	virtual Type getType() const;
};

class StreamContextHelper: public Context
{
	void operator=(const StreamContextHelper&);
	PPKeyword mPPKeyword;
	Keyword mKeyword;
	Identifier mIdentifier;
	WhiteSpace mWhiteSpace;
	LineFeed mLineFeed;
	SemicolonDelimiter mSemicolonDelimiter;
	LeftBracketDelimiter mLeftBracketDelimiter;
	RightBracketDelimiter mRightBracketDelimiter;
	Operator mOperator;
	SingleLineComment mSingleLineComment;
	StringLiteral mStringLiteral;
	CharLiteral mCharLiteral;
	DecimalLiteral mDecimalLiteral;
	OctLiteral mOctLiteral;
	HexLiteral mHexLiteral;
	IntegralFloatLiteral mIntegralFloatLiteral;
	NonIntegralFloatLiteral mNonIntegralFloatLiteral;
	ExpIntegralFloatLiteral mExpIntegralFloatLiteral;
	ExpNonIntegralFloatLiteral mExpNonIntegralFloatLiteral;
	BlockComment mBlockComment;
public:
	class InputStreamHelper
	{
		void operator=(const InputStreamHelper&);
	public:
		InputStreamHelper(istream& istr);
		bool hasChar();
		char getChar();
		void ungetChar(char c);
		void ungetString(const string& s);
	private:
		string mCharsRead;
		istream& mInputStream;
	};
private:
	class LocalInfo:public Info
	{
		string mName;
	public:
		void setName(const string& name);
		virtual string getName() const;
	};
	LocalInfo mInfo;
	Parser& mParser;
	InputStreamHelper& mInputStreamHelper;
	string mLastWord;
	Type mLastType;
	vector<Word*> mWordList;
	char getEscapedChar(string& s);
public:
	StreamContextHelper(InputStreamHelper&,Parser&);
	virtual ~StreamContextHelper();
	virtual void run();
	virtual const Info& getInfo() const;
public:
	void addWord(Word&);
	bool runOnce(void);
	const string& getLastWord(void) const;
	Type getLastType(void) const;
	void setInfoName(const string&);
	string getInfoName(void) const;
};

}

#endif //__D22E6047944D6CFD4F8B7D8CF361D3DB_B20098E03CA1__

⌨️ 快捷键说明

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