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

📄 accidenceanalysis.cpp

📁 自己在学习编译原理时用vc++写的算符优先分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// AccidenceAnalysis.cpp:implementation of the CAccidenceAnalysis class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "MyFormulary.h"
#include "AccidenceAnalysis.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

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

CAccidenceAnalysis::CAccidenceAnalysis()
{

}

CAccidenceAnalysis::~CAccidenceAnalysis()
{

}

void CAccidenceAnalysis::GetTablePointer(CTable *pt)
{
	m_pTable=pt;
}

int CAccidenceAnalysis::PreCheck(CString &source, CString &destination)
{
	destination=_T("");
	STATUS Status=COMMON;
	int nError=0;
	int nCurrPos=0,nLength=source.GetLength();
	int nCount=0;

	while(nCurrPos<nLength)
	{
		switch(Status)
		{
		case COMMON:
			switch(source.GetAt(nCurrPos))
			{
			case ' ':
				Status=SPACE;
				destination+=' ';
				break;
			case '/':
				if(++nCurrPos==nLength)destination+='/';
				else if(source.GetAt(nCurrPos)=='/')Status=SINGLELINE_REMARK;
				else if(source.GetAt(nCurrPos)=='*')Status=MULTILINE_REMARK;
				else destination+='/';
				break;
			case '\'':
				Status=SINGLE_QUOTATION;
				destination+='\'';
				break;
			case '\"':
				Status=DOUBLE_QUOTATION;
				destination+='\"';
				break;
			default:
				destination+=source.GetAt(nCurrPos);
				nCurrPos++;
			}
			break;
		case SPACE:
			while(++nCurrPos<nLength && source.GetAt(nCurrPos)==' ');
			Status=COMMON;
			break;
		case SINGLELINE_REMARK:
			while(++nCurrPos<nLength && source.GetAt(nCurrPos)!='\r');
			Status=COMMON;
			break;
		case MULTILINE_REMARK:
			while(++nCurrPos<nLength)
			{
				if(source.GetAt(nCurrPos)=='*' && nCurrPos<nLength-1 && source.GetAt(nCurrPos+1)=='/')
				{
					nCurrPos+=2;
					Status=COMMON;
					break;
				}
				else if(source.GetAt(nCurrPos)=='\r' || source.GetAt(nCurrPos)=='\n')destination+=source.GetAt(nCurrPos);
			}
			if(Status==MULTILINE_REMARK)
			{
				nError++;
				destination+=_T("|错误:多行注释不完整|");
				nCurrPos++;
				Status=COMMON;
			}
			break;
		case SINGLE_QUOTATION:
			nCount=0;
			while(++nCurrPos<nLength)
			{
				if(source.GetAt(nCurrPos)=='\\')
				{
					if(++nCurrPos<nLength)
					{
						nCount++;
						destination+='\\';
						switch(source.GetAt(nCurrPos))
						{
						case 'a':
						case 'b':
						case 'f':
						case 'n':
						case 'r':
						case 't':
						case 'v':
						case '\\':
						case '\'':
						case '\"':
						case '?':
						case '0':
							destination+=source.GetAt(nCurrPos);
							break;
						default:
							nError++;
							destination+=_T("|错误:应写作\\\\|");
							destination+=source.GetAt(nCurrPos);
						}
					}
					else break;
				}
				else if(source.GetAt(nCurrPos)=='\'')
				{
					destination+=source.GetAt(nCurrPos++);
					if(nCount!=1)
					{
						nError++;
						if(nCount)destination+=_T("|错误:字符个数过多|");
						else destination+=_T("|错误:字符个数过少|");
					}
					Status=COMMON;
					break;
				}
				else if(source.GetAt(nCurrPos)=='\"')
				{
					nCount++;
					nError++;
					destination+='\"';
					destination+=_T("|错误:应写作\\\"|");
				}
				else if(source.GetAt(nCurrPos)=='\r')break;
				else
				{
					nCount++;
					destination+=source.GetAt(nCurrPos);
				}
			}
			if(Status==SINGLE_QUOTATION)
			{
				nError++;
				destination+=_T("|错误:字符常量不完整|");
				Status=COMMON;
			}
			break;
		case DOUBLE_QUOTATION:
			while(++nCurrPos<nLength)
			{
				if(source.GetAt(nCurrPos)=='\\')
				{
					if(++nCurrPos<nLength)
					{
						destination+='\\';
						switch(source.GetAt(nCurrPos))
						{
						case 'a':
						case 'b':
						case 'f':
						case 'n':
						case 'r':
						case 't':
						case 'v':
						case '\\':
						case '\'':
						case '\"':
						case '?':
						case '0':
							destination+=source.GetAt(nCurrPos);
							break;
						default:
							nError++;
							destination+=_T("|错误:应写作\\\\|");
							destination+=source.GetAt(nCurrPos);
						}
					}
					else break;
				}
				else if(source.GetAt(nCurrPos)=='\"')
				{
					destination+=source.GetAt(nCurrPos++);
					Status=COMMON;
					break;
				}
				else if(source.GetAt(nCurrPos)=='\'')
				{
					nError++;
					destination+='\'';
					destination+=_T("|错误:应写作\\\'|");
				}
				else if(source.GetAt(nCurrPos)=='\r')break;
				else destination+=source.GetAt(nCurrPos);
			}
			if(Status==DOUBLE_QUOTATION)
			{
				nError++;
				destination+=_T("|错误:字符串常量不完整|");
				Status=COMMON;
			}
		}
	}
	return nError;
}

