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

📄 mcode.cpp

📁 为L语言设计一个语法分析器。 读入源程序
💻 CPP
字号:
#include <stdlib.h>
#include<stdio.h>
#include <iostream.h>
#include <fstream.h>
#include<string.h>

//ifstream fin("generate.txt",ios::nocreate);	
ifstream finflag("flag.txt",ios::nocreate);	
ofstream outFinal("final.txt");
ofstream outFile("output.txt");
char ch;
int count = 0;
char *Xtype;	
int Xwidth;
int offset = 0;
int m = 0;
int address = 100;
FILE *fp;

int choose(char *gene)
{
	cout << "gene[0]" << gene[0] << endl;
	if(gene[0] == 'X')
		return 1;
	if(gene[0] == 'M')
		return 2;
	if(gene[0] == 'E' )
		return 3;
	if(gene[0] == 'J')
		if(gene[3] == 'k')
			return 3;
		else return 4;
	if(gene[0] == 'L')
		return 5;
	return 0;
}	

void lookup(char *save)
{
	int search = 1, i = 0, j = 0;
	char ch1, word;
	count++; //全局变量
//	cout << "#####" << count << "#####" << endl;
	ifstream lookin("save.txt",ios::nocreate);	
	if(!lookin){
		cout<<"file not found!\n";
		exit(0);
	}	

	while(j != count)  //数取到的是第几个标志符
	{	
		lookin.get(word);
		if(word == '%')
		{
			j++;
			save[0] = '\0';
			i = 0;
			lookin.get(ch1);
			while(ch1 != '\n')
			{
				save[i++] = ch1;
				lookin.get(ch1);
			}
			save[i] = '\0'; //此时ch1里存的是回车符
		//	cout << ch1 << endl;
		}
		else while(ch1 != '\n')
				lookin.get(ch1);
	}
	//return save;
}

void lookupy()
{
	char save1,ch2;
	//count++; //全局变量
	//cout << "#####" << count << "#####" << endl;
	ifstream lookin("save.txt",ios::nocreate);	
	if(!lookin){
		cout<<"file not found!\n";
		exit(0);
	}	
	lookin.get(ch2);
	while(ch2 != '*' && ch2 != '+'&& ch2 != '-'&& ch2 != '/')
	{
		while(ch2 != '\n')
			lookin.get(ch2);
		lookin.get(ch2);
	}
	save1 = ch2;
	outFile << save1;
}


//说明语句的翻译
void explain(char *gene) //把符号表写入文件explain.txt
{
	char *t = "type";
	int i = 0;
	int flag = 0;
	char a[] = "int";
//	char p[10];
	cout << "explain!" << endl;
	cout << gene[0] << endl;
	//普通
	
	if(gene[0] == 'X') 
	{
		Xtype = a; //cout << "@@" << Xtype << endl;
		Xwidth = 4; //cout << "@@" << Xwidth << endl;
	}
	else if(gene[0] == 'V')
	{
		//lookup(p);
		outFinal << "变量说明符号表:" << endl;
		outFinal << "k" << "," << Xtype << "," << offset << endl;
		offset = offset + Xwidth;
	}
	else if(gene[0] == 'M')
			while(gene[i] != '\n'){
				i++;
				if(gene[i] == 'n'){
					flag = 1; break;
				}
			}
	else cout << "error!" << endl;
	if(flag == 0) offset = 0;
	//数组
	else if(flag == 1){	
		outFinal << "变量说明符号表:" << endl;
		//lookup(p);
		outFinal << gene[i+1] << "," << t << ",array," << gene[i+3] << "_num" << ',' << offset << endl;
	}
	return;	
}

int getgene(char *gene,int *length)
{	
	int i = 0;
	*length = 0;
	gene[0] = '\0';
	ch = fgetc(fp);
	if(ch == EOF) return 0;
	while(ch >= '0' && ch <='9')
		ch = fgetc(fp);
	//cout << "%$$" << ch << endl;
	//fscanf(fp,"%d",n);	cout << "&&&&&" << n;
	//if(n == -1) return 0;
	//fseek(fp,-1L,SEEK_CUR);
	//ch = fgetc(fp);//cout << ch;
	while(ch != '\n')
	{	
		(*length)++;
		ch = fgetc(fp);
		gene[i++] = ch;
	}
	gene[i] = '\0';   //gene的最后一个字符是回车*/
	//cout << "get " << gene;
	//cout << length;
	//fseek(fp,-length,SEEK_CUR);
	//return 0;
}

