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

📄 cifa.cpp

📁 编译原理中,一些解释器的实验~以及相关操作
💻 CPP
字号:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <conio.h>
int zs=0;

/*void output_token(FILE *fp,char s[]);//output code
void error(char s[],int num);//error
void output_int(char s[]);//output integer
void output_ide(char s[],int m,int count);//out put identification
int Readtoken(char s[]);
int orins(char s[]);//delete instruction*/

char Line[200], out[200];
char buf[200];
int count=0;
int lines=0, items=0;

int Lookup( FILE *ftb)
{
	fseek(ftb, 0, SEEK_SET);
	int found=0;
	lines=3;
	while(!feof(ftb))
	{
		buf[0]=0;
		fscanf(ftb, "%s", buf);
		char *p=0;
		if(strlen(buf))
			p=strstr(Line,buf);
		if(p)
		{
			if(p==Line)
			{
				found=1;
				memmove(Line, &Line[strlen(buf)], strlen(Line));
				break;
			}
		}
		lines++;
	}
	return found;
}

int OutItem(FILE *fout)
{
	if(lines>=3)
		fprintf(fout, "%s (%d, 0)\n", buf, lines);
	else
		fprintf(fout, "%s (%d, %d)\n", buf, lines, items);
	return 0;
}

int OutNewItem(FILE *fout)
{
	fprintf(fout, "%s (%d, %d)\n", Line, lines, ++items);
	return 0;

}

int AppendSave(FILE *fout)
{
	fprintf(fout, "%s\n", Line);
	Line[0]=0;
	return 0;

}

int main()
{
	
	FILE *fin, *fout, *ftk, *fsave;
	if((fin=fopen("input.txt","r"))==NULL)//open program
	{
		printf("cannot open the input file\n");
		getch();
		return -1;
	}
	if((fout=fopen("Output.txt","w"))==NULL)//open program
	{
		printf("cannot open the output file\n");
		getch();
		return -1;
	}
	if((ftk=fopen("token.txt","r"))==NULL)//open program
	{
		printf("cannot open the token file\n");
		getch();
		return -1;
	}
	if((fsave=fopen("save.txt","w"))==NULL)//open program
	{
		printf("cannot open the output file\n");
		getch();
		return -1;
	}
	fclose(fsave);
	fscanf(fin, "%s", Line);
	while(!feof(fin))
	{
		if(strlen(Line)==0)
			continue;

		while(strlen(Line))
		{
			if(Lookup(ftk))
				OutItem(fout);
			else
			{
				if((fsave=fopen("save.txt","r"))==NULL)//open program
				{
					printf("cannot open the save file\n");
					getch();
					return -1;
				}
				if(Lookup(fsave))
				{
					OutItem(fout);
					fclose(fsave); 
				}
				else
				{
					OutNewItem(fout);
					fclose(fsave); 
					if((fsave=fopen("save.txt","a"))==NULL)//open program
					{
						printf("cannot open the save file\n");
						getch();
						return -1;
					}
					AppendSave(fsave); 
					Line[0]=0;
					fclose(fsave); 
					count++;
				}
			}
		}
		fscanf(fin, "%s", Line);
	}
	return 0;
}
/*	
	ch=fgetc(fp1);
	while(!feof(fp1))
	{
		i=0;f=1;

		while((ch=='\n'||ch==' ')&&(!feof(fp1)))
			ch=fgetc(fp1);//如果是回车或者是空格继续读取
	
		while((ispunct(ch))&&(!feof(fp1)))//是否是标点
		{
			
			word[i++]=ch;
			y++;
			ch=fgetc(fp1);
			f=0;
   		}
		if(y==1){word[1]=' ';y=0;}
	   
	    
		if(f)//如果不是标点
		{	
			while((isalnum(ch))&&(ch!=EOF))//是否是字母或者数字
			{
				if(i>8)
				{
					error(p,0);
					break;
				}
				else {
					word[i++]=ch;
					ch=fgetc(fp1);
				}
			}//read a letter of the program
		}
	
//		for(int n=i;n<15;n++)word[n]='\0';
		word[i++]=0;
   
		p=word;
        temp=orins(p);//check whether it is instruction or not

		if(temp)
			continue;//yes,then go on

        if(!temp)
		{
	        
			if (isdigit(word[0]))//no,check whether it is number
			{
			flag=0;
			for(int m=1;m<i;m++)
				if(!isdigit(word[m]))flag=1;

				if(flag==1)//wrong id
				{
					error(p,1);
					 
				}
			
			else 
			{
			p=word;
			output_int(p);
			}//is integer
            
			if(!feof(fp1))continue;
			else break;
			}	
		
	
		
		else
		{	
			flag=0;
			for(int m=0;m<i;m++)
				if((ispunct(word[m]))||(isupper(word[m])))flag=1;
			if(i>8)
			{
				error(p,2);
				
			}
            else if(flag==1)error(p,3);

			else {		
			p=word;
			output_ide(p,i,cou);
			cou++;
			}
		}
		}
	}
	fclose(fp1);
}
*/

⌨️ 快捷键说明

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