int CAccidenceAnalysis::Analysis(CString &source,CWordArray &output,CString &destination)
{
	m_pTable->TablesInitialization();
	output.RemoveAll();
	destination=_T("");

	CTable::WORDTYPE Wordtype=CTable::UNKNOWN;
	BYTE chCurrChar='\0';
	CString CurrString=_T("");
	CString Temp=_T("");
	WORD wNumber=0;
	DWORD dwCurrNum=0;
	double fCurrNum=0;
	int nCount=0,nError=0;
	int nIndex=0,nSize=0;
	int nCurrPos=0,nLength=source.GetLength();

	while(nCurrPos<nLength)
	{
		while((chCurrChar=source.GetAt(nCurrPos))<=' ')if(++nCurrPos==nLength)break;

		if(chCurrChar<=' ')break;
		else if(chCurrChar=='\'')Wordtype=CTable::CHARACTER;
		else if(chCurrChar=='\"')Wordtype=CTable::STRING;
		else if(chCurrChar=='#')Wordtype=CTable::COMMAND;
		else if(chCurrChar=='_' || (chCurrChar>='A' && chCurrChar<='Z') || (chCurrChar>='a' && chCurrChar<='z'))Wordtype=CTable::KEYWORD;
		else if((chCurrChar>='0' && chCurrChar<='9') || (chCurrChar=='.' && nCurrPos+1<nLength && source.GetAt(nCurrPos+1)>='0' && source.GetAt(nCurrPos+1)<='9'))Wordtype=CTable::REAL;
		else Wordtype=CTable::OPERATOR;

		do
			switch(Wordtype)
			{
			case CTable::COMMAND:
				CurrString=chCurrChar;
				while(++nCurrPos<nLength)
				{
					chCurrChar=source.GetAt(nCurrPos);
					if(chCurrChar=='_' || (chCurrChar>='0' && chCurrChar<='9') || (chCurrChar>='A' && chCurrChar<='Z') || (chCurrChar>='a' && chCurrChar<='z'))CurrString+=chCurrChar;
					else break;
				}
				nSize=m_pTable->m_CommandTable.GetSize();
				for(nIndex=0;nIndex<nSize;nIndex++)
					if(CurrString==m_pTable->m_CommandTable.GetAt(nIndex))
					{
						wNumber=CTable::COMMAND+nIndex;
						Wordtype=CTable::UNKNOWN;
						break;
					}
				if(Wordtype==CTable::COMMAND)
				{
					nError++;
					wNumber=CTable::ERRORCODE;
					CurrString=_T("|错误:未知命令字 ")+CurrString+_T(" |");
					Wordtype=CTable::UNKNOWN;
				}
				output.Add(wNumber);
				Temp.Format("%.2d.命令字:",output.GetSize());
				destination+=Temp;
				destination+=CurrString;
				destination+=_T("\r\n");
				break;
			case CTable::KEYWORD:
				CurrString=chCurrChar;
				while(++nCurrPos<nLength)
				{
					chCurrChar=source.GetAt(nCurrPos);
					if(chCurrChar=='_' || (chCurrChar>='0' && chCurrChar<='9') || (chCurrChar>='A' && chCurrChar<='Z') || (chCurrChar>='a' && chCurrChar<='z'))CurrString+=chCurrChar;
					else break;
				}
				nSize=m_pTable->m_KeywordTable.GetSize();
				for(nIndex=0;nIndex<nSize;nIndex++)
					if(CurrString==m_pTable->m_KeywordTable.GetAt(nIndex))
					{
						wNumber=CTable::KEYWORD+nIndex;
						Wordtype=CTable::UNKNOWN;

⌨️ 快捷键说明

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