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

📄 parsing.cpp

📁 简单的有穷自动机词法分析
💻 CPP
字号:
// Parsing.cpp: implementation of the CParsing class.
//
//////////////////////////////////////////////////////////////////////

#include "Parsing.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CParsing::CParsing()
{
	//m_nStackDepth = 0;
	m_pStackNode = NULL;
	m_nNodeLength = 0;
	//m_iSentenceStartPos = 0;

	m_priorityTable[0][0] = 2;m_priorityTable[0][1] = -1;m_priorityTable[0][2] = -1;
	m_priorityTable[0][3] = -1;m_priorityTable[0][4] = -1;m_priorityTable[0][5] = -1;
	m_priorityTable[0][6] = -2;
	m_priorityTable[1][0] = 1;m_priorityTable[1][1] = 1;m_priorityTable[1][2] = 1;
	m_priorityTable[1][3] = -1;m_priorityTable[1][4] = -1;m_priorityTable[1][5] = -1;
	m_priorityTable[1][6] = 1;
	m_priorityTable[2][0] = 1;m_priorityTable[2][1] = 1;m_priorityTable[2][2] = 1;
	m_priorityTable[2][3] = -1;m_priorityTable[2][4] = -1;m_priorityTable[2][5] = -1;
	m_priorityTable[2][6] = 1;
	m_priorityTable[3][0] = 1;m_priorityTable[3][1] = 1;m_priorityTable[3][2] = 1;
	m_priorityTable[3][3] = 1;m_priorityTable[3][4] = 1;m_priorityTable[3][5] = -1;
	m_priorityTable[3][6] = 1;
	m_priorityTable[4][0] = 1;m_priorityTable[4][1] = 1;m_priorityTable[4][2] = 1;
	m_priorityTable[4][3] = 1;m_priorityTable[4][4] = 1;m_priorityTable[4][5] = -1;
	m_priorityTable[4][6] = 1;
	m_priorityTable[5][0] = -2;m_priorityTable[5][1] = -1;m_priorityTable[5][2] = -1;
	m_priorityTable[5][3] = -1;m_priorityTable[5][4] = -1;m_priorityTable[5][5] = -1;
	m_priorityTable[5][6] = 0;
	m_priorityTable[6][0] = -2;m_priorityTable[6][1] = -2;m_priorityTable[6][2] = -2;
	m_priorityTable[6][3] = -2;m_priorityTable[6][4] = -2;m_priorityTable[6][5] = -2;
	m_priorityTable[6][6] = -2;
}

CParsing::~CParsing()
{
	
}
void  CParsing::Create(SResultNode *pNode,int nLength)
{
	m_pStackNode = new SStackNode[nLength];
	m_nNodeLength = nLength;
	for(int i=0;i<nLength;i++)
	{
		if(pNode[i].m_iCode == 1)
		{
			m_pStackNode[i].iStackID = 1;
			m_pStackNode[i].iCode    = 2;
		}
		else if(pNode[i].m_iCode == 2)
		{
			m_pStackNode[i].iStackID = 1;
			m_pStackNode[i].iCode    = 1;
		}
		else if(pNode[i].m_iCode == 3)
		{
			m_pStackNode[i].iStackID = 2;
			if(pNode[i].m_pWord[0] == '+')m_pStackNode[i].iCode = 1;
			else if(pNode[i].m_pWord[0] == '-')m_pStackNode[i].iCode = 2;
			else if(pNode[i].m_pWord[0] == '*')m_pStackNode[i].iCode = 3;
			else if(pNode[i].m_pWord[0] == '/')m_pStackNode[i].iCode = 4;
			else if(pNode[i].m_pWord[0] == '=')m_pStackNode[i].iCode = -1;
			else {cout<<"错误输入!"<<endl;return;}
		}
		else if(pNode[i].m_iCode == 4)
		{
			m_pStackNode[i].iStackID = 2;
			if( pNode[i].m_pWord[0] == ';' )m_pStackNode[i].iCode = 0;
			else if( pNode[i].m_pWord[0] == '(' )m_pStackNode[i].iCode = 5;
			else if(pNode[i].m_pWord[0] == ')')m_pStackNode[i].iCode = 6;
			else {cout<<"错误的输入!";return;}
		}
		else {cout<<"错误输入!";return;}
	}
}

bool  CParsing::PerSentenceAnalyzing(int iBeginPos,int iEndPos)
{
	if(m_pStackNode[iBeginPos].iStackID !=1 || m_pStackNode[iBeginPos].iCode !=2 )
		return false;
	if(m_pStackNode[iBeginPos+1].iStackID !=2 || m_pStackNode[iBeginPos+1].iCode !=-1)
		return false;
	int nLength = iEndPos-iBeginPos+1;//栈的长度
	int iBegin  = iBeginPos+2;		//语法分析的开始位置
	m_stackData.Create(nLength);
	m_stackOperator.Create(nLength);
	SStackNode node;
	node.iStackID = 2;
	node.iCode = 0;
	m_stackOperator.Push(node);
	for(int i= iBegin;i<=iEndPos;i++)
	{
		if(m_pStackNode[i].iStackID ==1 )
			m_stackData.Push(m_pStackNode[i]);
		else
		{
			if(!m_stackOperator.IsEmpty())
			{
				m_stackOperator.GetTop(node);
				if(m_priorityTable[node.iCode][m_pStackNode[i].iCode] == -2)return false;
				else if(m_priorityTable[node.iCode][m_pStackNode[i].iCode] == -1)m_stackOperator.Push(m_pStackNode[i]);
				else if(m_priorityTable[node.iCode][m_pStackNode[i].iCode] == 0)m_stackOperator.Popup();
				else if(m_priorityTable[node.iCode][m_pStackNode[i].iCode] == 1)
				{
					m_stackOperator.Popup();
					if(!m_stackData.IsEmpty()){m_stackData.Popup();}
					else return false;
					i--;
				}
				else if(m_priorityTable[node.iCode][m_pStackNode[i].iCode] == 2)
				{
					m_stackData.Popup();
					if(m_stackData.IsEmpty())return true;
					return false;
				}
			}
			
		}
	}

	return false;
}

void  CParsing::ProgramAnalyzing()
{	
	int iSentences = 1;
	int iBeginPos = 0;
	for(int i=0;i<m_nNodeLength;i++)
	{
		if((m_pStackNode[i].iStackID == 2 && m_pStackNode[i].iCode == 0)||i==m_nNodeLength-1)
		{
			//cout<<"begin:"<<iBeginPos<<" "<<"end:"<<i<<endl;
			if( !PerSentenceAnalyzing(iBeginPos,i) )cout<<"语句"<<iSentences<<"错误!"<<endl;
			iBeginPos = i+1;
			iSentences++;
		}
	}
}
void  CParsing::Destroy()
{

}

void   CParsing::display()
{
	for(int i=0;i<m_nNodeLength;i++)
	{
		cout<<m_pStackNode[i].fWorth<<" "<<m_pStackNode[i].iCode<<" "<<m_pStackNode[i].iStackID<<endl;
	}
	for(i=0;i<7;i++)
	{
		for(int j=0;j<7;j++)
		{
			cout<<m_priorityTable[i][j]<<" ";
		}
		cout<<endl;
	}
}

⌨️ 快捷键说明

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