//赋值语句的翻译
void givev(char * gene)
{
	//char p[10];
	//outFile << gene << endl;
	if(!finflag){
		cout<<"file not found!\n";
		exit(0);
	}

	if(gene[0] == 'L')
	{
		//lookup(p);
		outFile << gene[0] << ":=" << "z_num" << endl;
	}
	if(gene[0] == 'E')
	{	
		//lookup(p);
		if(gene[5]!='\n') {
			
			outFile << address++ << ":";
			outFile << gene[0] << ":=" << gene[3];
			lookupy();//cout << "13213" << endl;
			outFile << gene[5] << "_num";
		}
		else outFile << gene[0] << ":=" << gene[3];
		outFile << endl;
		//cout << gene << endl;
	}
	if(gene[0] == 'J')
		if(gene[3] == 'k')
		{
			//lookup(p);
			outFile << address++ << ":";
			outFile << gene[3] << ":=" << gene[6] << endl;
		}

}

void control(char *gene,int length)
{
	cout << "调用" << endl;
	int save = 0;
	int flag[10];
	int l, isif = 0;
	char ch;
//	char p1[20];
//	char p2[20];
	//int length = 0;
	char genelast[20];
	char genenext[20];
		if(gene[3] == 'j')
		{
			finflag >> flag[m];
	
			if(flag[m] == 0) //if
			{		
				finflag >> flag[++m];
				cout << "if" << endl;
				//if(flag[m] == 0)
				//	isif = 1;
				fseek(fp,3,SEEK_CUR);
				ch = fgetc(fp);
				if(ch == 'J')
				{
					cout << "while" << endl;
					save = address;
					fseek(fp,-4,SEEK_CUR);
					getgene(genenext,&l);
					outFile << address++ << ":";
					//cout << genenext << "!" << endl;
					//lookup(p1);
					outFile << "(j" << gene[5] << "," << gene[4];				
					//lookup(p1);
					outFile << "," << gene[6] << "," << address+1 << ")" << endl;
					fseek(fp,-15,SEEK_CUR);
					outFile << address++ << ":"; 
					outFile << "(j,-,-," << "Lnext" << ")" << endl;
				}
				//ch = fgetc(fp);
				//cout << "sad" << ch << endl;
				
				
				//dealif(gene);
				outFile << address++ << ":";
				outFile <<"(j"<<gene[5]<<","<<gene[4]<<","<<gene[6]<<","<<address+1<<")"<<endl;
				//S执行完打印第二条四元式

				fseek(fp,-length-8,SEEK_CUR);
				ch = fgetc(fp);
				//cout << "!!!" << ch << "!!!";

				if(ch == 'E')//前追三条,然后赋值语句的翻译
				{
					outFile << address++ << ":";
					if(save != 0 && flag[m] == 0)
						outFile << "(j,-,-," << save << ")" << endl;
					else
						outFile << "(j,-,-," << address+4 << ")" << endl;
					fseek(fp,-30,SEEK_CUR);
					//ch = fgetc(fp);
					//cout << "!!!" << ch << "!!!";
					outFile << address++ << ":";
					getgene(genelast,&l);
					getgene(genelast,&l);
					givev(genelast);
					outFile << address++ << ":";
					getgene(genelast,&l);
					givev(genelast);
					//outFile << address++ << ":";
					getgene(genelast,&l);
					//cout << "function" << genelast;
					givev(genelast);
					fseek(fp,15,SEEK_CUR);
					outFile << address++ << ":";
					if(save != 0)
						outFile << "(j,-,-," << save+2 << ")" << endl;
					else
						outFile << "(j,-,-," << address << ")" << endl;
				}
				m++;
			}
			if(flag[m] == 1) //while
			{
				cout << "while" << endl;
				
				fseek(fp,3,SEEK_CUR);
				ch = fgetc(fp);
				if(ch == 'J')
				{
					cout << "if" << endl;
					fseek(fp,-4,SEEK_CUR);
					getgene(genenext,&l);
					cout << address++ << ":";
					//cout << genenext << "!" << endl;
					cout<<"(j"<<gene[5]<<","<<gene[4]<<","<<gene[6]<<","<<address+1<<")"<<endl;
					fseek(fp,-15,SEEK_CUR);
					cout << address++ << ":"; 
					cout << "(j,-,-," << "Lnext" << ")" << endl;
				}
				//ch = fgetc(fp);
				//cout << "sad" << ch << endl;
				
				
				//dealif(gene);
				save = address;
				cout << address++ << ":";
				cout<<"(j"<<gene[5]<<","<<gene[4]<<","<<gene[6]<<","<<address+1<<")"<<endl;
				//S执行完打印第二条四元式

				fseek(fp,-length-8,SEEK_CUR);
				ch = fgetc(fp);
				//cout << "!!!" << ch << "!!!";

				if(ch == 'E')//前追三条,然后赋值语句的翻译
				{
					cout << address++ << ":";	
					cout << "(j,-,-," << "Lnext" << ")" << endl;
					fseek(fp,-30,SEEK_CUR);
					//ch = fgetc(fp);
					//cout << "!!!" << ch << "!!!";
					cout << address++ << ":";
					getgene(genelast,&l);
					getgene(genelast,&l);
					givev(genelast);
					cout << address++ << ":";
					getgene(genelast,&l);
					givev(genelast);
					cout << address++ << ":";
					getgene(genelast,&l);
					//cout << "function" << genelast;
					givev(genelast);
					fseek(fp,15,SEEK_CUR);
					cout << address++ << ":";
					if(save != 0)
						cout << "(j,-,-," << save << ")" << endl;
					else
						cout << "(j,-,-," << address << ")" << endl;
				}
				m++;
			}
			//判断j是while还是do
			//判断j是while还是do
		}
}

