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

📄 embsql.h

📁 一个经典的词法分析器
💻 H
📖 第 1 页 / 共 2 页
字号:
	CSTRING,								// type is a char string
	CFLOAT,									// type is a float
	CLONG,									// rest are yet to be used
	CINT,									// yet to be used
	CUNSIGNED,								// yet to be used
	CTIME									// yet to be used
};

class SCColumn : public CObject
{
public:
	DLL_EXPORT SCColumn(CString name, int type, int width);	// create a column descriptor
	DLL_EXPORT ~SCColumn();
	DLL_EXPORT CString Name();								// name of this column
	DLL_EXPORT int Type();									// type of this column
	DLL_EXPORT int Width();									// maximum width of this column
	DLL_EXPORT void *Value();								// return what this thing points to
	DLL_EXPORT void Value(const void *);					// set the value pointer

	DLL_EXPORT void Value(char *s);							// set a string value
	DLL_EXPORT void Value(int);								// set a value from the int
	DLL_EXPORT void Value(double);							// set a value from a float
	DLL_EXPORT void Value(unsigned);						// set a value from an unsigned
	DLL_EXPORT void SetValueByType(CString);				// set a value from string by type
	DLL_EXPORT void *operator new(size_t t);
	DLL_EXPORT void operator delete(void *ptr);

private:
	SCString *name;							// column name attribute
	int width;								// max width attribute
	int type;								// type of this attribute
	void *value;							// value of this attribute	
};

/////////////////////////////////////
// TABLE.CPP NEEDS

enum compareEnums {
	CLT,									// Less than							
	CLE,									// Less than equal to
	CEQ,									// Equal to
	CGT,									// Greater than equal to
	CGE										// Greater than equal to
};

#define MAX_COLUMNS 128						// maximum columns supported

class SCTable : public CObject 
{
public:
	void *operator new(size_t size);						// SHARED MEMORY allocator
	void operator delete(void *ptr);						// SHARED MEMORY deallocator
	SCTable(CString nm, SCColumn *cols[], int sz);			// define a table
	~SCTable();												// destructor
	CString SCTable::Name();									// return name of table
	int Columns();											// return # of columns
	SCColumn** GetColumnInfo();								// return the columns
	int Index(CString nm);									// given name of column, return its index
	int Type(int n);										// given a column index, return its type
	int Type(CString nm);									// given a column index return its type
	SCColumn* Column(int n);									// given an index, return the column
	SCColumn* Column(CString n);								// given a name, return the column
	bool AddRow(const void **newRow);						// add the array of voids to the table
	bool AddRow(SCColumn* cols[]);							// add the array of columns to the table
	bool AddRow();											// add a row using the column Value() ptrs
	int RecordInTable(SCColumn* col[], int ops[], int nColumns, int start); // return 1st row that meets the specified criteria
	int Compare(SCColumn *a, SCColumn *b, int op);			// compare column ptrs based on op
	void **GetRow(int i);									// given the index, return the array of void ptrs (the row)
	bool DeleteRow(int i);									// delete the array of pointers at row i
	void DeleteAllRows();									// delete all rows
	int NRows();											// return the number of rows

	bool PrintRow(int which);								// print a row number i of the table
	bool PrintRow(void **);									// print the provided array of void pointers
	DLL_EXPORT static bool PrintRow(SCColumn**,int,void**);	// print the provided row of void pointers, based on the types provided in the columns provided
	static double InterpretAsFloat(int i, void*);			// interpret void pointer as float, based on column position i's type
	static CString* InterpretAsString(int i, void*);		// interpret void pointer as string, based on column position i's type

private:
	SCString *name;											// name of table					
	SCColumn **columns;										// array of defining columns
	int nCols;												// numbner of defining columns
	SCCollection *rows;										// data rows
};

SCTable* FindTable(CString s);			// find a table in the collection of tables

///////////////////////////////////
//
// ExecuteStack.cpp needs


class CEStack : public CObject				// Stack execution object
{
public: 
	void *operator new(size_t size);		// SHARED MEMORY allocator
	void operator delete(void *ptr);		// SHARED MEMORY de-allocator

