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

📄 analyze.cpp

📁 一门课程的proj
💻 CPP
字号:
#include "StdAfx.h"
#include ".\analyze.h"

Analyze::Analyze(void)
{
	fr.setFileName("test.txt");
	fr.OpenFile();

	strcpy(strToken, "");
	ch = ' ';
	size = 0;
}

int Analyze::getNextWord()
{
	if( fr.IsEnd() )
		return -1;
	if( fr.IsLineEnd() )
		fr.ReadLine();
	return StepForward();
}

void Analyze::Close()
{
	fr.CloseFile();;
}

int Analyze::StepForward()
{
	strcpy(strToken, "");
	int code = 0;
	int value = 0;
	fr.SkipBlank();
	ch = fr.getFirstChar();
	if(IsDigit() || ch == '.' )
	{
		while(IsDigit())
		{
			Concat();
			ch = fr.getFirstChar();
		}
		if( ch == '.' )
		{
			Concat();
			ch = fr.getFirstChar();
			if(!IsDigit())
			{
				this->ProcError();
				return -1;
			}
			while(IsDigit())
			{	
				Concat();
				ch = fr.getFirstChar();
			}
			Retract(); 
			if(strToken[0] == '.')//$Real
			{
				return 200;
			}
			return 200;
		}
		Retract();//$Int
		return 100;
	} 
	else if( IsLetter() )
	{
		Concat();
		ch = fr.getFirstChar();
		
		while(IsDigit() || IsLetter())
		{
			Concat();
			ch = fr.getFirstChar();
		}
		if( strcmp(strToken, "END") == 0 )
		{
			if( ch == '.')//$END.
			{
				return 1420;
			}
			else 
			{
				Retract();		
			}
		}
		code = Reserve();
		if( code == -1 )								//非保留字
		{
			value = InsertID();//$ID
			return 700;
		}
		else 
		{
			if(code == 0)//$TRUE
			{
				return 301;
			}
			else if(code == 1)//$FALSE
			{
				return 302;
			}
			else if( code == 2)
			{
				//	record<<"($OPE_LOG, 1)"<<endl;
				return	601;
			}
			else if(code == 3)
			{
				//	record<<"($OPE_LOG, 2)"<<endl;
				return 602;
			}
			else if( code == 4)
			{
				//	record<<"($OPE_LOG, 3)"<<endl;
				return 603;
			}
			//	record<<"($"<<strToken<<",-)"<<endl;
			return code;
		}
	}
	else if( ch == ':')
	{
		ch = fr.getFirstChar();
		if( ch == '=')
		{
			//	record<<"($ASSIGN, -)"<<endl;
			return 1500;
		}
		else
		{
			Retract();
			return 1800;
		}
	}
	else if( ch == '=')
	{
		//	record<<"($OPE_REL, 5)"<<endl;
		return 505;
	}
	else if( ch == '>')
	{
		ch = fr.getFirstChar();
		if( ch == '=') 
		{
			//	record<<"($OPE_REL, 3)"<<endl;
			return 504;
		}
		Retract();
		//	record<<"($OPE_REL, 4)"<<endl;
		return 503;
	}
	else if( ch == '<')
	{
		ch = fr.getFirstChar();
		if( ch == '=')
		{
			//	record<<"($OPE_REL, 2)"<<endl;
			return 502;
		}
		else if( ch == '>')
		{
			//	record<<"($OPE_REL, 6)"<<endl;
			return 506;
		}
		Retract();
		//record<<"($OPE_REL, 1)"<<endl;
		return 501;
	}
	else if( ch == '+')
	{
		//	record<<"($OPE_ARI, 1)"<<endl;
		return 401;
	}
	else if( ch == '-')
	{
		//	record<<"($OPE_ARI, 2)"<<endl;
		return 402;
	}
	else if( ch == '/')
	{
		//	record<<"($OPE_ARI, 4)"<<endl;
		return 404;
	}
	else if( ch == '*')
	{
		//	record<<"($OPE_ARI, 3)"<<endl;
		return 403;
	}
	else if( ch == '[')
	{
		//	record<<"($LPAR_M, -)"<<endl;
		return 1000;
	}
	else if( ch == ']')
	{
		//	record<<"($RPAR_M, -)"<<endl;
		return 1100;
	}
	
	else if( ch == ';')
	{
		//	record<<"($SEMICOLON, -)"<<endl;
		return 1600;
	}
	else if( ch == '(')
	{
		//	record<<"($LPAR_S, -)"<<endl;
		return 800;
	}
	else if( ch == ')')
	{
		//	record<<"($RPAR_S, -)"<<endl;
		return 900;
	}
	else if( ch == '{')
	{
		//	record<<"($LPAR_L, -)"<<endl;
		return 1200;
	}
	else if( ch == '}')
	{
		//	record<<"($RPAR_L, -)"<<endl;
		return 1300;
	}
	else if( ch == ',')
	{
		return 1700;
	}
	else 
	{
		ProcError();
		return -1;
	}
}

