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

📄 cifa.cpp

📁 词法分析的目的是将输入的源程序进行划分
💻 CPP
字号:
#include<fstream>
#include<iostream>
#include<string>

using namespace std;

ifstream fin("input.txt");
ofstream fout("output.txt");

int start=0;		//当前正在检查的状态图的开始状态标号
int state=0;		//当前所在状态的标号
int token_beginning;//一个token在buf中的开始位置
char c;				//用于存储从buf中提取的一个字符
int forward=0;		//当前正在检查的字符在buf中的位置

#define BSIZE 128	//buf的大小
char buf[BSIZE];	//缓冲区
int bufend=0;
		//输入某一程序段所占buf的大小
#define keyword 1
#define id 2	    //基本保留字、标识符
#define num 3	    //常数
#define relation 4  //关系运算符
#define operation 4	//算术运算符
#define separation 5 //分隔符


int KEY_lenth=17;
char * keywords[]= {	//关键字
	"int","char","float","double","bool","void",
		"procedure","call","begin","end",
		"if","then","while","do","var","odd","const"
};

char nextchar()  //取下一个字符;
{
	return buf[forward++];
	
}

void recover(){		//错误恢复,向前指针前移
	forward++;
}
void retract(int i){	//向前指针回退
	forward-=i;
}
void install_id(){};	//将id写入符号表,还未定义
void install_num(){};	//将num写入符号表,还未定义

int fail()			//将start置为下一状态图的开始状态,用下一状态图识别此token。
{
	forward=token_beginning;
	switch(start){
	case 0:
		start=9;
		break;
	case 9:
		start=12;
		break;
	case 12:
		start=20;
		break;
	case 20:
		start=25;
		break;
	case 25:
		start=28;
		break;
	case 28:
		start=30;
		break;
	case 30:
		recover();
		start=-1;
		break;
	default:
		;
	}
	return start;
}



int nexttoken()		//识别下一个词素,根据状态转换图编写。
{
	start=state=0;
	token_beginning=forward;
	while(1){
		switch(state){
		case 0:			//0~8,识别关系运算符。
			c=nextchar();
			if(c==' '||c=='\t'||c=='\n'){
				state=0;
				token_beginning++;
			}
			else if(c=='<')
				state=1;
			else if(c=='=')
				state=5;
			else if(c=='>')
				state=6;
			else 
				state=fail();
			break;
		case 1:
			c=nextchar();
			if(c=='=')
				state=2;
			else if(c=='>')
				state=3;
			else{
				state=4;
			}
			break;
		case 2:
			return(4);
		case 3:
			return(4);
		case 4:	
			retract(1);
			return(4);
		case 5:
			return(4);
		case 6:
			c=nextchar();
			if(c=='=')
				state=7;
			else
				state=8;
			break;
		case 7:
			return(4);
		case 8:
			retract(1);
			return(4);

		case 9:			
			c=nextchar();
			if(isalpha(c))
				state=10;
			else
				state=fail();
			break;
		case 10:
			c=nextchar();
			if(isalpha(c))
				state=10;
			else if(isdigit(c))
				state=10;
			else state=11;
			break;
		case 11:
			retract(1);
			//install_id();
			return(2);

		case 12:		//12~19,识别形如1.2E+123的数。
			c=nextchar();
			if(isdigit(c))
				state=13;
			else
				state=fail();
			break;
		case 13:
			c=nextchar();
			if(isdigit(c))
				state=13;
			else if(c=='.')
				state=14;
			else if(c=='E')
				state=16;
			else 
				state=fail();
			break;
		case 14:
			c=nextchar();
			if(isdigit(c))
				state=15;
			else
				state=fail();
			break;
		case 15:
			c=nextchar();
			if(isdigit(c))
				state=15;
			else if(c=='E')
				state=16;
			else
				state=fail();
			break;
		case 16:
			c=nextchar();
			if(c=='+'||c=='-')
				state=17;
			else if(isdigit(c))
				state=18;
			else
				state=fail();
			break;
		case 17:
			c=nextchar();
			if(isdigit(c))
				state=18;
			else
				state=fail();
			break;
		case 18:
			c=nextchar();
			if(isdigit(c))
				state=18;
			else
				state=19;
			break;
		case 19:
			retract(1);
			///install_num();
			return(3);

		case 20:		//20~24,识别形如1.234的数。
			c=nextchar();
			if(isdigit(c))
				state=21;
			else
				state=fail();
			break;
		case 21:
			c=nextchar();
			if(isdigit(c))
				state=21;
			else if(c=='.')
				state=22;
			else
				state=fail();
			break;
		case 22:
			c=nextchar();
			if(isdigit(c))
				state=23;
			else
				state=fail();
			break;
		case 23:
			c=nextchar();
			if(isdigit(c))
				state=23;
			else
				state=24;
			break;
		case 24:
			retract(1);
			//install_num();
			return(3);

		case 25:		//25~27,识别形如123的数。
			c=nextchar();
			if(isdigit(c))
				state=26;
			else
				state=fail();
			break;
		case 26:
			c=nextchar();
			if(isdigit(c))
				state=26;
			else
				state=27;
			break;
		case 27:
			retract(1);
			return(3);

		case 28:		//28~29,识别运算符。
			c=nextchar();
			if(c=='+'||c=='-'||c=='*'||c=='/'||
				c=='('||c==')'||c=='['||c==']'||c=='{'||c=='}')
				state=29;
			else
				state=fail();
			break;
		case 29:
			return(5);

		case 30:		//30~31,识别分隔符。
			c=nextchar();
			if(c==','||c==';')
				state=31;
			else 
				state=fail();
			break;
		case 31:
			return(5);
		default:
			return 0;
		}
	}
}

void judge_print(int type)	 //根据type,选择输出。
{
	int i,j=0;
	char str[30];	//将词素保存在str数组中。
	for(i=token_beginning;i<forward;i++)
		str[j++]=buf[i];
	str[j]=0;

	if(type==id){
		for(int i=0;i<KEY_lenth;i++)	//判断id是否是关键字。
			if(strcmp(str,keywords[i])==0){
				cout<<"[1,"<<str<<']'<<endl;
				fout<<"[1,"<<str<<']'<<endl;
				break;
			}
		if(i==KEY_lenth){	//没有关键字与id匹配
			cout<<"[2,"<<str<<']'<<endl;
			fout<<"[2,"<<str<<']'<<endl;
		}
	}
	else if(type==num){
		cout<<"[3,"<<str<<']'<<endl;
		fout<<"[3,"<<str<<']'<<endl;
	}
	else if(type==relation||type==operation){
		cout<<"[4,"<<str<<']'<<endl;
		fout<<"[4,"<<str<<']'<<endl;
	}
	else if(type==separation){
		cout<<"[5,"<<str<<']'<<endl;
		fout<<"[5,"<<str<<']'<<endl;
	}
}


int main()
{
	string c;
	int i;
	while(fin>>c){	//将输入保存在buf数组中。
		for(i=0;c[i];i++)
			buf[bufend++]=c[i];
		buf[bufend++]=' ';
	}

	int type;
	while(forward<bufend){	//识别、打印每个token,直到buf结尾
		type=nexttoken();
		judge_print(type);
	}
	return 0;
}

⌨️ 快捷键说明

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