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

📄 luluview.cpp

📁 整个工作过程主要划分成词法分析阶段、语法分析阶段、中间代码生成和目标代码生成四个部分。 第一阶段是把输入的字符串形式的源程序分割成一个个单词符号。 第二阶段是识别由词法分析给出的单词符号序列在
💻 CPP
📖 第 1 页 / 共 2 页
字号:
		fprintf(p,"%s ",stacks[i]);
	fprintf(p,"\n");
}

struct emit4{
	int sn;//编号
	CString p1;
	CString p2;
	CString p3;
	CString p4;//四元组的四个项
	emit4 * truelist;
	emit4 * falselist;
	emit4 * nextlist;
	emit4 * realnext;//连接下一个4元组
};
int nextquad=100;//初始编号
emit4 reallist[128];//保存四元组的数组
int current;

emit4 stacke4[128];//将状态压入
int tope4=-1;
void pushe4(emit4 symbol)
{
	if(tope4<128)
		stacke4[++tope4]=symbol;
}

emit4 pope4()
{
	if (tope4>-1)
		return stacke4[tope4--];
	else
	{
		emit4 nul;
		nul.sn=-999;
		return nul;
	}
}

CString GetN()
{
	FILE * p;
	int num,len;
	char * temp=new char[128];
	CString nombel,last;
	p=fopen("word.wl","r");
	fseek(p,wordfile,0);
	fgets(temp,128,p);
	nombel.Format(temp);
	len=nombel.GetLength();
	if(feof(p))
	{
		passlength=-1;
		nombel="#";
		return nombel;
	}
	passlength=len;
	//wordfile+=len+1;
	num=nombel.Find(",");
	last=nombel;
	if(num!=1)
	{
		last=last.Right(len-num-1);
		last=last.Left(len-num-2);
		if(!last.Compare("const>")) 
			last="const";
		else if( !last.Compare("id>"))//保存 id
		{
			last="id";
			nombel=nombel.Left(num);
			nombel=nombel.Right(num-1);
			pushid(nombel);//压栈
		}
		else
		{
			last=nombel;
			last=last.Right(len-1);
			last=last.Left(num-1);
		}
	}
	fclose(p);
	return last;
}

int findposition(CString s)
{
	FILE * p;
	CString nombel;
	int len,count=0;
	char* pp;
	char ch;
	char * temp=new char[128];
	p=fopen("lrtable.txt","r");
	fgets(temp,128,p);
	nombel.Format(temp);
	len=nombel.Find(s);
	pp=nombel.GetBuffer(0)+len;
	while(pp-nombel.GetBuffer(0)>0)
	{
		ch=*(--pp);
		if(ch==9)
			count++;
	}

	fclose(p);
	return count;
}


int findnext(int cur,CString nombel,int ** table)
{
	int position;
	/*num=nombel.Find(",");
	if(num!=1)
	{
		CString last=nombel.GetBuffer(num+1);
		if(last.Compare("const>") || last.Compare("id>"))
			position=0;
		else
		{
			last=nombel.GetBuffer(1);
			last.Left(num-1);
			position=findposition(last);
		}
	}
	else
		position=-1;
		*/
	position=findposition(nombel);
	if(position>-1)
		return table[cur][position];
	else
		return -9998;
	
}

CString findrule(int num,FILE * file)
{
	FILE * p;
	CString s;
	int number=-1*num;
	char * temp=new char[128];
	p=fopen("lr.txt","r");
	for(int i=0;i<=number;i++)
	{
		fgets(temp,128,p);
		s.Format(temp);
	}
	fprintf(file,"%s",s);
	i=s.Find("->");
	CString temps=s;
	temps=temps.Right(s.GetLength()-i-2);
	//rnum=temps.GetLength()-1;
	char * pp=temps.GetBuffer(0);
	char ch=*pp;
	int count=1;
	while(ch!=10)
	{
		if(ch==' ')
			count++;
		ch=*(++pp);
	}
	rnum=count;
	s=s.Left(i);
	fclose(p);
	return s;
}

int findcur(int cur,CString nombel,int ** table)
{
	int position=findposition(nombel);
	return table[cur][position];
}

void backpatch(emit4 * list,int quad)//回填
{
	do{

		(*list).p4.Format("%d",quad);
		list=(*list).nextlist;
	}
	while(list!=NULL);
}

emit4 * merge(emit4 * list1,emit4 * list2)//
{
	emit4 * p;
	emit4 * list;
	emit4 * pl;
	list=list1;
	pl=list1;
	do{
		p=list1;
		pl=(*pl).nextlist;
	}
	while(pl!=NULL);
	(*p).nextlist=list2;
	return list;
}

void rule13()
{
	emit4 p;
	p.sn=nextquad++;
	p.p1="j>";
	p.p3=popid();
	p.p2=popid();
	p.p4="0";
	p.nextlist=NULL;
	reallist[current++]=p;
	//(*current).realnext=&p;
	//current=&p;
	emit4 pp;
	pp.sn=nextquad++;
	pp.p1="j";
	pp.p3="-";
	pp.p2="-";
	pp.p4="0";
	pp.nextlist=NULL;
	reallist[current++]=pp;
	//(*current).realnext=&pp;
	//current=&pp;
	emit4 e;
	e.sn=-1;
	e.truelist=&reallist[current-2];
	e.falselist=&reallist[current-1];
	e.nextlist=NULL;
	/////////入栈
	pushe4(e);
									/*	FILE * fp;
										fp=fopen("mcode.txt","a");
										emit4 * fl;
										fl=&head;
										do
										{
											fprintf(fp,"%d (%s,%s,%s,%s)\n",(*(*fl).realnext).sn,(*(*fl).realnext).p1,
												(*(*fl).realnext).p2,(*(*fl).realnext).p3,(*(*fl).realnext).p4);
											fl=(*fl).realnext;
										}
										while(fl!=current);
										fclose(fp);*/
}

