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

📄 test.cpp

📁 递归子程序法:对应每个非终结符语法单元编一个独立的处理过程(或子程序)。语法分析从读入第一个单词开始
💻 CPP
字号:
#include<stdio.h>
#include<malloc.h>
#include<iostream.h>
#define MAX 200;
typedef struct
{
	char *base;
	char *top;
	int stacksize;
}stack;
stack s;
typedef struct
{
	char pLeft;//产生式的左部符号
	char pRight[20];//产生式的右部最多包括20个字符
}Rule;
Rule rule[12];//定义文法最多可以包含12个产生式
int end_num=0;/*终结符的个数*/
int no_end_num=0;/*非终结符个数*/
int rule_num=0;/*规则数*/
char *p;/*存放非终结符*/
char *q;/*存放终结符*/
char **FristVT;/*表示no_end_num行end_num列矩阵*/
char **LastVT;/*表示no_end_num行end_num列矩阵*/
char *temptChar;
void create(stack &s)
{
	s.base=s.top=(char*)malloc(sizeof(char));
	s.stacksize=MAX;
}
void push(stack &s,char e)
{
	*s.top++=e;
}
void push(stack &s,char p,char a)/*压栈,每次压两个字符,其中p是非终结符,a是终结符*/
{
	*s.top++=p;
	*s.top++=a;
}
void pop(stack &s,char &e)
{
	if(s.top==s.base)
	{
		return;
	}
	else
	{
		e=*--s.top;
	}
}
void pop(stack &s,char &p,char &a)/*出栈,同样每次也得出两个字符*/
{
	if(s.top==s.base)
	{
		return;
	}
	else
	{
		p=*--s.top;
		a=*--s.top;
	}
}
bool Empty(stack &s)
{
	if(s.top<=s.base)
	{
		return true;
	}
	else
	{
		return false;
	}
}
void Copy(char *p,char*q,int n)
{
	for(int i=0;i<n;i++)
	{
		*p++=*q++;
	}
}
int InitP()/*初始化P返回字符个数*/
{
	int i=0;
	char ch;   
	char *tempt=new char[100];
	while((ch=getchar())!='\n')
	{
		tempt[i]=ch;
		++i;
	}
	p=new char[i];
	Copy(p,tempt,i);
	delete tempt;
	return i;
}
int InitQ()/*初始化Q返回字符个数*/
{
	int i=0;
	char ch;  
	char *tempt=new char[100];
	while((ch=getchar())!='\n')
	{
		tempt[i]=ch;
		++i;
	}
	q=new char[i];
	Copy(q,tempt,i);
	delete tempt;
	return i;
}
void Initialize()/*初始化矩阵,非终结符数组,终结符数组*/
{
	printf("请输入非终结符:");
	no_end_num=InitP();
	printf("请输入终结符:");
	end_num=InitQ();
	FristVT=(char**)malloc(sizeof(char*)*(no_end_num+1));
	for(int i=0;i<no_end_num+1;i++)
	{
		FristVT[i]=(char*)malloc(sizeof(char)*(end_num+1));
	}

	LastVT=(char**)malloc(sizeof(char*)*(no_end_num+1));
	for(int j=0;j<no_end_num+1;j++)
	{
		LastVT[j]=(char*)malloc(sizeof(char)*(end_num+1));
	}

	for(int a=1;a<no_end_num+1;a++)
	{
		for(int b=1;b<end_num+1;b++)
		{
			FristVT[a][b]='0';
			LastVT[a][b]='0';
		}
	}
	for(int e=1;e<no_end_num+1;e++)
	{
		FristVT[0][e]=p[e-1];
		LastVT[0][e]=p[e-1];
	}
	for(int f=1;f<end_num+1;f++)
	{
		FristVT[f][0]=q[f-1];
		LastVT[f][0]=q[f-1];
	}
}
void InitRule()/*输入文法*/
{
	printf("请输入文法个数 ");
	int i=0;
	cin>>i;
	rule_num=i;
	for(int j=0;j<i;j++)
	{
		printf("请输入文法%d:",j);
		int count=0;
		char ch;  
		char *temp=new char[20];//定义最多20个字符
		while((ch=getchar())!='\n')
		{
			temp[count]=ch;
			count++;
		}
		rule[j].pLeft=temp[0];
		for(int a=1;a<count;a++)
		{
			rule[j].pRight[a]=temp[a];
		}
	}
}
char GetF(char p,char a)/*获得F[p,a]的值*/
{
	int row=0;
	int column=0;
	for(int i=0;i<no_end_num+1;i++)
	{
		if(FristVT[0][i]==p)
		{
			column=i;
		}
	}
	for(int j=0;j<end_num+1;j++)
	{
		if(FristVT[j][0]==a)
		{
			row=j;
		}
	}
	return FristVT[row][column];
}
char GetL(char p,char a)/*获得F[q,a]的值*/
{
	int row=0;
	int column=0;
	for(int i=0;i<no_end_num+1;i++)
	{
		if(FristVT[0][i]==p)
		{
			column=i;
		}
	}
	for(int j=0;j<end_num+1;j++)
	{
		if(FristVT[j][0]==a)
		{
			row=j;
		}
	}
	return FristVT[row][column];
}
void SetF(char q,char a)
{
	int row=0;
	int column=0;
	for(int i=0;i<no_end_num+1;i++)
	{
		if(LastVT[0][i]==q)
		{
			column=i;
		}
	}
	for(int j=0;j<end_num+1;j++)
	{
		if(LastVT[j][0]==a)
		{
			row=j;
		}
	}
	FristVT[row][column]='1';
}
void SetL(char q,char a)
{
	int row=0;
	int column=0;
	for(int i=0;i<no_end_num+1;i++)
	{
		if(LastVT[0][i]==q)
		{
			column=i;
		}
	}
	for(int j=0;j<end_num+1;j++)
	{
		if(LastVT[j][0]==a)
		{
			row=j;
		}
	}
	LastVT[row][column]='1';
}
int LeachChar()/*滤去"->"字符串*/
{
	return 1;
}

void GetFirstVT(char c)/*获得FirstVT*/
{

}
void GetLastVT(char c)/*获得LastVT*/
{

}
void InsertF(char p,char a)
{
}
void InsertL(char q,char a)
{

}
void main()
{
	Initialize();
	printf("%d",no_end_num);
	printf("%d",end_num);
	//InitRule();
	//printf("%s",rule[0].pRight);
}

⌨️ 快捷键说明

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