void Analyze::ProcError()
{
	record<<"error:错误产生在第"<<fr.getLineNumber()<<"行处";
	if( strlen(strToken) == 0 )
		record<<ch<<endl;
	record<<strToken<<endl;
}

int Analyze::CheckIDExist()
{
	for( int i = 0 ; i < _global.v_table.size() ; i++)
		if( _global.v_table[i].name.compare((string)strToken) == 0 )//equal
			return i;
	return 0;
}

int Analyze::InsertID()
{
	int s = this->CheckIDExist();
	if(s != 0)
	{
		attribute *newAttr = new attribute();
		newAttr->att_name = "checkID";
		newAttr->content = _global.v_table[s].name;
		newAttr->att_value = _global.v_table[s].type;
		_global.att.push(*newAttr);
		return s;
	}
	attribute *newAttr = new attribute();
	newAttr->att_name = "newID";
	newAttr->content = (string)strToken;
	_global.att.push(*newAttr);
}

void Analyze::Retract()
{
	fr.backwardReadPosition(1);
	ch = ' ';
}

int Analyze::Reserve()
{
	return _sm.getInt((string)strToken);
}

int Analyze::Concat()
{
	strncat(this->strToken, &ch, 1);
	if(strlen(strToken) >= STRING_SIZE)
		return 1;
	return 0;
}

int Analyze::IsDigit()
{
	int temp = (int)ch;
	if(temp <= 57 && temp >= 48)
		return 1;
	return 0;
}

int Analyze::IsLetter()
{
	int temp = (int)ch;
	if(temp <= 90 && temp >= 65)
		return 1;
	if(temp <= 122 && temp >= 97)
		return 1;
	return 0;
}

anaStatus::anaStatus()
{
	status_num = 0;
	next = NULL;
}

anaSymbol::anaSymbol()
{
	symbol = 0;
	next = NULL;
}

anaStack::anaStack()
{
	content = NULL;
	_symbol = NULL;
}

void anaStack::outputStatus()
{
	cout<<"输出状态栈的内容: ";
	anaStatus *first = content;
	while(first != NULL)
	{
		cout<<first->status_num<<",";
		first = first->next;
	}
}

void anaStack::outputSymbol()
{
	symbol_mapping temp;
	string name;
	cout<<"输出符号栈的内容: ";
	anaSymbol *first = _symbol;
	while(first != NULL)
	{
		name = temp.getName(first->symbol);
		cout<<name<<",";
		first = first->next;
	}
}

int anaStack::IsSymbolEmpty()
{
	if(_symbol == NULL)
		return 1;
	return 0;
}

int anaStack::IsEmpty()
{
	if(content == NULL)
		return 1;
	return 0;
}

void anaStack::PopSymbolIn(anaSymbol *src)
{
	anaSymbol *temp = new anaSymbol();
	temp->symbol = src->symbol;
	temp->next = _symbol;
	_symbol = temp;
}

void anaStack::PopIn(anaStatus *src)
{
	anaStatus *temp = new anaStatus();
	temp->status_num = src->status_num;
	temp->next = content;
	content = temp;
}

void anaStack::PushSymbolOut(anaSymbol *dst, int num)
{
	anaSymbol *temp = _symbol;
	while( num > 0 )
	{
		_symbol = _symbol->next;
		delete temp;
		temp = _symbol;
		num--;
	}
}

void anaStack::PushOut(anaStatus *dst, int num)
{
	anaStatus *temp = content;
	while( num > 0 )
	{
		content = content->next;
		delete temp;
		temp = content;
		num--;
	}
}

⌨️ 快捷键说明

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