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

📄 data.h

📁 数据库模拟程序 实现控制台命令词法分析
💻 H
📖 第 1 页 / 共 2 页
字号:
				    	strcpy(wordlist[wordlist_count].mark,"r");
						name_count=0;
						i=100;
						cout<<"Error at line "<<line<<" : "<<wordlist[wordlist_count].name<<" is wrong!"<<endl;	
				}
	    	    else
				{
					ungetc(w,stdin);
			        name_count=0;
					wordlist[wordlist_count].kind=5;
					strcpy(wordlist[wordlist_count].mark,"r");
					wordlist_count++;//********前面没有++;				
			    	i=100;//跳出循环;
				}
		}
	}
	if(i==20)//不是运算符再判断是否是界符;
	{
    	for(j=0;j<20;j++)					//界符识别.
		{
			if(w==fengefu[j])					//		";,()'[]{}?";			
			{	
			        name_count=0;
					wordlist[wordlist_count].kind=4;
					strcpy(wordlist[wordlist_count].mark,wordlist[wordlist_count].name);
					wordlist_count++;//********前面没有++;				
			    	i=100;
			}
		}
	}
	if((i==20)&&(j==20))
	{
		    name_count=0;
	        wordlist[wordlist_count].kind=-1;
			cout<<"Error at line "<<line<<" : "<<wordlist[wordlist_count].name<<" is wrong!"<<endl;	
    		wordlist_count++;
	}
}
//*****************************************************************************************
void CMyAnylise::wordAnalyze()
{		  
	char input;
	cout<<"Please enter the code:"<<endl;
	input=PopChar();
	while(input!=';')
	{
      if(input==' '||input=='\t')
					;
	  else if(input=='\n')
	     line=line+1;
	  else if(isdigit(input))
 	     number(input);
	  else if((input>=65&&input<=90)||(input>=97&&input<=122)||(input=='_'))//字符
		 symbolJudge(input);
	  else  
		 elseSymbol(input);
	  input=PopChar();	
    }
}
//******************************************************************************

