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

📄 pascalsintanalyzer.cpp

📁 C++ mfc 源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// PascalSintAnalyzer.cpp: implementation of the CPascalCompiler class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "zscpascal.h"
#include "PascalSintAnalyzer.h"

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

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

CPascalCompiler::CPascalCompiler(CString str): CPascalLexAnalyzer(str)
{
	m_SymbTable.InitHashTable( MAX_SYMB );
}

CPascalCompiler::~CPascalCompiler()
{
	SymbolList *list = NULL;
	CString key;
	POSITION pos = m_SymbTable.GetStartPosition();
	while (pos != NULL)
	{
		m_SymbTable.GetNextAssoc(pos,key,list);
		delete list;
	}
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::ProgramSursa()
{
	if (NextToken() != TT_KW_PROGRAM)
		throw error(SET_EXPECTED,CString("\"program\""));
	
	if (NextToken() != TT_WORD)
		throw error(SET_EXPECTED, CString("identifier"));
	// proceseaza nume program

	if (NextToken() != ';')
		throw error(SET_EXPECTED, CString(";"));

	Bloc();

	if (NextToken() != '.')
		throw error(SET_EXPECTED, CString("."));
}
/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::Bloc()
{
	int val;
	while  (1)
	{
		if ((val = NextToken()) == TT_KW_CONST)
			ListaDeclConst();
		else
			if (val == TT_KW_VAR)
				ListaDeclVar();
			else 
				if (val == TT_KW_PROCEDURE)
				{
					AntetProc();
					Bloc();
					if (NextToken() != ';')
						throw error(SET_EXPECTED, CString(";"));
				}
				else 
					if (val == TT_KW_FUNCTION)
					{
						AntetFunc();
						Bloc();
						if (NextToken() != ';')
							throw error(SET_EXPECTED, CString(";"));
					}
					else
					{
						PushBack();
						InstrCompusa();
						break;
					}
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::ListaDeclConst()
{
	while (1)
	{
		DeclConst();
		if (NextToken() != ';')
			throw error(SET_EXPECTED, CString(";"));
		if (NextToken() != TT_WORD)
		{
			PushBack();
			break;
		}
		PushBack();
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::DeclConst()
{
	if (NextToken() != TT_WORD)
		throw error(SET_EXPECTED, CString("identifier"));
	if (NextToken() != '=')
		throw error(SET_EXPECTED, CString("="));
	ExprStatica();
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::ListaDeclVar()
{
	while (1)
	{
		DeclVar();
		if (NextToken() != ';')
			throw error(SET_EXPECTED, CString(";"));
		if (NextToken() != TT_WORD)
		{
			PushBack();
			break;
		}
		PushBack();
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::AntetProc()
{
	if (NextToken()!= TT_WORD)
		throw error(SET_EXPECTED, CString("identifier"));
	// insert symbol to the symbol table
	Symbol s;
	s.m_sName = GetStrValue();
	s.m_nClass = CT_PROCEDURE;
	
	InsertSymbol(s);
	
	
	
	if (NextToken()== ';')
		return;
	PushBack();
	Parametri();
	if (NextToken()!= ';')
		throw error(SET_EXPECTED, CString(";"));
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::AntetFunc()
{
	if (NextToken()!= TT_WORD)
		throw error(SET_EXPECTED, CString("identifier"));;
	// insereaza symbol in tabela de symboluri
	Symbol s;
	s.m_sName = GetStrValue();
	s.m_nClass = CT_FUNCTION;
	
	InsertSymbol(s);
	if (NextToken()!= ':')
	{
		PushBack();
		Parametri();
		if (NextToken()!= ':')
			throw error(SET_EXPECTED, CString(":"));
	}
	TipSimplu();
	if (NextToken()!= ';')
		throw error(SET_EXPECTED, CString(";"));
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::Tip()
{
	int val = NextToken();
	if (val == TT_KW_INTEGER
		|| val == TT_KW_REAL
		|| val == TT_KW_CHAR)
	{
		PushBack();
		TipSimplu();
	}
	if (val == TT_KW_ARRAY)
	{
		PushBack();
		TipTablou();
	}
	if (val == TT_KW_RECORD)
	{
		PushBack();
		TipRecord();
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::TipSimplu()
{
	int val = NextToken();
	if (val == TT_KW_INTEGER)
	{
		;
	}
	else 
		if (val == TT_KW_REAL)
		{
			;
		}
		else
			if (val == TT_KW_CHAR)
			{
				;
			}
			else 
				throw error(SET_EXPECTED, CString("Basic Type"));;
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::Parametri()
{
	if (NextToken()!= '(')
		throw error(SET_EXPECTED, CString("("));;
	while (1)
	{
		DeclParam();
		if (NextToken()!= ';')
		{
			PushBack();
			break;
		}
	}
	if (NextToken()!= ')')
		throw error(SET_EXPECTED, CString(")"));;
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::DeclParam()
{
	if (NextToken() == TT_KW_VAR)
		NextToken();
	PushBack();
	while (1)
	{
		if (NextToken() != TT_WORD)
			throw error(SET_EXPECTED, CString("identifier"));;
		if (NextToken()!=',')
		{
			PushBack();
			break;
		}
	}
	
	if (NextToken() != ':')
			throw error(SET_EXPECTED, CString(":"));;

	TipSimplu();
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::InstrCompusa()
{
	if (NextToken()!= TT_KW_BEGIN)
		throw error(SET_EXPECTED, CString("begin"));;
	while (1)
	{
		Instr();
		if (NextToken()==TT_KW_END)
		{
			PushBack();
			break;
		}
		PushBack();
		if (NextToken()==';')
			if (NextToken()==TT_KW_END)
			{
				PushBack();
				break;
			}
			else PushBack();
		else throw error(SET_EXPECTED, CString("end or ;"));
	}
	if (NextToken()!=TT_KW_END)
		throw error(SET_EXPECTED, CString("end"));
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::ExprStatica()
{
	while (1)
	{
		TermenStatic();
		int val = NextToken();
		if (val!='+' && val != '-')
		{
			PushBack();
			break;
		}
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::DeclVar()
{
	while (1)
	{
		if (NextToken() != TT_WORD)
			throw error(SET_EXPECTED, CString("identifier"));
		if (NextToken()!=',')
		{
			PushBack();
			break;
		}
	}
	if (NextToken()!=':')
		throw error(SET_EXPECTED, CString(":"));
	Tip();
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::TipTablou()
{
	if (NextToken() != TT_KW_ARRAY)
		throw error(SET_EXPECTED, CString("array"));
	if (NextToken() != '[')
		throw error(SET_EXPECTED, CString("["));

	if (NextToken()	!= TT_NUMBER)	// trebuie modificat pentru numar zecimal
		throw error(SET_EXPECTED, CString("number"));

	if (NextToken() != '.')
		throw error(SET_EXPECTED, CString(".."));
	if (NextToken() != '.')
		throw error(SET_EXPECTED, CString(".."));
	
	if (NextToken() != TT_NUMBER)	// trebuie modificat pentru numar zecimal
		throw error(SET_EXPECTED, CString("number"));
	
	if (NextToken() != ']')
		throw error(SET_EXPECTED, CString("]"));
	if (NextToken() != TT_KW_OF)
		throw error(SET_EXPECTED, CString("of"));
	TipSimplu();	
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::TipRecord()
{
	if (NextToken() != TT_KW_RECORD)
		throw error(SET_EXPECTED, CString("record"));;
	while (1)
	{
		DeclCamp();
		if (NextToken() != ';')
		{
			PushBack();
			break;
		}
	}
	if (NextToken() != TT_KW_END)
		throw error(SET_EXPECTED, CString("end"));;
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::DeclCamp()
{
	while (1)
	{
		if (NextToken()!= TT_WORD)
			throw error(SET_EXPECTED, CString("identifier"));
		if (NextToken()!= ',')
		{
			PushBack();
			break;
		}
	}
	if (NextToken()!=':')
		throw error(SET_EXPECTED, CString(":"));
	TipSimplu();

}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/

void CPascalCompiler::TermenStatic()
{
	while (1)
	{
		FactorStatic();
		int val = NextToken();
		if (val!='+' && val != '-' && val!=TT_KW_MOD && val != TT_KW_DIV)
		{
			PushBack();
			break;
		}
	}
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::FactorStatic()
{
	int val = NextToken();
	if (val == TT_WORD)
		return;
	if (val == TT_NUMBER)
		return;
	if (val == TT_STRING)
		return;
	if (val == '(')
	{
		ExprStatica();
		if (NextToken()!=')')
			throw error(SET_EXPECTED, CString(")"));
		return;
	}
	throw error(SET_EXPECTED, CString("Identifier or number or constant"));
}

/*
***header
***name
***memberof
***history
1.0.0 ; 1999.1.7 ; FZ ; Creation
***description
***notes
***uses
***var_in
***var_out
***err_out
***flags
***specifications
***documents
*/


void CPascalCompiler::Instr()
{
	int val = NextToken();
	switch (val)
	{
	case TT_KW_WHILE:
		{
			PushBack();
			InstrWhile();
			break;
		}
	case TT_KW_REPEAT:
		{
			PushBack();
			InstrRepeat();
			break;
		}
	case TT_KW_FOR:
		{
			PushBack();
			InstrFor();
			break;
		}
	case TT_KW_CASE:
		{
			PushBack();
			InstrCase();
			break;
		}
	case TT_KW_BEGIN:
		{
			PushBack();
			InstrCompusa();
			break;
		}

⌨️ 快捷键说明

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