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

📄 stack.h

📁 系个简单的词法分析程序源代码
💻 H
字号:
#include<iostream.h>
const  int length=30;//the length of identier no more than 30 bytes 
const  int capacity=200;  //the capacity of the stack is 50
struct  node{
	int  encode;
	int  readdr;//short for relative address
    char  word[length];
	int wordlen;
    int amount;
};
class  stack{
private:
	node array[capacity];
	int size;
  public:
	  stack();
	   int  pushretain(char  str[],int len);
	    int  pushvar(char  str[],int len,int error);
		 int  pushnum(char  str[],int len);
		  void  show(int result);
		   int   compare(char str[],int len);
		    void push(char str[],int len,int code,int addr);
  };
  stack::stack()
{
   size=0;
}
int  stack::pushretain(char  str[],int len)
{
	array[size].wordlen=len;
	 if(size<5)
	   array[size].encode=size+1;
	   else
		   array[size].encode=size+3;
	   for(int i=0;i<len;i++)
		   array[size].word[i]=str[i];
	     size++;
		 return array[size-1].encode;
}
int  stack::pushvar(char str[],int len,int error)
{
	array[size].wordlen=len;
	if(!error)
	 array[size].encode=6;
	  else
		  array[size].encode=0;//给错误编码为零
	  for(int i=0;i<len;i++)
		  array[size].word[i]=str[i];
	   array[size].readdr=size;
	     size++;
		  return size-1;
}
int  stack::pushnum(char str[],int len)
{
	array[size].encode=7;
	 int num=0;
	 for(int i=0;i<len;i++)
	 {
		 array[size].word[i]=str[i];
		   num*=10;
			 num+=str[i]-'0';
	 }
	   array[size].wordlen=len;
	    array[size].amount=num;
	     array[size].readdr=size;
		   size++;
		   return size-1;
}
void  stack::push(char str[],int len,int code,int addr)
{
	array[size].encode=code;
	 array[size].wordlen=len;
	  array[size].readdr=addr;
	   for(int i=0;i<len;i++)
		   array[size].word[i]=str[i];
	   size++;
}
void  stack::show(int result)
{
	if(size>0)
	{
		for(int i=0;i<size;i++)
		{
			if(array[i].encode==6||array[i].encode==7)
			{
				if(!result)
				{
					cout<<" ";
				  for(int j=0;j<array[i].wordlen;j++)
				     cout<<array[i].word[j];
			            	cout<<"--";
				}
				   cout<<array[i].encode<<"-"<<array[i].readdr<<' '; 
			}
			   else
				   if(array[i].encode==0)
				   {
					   if(!result)
					   {
						   cout<<" ";
					   for(int j=0;j<array[i].wordlen;j++)
					     cout<<array[i].word[j];
					       cout<<"--";
					   }
					         cout<<"error"<<' ';
				   }
				   else
				   {
					   if(!result)
					   {
						   cout<<" ";
					     for(int j=0;j<array[i].wordlen;j++)
					       cout<<array[i].word[j];
					         cout<<"--";
					   }
					         cout<<array[i].encode<<' ';
				   }
		}
	}
}
int stack::compare(char  str[],int len)
{
	int flag=0;
	for(int i=0;i<size;i++)
	{
		flag=1;
	    if(array[i].encode==0)
			flag=0;
		  else
		  {
			  if(len!=array[i].wordlen)
				  flag=0;
			    else
				{
					for(int j=0;j<len;j++)
                        if(array[i].word[j]!=str[j])
						{
							flag=0;
							 break;
						}
				}
		  }
            if(flag)
				break;
	}
	if(flag)
	{
		if(array[i].encode==6||array[i].encode==7)
			return array[i].readdr;
		  else
			  return array[i].encode;
	}
	return -1;
}

⌨️ 快捷键说明

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