//****************************************
void CMyAnylise::init()
{

	if(strcmp(wordlist[0].name,"CREATE")==0)
	{
		bound=10;////0代表NULL;
		item1[0].n='E'; item1[0].t='c'; strcpy(item1[0].gen,"ctg(F)");
		item1[1].n='F'; item1[1].t='g'; strcpy(item1[1].gen,"gW");
		item1[2].n='W'; item1[2].t='a'; strcpy(item1[2].gen,"a(n)Q");
		item1[3].n='W'; item1[3].t='i'; strcpy(item1[3].gen,"iP");
		item1[4].n='Q'; item1[4].t=','; strcpy(item1[4].gen,",F");
		item1[5].n='Q'; item1[5].t='$'; strcpy(item1[5].gen,"0");
		item1[6].n='P'; item1[6].t=','; strcpy(item1[6].gen,",F");
		item1[7].n='P'; item1[7].t='$'; strcpy(item1[7].gen,"0");
		item1[8].n='Q'; item1[8].t=')'; strcpy(item1[8].gen,"0");
		item1[9].n='P'; item1[9].t=')'; strcpy(item1[9].gen,"0");
	}

	if(strcmp(wordlist[0].name,"ALTER")==0)
	{
		bound=6;////0代表NULL;
		item1[0].n='E'; item1[0].t='l'; strcpy(item1[0].gen,"ltgW");
		item1[1].n='F'; item1[1].t='g'; strcpy(item1[1].gen,"gH");
		item1[2].n='H'; item1[2].t='a'; strcpy(item1[2].gen,"a(n)");
		item1[3].n='H'; item1[3].t='i'; strcpy(item1[3].gen,"i");
		item1[4].n='W'; item1[4].t='d'; strcpy(item1[4].gen,"dF");
		item1[5].n='W'; item1[5].t='r'; strcpy(item1[5].gen,"rF");
	}
	if(strcmp(wordlist[0].name,"DROP")==0)
	{
		bound=4;////0代表NULL;
		item1[0].n='E'; item1[0].t='r'; strcpy(item1[0].gen,"rW");
		item1[1].n='W'; item1[1].t='t'; strcpy(item1[1].gen,"tg");
		item1[2].n='W'; item1[2].t='x'; strcpy(item1[2].gen,"xg");
		item1[3].n='W'; item1[3].t='v'; strcpy(item1[3].gen,"vg");
	}
	if(strcmp(wordlist[0].name,"SELECT")==0)
	{
		bound=30;////0代表NULL;
		item1[0].n='E'; item1[0].t='s'; strcpy(item1[0].gen,"sAfNQ");
		item1[1].n='Q'; item1[1].t='w'; strcpy(item1[1].gen,"wW");
		item1[2].n='Q'; item1[2].t='$'; strcpy(item1[2].gen,"0");
		item1[3].n='A'; item1[3].t='r'; strcpy(item1[3].gen,"r");
        item1[4].n='A'; item1[4].t='g'; strcpy(item1[4].gen,"B");
		item1[5].n='B'; item1[5].t='g'; strcpy(item1[5].gen,"gM");
		item1[6].n='M'; item1[6].t=','; strcpy(item1[6].gen,",B");
		item1[7].n='M'; item1[7].t='.'; strcpy(item1[7].gen,".gD");
		item1[8].n='M'; item1[8].t='f'; strcpy(item1[8].gen,"0");
		item1[9].n='M'; item1[9].t='$'; strcpy(item1[9].gen,"0");
		item1[10].n='D'; item1[10].t=','; strcpy(item1[10].gen,",B");
		item1[11].n='D'; item1[11].t='f'; strcpy(item1[11].gen,"0");
		item1[12].n='D'; item1[12].t='$'; strcpy(item1[12].gen,"0");
		item1[13].n='F'; item1[13].t='w'; strcpy(item1[13].gen,"0");
        item1[14].n='F'; item1[14].t=','; strcpy(item1[14].gen,",N");
		item1[15].n='F'; item1[15].t='$'; strcpy(item1[15].gen,"0");
		item1[16].n='W'; item1[16].t='g'; strcpy(item1[16].gen,"gZ");
		item1[17].n='W'; item1[17].t='('; strcpy(item1[17].gen,"(W)K");
		item1[18].n='K'; item1[18].t='a'; strcpy(item1[18].gen,"aW");
		item1[19].n='K'; item1[19].t='o'; strcpy(item1[19].gen,"oW");
		item1[20].n='Z'; item1[20].t='.'; strcpy(item1[20].gen,".gU");
		item1[21].n='Z'; item1[21].t='r'; strcpy(item1[21].gen,"rY");
		item1[22].n='Y'; item1[22].t='\''; strcpy(item1[22].gen,"'g'K");
		item1[23].n='Y'; item1[23].t='n'; strcpy(item1[23].gen,"nK");
        item1[24].n='N'; item1[24].t='g'; strcpy(item1[24].gen,"gF");
		item1[25].n='U'; item1[25].t='r'; strcpy(item1[25].gen,"rR");
		item1[26].n='R'; item1[26].t='\''; strcpy(item1[26].gen,"'g'K");
		item1[27].n='R'; item1[27].t='n'; strcpy(item1[27].gen,"nK");
		item1[28].n='K'; item1[28].t='$'; strcpy(item1[28].gen,"0");
		item1[29].n='K'; item1[29].t=')'; strcpy(item1[29].gen,"0");//后补的;
	}
    if(strcmp(wordlist[0].name,"INSERT")==0)
	{
		bound=16;////0代表NULL;
		item1[0].n='E'; item1[0].t='i'; strcpy(item1[0].gen,"itgW");
		item1[1].n='W'; item1[1].t='v'; strcpy(item1[1].gen,"K");
		item1[2].n='W'; item1[2].t='('; strcpy(item1[2].gen,"(G)K");
		item1[3].n='G'; item1[3].t='g'; strcpy(item1[3].gen,"gM");
        item1[4].n='M'; item1[4].t=','; strcpy(item1[4].gen,",G");
		item1[5].n='M'; item1[5].t='$'; strcpy(item1[5].gen,"0");
		item1[6].n='M'; item1[6].t=')'; strcpy(item1[6].gen,"0");
		item1[7].n='F'; item1[7].t='\''; strcpy(item1[7].gen,"'g'H");
		item1[8].n='F'; item1[8].t='n'; strcpy(item1[8].gen,"nQ");
		item1[9].n='H'; item1[9].t='$'; strcpy(item1[9].gen,"0");
		item1[10].n='H'; item1[10].t=')'; strcpy(item1[10].gen,"0");
		item1[11].n='H'; item1[11].t=','; strcpy(item1[11].gen,",F");
		item1[12].n='Q'; item1[12].t=','; strcpy(item1[12].gen,",F");
		item1[13].n='Q'; item1[13].t='$'; strcpy(item1[13].gen,"0");
        item1[14].n='Q'; item1[14].t=')'; strcpy(item1[14].gen,"0");
		item1[15].n='K'; item1[15].t='v'; strcpy(item1[15].gen,"v(F)");
	}   
	if(strcmp(wordlist[0].name,"UPDATE")==0)
	{
		bound=15;////0代表NULL;
		item1[0].n='E'; item1[0].t='u'; strcpy(item1[0].gen,"ugsFwW");
		item1[1].n='F'; item1[1].t='g'; strcpy(item1[1].gen,"grH");
		item1[2].n='H'; item1[2].t='\''; strcpy(item1[2].gen,"'g'T");
		item1[3].n='H'; item1[3].t='n'; strcpy(item1[3].gen,"nT");
        item1[4].n='T'; item1[4].t='$'; strcpy(item1[4].gen,"0");
		item1[5].n='T'; item1[5].t=','; strcpy(item1[5].gen,",F");
		item1[6].n='W'; item1[6].t='('; strcpy(item1[6].gen,"(W)Z");
		item1[7].n='W'; item1[7].t='g'; strcpy(item1[7].gen,"grK");
		item1[8].n='K'; item1[8].t='\''; strcpy(item1[8].gen,"'g'Z");
		item1[9].n='K'; item1[9].t='n'; strcpy(item1[9].gen,"nZ");
		item1[10].n='Z'; item1[10].t='a'; strcpy(item1[10].gen,"aW");
		item1[11].n='Z'; item1[11].t='o'; strcpy(item1[11].gen,"oW");
		item1[12].n='Z'; item1[12].t=')'; strcpy(item1[12].gen,"0");
		item1[13].n='Z'; item1[13].t='$'; strcpy(item1[13].gen,"0");
		item1[14].n='T'; item1[14].t='w'; strcpy(item1[14].gen,"0");
	}
	if(strcmp(wordlist[0].name,"DELETE")==0)
	{
		bound=9;////0代表NULL;
		item1[0].n='E'; item1[0].t='d'; strcpy(item1[0].gen,"dfgwW");
		item1[1].n='W'; item1[1].t='('; strcpy(item1[1].gen,"(W)Z");
		item1[2].n='W'; item1[2].t='g'; strcpy(item1[2].gen,"grK");
		item1[3].n='K'; item1[3].t='\''; strcpy(item1[3].gen,"'g'Z");
		item1[4].n='K'; item1[4].t='n'; strcpy(item1[4].gen,"nZ");
		item1[5].n='Z'; item1[5].t='a'; strcpy(item1[5].gen,"aW");
		item1[6].n='Z'; item1[6].t='o'; strcpy(item1[6].gen,"oW");
		item1[7].n='Z'; item1[7].t=')'; strcpy(item1[7].gen,"0");
		item1[8].n='Z'; item1[8].t='$'; strcpy(item1[8].gen,"0");
	}
	if(strcmp(wordlist[0].name,"GRANT")==0||strcmp(wordlist[0].name,"REVOKE")==0)
	{
		bound=9;////0代表NULL;
		item1[0].n='E'; item1[0].t='a'; strcpy(item1[0].gen,"aAogtU");
		item1[1].n='E'; item1[1].t='r'; strcpy(item1[1].gen,"rAogfU");
		item1[2].n='A'; item1[2].t='g'; strcpy(item1[2].gen,"gP");
		item1[3].n='P'; item1[3].t='$'; strcpy(item1[3].gen,"0");
		item1[4].n='P'; item1[4].t='o'; strcpy(item1[4].gen,"0");
		item1[5].n='P'; item1[5].t=','; strcpy(item1[5].gen,",A");
		item1[6].n='U'; item1[6].t='g'; strcpy(item1[6].gen,"gQ");
		item1[7].n='Q'; item1[7].t=','; strcpy(item1[7].gen,",U");
		item1[8].n='Q'; item1[8].t='$'; strcpy(item1[8].gen,"0");
	}
	if((strcmp(wordlist[0].name,"CREATE")==0)&&(strcmp(wordlist[1].name,"VIEW")==0))
	{
		bound=31;////0代表NULL;
		item1[0].n='G'; item1[0].t='s'; strcpy(item1[0].gen,"sAfNQ");
		item1[1].n='Q'; item1[1].t='w'; strcpy(item1[1].gen,"wW");
		item1[2].n='Q'; item1[2].t='$'; strcpy(item1[2].gen,"0");
		item1[3].n='A'; item1[3].t='r'; strcpy(item1[3].gen,"r");
        item1[4].n='A'; item1[4].t='g'; strcpy(item1[4].gen,"B");
		item1[5].n='B'; item1[5].t='g'; strcpy(item1[5].gen,"gM");
		item1[6].n='M'; item1[6].t=','; strcpy(item1[6].gen,",B");
		item1[7].n='M'; item1[7].t='.'; strcpy(item1[7].gen,".gD");
		item1[8].n='M'; item1[8].t='f'; strcpy(item1[8].gen,"0");
		item1[9].n='M'; item1[9].t='$'; strcpy(item1[9].gen,"0");
		item1[10].n='D'; item1[10].t=','; strcpy(item1[10].gen,",B");
		item1[11].n='D'; item1[11].t='f'; strcpy(item1[11].gen,"0");
		item1[12].n='D'; item1[12].t='$'; strcpy(item1[12].gen,"0");
		item1[13].n='F'; item1[13].t='w'; strcpy(item1[13].gen,"0");
        item1[14].n='F'; item1[14].t=','; strcpy(item1[14].gen,",N");
		item1[15].n='F'; item1[15].t='$'; strcpy(item1[15].gen,"0");
		item1[16].n='W'; item1[16].t='g'; strcpy(item1[16].gen,"gZ");
		item1[17].n='W'; item1[17].t='('; strcpy(item1[17].gen,"(W)K");
		item1[18].n='K'; item1[18].t='a'; strcpy(item1[18].gen,"aW");
		item1[19].n='K'; item1[19].t='o'; strcpy(item1[19].gen,"oW");
		item1[20].n='Z'; item1[20].t='.'; strcpy(item1[20].gen,".gU");
		item1[21].n='Z'; item1[21].t='r'; strcpy(item1[21].gen,"rY");
		item1[22].n='Y'; item1[22].t='\''; strcpy(item1[22].gen,"'g'K");
		item1[23].n='Y'; item1[23].t='n'; strcpy(item1[23].gen,"nK");
        item1[24].n='N'; item1[24].t='g'; strcpy(item1[24].gen,"gF");
		item1[25].n='U'; item1[25].t='r'; strcpy(item1[25].gen,"rR");
		item1[26].n='R'; item1[26].t='\''; strcpy(item1[26].gen,"'g'K");
		item1[27].n='R'; item1[27].t='n'; strcpy(item1[27].gen,"nK");
		item1[28].n='K'; item1[28].t='$'; strcpy(item1[28].gen,"0");
		item1[29].n='K'; item1[29].t=')'; strcpy(item1[29].gen,"0");
		item1[30].n='E'; item1[30].t='c'; strcpy(item1[30].gen,"cvgaG");
	}
}
//******************************************

