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

📄 scanner.cpp

📁 自己写的JAVA词法分析器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
            state = 0;
			token = 0x11c;
			Ending(token,char1,1);
			break;  // ~,++,--,!
		case 25:
			char1 = NextChar();
			if (char1 == '=')
				state = 28;
			else if (char1 == '|')
				state = 27;
			else {
				state = 26;
				UntRead(char1);
			}
			break;
		case 26:                 //0x114
	        state = 0;
			token = 0x114;
			Ending(token,char1,1);
			break;  // |
		case 27:                 //0x112
			state = 0;
			token = 0x112;
			Ending(token,char1,1);
			break;  // ||
		case 29:
			char1 = NextChar();
			if (char1 == '&')
				state = 31;
			else if (char1 == '=')
				state = 32;
			else {
				state = 30;
				UntRead(char1);
			}
			break;
		case 30:                     //0x116
			state = 0;
			token = 0x116;
			Ending(token,char1,1);
			break;  // &
		case 31:                    //0x113
			state = 0;
			token = 0x113;
			Ending(token,char1,1);
			break;  // &&
		case 33:
			char1 = NextChar();
			if (char1 == '=')
				state = 35;
			else {
				state = 34;
				UntRead(char1);
			}
			break;
		case 34:                     //0x115
            state = 0;
			token = 0x115;
			Ending(token,char1,1);
			break;  // ^
		case 36:
            char1 = NextChar();
			if (char1 == '=')
				state = 38;
			else {
				state = 37;
				UntRead(char1);
			}
			break;
		case 38: case 40:                    //0x117
			state = 0;
			token = 0x117;
			Ending(token,char1,1);
			break;  // ==,!=
		case 39:
			char1 = NextChar();
			if (char1 == '=')
				state = 40;
			else {
				state = 92;
				UntRead(char1);
			}
			break;
		case 41:                    // 0x111
		    state = 0;
			token = 0x111;
			Ending(token,char1,1);
			break;  // ?:
		case 43:
		    char1 = NextChar();
			if (char1 == '=')
				state = 45;
			else if (char1 == '/')
				state = 46;
			else if (char1 == '*')
				state = 48;
			else {
				state = 44;
				UntRead(char1);
			}
			break;
		case 46:
			while (char1 != '\n' && char1 != NULL)
				char1 = NextChar();
			state = 47;
			UntRead(char1);
			break;
		case 47: case 50:               //0x101
			state = 0;
			token = 0x101;
			Ending(token,char1,0);
			break;  // //,/*
		case 48:
			char1 = NextChar();
			while (char1 != '*' && char1 != NULL) {
				if (char1 == '\n')
					line++;
				char1 = NextChar();
			}
			if (char1 == NULL) {
				state = 100;
				UntRead(char1);
			}
			else state = 49;
			break;
		case 49:
			char1 = NextChar();
			if (char1 == '/')
				state = 50;
			else state = 48;
			if (char1 == '\n')
				line++;
			break;
		case 51:                      //0x120
			state = 0;
			token = 0x120;
			Ending(token,char1,1);
			break;  // ,
		case 52:                      //0x122
			state = 0;
			token = 0x121;
			Ending(token,char1,1);
			break;  // ;
		case 53:                  //0x121
			state = 0;
			token = 0x121;
			Ending(token,char1,1);
			break;  // {,}
		case 54: case 84:                     //0x11d
			state = 0;
			token = 0x11d;
			Ending(token,char1,1);
			break;  // [,],(,),.
		case 55:
		    char1 = NextChar();
			while (char1 != NULL && char1 != '"' && char1 != '\n' && char1 != '\\')
				char1 = NextChar();
			if (char1 == NULL || char1 == '\n') {
				state = 100; 
				UntRead(char1);
			}
			else if (char1 == '\\') {
				char1 = NextChar();
				if (char1 == NULL || char1 == '\n') {
                    state = 100; 
				    UntRead(char1);
				}
                else state = 55;
			}
			else if (char1 == '"')
			    state = 56;
			break;
		case 56:                    //0x109
			state = 0;
			token = 0x109;
			Ending(token,char1,1);
			break;  // 字符串
		case 57:
			char1 = NextChar();
			if (char1 == '\n' || char1 == NULL || char1 == '\'') {
				state = 100;
				UntRead(char1);
			}
			else if (char1 == '\\')
				state = 93;
			else state = 58;
			break;
		case 58:
			char1 = NextChar();
			if (char1 == '\'')
				state = 59;
			else {
				state = 100;
				UntRead(char1);
			}
			break;
		case 59:                   //0x106
			state = 0; 
			token = 0x106;
			Ending(token,char1,1);
			break;  // 字符串
		case 60:
			char1 = NextChar();
			if (char1 == '+')
				state = 62;
			else if (char1 == '=')
				state = 63;
			else {
				state = 61;
				UntRead(char1);
			}
			break;
		case 61: case 65:                     //0x11a
			state = 0;
			token = 0x11a;
			Ending(token,char1,1);
			break;  // +,-
		case 64:
			char1 = NextChar();
			if (char1 == '-')
				state = 66;
			else if (char1 == '=')
				state = 67;
			else {
				state = 65;
				UntRead(char1);
			}
			break;
		case 68:
			char1 = NextChar();
			if (char1 == 'x' || char1 == 'X')
				state = 69;
			else if (char1 <= '7' && char1 >= '0')
				state = 72;
			else if (char1 == '.')
				state = 77;
			else {
				state = 74;
				UntRead(char1);
			}
			break;
		case 69: 
			char1 = NextChar();
            if ((char1 >='a' && char1 <= 'f') || isdigit(char1) || (char1 >='A' && char1 <= 'F'))
				state = 70;
			else {
				state = 100;
				UntRead(char1);
			}
			break;
		case 70:
			char1 = NextChar();
			while ((char1 >='a' && char1 <= 'f') || isdigit(char1) || (char1 >='A' && char1 <= 'F'))
				char1 = NextChar();
			if (char1 == 'L' || char1 == 'l')
				state = 85;
			else { 
			    state = 71;
			    UntRead(char1);
			}
			break;
		case 72:
			char1 = NextChar();
			while (char1 >='0' && char1 <= '7')
				char1 = NextChar();
			if (char1 == 'L' || char1 == 'l')
				state = 87;
			else if (char1 == 'f' || char1 == 'F')
				state = 88;
			else {
			    state = 73;
			    UntRead(char1);
			}
			break;
		case 71: case 73: case 74: case 76: case 82: case 85: case 87:            //0x107
			state = 0;
			token = 0x107;
			Ending(token,char1,1);
			break;  // 16进制,8进制,0,长整型
		case 75:
			char1 = NextChar();
			while (isdigit(char1))
				char1 = NextChar();
			if (char1 == 'L' || char1 == 'l') 
				state = 74;
			else if (char1 == '.')
				state = 77;
			else if (char1 == 'e' || char1 == 'E')
				state = 79;
			else if (char1 == 'F' || char1 == 'f')
				state = 86;
			else {
				state = 82;
				UntRead(char1);
			}
			break;
		case 77:
			char1 = NextChar();
			while (isdigit(char1))
				char1 = NextChar();
			if (char1 == 'e' || char1 == 'E')
				state = 79;
			else if (char1 == 'f' || char1 == 'F')
				state = 89;
			else {
				state = 78;
				UntRead(char1);
			}
			break;
		case 78: case 81: case 88: case 89: case 90: case 86:                  //0x108
			state = 0;
			token = 0x108;
			Ending(token,char1,1);
			break; // 浮点,E
		case 79:
			char1 = NextChar();
			if (isdigit(char1))
				state = 80;
			else if (char1 == '+' || char1 == '-')
				state = 91;
			else {
				state = 100;
				UntRead(char1);
			}
			break;
		case 80:
			char1 = NextChar();
			while (isdigit(char1))
				char1 = NextChar();
			if (char1 == 'F' || char1 == 'f')
				state = 90;
			else {
			    state = 81;
			    UntRead(char1);
			}
			break;
		case 83:
			char1 = NextChar();
			if (isdigit(char1))
				state = 77;
			else {
				state = 84;
				UntRead(char1);
			}
			break;
		case 91:
			char1 = NextChar();
			if (isdigit(char1))
				state = 80;
			else {
				state = 100;
				UntRead(char1);
			}
			break;
		case 93:
			char1 = NextChar();
			if (char1 == 't' || char1 == 'b' || char1 == 'n' || char1 == 'r' || char1 == '\'' || char1 == '\\' || char1 == 'f')
				state = 58;
			else if (char1 >= '0' && char1 <= '7')
				state = 94;
			else if (char1 == 'u')
				state = 95;
			else {
                state = 100;
				UntRead(char1);
			}
			break;
		case 94:
            char1 = NextChar();
			ct = 0;
			while (ct <= 2 && char1 >= '0' && char1 <= '7') {
				char1 = NextChar();
			    ct++; 
			}
			if (char1 == '\'')
				state = 59;
			else {
				state = 100;
				UntRead(char1);
			}
			break;
		case 95:
			char1 = NextChar();
			if ((char1 >= '0' && char1 <= '9') || (char1 >= 'a' && char1 <= 'f') || (char1 >= 'A' && char1 <= 'F'))
				state = 96;
			else {
				state = 100;
				UntRead(char1);
			}
			break;
		case 96:
			ct = 0;
			char1 = NextChar();
			while ( ct <= 3 && (char1 >= '0' && char1 <= '9') || (char1 >= 'a' && char1 <= 'f') || (char1 >= 'A' && char1 <= 'F')) {
				char1 = NextChar();
			    ct++;
			}
			if (char1 == '\'')
				state = 59;
			else {
				state = 100;
				UntRead(char1);
			}
			break; 
		case 100:                    //0x100
			state = 0;
			token = 0x100;
			Ending(token,char1,1);
			break;  // error
		}
	}
	if (word_line != 0)
	    DisplayLineInfo();
    DisplayTotal();
}

void Scanner::DisplayTotal() {
	fprintf(fp_write,"*********************\n The sum of Word:%d\n*********************",word_sum);
	fclose(fp_write);
}

int main() {
	ReadFile *r = new ReadFile("Test-Lexcial.java");
	Scanner *p = new Scanner(r->Read());
	p->Analysis();
	r->Close();
	delete r;
	delete p;
	return 0;
}

⌨️ 快捷键说明

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