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

📄 wordparse.cpp

📁 表达式分析, 支持算术运算,括号,关系运算,逻辑运算,字符串的like运算等。采用了有限自动机做词法分析, 语法分析用算符优先分析方法
💻 CPP
字号:
#include <stdio.h>
#include <string.h>
#include "SqlParse.h"
/*
表达式分析, 采用了有限自动机做词法分析, 
语法分析用算符优先分析方法, 分析的结果是逆波兰式, 存在一个链表中。
在逆波兰式的基础上,用一个栈来进行求值。在vc++6.0下试验通过。
如有问题,可以mail: zch888email@163.com
我将尽快回复你。不要说我是骗子在骗积分哦。
*/

std::vector<FieldInfo> fieldInfoTable;
std::vector<FieldValueInfo> valueInfoTable;
void main()
{
	char *str1 = "'10' like id && area >= 3.1415926 * 3.0 * 3.0";
	char *str2 = "'10' like id && area % 3.0 <> 0";
	char *str3 = "'104' like id && area >= 3.1415926 * 3.0 * 3.0";
	char *str4 = "'104' like id || area >= 3.1415926e+1 * 3.0 * 3.0";
	char *str5 = "3>5 || -5<=-3 || 3 != 5-8";
	char *str6 = "3 + -5 * (8.8e+1 - 8)"; /*8.8e+1 = 88*/
	Token token;

	FieldInfo fi;
	strcpy(fi.fieldName, "id");
	fi.fieldType = 3;					/*id is string type*/
	fieldInfoTable.push_back(fi);
	strcpy(fi.fieldName, "area");
	fi.fieldType = 2;					/*area is double type*/
	fieldInfoTable.push_back(fi);

	FieldValueInfo fvi;
	strcpy(fvi.fieldName, "id");
	strcpy(fvi.stringvalue, "1033");	/*value of id*/
	valueInfoTable.push_back(fvi);
	strcpy(fvi.fieldName, "area");
	fvi.numberValue = 32.55;			/*value of area*/
	valueInfoTable.push_back(fvi);

	printf("id = '%s', area = %f\n", "1033", 32.55);

	cSQLParse sqlParser;
	printf("\n%s\n", str1);
 	if(!sqlParser.SQLPreCompile(str1, fieldInfoTable))
		printf("%s\n", sqlParser.GetLastError());
	if(sqlParser.SQLInputValue(valueInfoTable, token))
		printf("Result is: %f\n", token.num_val);	
	else
		printf("%s\n", sqlParser.GetLastError());
	

	printf("\n%s\n", str2);
	if(!sqlParser.SQLPreCompile(str2, fieldInfoTable))
		printf("Correct!\n");
	if(sqlParser.SQLInputValue(valueInfoTable, token))
		printf("Result is: %f\n", token.num_val);
	else
		printf("%s\n", sqlParser.GetLastError());


	printf("\n%s\n", str3);
	if(!sqlParser.SQLPreCompile(str3, fieldInfoTable))
		printf("%s\n", sqlParser.GetLastError());
	if(sqlParser.SQLInputValue(valueInfoTable, token))
		printf("Result is: %f\n", token.num_val);
	else
		printf("%s\n", sqlParser.GetLastError());
	

	printf("\n%s\n", str4);
	if(!sqlParser.SQLPreCompile(str4, fieldInfoTable))
		printf("%s\n", sqlParser.GetLastError());
	if(sqlParser.SQLInputValue(valueInfoTable, token))
		printf("Result is: %f\n", token.num_val);
	else
		printf("%s\n", sqlParser.GetLastError());

	
	printf("\n%s\n", str5);
	if(!sqlParser.SQLPreCompile(str5, fieldInfoTable))
		printf("%s\n", sqlParser.GetLastError());
	if(sqlParser.SQLInputValue(valueInfoTable, token))
		printf("Result is: %f\n", token.num_val);
	else
		printf("%s\n", sqlParser.GetLastError());

	printf("\n%s\n", str6);
	if(!sqlParser.SQLPreCompile(str6, fieldInfoTable))
		printf("%s\n", sqlParser.GetLastError());
	if(sqlParser.SQLInputValue(valueInfoTable, token))
		printf("Result is: %f\n", token.num_val);
	else
		printf("%s\n", sqlParser.GetLastError());


	/*print out RPN(逆波兰式)*/
	printf("\nthe RPN of '%s' is:\n", str6);
	cGramAnalysis ga;
	TokenList* tl = ga.GrammerAnalysis(str6);
	if(!tl)
	{
		printf("%s\n", ga.GetLastErrer());
		return;
	}

	while(! tl->empty())
	{
		token = tl->front();
		tl->pop_front();
		switch(token.type)
		{
		case EOI:
			return;
		case IDENTIFIER:
			printf("IDENTIFIER: %s\n", token.str_val);
			break;
		case POSTIVE:
			printf("OPERATOR:   Postive\n");
			break;
		case NEGTIVE:
			printf("OPERATOR:   Negative\n");
			break;
		case CONSTANT:
			printf("CONSTANT:   %f\n", token.num_val);
			break;
		case STRING:
			printf("STRING:     %s\n", token.str_val);
			break;
		case PLUS:
			printf("OPERATOR:   +\n");
			break;
		case MINUS:
			printf("OPERATOR:   -\n");
			break;
		case MULTI:
			printf("OPERATOR:   *\n");
			break;
		case DIV:
			printf("OPERATOR:   /\n");
			break;				
		case REMAINDER:
			printf("OPERATOR:   %%\n");
			break;
		case AND:
			printf("OPERATOR:   AND\n");
			break;
		case OR:
			printf("OPERATOR:   OR\n");
			break;
		case NOT:
			printf("OPERATOR:   NOT\n");
			break;
		case GREAT:
			printf("OPERATOR:   >\n");
			break;
		case GREATEQ:
			printf("OPERATOR:   GREATEQ\n");
			break;
		case LESS:
			printf("OPERATOR:   <\n");
			break;
		case LESSEQ:
			printf("OPERATOR:   LESSEQ\n");
			break;
		case EQ:
			printf("OPERATOR:   EQUAL\n");
			break;
		case NOTEQ:
			printf("OPERATOR:   NOTEQUAL\n");
			break;
		case PERIOD:
			printf("OPERATOR:   .\n");
			break;
		case LPARENTHESES:
			printf("OPERATOR:   (\n");
			break;
		case RPARENTHESES:
			printf("OPERATOR:   )\n");
			break;
		case LIKE:
			printf("OPERATOR:   LIKE\n");
			break;
		default:
			printf("UNKNOWN:   %d\n", token.type);
			break;
		}	
	}	
	
	/*
		cGramAnalysis ga;
	TokenList* tl = ga.GrammerAnalysis(str);
	if(!tl)
	{
		printf("%s\n", ga.GetLastErrer());
		return;
	}

	int a = tl->size();
	while(! tl->empty())
	{
		token = tl->front();
		tl->pop_front();
		switch(token.type)
		{
		case EOI:
			return;
		case IDENTIFIER:
			printf("IDENTIFIER: %s\n", token.str_val);
			break;
		case POSTIVE:
			printf("OPERATOR:   Postive\n");
			break;
		case NEGTIVE:
			printf("OPERATOR:   Negative\n");
			break;
		case CONSTANT:
			printf("CONSTANT:   %f\n", token.num_val);
			break;
		case STRING:
			printf("STRING:     %s\n", token.str_val);
			break;
		case PLUS:
			printf("OPERATOR:   +\n");
			break;
		case MINUS:
			printf("OPERATOR:   -\n");
			break;
		case MULTI:
			printf("OPERATOR:   *\n");
			break;
		case DIV:
			printf("OPERATOR:   /\n");
			break;				
		case REMAINDER:
			printf("OPERATOR:   %%\n");
			break;
		case AND:
			printf("OPERATOR:   AND\n");
			break;
		case OR:
			printf("OPERATOR:   OR\n");
			break;
		case NOT:
			printf("OPERATOR:   NOT\n");
			break;
		case GREAT:
			printf("OPERATOR:   >\n");
			break;
		case GREATEQ:
			printf("OPERATOR:   GREATEQ\n");
			break;
		case LESS:
			printf("OPERATOR:   <\n");
			break;
		case LESSEQ:
			printf("OPERATOR:   LESSEQ\n");
			break;
		case EQ:
			printf("OPERATOR:   EQUAL\n");
			break;
		case NOTEQ:
			printf("OPERATOR:   NOTEQUAL\n");
			break;
		case PERIOD:
			printf("OPERATOR:   .\n");
			break;
		case LPARENTHESES:
			printf("OPERATOR:   (\n");
			break;
		case RPARENTHESES:
			printf("OPERATOR:   )\n");
			break;
		case LIKE:
			printf("OPERATOR:   LIKE\n");
			break;
		default:
			printf("UNKNOWN:   %d\n", token.type);
			break;
		}	
	}	
	*/
	/*
	unsigned char a;
	cSourceString str("a+b.cdef()gea");
	str.GetChar();
	str.GetChar();
	str.PushBackOne();
	str.PushBackOne();
	str.PushBackOne();
	while(a = str.GetChar())
		printf("%c", a);
	printf("\n");
	*/
/*
	int i;
	FILE * fp = fopen("type.c", "w");
	char type[256];

	//other characters are 25, set first;
	for(i = 0; i < 256; i++)
		type[i] = 25;

	//0 is the end_identifier, set to type 0;
	type[0] = 0;

	//Number type is 1
	for(i = '0'; i <= '9'; i++)
	{
		type[i] = 1;
	}

	//letter type is 2
	for(i = 'a'; i <= 'z'; i++)
		type[i] = 2;
	for(i = 'A'; i <= 'Z'; i++)
		type[i] = 2;
	type['_'] = 2;
	
	type['\'']	= 3;
	type['+']	= 4;
	type['-']	= 5;
	type['*']	= 6;
	type['/']	= 7;
	type['%']	= 8;
	type['=']	= 9;
	type['&']	= 10;
	type['|']	= 11;
	type['>']	= 12;
	type['<']	= 13;
	type['!']	= 14;
	type['.']	= 15;
	type['(']	= 16;
	type[')']	= 17;


	//white spaces are 24;
	type[10] = 24;
	type[13] = 24;
	type[32] = 24;
	type['\t'] = 24;

	//others are four
	;

	for(i = 0; i < 256; i++)
	{
		if(i % 15 == 0)
			fprintf(fp, "\n");
		if(type[i] < 10)
			fprintf(fp, " %d, ", type[i]);
		else
			fprintf(fp, "%d, ", type[i]);

	}
	fclose(fp);
	*/
}

⌨️ 快捷键说明

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