//********************************************
void CMyAnylise::SETNULL(seqstack *s1)    //置空
{
   s1->top=-1;
}
//*********************************************
int CMyAnylise::EMPTY(seqstack *s1)//判断栈是否为空
{
if(s1->top>=0)
   return(0);
else
   return(1);
}
//***********************************************
void CMyAnylise::PUSH(seqstack *s1,char y)   //入栈
{
 if(s1->top==MAXSIZE-1)
    {
	 printf("overflow");
	} 
 else
    {
    s1->top++;
    s1->data[s1->top]=y;
    }
}
//***************************************
void CMyAnylise::POP(seqstack *s1)//退栈
{
    if(EMPTY(s1))
     {
     printf("underflow");
     }
    else
     {
      s1->top--;
     }
}
void CMyAnylise::find()///查找分析表;
{
	int i=0;
	for(i=0;i<bound;i++)
	{
    	if(stack.data[stack.top]==item1[i].n)
		{
			if(input.data[input.top]==item1[i].t)
			{
				loc=i;
				error=0;
			    break;
			}
		}
	}
	if(i==bound)
	{
		error=1;
	}
}
//******************************************************************************
void CMyAnylise::reverseOut()//输出两个栈的元素。将第二个栈的元素倒置输出。
{
			char ch[70];
			int i=0;
            stack.data[stack.top+1]='\0';
			cout<<setiosflags(ios::left)<<setw(14)<<stack.data;
			for(int j=input.top;j>=0;j--)
			{
				ch[i]=input.data[j];
				i++;
			}
			ch[i]='\0';
            cout<<resetiosflags(ios::left)<<setw(55)<<ch;
			cout<<" ";
}
//******************************************************************************
void CMyAnylise::clean()///清除本次语法分析的痕迹;
{
        wordlist_count=0;
	    symbol_count=0;
	    constant_count=0;
	    name_count=0;
	    key_count=0;
		for(int i=0;i<MAXSIZE;i++)
		{
          item1[i].n='\0';
		  item1[i].t='\0';
		  for(int m=0;m<10;m++)
		  {
			item1[i].gen[m]='\0';
		  }
		}
		for(int j=0;j<MAXSIZE;j++)
		{ch[j]='\0';}
		for(int m=0;m<10;m++)
		{
			name[m]='\0';
		}
		for(int k=0;k<100;k++)
		{
			for(int w=0;w<10;w++)
			{
				wordlist[k].name[w]='\0';
			    symbol[k].name[w]='\0';
			}
		}
}
//****************************************************************************************
void CMyAnylise::yufa()
{
	init();
	int i=0;
	char save_stack;
	string sign,save_input;
	SETNULL(&input);
    SETNULL(&stack);
	PUSH(&stack,'$');
	PUSH(&stack,'E');
	PUSH(&input,'$');
	for(int j=(wordlist_count-1);j>=0;j--)
	{
	 ch[i]=wordlist[j].mark[0];////////////倒置过程
	 i++;
	}
	ch[i]='\0';/////
	i=0;
	while(i<wordlist_count)
	{
	    PUSH(&input,ch[i]);	
		i++;
	}
	save_stack=' ';///
	sign="   ";//可:="->",为输出显示用;
	save_input="   ";///
	while(stack.data[stack.top]!='$'||input.data[input.top]!='$')
	{ 
		if(stack.data[stack.top]==input.data[input.top])
		{
                reverseOut();
				cout<<save_stack<<sign<<save_input<<endl;
			    POP(&stack);
			    POP(&input);
				save_stack=' ';
				sign="  ";
				save_input="   ";
		}
		else                                  //栈顶不相同的情况
		{	
			find();
			if(error==0)
			{
                reverseOut();
				cout<<save_stack<<sign<<save_input<<endl;//输出推倒公式;
				save_stack=stack.data[stack.top];//为下一次输出准备;
				sign="->";
				save_input=item1[loc].gen;
				POP(&stack);//推出空 或是 非空都要出栈;
				if(strcmp(item1[loc].gen,"0")!=0)//推出非空时,将推出元素倒置入栈。
				{	
					i=-1;
					while(item1[loc].gen[i+1]!='\0')
					{
						i++;
					}
					for(int j=i;j>=0;j--)
					{
				     PUSH(&stack,item1[loc].gen[j]);
					}
				}			
			}
			else//错误情况;
				break;
		}
	}
	if(error==0)
	{
	   cout<<"$                                                                   $ "<<save_stack<<sign<<save_input<<endl;
	   cout<<"语法分析成功!!!"<<endl<<"                      ^_^"<<endl<<"-------------------------------------------"<<endl;
       clean();
	}
	else
	{
		error=0;             
	    cout<<"语法错误!!!"<<endl<<endl<<"-----------------------------------------------"<<endl;
        clean();
	}
}
//**********************************************************************************
void CMyAnylise::go()
{  
	{
		wordAnalyze();
		fileout();
		yufa();
	}
}
//***********************************************************************************

⌨️ 快捷键说明

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