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

📄 cffxq4.cpp

📁 这是一个词法分析器,采用C++语言编写,具有很强的分析功能
💻 CPP
字号:
# include<iostream.h>
# include<stdio.h>
# include<string.h>

char *str[21]={"","begain","do","else","end","if","procedure","program","then","var","while",
"cout","endl","switch","return","for","exit","void","int","main","char"};
char ch=' ',strToken[20]={0},x[100]={0};int code,i=0;
   void GetChar()
{
	ch=x[i];
	i++;

}
void GetBC()//检查CH是否为空;
{
	while(ch==' ')
		GetChar();
}
int Isletter()//判断是否为字母;
{
	if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
		return 1;
	return 0;
}
int IsDigit()//判断是否为数字;
{
	if(ch>='0'&&ch<='9')
		return 1;
	return 0;
}
void Concat()//将CH中字符接连到STRTOKEN之后;
{
	int k=0;
	while(strToken[k])k++;
	strToken[k]=ch;
}
int Reserve()//查找保留字表;
{
	int m=0;
	for(m=1;m<=20;m++)
		if(strcmp(str[m],strToken)==0)
			return m;
		return 0;
}
void change(int n)
{ if(n<=0) cout<<0;
  else if(n>0)
  {   
  change(n/2);cout<<n%2;
  }
}
int get(char *s)//取数字;
{
int sum=0,j=0;
while(s[j])
{sum=sum*10+s[j]-'0';
 j++;
}
return sum;
}
void Placezero()//置零;
{
	int k;
	for(k=0;k<20;k++)
		strToken[k]=0;
}
 void main()
{

	cout<<"Input a sentence:"<<endl;
	int j=0;
	while(ch!='@'&&j<100)
	{
		ch=getchar();
		if(ch!=100)
		{
			x[j]=ch;
		    j++;
		}
	}
   GetChar();GetBC();
    cout<<"The phrase method outputs as follows:"<<endl;
	while(ch!='@')
	{
	     if(Isletter())
		{
			while(Isletter()||IsDigit())
			{
			 Concat();GetChar();}
			 code=Reserve();
			if(code)cout<<"("<<strToken<<","<<code+1<<")"<<endl;
			else
				cout<<"("<<strToken<<",--)"<<endl;
			Placezero();
		}
		else if(IsDigit())
		{
			while(IsDigit())
			{Concat();GetChar();}
			int j=get(strToken);
			cout<<"(";
			cout<<j;
			cout<<",";
			change(j);
			cout<<")"<<endl;
			Placezero();
		}
		else if(ch=='=')
		{   cout<<"(=,24)"<<endl;
		    GetChar();
		}
		else if(ch=='<')
		{
			GetChar();
			if(ch=='=')
			{
			  cout<<"(<=,48)"<<endl;
			  GetChar();
			}
			else if(ch=='<')
			{  cout<<"(<<,44)"<<endl;
			   GetChar();
			}
			else
				cout<<"(<,47)"<<endl;
		}
			else if(ch=='>')
		{
			GetChar();
			if(ch=='=')
			{  cout<<"(>=,23)"<<endl;
			   GetChar();
			}
            else if(ch=='>')
			{  cout<<"(>>,45)"<<endl;
			   GetChar();
			}
			else
				cout<<"(>,22)"<<endl;
		}
			else if(ch=='('){cout<<"((,25)"<<endl;GetChar();}
			else if(ch==')'){cout<<"(),26)"<<endl;GetChar();}
       else if(ch=='*')
		{
			GetChar();
			if(ch=='*')
			{
			cout<<"(**,27)"<<endl;
			GetChar();
			}
			else
				cout<<"(*,28)"<<endl;
		}
      else if(ch==':')
		{
			GetChar();
			if(ch=='=')
			{
			cout<<"(:=,29)"<<endl;
			GetChar();
			}
		}
	  else if(ch=='+'){cout<<"(+,30)"<<endl;GetChar();}
	  else if(ch=='-'){cout<<"(-,31)"<<endl;GetChar();}
	  else if(ch=='?'){cout<<"(?,32)"<<endl;GetChar();}
	  else if(ch==','){cout<<"(,,33)"<<endl;GetChar();}
	  else if(ch==';'){cout<<"(;,34)"<<endl;GetChar();}
      else if(ch=='!'){cout<<"(!,35)"<<endl;GetChar();}
	  else if(ch=='&'){cout<<"(&,36)"<<endl;GetChar();}
	  else if(ch=='$'){cout<<"($,37)"<<endl;GetChar();}
	  else if(ch=='#'){cout<<"(#,38)"<<endl;GetChar();}
	  else if(ch=='^'){cout<<"(^,39)"<<endl;GetChar();}
      else if(ch=='{'){cout<<"({,40)"<<endl;GetChar();}
	  else if(ch=='}'){cout<<"(},41)"<<endl;GetChar();}
	  else if(ch=='['){cout<<"([,42)"<<endl;GetChar();}
	  else if(ch==']'){cout<<"(],43)"<<endl;GetChar();}
      else if(ch=='.'){cout<<"(.,46)"<<endl;GetChar();}
     else if(ch==' '){GetChar();GetBC();}
     else GetChar();
       
}
}

⌨️ 快捷键说明

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