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

📄 syntax.cpp

📁 词法分析程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Syntax.cpp: implementation of the Syntax class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "SmartC.h"
#include "Syntax.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define MYOK 202
#define ExceptToken 11111
#define UnKnowToken 22222
#define MAXLEN  200
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Syntax::Syntax(CEdit* MyEdit,Error* MyError,COutputTreeView* pView):Cifa(MyEdit,MyError)
{
	ptreeview=pView;
	//id,num
	ValueFirstSet[0]=IDSY;		ValueFirstSet[1]=DIGITSY;
	//*,/,&&,+,-,||,<=,<,>,>=,==,!=,;,),]
	ValueFollowSet[0]=STARSY;	ValueFollowSet[1]=DIVISY;	ValueFollowSet[2]=ANDSY;
	ValueFollowSet[3]=PLUSSY;	ValueFollowSet[4]=MINUSSY;	ValueFollowSet[5]=ORSY;
	ValueFollowSet[6]=LESSEQUSY;ValueFollowSet[7]=LESSSY;	ValueFollowSet[8]=GREATSY;
	ValueFollowSet[9]=GREATEQUSY;ValueFollowSet[10]=EQUSY;	ValueFollowSet[11]=UNEQUSY;
	ValueFollowSet[12]=SEMISY;	ValueFollowSet[13]=RPARSY;	ValueFollowSet[14]=RMPARSY;
	//(,id,num,!
	FactorFirstSet[0]=LPARSY;			FactorFirstSet[1]=IDSY;
	FactorFirstSet[2]=DIGITSY;			FactorFirstSet[3]=NOTSY;
	//*,/,&&,+,-,||,<=,<,>,>=,==,!=,;,)
	FactorFollowSet[0]=STARSY;	FactorFollowSet[1]=DIVISY;	FactorFollowSet[2]=ANDSY;
	FactorFollowSet[3]=PLUSSY;	FactorFollowSet[4]=MINUSSY;	FactorFollowSet[5]=ORSY;
	FactorFollowSet[6]=LESSEQUSY;FactorFollowSet[7]=LESSSY;	FactorFollowSet[8]=GREATSY;
	FactorFollowSet[9]=GREATEQUSY;FactorFollowSet[10]=EQUSY;FactorFollowSet[11]=UNEQUSY;
	FactorFollowSet[12]=SEMISY;	FactorFollowSet[13]=RPARSY;
	//(,id,num,!
	TermFirstSet[0]=LPARSY;				TermFirstSet[1]=IDSY;	
	TermFirstSet[2]=DIGITSY;			TermFirstSet[3]=NOTSY;
	//+,-,||,<=,<,>,>=,==,!=,;,)
	TermFollowSet[0]=PLUSSY;	TermFollowSet[1]=MINUSSY;	TermFollowSet[2]=ORSY;
	TermFollowSet[3]=LESSEQUSY;	TermFollowSet[4]=LESSSY;	TermFollowSet[5]=GREATSY;
	TermFollowSet[6]=GREATEQUSY;TermFollowSet[7]=EQUSY;		TermFollowSet[8]=UNEQUSY;
	TermFollowSet[9]=SEMISY;	TermFollowSet[10]=RPARSY;
	//(,id,num,!
	Simple_expressionFirstSet[0]=LPARSY;		Simple_expressionFirstSet[1]=IDSY;	
	Simple_expressionFirstSet[2]=DIGITSY;		Simple_expressionFirstSet[3]=NOTSY;
	//<=,<,>,>=,==,!=,;,)
	Simple_expressionFollowSet[0]=LESSEQUSY;	Simple_expressionFollowSet[1]=LESSSY;
	Simple_expressionFollowSet[2]=GREATSY;		Simple_expressionFollowSet[3]=GREATEQUSY;
	Simple_expressionFollowSet[4]=EQUSY;		Simple_expressionFollowSet[5]=UNEQUSY;
	Simple_expressionFollowSet[6]=SEMISY;		Simple_expressionFollowSet[7]=RPARSY;
	//(,id,num,!
	ExpressionFirstSet[0]=LPARSY;				ExpressionFirstSet[1]=IDSY;	
	ExpressionFirstSet[2]=DIGITSY;				ExpressionFirstSet[3]=NOTSY;
	//;,)
	ExpressionFollowSet[0]=SEMISY;				ExpressionFollowSet[1]=RPARSY;
	//id,;
	ForexpressionFirstSet[0]=IDSY;				ForexpressionFirstSet[1]=SEMISY;
	//;,)
	ForexpressionFollowSet[0]=SEMISY;			ForexpressionFollowSet[1]=RPARSY;
	//void,int
	ParamsFirstSet[0]=VOIDSY;					ParamsFirstSet[1]=INTSY;	
	//)
	ParamsFollowSet[0]=RPARSY;
	//void,int
	ProgrameFirstSet[0]=VOIDSY;					ProgrameFirstSet[1]=INTSY;	
	//void,int
	DeclartionFirstSet[0]=VOIDSY;				DeclartionFirstSet[1]=INTSY;	
	//void,int
	DeclartionFollowSet[0]=VOIDSY;				DeclartionFollowSet[1]=INTSY;	
	//id,if,while,for,return,{,;
	StatementFirstSet[0]=IDSY;					StatementFirstSet[1]=IFSY;
	StatementFirstSet[2]=WHILESY;				StatementFirstSet[3]=FORSY;
	StatementFirstSet[4]=RETURNSY;				StatementFirstSet[5]=LBPARSY;
	StatementFirstSet[6]=SEMISY;
	//id,if,while,for,return,{,;,Else,}
	StatementFollowSet[0]=IDSY;					StatementFollowSet[1]=IFSY;
	StatementFollowSet[2]=WHILESY;				StatementFollowSet[3]=FORSY;
	StatementFollowSet[4]=RETURNSY;				StatementFollowSet[5]=LBPARSY;
	StatementFollowSet[6]=SEMISY;				StatementFollowSet[7]=ELSESY;
	StatementFollowSet[8]=RBPARSY;
	//{
	Compound_stmtFirstSet[0]=LBPARSY;
	//id,if,while,for,return,{,;,Else,}
	Compound_stmtFollowSet[0]=IDSY;				Compound_stmtFollowSet[1]=IFSY;
	Compound_stmtFollowSet[2]=WHILESY;			Compound_stmtFollowSet[3]=FORSY;
	Compound_stmtFollowSet[4]=RETURNSY;			Compound_stmtFollowSet[5]=LBPARSY;
	Compound_stmtFollowSet[6]=SEMISY;			Compound_stmtFollowSet[7]=ELSESY;
	Compound_stmtFollowSet[8]=RBPARSY;			Compound_stmtFollowSet[9]=INTSY;
	Compound_stmtFollowSet[10]=VOIDSY;
}