	CEStack();								// constructor
	void Load(SCCollection *);				// set the stack
	~CEStack();								// destructor
	void DeleteResult();					// delete the results of the sql
	SCCollection* Execute();					// executes the stack
	CString TableInQuestion();				// returns the table in question

private:
	CString* PopProduction();				// pop a production off the stack
	bool ExecuteSelect();					// do a select operation
	bool ExecuteUpdate();					// do an update operation
	bool ExecuteInsert();					// do an insert operation
	bool ExecuteDelete();					// do a delete operation
	bool ExecuteAssign();					// do a literal assignment
	bool ExecuteCreate();					// create a table
	bool ExecuteOperator(CString);			// execute an operator
	double Numberize(CString);				// turn object into a number, if you can
	bool IsOperator(CString);				// is this string an operator
	double Compare(CString a, CString b);	// compare, with wildcards
	CString* GetValue(CString);				// given a string object return the value of what it points to
	double CEStack::DoOperation(CString a, CString b, CString op); // does the indicate operation

private:
	CString tableName;						// table we created
	SCCollection* stack;						// internal execution stack
	CString errorString;					// error string
	void **rowInQuestion;					// pointer to the row being examined
	SCTable* tableInQuestion;				// pointer to the table being examined
	SCCollection* result;					// result collection
};
/////////////////////////////////
//
// PARSER.CPP NEEDS


class CESql : public CObject				// Embedded Shared Memory SQL Object
{
public:	  
	DLL_EXPORT CESql();										// create, using anonymous db shared memory, size = 20MB
	~CESql();												// destructor
	DLL_EXPORT bool DeleteTable(CString table);				// delete a table by name
	DLL_EXPORT bool DefineTable(CString name, SCColumn *cols[], int n); // define a table
	DLL_EXPORT SCColumn** GetColumnInfo(CString nm, int *nC);// get the column info array
	DLL_EXPORT bool AddRow(CString name, SCColumn *cols[]);	// add a row to a table
	DLL_EXPORT SCCollection* Sql(const CString stmt);		// issue an SQL statement
	DLL_EXPORT int Catalog();								// return # of tables in db
	DLL_EXPORT CString GetCatalog(int);						// return catalog at #
	DLL_EXPORT const CString ReturnError();					// return the error string
	DLL_EXPORT void SetError(CString s);					// set the error string
	DLL_EXPORT static SCTable* FindTable(CString nm);		// find a table by name
	DLL_EXPORT static bool IsNumber(const CString);			// is the item a number?
	DLL_EXPORT static bool IsIdentifier(const CString);		// is the item an identifier
	DLL_EXPORT static bool IsStringLiteral(CString);		// is the item a 'string literal'
	DLL_EXPORT SCColumn** GetColumnInfo();					// returns pointer to  columns array of the table from last query
	DLL_EXPORT int GetColumnCount();						// returns # of columns of the table
	DLL_EXPORT void PrintStack(BOOL);						// enables stack printing

private:
	void SetColumnInfo(CString table);						// sets the # of columns of the table in this transaction
	bool Select();											// select root of grammar
	bool Update();											// upodate root of grammar
	bool Insert();											// insert root of grammar
	bool Delete();											// delete root of grammar
	bool CreateTable();										// define a table
	bool GetListIds();										// List of IDs production
	bool GetListExps();										// List of expressions production
	bool GetListDecls();									// Get list of table declarations
	bool WhereClause();										// where root of grammar
	void Accept();											// accept (and advance) input scanner
	bool GetAssignment();									// get an assignment from the intput
	bool GetIdentifier();									// get an identifier from the input scanner
	bool GetNumber();										// get a number from the input scanner
	bool GetStringLiteral();								// get a string literal from the input scanner

	bool GetToken(CString hunt = "", CString span = " \t\n"); // get the next token delimited by the span
	bool AttributeExists(CString, CString);					// Does the attribute exist in table?
	const CString ReturnToken();							// return the last token from the input scanner
//
// Recursive descent parser of the embedded shared memory sql
//
	bool LHS();					// Get left hand side of an assignment statement
	bool E();					// Root of operator precendence grammar of the recursive descent parser
	bool ZPRIME();				// From ZPRIME to F() the productions have more precedence. F is highest
	bool EPRIME();
	bool T();
	bool Z();
	bool TPRIME();
	bool F();
	int TokenToType(CString);
	void Execute(SCCollection*);	// Executes the stack as constructed by the recursive descent parser
//
// private attributes of the object
//
	BOOL printStack;			// stack printing flag
	int nCols;					// number of columns in the table of the last transaction
	SCColumn** columnInfo;		// the pointer to the columns of the table in the last transaction
	CEStack* eStack;			// Stack executor object
	SCCollection* stack;		// the stack to execute
	CString inputString;		// the input string to the parser
	CString errorString;		// the error string, if any from result of parsing
	CString tokenString;		// the last token we recognized
	SCCollection* result;		// the result of the execution of eStack
};

//////////////////////////////////////

⌨️ 快捷键说明

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