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

📄 cifa.cpp

📁 编译原理课程设计
💻 CPP
字号:
#include "stdio.h"
#include <ctype.h>
#include <string.h>
#include "guize.h"
#include "zichx.h"
#include <iostream.h>
FILE *fp;
char fpname[20];
// 保存完整字串,也许是一个关键词 / 一个一个地接字符
char temp[20], ch;
//跳过错误警告
int bypass_error;
int i;

void main (int argc, char *argv[])
{	
	cout<<"请输入要分析的文件名(以#结束):"<<endl;
	for(int a=0;;a++)
	{
		fpname[a]=getchar();
		if(fpname[a]=='#')
		{
			fpname[a]='\0';
			break;
		}
	}
	if ((fp=fopen(fpname,"r"))==NULL)//打开文件名为cpp1.cpp的文件,打开类型为r(只读)
		printf("error");
	else
	{
		//得第一个"字符"
		ch = fgetc(fp);
		while(ch!=EOF)
		{
			//重置略错
			bypass_error = 0;
			//清空temp
			i = 0;
			for (i = 0; i <= 20; i++)
			{
				temp[i] = 0;
			}
			//########################################################
			//第一个字符是字母
			i = 0;
			if (isalpha(ch))
			{
				//把一个完整的合法字串放temp,temp可能是关键词,也可能是其它非数字开头的字串
				while ((isalpha(ch))||(isdigit(ch)))
				{
					temp[i++]=ch;
					ch=fgetc(fp);
				}
				
				//到此,temp中存放了一个完整字串
				temp[i+1]='\0';
				///////////////////////////////////////////////
				//如果是关键字
				if (is_keyword(temp))
				{
					printf("(1 ,“ %s ”)\n",temp); 
				}
				//否则作一般字串
				else
				{
					printf("(2 ,“ %s ”)\n",temp); 
				}
			}
			//########################################################
		    //第一个字符是数字
			else if (isdigit(ch))
			{
				i = 0;
				while (isdigit(ch))
				{
					temp[i++]=ch;
					ch=fgetc(fp);
				}
				temp[i+1]='\0';
				printf("(3 ,“ %s ”)\n",temp);
			}
			//########################################################
			
			else
			{
				i = 0;
				temp[i++]=ch;
				//是分隔符
				if(is_split(temp))
				{
					printf("(5 ,“ %s ”)\n",temp);
					bypass_error = 1;
				}
				//是一个 猜测运算符,除“!”号外都是真正的运算符
				else if(is_matopt_g(temp))
				{
					//是不是一个单字节运算符
					int is_s = 0;
					//是不是一个真正的运算符
					int is_t = 0;
					//是不是一个“!”;
					int is_oh = 0;	

					is_oh = (ch == '!') ? 1 : 0;
					is_s = is_matopt_s(temp);

					ch=fgetc(fp);
					temp[i++]=ch;

					//是一个双字节运算符
					//是一个真正的运算符
					if(is_matopt_m(temp) || is_s)
					{
						printf("(4 ,“ %s ”)\n",temp);
						is_t = 1;
						bypass_error = 1;
					}

					if(!is_t && is_oh)
					{
						printf("Error,“ !”is not a legal word!\n",ch);
					}

				}
				if (!bypass_error && ( (ch!='\n') && (ch!=' ') ) )
				{
					printf("Error,“ %c ”is not a legal word!\n",ch);
				}
				ch=fgetc(fp);
			}
		}
	}
	printf("\n Done! \n");
	fclose(fp);
}

⌨️ 快捷键说明

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