void rule12()
{
	emit4  e1;
	emit4  e2;
	e2=pope4();
	e1=pope4();
	backpatch((e1).truelist,nextquad-2);
	emit4 e;
	e.truelist=(e2).truelist;
	e.falselist=merge((e1).falselist,(e2).falselist);
	e.nextlist=NULL;
	/////////入栈
	pushe4(e);
										/*FILE * fp;
										fp=fopen("mcode.txt","a+");
										int fl=-1;
										do
										{
											fl++;
											fprintf(fp,"%d (%s,%s,%s,%s)\n",reallist[fl].sn,reallist[fl].p1,
												reallist[fl].p2,reallist[fl].p3,reallist[fl].p4);
										
										}
										while(fl!=current);
										fclose(fp);*/
}

void rule6()
{
	emit4 p;
	p.sn=nextquad++;
	p.p1="+";
	p.p3=popid();
	p.p2=popid();
	p.p4="T";
	p.nextlist=NULL;
	reallist[current++]=p;
	pushid("T");
}

void rule14()
{
	emit4 p;
	p.sn=nextquad++;
	p.p1=":=";
	p.p2=popid();
	p.p4=popid();
	p.p3="-";
	p.nextlist=NULL;
	reallist[current++]=p;
	emit4 s;
	//s.nextlist=&reallist[current];
	s.nextlist=NULL;
	/////////入栈
	pushe4(s);
}

void rule15()
{
	emit4  e;
	emit4  s1;
	s1=pope4();
	e=pope4();
	backpatch((e).truelist,nextquad-2);
	emit4 s;
	//s.nextlist=merge((e).falselist,(s1).nextlist);
	s.nextlist=e.falselist;
	/////////入栈
	pushe4(s);
}

void rule16()
{
	emit4  e;
	emit4  s1;
	s1=pope4();
	e=pope4();
	backpatch((e).truelist,nextquad-2);
	//backpatch(s1.nextlist,nextquad-4);
	emit4 s;
	s.nextlist=e.falselist;
	/////////入栈
	pushe4(s);
	emit4 pp;
	pp.sn=nextquad++;
	pp.p1="j";
	pp.p3="-";
	pp.p2="-";
	pp.p4.Format("%d",nextquad-5);
	pp.nextlist=NULL;
	reallist[current++]=pp;
}

void rule18()
{
	emit4  s;
	emit4  l1;
	s=pope4();
	l1=pope4();
	backpatch(l1.nextlist,nextquad-5);
	//backpatch(s1.nextlist,nextquad-4);
	emit4 l;
	l.nextlist=s.nextlist;
	/////////入栈
	pushe4(l);
}

void rule20()
{
	emit4  s;
	s=pope4();
	backpatch(s.nextlist,nextquad);
}

void generate(int num)//寻找执行规则
{
	if(num==-12)
		rule13();
	if(num==-11)
		rule12();
	if(num==-5)
		rule6();
	if(num==-13)
		rule14();
	if(num==-14)
		rule15();
	if(num==-15)
		rule16();
	if(num==-17)
		rule18();
	if(num==-19)
		rule20();
}

void CLuluView::OnGrammer() 
{
	// TODO: Add your command handler code here
	current = 0;
	topc=-1;
	tops=-1;
	wordfile=0;
	passlength=0;
	rnum=0;
	int ** table;
	FILE * p;
	table=Readlrtable();
									/*	p=fopen("test.txt","w");///把表格打出来
										for(int i=0;i<64;i++)
											for(int j=0;j<64;j++)
												fprintf(p,"%d  ",table[i][j]);
										fclose(p);*/
	int num=0,cur=0; 
	CString nombel,newnombel;
	pushc(cur);
	pushs("#");
										p=fopen("pro.txt","w");
	while(num!=-9999)
	{
										//p=fopen("pro.txt","a");
										fprintf(p,"%s \n\n",nombel);
										outputc(p);
										outputs(p);
										//fclose(p);
		nombel=GetN();
		num=findnext(cur,nombel,table);
		if(num>0)
		{
			cur=num;
			pushc(cur);
			pushs(nombel);
			wordfile+=passlength+1;
			fprintf(p,"移进\n");
		}
		else if(num<0 && num>-9990)
		{
			fprintf(p,"规约\n");
			newnombel=findrule(num,p);
			generate(num);//
			for(int i=0;i<rnum;i++)
			{
				pops();
				popc();
			}
			pushs(newnombel);
			
			cur=gettopc();
			cur=findcur(cur,newnombel,table);
			pushc(cur);
		}
		else if(num==-9999)
			CMessage::PrintStatusInfo("语法分析结束!");
		else
		{
			CMessage::PrintStatusInfo("语法分析中断!程序有语法错误!");
			break;
		}
	}
										fclose(p);//写出四元式
										p=fopen("mcode.txt","w");
										
										int fl=0;
										do
										{
											
											fprintf(p,"%d (%s,%s,%s,%s)\n",reallist[fl].sn,reallist[fl].p1,
												reallist[fl].p2,reallist[fl].p3,reallist[fl].p4);
											fl++;
										}
										while(fl!=current);
										fprintf(p,"%d\n",nextquad);
										fclose(p);
}

⌨️ 快捷键说明

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