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

📄 temp.cpp

📁 针对java语言的最简词法分析器 输入原文件输出目标文件。txt
💻 CPP
字号:
#include<stdio.h>
#include<ctype.h>
#include<stdlib.h>
#include<istream.h>
#include<fstream.h>
#include<string.h>
int state = 0;
char str[55][2][8] = {
	{"0x100","出错"},{"0x101","注释"},{"0x102"," "},{"0x103","关键字"},{"0x104","标识符"},
	{"0x105","bool"},{"0x106","char"},{"0x107","int"},{"0x109","string"},{"0x110","="},
	{"0x110","+="},{"0x110","-="},{"0x110","*="},{"0x110","/="},{"0x110","%="},
	{"0x110","&="},{"0x110","^="},{"0x110","|="},{"0x110",">>="},{"0x110","<<="},
	{"0x110",">>>="},{"0x111","?"},{"0x111",":"},{"0x112","||"},{"0x113","&&"},
	{"0x114","|"},{"0x115","^"},{"0x116","&"},{"0x117","=="},{"0x117","!="},
	{"0x118","<"},{"0x118",">"},{"0x118","<="},{"0x118",">="},{"0x119","<<"},
	{"0x119",">>"},{"0x119",">>>"},{"0x11a","+"},{"0x11a","-"},{"0x11b","*"},
	{"0x11b","/"},{"0x11b","%"},{"0x11c","++"},{"0x11c","--"},{"0x108","float"},
	{"0x11c","!"},{"0x11c","~"},{"0x11d","["},{"0x11d","]"},{"0x11d","("},
	{"0x11d",")"},{"0x120",","},{"0x121","{"},{"0x121","}"},{"0x122",";"}
};
int main(){
	char buffer[50],buffer2[50];
  	int row_num=0,Rtotal[9000]={0},total=0,b=0,condition=0,condition2=0,i=0;
	char in,out1,out2,tempbuffer[10]={0},len;
	char buffer1[]=" ";
	printf("please input the name of the file:");
	scanf("%s",buffer);
	getchar();
	for(i=0;buffer[i]!='.';i++)
		buffer2[i]=buffer[i];
	buffer2[i]='\0';
	ifstream fpin(buffer);
	strcat(buffer2,"_scanner_output.txt");
	ofstream fpout(buffer2);
	if(fpin.fail())
		exit(0);
	if(fpout.fail())
		exit(0);
	while(!fpin.eof()){
		in=fpin.get();
		if(in==-1){
			goto exit;
		}
		switch(state) {
			case 0:				
				if(isalpha(in) || in=='$' || in=='_'){
					state=1;  //关键字 标识符	
					fpin.putback(in);
				}
				else if(isdigit(in)){
					state=44;  //数字
					fpin.putback(in);
				}							
				else if(in=='\'')
					state=5;   //'
				else if(in=='\"')
					state=6;  //"
				else if(in=='\\')
					state=7;  //转义
				else if(in=='{'){
					state=52;  //{
					fpin.putback(in);
				}
				else if(in=='}'){
					state=53;  //}
					fpin.putback(in);
				}
				else if(in=='('){
					state=49;  //(
					fpin.putback(in);
				}
				else if(in==')'){
					state=50;  //)
					fpin.putback(in);
				}
				else if(in=='['){
					state=47;  //[
					fpin.putback(in);
				}
				else if(in==']'){
					state=48;  //]
					fpin.putback(in);
				}
				else if(in==';'){
					state=54;  //;
					fpin.putback(in);
				}
				else if(in=='.'){
					state=55;  //.
					fpin.putback(in);
				}
				else if(in==','){
					state=51;  //,
					fpin.putback(in);
				}
				else if(in==' '){
					state=2;  //空格 
					fpin.putback(in);
				}
				else if(in=='\n'){
					state=8;  //回车
					fpin.putback(in);
				}
				else if(in=='-'){
					in=fpin.get();
				    if(in=='=')
						state=11;   //-=
					else if(in=='-')
						state=43;  //--
					else						
						state=36;  //-
					fpin.putback(in);
				}
				else if(in=='+'){
					in=fpin.get();
				    if(in=='=')
						state=10;   //+=
					else if(in=='+')
						state=42;  //++
					else					
						state=37;    //+
					fpin.putback(in);
				}
				else if(in=='&'){
					in=fpin.get();
				    if(in=='=')
						state=15;   //&=
					else if(in=='&')
						state=24;  //&&
					else 						
						state=27;    //&
					fpin.putback(in);
				}
				else if(in=='='){
					in=fpin.get();
				    if(in=='=')
						state=28;   //==				
					else					
						state=9;    //=
					fpin.putback(in);
				}				
				else if(in=='~'){
					state=46;
					fpin.putback(in);
				}
				else if(in=='|'){
					in=fpin.get();
				    if(in=='=')
						state=17;   //|=
					else if(in=='|')
						state=23;  //||
					else {						
						state=25;    //|
					}
					fpin.putback(in);
				}
				else if(in=='^'){
					in=fpin.get();
				    if(in=='=')
						state=16;   //^=					
					else {						
						state=26;    //^
					}
					fpin.putback(in);
				}
				else if(in=='?'){
					state=4;     //?
					fpin.putback(in);
				}
				else if(in=='/'){
					in=fpin.get();
				    if(in=='=')
						state=13;   // /=
					else if(in=='*')
						state=21;   // /*
					else if(in=='/')
						state=3;   // //
					else {						
						state=40;    // /
					}
					fpin.putback(in);
				}				
				else if(in=='>'){
					in=fpin.get();
					if(in=='>'){
						in=fpin.get();
						if(in=='='){
							state=18;   //>>=
							fpin.putback(in);
						}
						else if(in=='>'){
							in=fpin.get();
							if(in=='=')
								state=20;    // >>>=
							else
								state=36;   // >>>
							fpin.putback(in);
						}
						else {						
							state=35;    //>>
							fpin.putback(in);
						}											
					}
					else if(in=='='){
						state=33;   //>=
						fpin.putback(in);
					}
					else {						
						state=31;    //>
						fpin.putback(in);						
					}					
				}
				else if(in=='<'){
					in=fpin.get();
				    if(in=='=')
						state=32;   // <=
					else if(in=='<'){
						in=fpin.get();
						if(in=='=')
							state=19;  // <<=
						else
							state=34;	 // <<					
					}
					else {						
						state=30;    // <
					}
					fpin.putback(in);
				}				
				else if(in=='%'){
					in=fpin.get();
					if(in=='=')
						state=14; // %=
					else
						state=41;  //%
					fpin.putback(in);
				}
				else if(in=='*'){
					in=fpin.get();
					if(in=='=')
						state=12; // *=
					else
						state=39;  // *
					fpin.putback(in);
				}
				else if(in=='!'){
					in=fpin.get();
					if(in=='=')
						state=24; // !=
					else
						state=45;  // !
					fpin.putback(in);
				}
				else if(in==':'){
					state=22;     // :
					fpin.putback(in);
				}
				else if(in=='\t'){
					state=2;     // :
					fpin.putback(in);
				}
				else{
					fpout<<str[0][1]<<' '<<str[0][0]<<endl;
					state=0;
				}
				break; 
			case 1:
				b=0;
				while(isalnum(in) || in=='$' || in=='_'){
					tempbuffer[b++]=in;
					in=fpin.get();
				}				
			    	fpin.putback(in);
				tempbuffer[b]='\0';
				b=0;				
					if(strcmp(tempbuffer,"abstract")==0 || strcmp(tempbuffer,"boolean")==0 || strcmp(tempbuffer,"break")==0||strcmp(tempbuffer,"break")==0 || strcmp(tempbuffer,"case")==0 || strcmp(tempbuffer,"catch")==0||strcmp(tempbuffer,"char")==0 || strcmp(tempbuffer,"class")==0 || strcmp(tempbuffer,"const")==0||strcmp(tempbuffer,"continue")==0 || strcmp(tempbuffer,"default")==0 || strcmp(tempbuffer,"do")==0||strcmp(tempbuffer,"double")==0 || strcmp(tempbuffer,"else")==0 || strcmp(tempbuffer,"extends")==0||strcmp(tempbuffer,"final")==0 || strcmp(tempbuffer,"finally")==0 || strcmp(tempbuffer,"float")==0||strcmp(tempbuffer,"for")==0 || strcmp(tempbuffer,"goto")==0 || strcmp(tempbuffer,"if")==0||strcmp(tempbuffer,"implements")==0 || strcmp(tempbuffer,"import")==0 || strcmp(tempbuffer,"instanceof")==0||
					strcmp(tempbuffer,"int")==0 || strcmp(tempbuffer,"interface")==0 || strcmp(tempbuffer,"long")==0||strcmp(tempbuffer,"native")==0 || strcmp(tempbuffer,"new")==0 || strcmp(tempbuffer,"null")==0||strcmp(tempbuffer,"package")==0 || strcmp(tempbuffer,"private")==0 || strcmp(tempbuffer,"protected")==0||strcmp(tempbuffer,"public")==0 || strcmp(tempbuffer,"return")==0 || strcmp(tempbuffer,"short")==0||strcmp(tempbuffer,"static")==0 || strcmp(tempbuffer,"super")==0 || strcmp(tempbuffer,"switch")==0||strcmp(tempbuffer,"synchronized")==0 || strcmp(tempbuffer,"this")==0 || strcmp(tempbuffer,"throw_num")==0||strcmp(tempbuffer,"throw_nums")==0 || strcmp(tempbuffer,"transient")==0 || strcmp(tempbuffer,"try")==0||strcmp(tempbuffer,"void")==0 || strcmp(tempbuffer,"volatile")==0 || strcmp(tempbuffer,"while")==0){	
						fpout<<tempbuffer<<' '<<str[3][0]<<endl;
						state=0;
						Rtotal[row_num]++;
						total++;
					}
					else if(strcmp(tempbuffer,"false")==0 || strcmp(tempbuffer,"true")==0){
						fpout<<tempbuffer<<' '<<str[5][0]<<endl;
						state=0;
						Rtotal[row_num]++;
						total++;
					}
					else{
						fpout<<tempbuffer<<' '<<str[4][0]<<endl;
						state=0;
						Rtotal[row_num]++;
						total++;
					}				
				break;

			case 2:
				fpout<<str[state][1]<<' '<<str[state][0]<<endl;
				goto POPIN3;
            		case 3:
				while(in!='\n'){
					in=fpin.get();
					if(in==-1 || fpin.eof())
						break;
				}
				fpin.putback(in);
				fpout<<str[1][1]<<' '<<str[1][0]<<endl;
				goto POPIN1;
			case 4:
				condition2=1;
				goto POPIN3;
			case 5:				
				if(in=='\''){
					fpout<<"\'空\'"<<' '<<str[6][0]<<endl;
					state=0;
					Rtotal[row_num]++;
					total++;	
				}
				else if(in=='\\'){
					in=fpin.get();
					out1=in;
					in=fpin.get();
					if(in=='\''){
						if(out1=='\'' || out1=='\\' || out1=='r' || out1=='n'|| out1=='f'|| out1=='t'|| out1=='b'){
							fpout<<'\\'<<out1<<' '<<str[6][0]<<endl;
							state=0;
							Rtotal[row_num]++;
							total++;
						}
						else{
							fpout<<str[0][1]<<' '<<str[0][0]<<endl;
							state=0;
						}
					}
					else{
						fpout<<str[0][1]<<' '<<str[0][0]<<endl;
						state=0;
					}
				}
				else{
					out2=in;
					in=fpin.get();
					if(in=='\''){
						fpout<<out2<<' '<<str[6][0]<<endl;
						state=0;
						Rtotal[row_num]++;
						total++;
					}
					else{						
						fpout<<str[0][1]<<' '<<str[0][0]<<endl;
						state=0;
					}
				}
				break;
			case 6:
				b=0;
				while(in!='\"'){
					tempbuffer[b++]=in;
					in=fpin.get();
					if(in==-1 || fpin.eof())
						break;
				}
				tempbuffer[b]='\0';
				len=strlen(tempbuffer);
				for(i=0,b=0;b<len;i++,b++){
					if(tempbuffer[b]=='\\'){				
						out1=tempbuffer[b+1];
						b++;
						if(out1=='\'' || out1=='\\' || out1=='r' || out1=='n'||out1=='f'||out1=='t'|| out1=='b'){
							fpout<<'\\'<<out1<<' '<<str[6][0]<<endl;
							state=0;
							Rtotal[row_num]++;
							total++;
						}
						else{
							fpout<<str[0][1]<<' '<<str[0][0]<<endl;
							state=0;
						}						
					}
				}

				fpout<<tempbuffer<<' '<<str[9][0]<<endl;
				goto POPIN1;
		    	case 7:	
				out1=in;
				if(out1=='\'' || out1=='\\' || out1=='r' || out1=='n'||out1=='f'|| out1=='t'|| out1=='b'){
					fpout<<str[6][1]<<' '<<str[6][0]<<endl;
					goto POPIN1;
				}
				else{
					fpout<<str[0][1]<<' '<<str[0][0]<<endl;
					goto POPIN3;
				}

            		case 8:
				if(condition2){
					condition2=0;
					fpout<<str[0][1]<<' '<<str[0][0]<<endl;
				}
				row_num++;
				goto POPIN3;	

			case 9:case 25:case 26:case 27:case 30:case 31:case 34:case 35:case 36:case 37:case 38:case 39:case 40:case 41:case 45:
				fpin.putback(in);
		    case 10:case 11:case 12:case 13:case 14:case 15:case 16:case 17:case 18:case 19:case 20:case 23:case 24:case 28:case 32:case 33:case 42:case 43:case 46:case 47:case 48:case 49:case 50:case 51:case 52:case 53:case 54:case 55:
				fpout<<str[state][1]<<' '<<str[state][0]<<endl;
POPIN1:				Rtotal[row_num]++;
				total++;
POPIN3:				state=0;
				break;
			case 21:
				in=fpin.get();
				do{					
					out1=in;
					in=fpin.get();
					if(in==-1 || fpin.eof())
						break;
				}while(out1!='*' || in!='/');					
				fpout<<str[1][1]<<' '<<str[1][0]<<endl;
				goto POPIN1;
			case 22:
				if(condition2)
					fpout<<str[21][1];
				fpout<<str[state][1]<<' '<<str[21][0]<<endl;
				goto POPIN1;
			case 44:							
				if(in=='0'){
					b=0;
					in=fpin.get();
					if(in=='x' || in=='X'){
						out1=in;						
						do{
							in=fpin.get();
							tempbuffer[b++]=in;
						}while(isdigit(in) || (in>=65 && in<=70) || (in>=97 && in<=102));
						tempbuffer[b-1]='\0';
						b=0;
						fpin.putback(in);
						fpout<<str[7][1]<<' '<<str[7][0]<<endl;
						state=0;
						Rtotal[row_num]++;
						total++;
						condition=0;
					}
					else{
						condition=1;
						fpin.putback(in);
					}
				}
				else
					condition=1;
				if(condition){
					b=0;
					while(isdigit(in)){
						tempbuffer[b++]=in;
						in=fpin.get();	
						if(in==-1 || fpin.eof())
							break;
					}				
					if(in=='.'){
						tempbuffer[b++]=in;					
						do{
							in=fpin.get();
							if(in==-1 || fpin.eof())
								break;
							tempbuffer[b++]=in;
						}while(isdigit(in));
						if(in=='e' || in=='E'){
							goto POPIN2;
						}
						else{
							fpin.putback(in);
							tempbuffer[b-1]='\0';
							b=0;
							fpout<<str[0][1]<<' '<<str[0][0]<<endl;
							state=0;
							Rtotal[row_num]++;
							total++;
						}
					}
					else if(in=='e' || in=='E'){
						tempbuffer[b++]=in;
POPIN2:					in=fpin.get();
						if(in=='+' || in=='-'){
							tempbuffer[b++]=in;
						}
						else if(isdigit(in))
							fpin.putback(in);
						else{
							tempbuffer[b]='\0';
							b=0;
							fpout<<str[0][1]<<' '<<str[0][0]<<endl;
							state=0;
						}
						if(state!=0){
							do{
								in=fpin.get();
								if(in==-1 || fpin.eof())
									break;
								tempbuffer[b++]=in;
							}while(isdigit(in));
							fpin.putback(in);
							tempbuffer[b-1]='\0';
							b=0;
							fpout<<str[state][1]<<' '<<str[state][0]<<endl;
							state=0;
							Rtotal[row_num]++;
							total++;
						}
					}
					else if(in=='l' || in=='L'){
						tempbuffer[b++]=in;
						tempbuffer[b]='\0';
						b=0;
						fpout<<str[7][1]<<' '<<str[7][0]<<endl;
						state=0;
						Rtotal[row_num]++;
						total++;	
					}
					else{
						fpin.putback(in);
						tempbuffer[b]='\0';
						b=0;
						fpout<<str[7][1]<<' '<<str[7][0]<<endl;
						state=0;
						Rtotal[row_num]++;
						total++;				
					}				
				}
				break;
		}
	}
exit:	printf("total: linenum = %d, wordnum = %d.\n",row_num+1,total);
	getchar();
    return 0;
}

⌨️ 快捷键说明

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