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

📄 gramanalysis.cpp

📁 表达式分析, 支持算术运算,括号,关系运算,逻辑运算,字符串的like运算等。采用了有限自动机做词法分析, 语法分析用算符优先分析方法
💻 CPP
字号:

#include "GramAnalysis.h"

char priorTable[24][24] = 
{
-1,  -2, -3, -4,1,1,-5,-6,-7,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
-8,  -9,-10,-11,1,1,-12,-13,-14,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
-15,-16,-17,-18,1,1,-19,-20,-21,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
  2,  2,  2,2,3,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,
-22,-23,-24,-25,1,1,-26,-27,-28,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
3,-29,3,-30,1,-31,-32,-33,-34,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,
2,2,2,2,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,3
};


bool				cGramAnalysis::IsUnaryOP(unsigned char preOP)
{
	switch(preOP)
	{
	case LPARENTHESES:
	case POSTIVE:
	case NEGTIVE:
	case NOT:
	case MULTI:
	case DIV:
	case REMAINDER:
	case PLUS:
	case MINUS:
	case GREAT:
	case LESS:
	case GREATEQ:
	case LESSEQ:
	case LIKE:
	case EQ:
	case NOTEQ:
	case AND:
	case OR:
	case EOI:	
		return true;
	default:
		return false;
	}
	return false;
}

cGramAnalysis::cGramAnalysis()
{
	m_tokenList.clear();
	strcpy(m_error_msg, "No error appeared!");
}

TokenList *		cGramAnalysis::GrammerAnalysis(char* strsource)
{
	Token token;
	bool  b_result;
	Token preTokenType;
	int j, k;
	k = 0; 
	preTokenType.type = EOI;
	m_stack[k] = preTokenType;
	cSourceString source(strsource);
	b_result = m_la.SetSource(&source);
	if(!b_result)
	{
		strcpy(m_error_msg, "Set source error!");
		return NULL;
	}
	do
	{	
		b_result = m_la.GetToken(token);
		if(!b_result)
		{
			strcpy(m_error_msg, m_la.GetLastError());
			return NULL;
		}
		(m_stack[k].type >= 0 && m_stack[k].type <= EOI) ? j = k : j = k-1;
		if(token.type == PLUS || token.type == MINUS)
		{
			if(IsUnaryOP(m_stack[j].type))
				token.type = (token.type == PLUS) ? POSTIVE : NEGTIVE;
		}
		while(priorTable[m_stack[j].type][token.type] == 1)
		{
			int temp = k;
			do
			{
				preTokenType = m_stack[j];
				(m_stack[j-1].type >= 0 && m_stack[j-1].type <= EOI) ? j -= 1 : j -= 2;
			}while(priorTable[m_stack[j].type][preTokenType.type] != 2);
			k = j+1;
			/////////////////////////////

			if(temp == k)
			{
				m_tokenList.push_back(m_stack[temp]);
			}
			else if(temp == k+1)
			{
				if(m_stack[temp].type != VNOTTERMINAL)
					m_tokenList.push_back(m_stack[temp]);
				m_tokenList.push_back(m_stack[temp-1]);
			}
			else if(temp == k+2)
			{
				if(m_stack[temp].type != RPARENTHESES)
				{
					if(m_stack[temp-2].type != VNOTTERMINAL)
						m_tokenList.push_back(m_stack[temp-2]);
					if(m_stack[temp].type != VNOTTERMINAL)
						m_tokenList.push_back(m_stack[temp]);
					m_tokenList.push_back(m_stack[temp-1]);

				}
			}
			else
			{
				strcpy(m_error_msg, "Fatal error!");
				return NULL;
			}
			preTokenType.type = VNOTTERMINAL;
			m_stack[k] = preTokenType;
		}
		if(priorTable[m_stack[j].type][token.type] >= 2)
		{
			k++;
			m_stack[k] = token;
		}
		else
		{
			strcpy(m_error_msg, "Grammer analysis error!");
			return NULL;
		}
	}while(token.type != EOI);
	if(k == 2 && m_stack[k].type == EOI)
		return &m_tokenList;
	return NULL;
}

char *				cGramAnalysis::GetLastErrer()
{
	return m_error_msg;
}

⌨️ 快捷键说明

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