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

📄 work.cpp

📁 小型编译器的课程设计实例哦 值得一看的
💻 CPP
字号:
#include"Work.h"

void CDL::GetStr()
{
	String s[40];
	int k=0;

	cin>>s[k];
	while(s[k]!="#")
	{
		k++;
		cin>>s[k];
	}
	for(int m=0;m<k;m++)
		str=str+s[m];
	cout<<str<<endl;
}

bool CDL::IsLetter(char c)
{
	int i;
	i=(int)c;
	if((i>=65&&i<=90)||(i>=97&&i<=122))
		return true;
	else
		return false;
}

bool CDL::IsDigit(char c)
{
	int i;
	i=(int)c;
	if(i>=48&&i<=57)
		return true;
	else
		return false;
}

void CDL::Check()
{
	while(str[ip])
	{

		if(IsDigit(str[ip]))
			ChDigit();
		else if(IsLetter(str[ip]))
			ChLetter();
		else
			ChDen();
	}
	cout<<endl;
}


bool CDL::ChKeyWord()
{
	int p=ip,k=0;
	for(int i=1;i<8;i++)
	{
		while(str[p]==souce.souce[i][k]&&souce.souce[i][k]!=0)
		{
			p++;
			k++;
		}
		if(souce.souce[i][k]==0)
		{
			syn[sp++]=i;
			token[tp++]=souce.souce[i];
			ip=p;
			cout<<"("<<i<<","<<souce.souce[i]<<") , ";
			return true;
		}
	}
	return false;
}

void CDL::ChDigit()
{
	char sl[20] ;
	int j=0;

	while(IsDigit(str[ip]))
		sl[j++]=str[ip++];

	int max=0,d,m=1,r=j-1;
	while(r>=0)
	{
		d=(int)sl[r];
		d=d-48;
		if(r!=j-1)
			for(int k=0;k<j-r;k++)
				m=m*10;
		max=max+d*m;
		m=1;
		r--;
	}
	if(max<32767)
	{
		sum[np++]=max;
		syn[sp++]=9;
		cout<<"(9,NUM) , ";
	}
	else
		cout<<"ERROR:  The Digit is too big!"<<endl;


}
void CDL::ChLetter()
{

	if(!ChKeyWord())
	{
		char sl[20];
		int j=0;
		while(IsLetter(str[ip])||IsDigit(str[ip]))
			sl[j++]=str[ip++];

		token[tp++]=sl;
		syn[sp++]=8;
		cout<<"(8,ID) , ";
	}
}

void CDL::ChDen()
{
	for(int k=10;k<32;k++)
		if(souce.souce[k][0]==str[ip])
		{
			syn[sp]=k;
			ip++;
			break;
		}

	if(k==10&&str[ip]==souce.souce[10][0])
	{
		k=syn[sp]=29;
		ip++;
	}
	else if(k==11&&str[ip]==souce.souce[10][0])
	{
		k=syn[sp]=30;
		ip++;
	}
	else if(k==25&&str[ip]==souce.souce[10][0])
	{
		k=syn[sp]=27;
		ip++;
	}
	else if(k==26&&str[ip]==souce.souce[10][0])
	{
		k=syn[sp]=28;
		ip++;
	}
	sp++;

	cout<<"("<<k<<","<<souce.souce[k]<<") , ";

}




void CDL::Compiler()
{
	int end=sp;
	sp=0;
	np=0;
	tp=0;
	Fun();

	cout<<endl;
	cout<<"文法编译完毕!"<<endl;

}
void CDL::Fun()
{
	if(syn[sp]==1)
		sp++;
	if(syn[sp]==16)
		sp++;
	if(syn[sp]==17)
	{
		sp++;
		line++;
	}
	Piece();

}

void CDL::Piece()
{
	if(syn[sp]==20)
	{
		sp++;
		line++;
	}
	else
	{
		cout<<"On Line:"<<line<<"  ERROR: 找不到语句块!"<<endl;
		error++;
	}
	Bunch();
	if(syn[sp]==21)
	{
		sp++;
		line++;
	}
	else
	{
		cout<<"On Line:"<<line<<"  ERROR: Missing '}' !"<<endl;
		error++;
	}
}

void CDL::Bunch()
{
	Sentence();
	if(syn[sp]!=24)
	{
		cout<<"On Line:"<<line<<"  ERROR: Missing ';'!"<<endl;
		error++;
	}
	while(syn[sp]==24)
	{
		sp++;
		line++;
		Sentence();
	}
}

void CDL::Sentence()
{
	if(syn[sp+1]==10)
		Evaluate();
	else if(syn[sp]==4)
		Judge();
	else if(syn[sp]==7)
		Cyc();
	else
	{
		cout<<"On Line:"<<line<<"  ERROR: 语句格式错误!"<<endl;
		error++;
	}

}

void CDL::Evaluate()
{
	if(syn[sp]==8)
		sp++;
	if(syn[sp]==10)
		sp++;
	Expression();
}

void CDL::Judge()
{
	if(syn[sp]==4)
		sp++;
	Condition();
	Piece();
}

void CDL::Cyc()
{
	if(syn[sp]==7)
		sp++;
	Condition();
	Piece();
}

void CDL::Condition()
{
	if(syn[sp]==16)
		sp++;

	Expression();
	Relation();
	Expression();

	if(syn[sp]==17)
		sp++;
}

void CDL::Expression()
{
	Member();

	while(syn[sp]==12||syn[sp]==13)
		Member();
}

void CDL::Member()
{
	Gene();

	while(syn[sp]==14||syn[sp]==15)
		Gene();
}

void CDL::Gene()
{
	if(syn[sp]==8)
		sp++;
	else if(syn[sp]==9)
		sp++;
	else if(syn[sp]==16)
	{
		sp++;
		Expression();
		if(syn[sp]==17)
			sp++;
	}
}

void CDL::Relation()
{
	if(syn[sp]>24&&syn[sp]<31)
		sp++;
}

⌨️ 快捷键说明

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