void change(char * addr3)
{
	char op[3], arg1[10], arg2[10], result[10]; 
	int i = 0, j = 0;

	if(!outFinal){
		cerr<< "cannot" << endl;
		exit(1);
	}
	while(addr3[i] != ':')
		result[j++] = addr3[i++];
	result[j] = '\0';
	j = 0;

	i++; i++;
	while(addr3[i] != '\0' && addr3[i] != '+' && addr3[i] != '-' && addr3[i] != '*' && addr3[i] != '/' && addr3[i] != 'or'  && addr3[i] != 'and')
		arg1[j++] = addr3[i++];
	arg1[j] = '\0';
	j = 0;

	if(addr3[i] != '\0') //有第二个参数
	{
		op[0] = addr3[i];  //取算符
		op[1] = '\0';
		i++;
		while(addr3[i] != '\0')
			arg2[j++] = addr3[i++];
		arg2[j] = '\0';
	}
	else                   //第二个参数为空
	{
		op[0] = ':';       //算符为:=
		op[1] = '=';
		op[2] = '\0';
		arg2[0] = '-';
		arg2[1] = '\0';
	}
	outFinal << "(" << op << "," << arg1 << "," << arg2 << "," << result << ")" << endl;
}

int changeFile()
{	
	ifstream finput("output.txt",ios::nocreate);	
	int i = 0, j = 0, n;
	char addr3[20];
	char skip; 
	if(!finput){
		cout<<"file not found!\n";
		exit(0);
	}	
	while(1)
	{
		j++;
		addr3[0] = '\0';
		i = 0;
		for(int k = 0; k < 4; k++)
		{
			finput.get(ch);
			outFinal << ch;
		}
		//掠过前四个字符
		finput.get(ch);
		skip = ch;
		while(ch != '\n')
		{
			addr3[i++] = ch;
			finput.get(ch);
		}
		addr3[i] = '\0';   //addr3的最后一个字符是回车
		cout << addr3 << endl;
		if(skip != '(')
			change(addr3);
		else outFinal << addr3 << endl;
		//return 0;
	}
}

void main()
{
	int choice = 0;
	char gene[20];
//	char addr3[20];
	char fname[] = "generate.txt";
	int length;
	//cout << addr3 << endl;
	//change(addr3);

	if((fp = fopen(fname, "r+")) == NULL){
		printf("Cannot open the file!\n");
		return;
	}
	cout << "Open!" << endl;

	
	if(!outFile){
		cerr<< "cannot" << endl;
		exit(1);
	}
	while((getgene(gene,&length)) != 0)
	{ 
		//fseek(fp,length,SEEK_CUR);
		choice = choose(gene);
		cout << endl << "main " << gene;
		cout << choice << endl;

		//变量说明
		if(choice == 1) 
		{
			explain(gene);
			//fseek(fp,length,SEEK_CUR);
			getgene(gene,&length);
			explain(gene);
			//fseek(fp,length,SEEK_CUR);
			getgene(gene,&length);
			explain(gene);
			//cout << gene << "####";
		}

		//数组说明
		if(choice == 2)
			explain(gene);
		
		//赋值语句
		if(choice == 3)
			givev(gene);
		
		//控制语句
		if(choice == 4)
			control(gene,length);

		//跳过赋值
		if(choice == 5)
		{
			getgene(gene,&length);
			getgene(gene,&length);
			getgene(gene,&length);
			if(gene[0] == 'J')
				fseek(fp,-16,SEEK_CUR);
			else
			{
				fseek(fp,-30,SEEK_CUR);
				getgene(gene,&length);
				givev(gene);
			}
		}

	}
	fclose(fp);
	outFinal << "四元式序列:" << endl; 
	changeFile();
	cout << "@@@@";
}

⌨️ 快捷键说明

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