Syntax::~Syntax()
{

}
//扫描源程序,直到当前token在synchset[]之中为止
//n 为synchset数组中的元素个数
int Syntax::Scanto(int synchset[],int n)
{
	int i;
	while(1)
	{
		for(i=0;i<n;i++)
			if(temptoken.tokentype==synchset[i])	
				break;
		if(i>=n)
		{
			if(GetNextToken(&temptoken)==MYEOF) 
				return MYEOF;
		}
		else
			break;
	}
	return READING;
}
//检查当前token是否在firstset之中,
//若不在就向下扫描源程序直到当前token在firstset[]或followset[]之中为止
//也就是检查程序代码是否出错,如果出错则向下读直到可以确认为新起点为止
int Syntax::Checkinput(int firstset[],int followset[],int firstnum,int follownum)
{
	int i;
	for(i=0;i<firstnum;i++)
		if(temptoken.tokentype==firstset[i])	break;
	if(i<firstnum)
		return MYOK;
	else
	{
		int *tempset=new int[firstnum+follownum];
		for(i=0;i<firstnum;i++)
			tempset[i]=firstset[i];
		for(i=0;i<follownum;i++)
			tempset[firstnum+i]=followset[i];
		i=Scanto(tempset,firstnum+follownum);
		delete [] tempset;
		return i;
	}
}
//用当前token.tokentype与tokentype比较,
//若相同则读下一个token,否则进行错误处理
int Syntax::Match(int tokentype)
{
	if(temptoken.tokentype==tokentype)
		return GetNextToken(&temptoken);
	else 
	{
		MyError(ExceptToken,GetLineNO());
		return 0;
	}
}
//匹配标识符Value的子程序
//程序处理的是一个数(通过各种方式得到的一个数)如:变量,常量,数组,函数返回值
//参阅状态转换图(自动机)
void Syntax::Value()
{
	int flag;
	char str[MAXLEN];
	char tokenstr[MAXLEN];
	//以下两个变量用在数组处理
	char ch[2];
	ch[0]='0';ch[1]=0;
	int i=0;
	flag=Checkinput(ValueFirstSet,ValueFollowSet,2,15);//
	if(flag==MYOK)
	{
		switch(temptoken.tokentype)
		{
		case IDSY:
			strcpy(tokenstr,temptoken.pchar);
			Match(IDSY);
			strcpy(str,"Value:");
			ptreeview->InsertItem(str);
			switch(temptoken.tokentype)
			{
			case LMPARSY:
				strcpy(str,"Array: ");
				strcat(str,tokenstr);
				str[MAXLEN-1]=0;
				ptreeview->InsertItem(str);
				while(temptoken.tokentype==LMPARSY)
				{
					Match(LMPARSY);
					i++;
					ch[0]=i+'0';
					strcpy(str,ch);
					strcat(str," Dimension Location: ");
					str[MAXLEN-1]=0;
					ptreeview->InsertItem(str);
					Value();
					ptreeview->GetParent();
					Match(RMPARSY);
				}
				ptreeview->GetParent(2);
				break;
			case LPARSY:
				strcpy(str,"FuctionCall: ");
				strcat(str,tokenstr);
				str[MAXLEN-1]=0;
				ptreeview->InsertItem(str);
				Match(LPARSY);
				if(temptoken.tokentype==LPARSY||temptoken.tokentype==IDSY||
						temptoken.tokentype==DIGITSY||temptoken.tokentype==NOTSY)
				{
					strcpy(str,"Params: ");
					ptreeview->InsertItem(str);
				}
				else
				{
					Match(RPARSY);
				//	Match(SEMISY);
					ptreeview->GetParent(2);
					return;
				}
				while(temptoken.tokentype==LPARSY||temptoken.tokentype==IDSY||
						temptoken.tokentype==DIGITSY||temptoken.tokentype==NOTSY)
				{
					Expression();
					if(temptoken.tokentype==COMMASY)
						Match(COMMASY);
					else
						break;
				}
				Match(RPARSY);
				//Match(SEMISY);
				ptreeview->GetParent(3);
				break;
			default:
				strcpy(str,"Var: ");
				strcat(str,tokenstr);
				str[MAXLEN-1]=0;
				ptreeview->InsertItem(str);
				ptreeview->GetParent(2);
				break;
			}
			break;
		case DIGITSY:
			strcpy(str,"Const: ");
			strcat(str,temptoken.pchar);
			str[MAXLEN-1]=0;
			ptreeview->InsertItem(str);
			ptreeview->GetParent();
			Match(DIGITSY);
			break;
		}
	}
	else
	{
		MyError(ExceptToken,GetLineNO());
		return ;
	}
	Checkinput(ValueFollowSet,NULL,15,0);
}
//匹配标识符Factor的子程序
//程序处理的是项中的一个因子,处理优先级最高的操作符(),!
//参阅状态转换图(自动机)
void Syntax::Factor()
{
	int flag;
	char str[MAXLEN];
	flag=Checkinput(FactorFirstSet,FactorFollowSet,4,14);
	if(flag!=MYOK)
	{
		MyError(ExceptToken,GetLineNO());
		return ;
	}
	else
	{
		strcpy(str,"Factor:");
		ptreeview->InsertItem(str);
		switch(temptoken.tokentype)
		{
		case LPARSY:
			Match(LPARSY);
			Expression();
			Match(RPARSY);
			break;
		case NOTSY:
			strcpy(str,"Logic: !");
			ptreeview->InsertItem(str);
			ptreeview->GetParent();
			Match(NOTSY);
			Factor();
			break;
		default:
			Value();
			break;
		}
		ptreeview->GetParent();
	}
	Checkinput(FactorFollowSet,NULL,14,0);
}
//匹配标识符Term的子程序
//程序处理的是简单表达式中的一个项,处理优先级第二高的操作符*,*,&&
//参阅状态转换图(自动机)
void Syntax::Term()
{
	int flag;
	char str[MAXLEN];
	flag=Checkinput(TermFirstSet,TermFollowSet,4,11);
	if(flag!=MYOK)
	{
		MyError(ExceptToken,GetLineNO());
		return ;
	}
	else
	{
		strcpy(str,"Term:");
		ptreeview->InsertItem(str);
		while(1)
		{
			Factor();
			if(temptoken.tokentype==ANDSY)
			{
				Match(ANDSY);
				strcpy(str,"Logic: &&");
				ptreeview->InsertItem(str);
				ptreeview->GetParent();
			}
			else if(temptoken.tokentype==DIVISY)
			{
				Match(DIVISY);
				strcpy(str,"Operation: /");
				ptreeview->InsertItem(str);
				ptreeview->GetParent();
			}
			else if(temptoken.tokentype==STARSY)
			{
				Match(STARSY);
				strcpy(str,"Operation: *");
				ptreeview->InsertItem(str);
				ptreeview->GetParent();
			}
			else
				break;
		}
		ptreeview->GetParent();
	}
	Checkinput(TermFollowSet,NULL,11,0);
}
//匹配标识符Simple_expression的子程序
//程序处理的是表达式中的简单表达式,处理优先级较低的操作符+,-,||
//参阅状态转换图(自动机)
void Syntax::Simple_expression()
{
	int flag;
	char str[MAXLEN];
	flag=Checkinput(Simple_expressionFirstSet,Simple_expressionFollowSet,4,8);
	if(flag!=MYOK)
	{
		MyError(ExceptToken,GetLineNO());
		return ;
	}
	else
	{
		strcpy(str,"Simple Expression:");
		ptreeview->InsertItem(str);
		while(1)
		{
			Term();
			if(temptoken.tokentype==ORSY)
			{
				Match(ORSY);
				strcpy(str,"Logic: ||");
				ptreeview->InsertItem(str);
				ptreeview->GetParent();
			}
			else if(temptoken.tokentype==PLUSSY)
			{
				Match(PLUSSY);
				strcpy(str,"Operation: +");
				ptreeview->InsertItem(str);
				ptreeview->GetParent();
			}
			else if(temptoken.tokentype==MINUSSY)
			{
				Match(MINUSSY);
				strcpy(str,"Operation: -");
				ptreeview->InsertItem(str);
				ptreeview->GetParent();
			}
			else
				break;
		}
		ptreeview->GetParent();
	}
	Checkinput(Simple_expressionFollowSet,NULL,8,0);
}
//匹配标识符Expression的子程序
//程序处理的是语句体中的表达式,处理优先级最低的关系操作符==,!=,>,>=,<,<=
//参阅状态转换图(自动机)
void Syntax::Expression()
{
	int flag;
	char str[MAXLEN];
	flag=Checkinput(ExpressionFirstSet,ExpressionFollowSet,4,2);
	if(flag!=MYOK)
	{
		MyError(ExceptToken,GetLineNO());
		return ;
	}
	else
	{
		strcpy(str,"Expression:");
		ptreeview->InsertItem(str);
		Simple_expression();
		switch(temptoken.tokentype)
		{
		case EQUSY:
			strcpy(str,"Relation: ==");
			ptreeview->InsertItem(str);
			Match(EQUSY);
			ptreeview->GetParent();
			Simple_expression();
			break;
		case LESSSY:
			strcpy(str,"Relation: <");
			ptreeview->InsertItem(str);
			Match(LESSSY);
			ptreeview->GetParent();
			Simple_expression();
			break;
		case LESSEQUSY:
			strcpy(str,"Relation: <=");
			ptreeview->InsertItem(str);
			Match(LESSEQUSY);
			ptreeview->GetParent();
			Simple_expression();
			break;
		case  GREATSY:
			strcpy(str,"Relation: >");
			ptreeview->InsertItem(str);
			Match(GREATSY);
			ptreeview->GetParent();
			Simple_expression();
			break;
		case GREATEQUSY:
			strcpy(str,"Relation: >=");
			ptreeview->InsertItem(str);
			Match(GREATEQUSY);
			ptreeview->GetParent();
			Simple_expression();
			break;
		case UNEQUSY	:
			strcpy(str,"Relation: !=");
			ptreeview->InsertItem(str);
			Match(UNEQUSY);
			ptreeview->GetParent();
			Simple_expression();
			break;
		default:
			break;
		}
		ptreeview->GetParent();
	}

⌨️ 快捷键说明

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