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

📄 word.cpp

📁 简单的词法分析器 可识别关键字 变量 整数 界符 操作符
💻 CPP
字号:
#include <cstdio>
#include <cstring>
#include <cstdlib>

#define space(c)   (c==' '||c=='\t'||c=='\10')
#define alpha(c)   (c>='a'&&c<='z')||(c>='A'&&c<='Z')
#define num(c)	   (c>='0'&&c<='9')
#define spt(c)	   (c==','||c==':'||c==';'||c=='('||c==')'||c=='{'||c=='}')
#define opt(c)	   (c=='+'||c=='-'||c=='*'||c=='/'||c=='%'||c=='='||c=='>'\
||c=='<'||c=='!'||c=='&'||c=='|')

FILE *fpin;
char c;
char word[20];
char string[100];
char key[32][20]={
	"main","coutinue","define","if","short","switch","volatile",
     "break","default","include","int","signed","typedef","while","case","do",
     "float","long","sizeof","union","char","double","for","register",
     "static","unsigned","const","else","goto","return","struct","void"};

void main(){
	
fpin=fopen("in.c","r");

while(!feof(fpin)){

int i=0,j;
do c=fgetc(fpin); while(space(c));




if(alpha(c)||c=='_') {
	
	do {word[i++]=c; c=fgetc(fpin);} while(alpha(c)||num(c)||c=='_');
	word[i]=0; 
	for(j=0;j<32;++j) 
		if(strcmp(key[j],word)==0)
{printf("keyword:%s\n",word);
if(!feof(fpin)) fseek(fpin,-1,SEEK_CUR);
goto next;}
	printf("indentify:%s\n",word);

if(!feof(fpin)) fseek(fpin,-1,SEEK_CUR);
				
}

else if(num(c)){
	do {word[i++]=c; c=fgetc(fpin);} while(num(c));
     word[i]=0;
	 printf("number:%s\n",word);

if(!feof(fpin)) fseek(fpin,-1,SEEK_CUR);


}


else if(c=='\"'){
	do {string[i++]=c;c=fgetc(fpin);}while(c!='\"');
	string[i]='\"';
    string[i+1]='\0';
	printf("string:%s\n",string);}


else if(spt(c)){
	word[0]=c;
    word[1]='\0';

	printf("seprator:%s\n",word);
}

else if(opt(c)){

	switch(c){
	
     case '+':
		 if((c=fgetc(fpin))=='+') strcpy(word,"++");
		else if(c=='=') strcpy(word,"+=");
		else{ fseek(fpin,-1,SEEK_CUR); strcpy(word,"+");}
		break;
       
	case '-':	
		if((c=fgetc(fpin))=='-') strcpy(word,"--");
		else if(c=='=') strcpy(word,"-=");
		else{ fseek(fpin,-1,SEEK_CUR); strcpy(word,"-");}
		break;


    case '*':
	   
        if((c=fgetc(fpin))=='=') strcpy(word,"*=");
		else{ fseek(fpin,-1,SEEK_CUR); strcpy(word,"*");}
		break;

	case '/':
		   
        if((c=fgetc(fpin))=='=') strcpy(word,"/=");
		else{ fseek(fpin,-1,SEEK_CUR); strcpy(word,"/");}
		break;
	
	case '%':
		strcpy(word,"%");
		break;

	case '=':
		if((c=fgetc(fpin))=='=') strcpy(word,"==");
		else{fseek(fpin,-1,SEEK_CUR); strcpy(word,"=");}
		break;
	case '>':
		if((c=fgetc(fpin))=='=') strcpy(word,">=");
		else{fseek(fpin,-1,SEEK_CUR); strcpy(word,">");}
		break;
	case '<':
		if((c=fgetc(fpin))=='=') strcpy(word,"<=");
		else{fseek(fpin,-1,SEEK_CUR); strcpy(word,"<");}
		break;

	case '!':
		if((c=fgetc(fpin))=='=') strcpy(word,"!=");
		else{fseek(fpin,-1,SEEK_CUR); strcpy(word,"!");}
		break;

    case '&':
		if((c=fgetc(fpin))=='&') strcpy(word,"&&");
		else{fseek(fpin,-1,SEEK_CUR); strcpy(word,"&");}
		break;
	case '|':
		if((c=fgetc(fpin))=='|') strcpy(word,"||");
		else{fseek(fpin,-1,SEEK_CUR); strcpy(word,"|");}
		break;
	
	
	
	
	}

printf("operator:%s\n",word);


}



next: ;



				}





}

⌨️ 快捷键说明

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