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

📄 source.txt.bak

📁 编译原理
💻 BAK
📖 第 1 页 / 共 2 页
字号:
int state;
char *pBegin;
char *pFinal;
char *pFstHalf;
char *pSecHalf;
bool end;

const char* keyword[KEYNUM] = {
	"auto", "break", "case", "char", "const", "continue", "default", "do", "double", "else",
	"enum", "extern", "float", "for", "goto", "if", "int", "long", "register", "return",
	"short", "signed", "sizeof", "static", "struct", "switch", "typedef", "union", "unsigned",
	"void", "volatile", "while"
};

AttributeByte changeState(char* pBuffer, FILE* pScr, FILE* pDes);
char getNextChar(char* pBuffer, FILE* pScr);
void backPoint(char* pBuffer);
void setValue(char* pBuffer, char* value);
void setString(char* pBuffer, char* value);
void doError();
bool judgeKey(char* strKey);
void goNextChar(char* pBuffer, FILE* pScr);

void main()
{
	FILE* pScr;
	FILE* pDes;
	char buffer[2 * BUFFERSIZE];
	AttributeByte aByte;
	pScr = initFile("res\\Source.txt", "r");
	pDes = initFile("res\\Destination.txt", "w");
	pBegin = pFinal = buffer;
	pFstHalf = buffer;
	pSecHalf = buffer + BUFFERSIZE;
	end = false;
	loadFromFileToBuffer(pScr, pFstHalf, BUFFERSIZE);
	while(!end)
	{
		aByte = changeState(buffer, pScr, pDes);
		putAttributeToFile(pDes, &aByte);
		pBegin = pFinal;
	}
	closeFile(pScr);
	closeFile(pDes);
}

AttributeByte changeState(char* pBuffer, FILE* pScr, FILE* pDes)
{
	char tempChar;
	char inputChar;
	AttributeByte aByte;
	
	state = 0;
	if(state == 0)
	{
		inputChar = *pFinal;
		if(inputChar == -1)
		{
			state = 99;
		}
		else if(isLetter(inputChar))
		{
			state = 1;
		}
		else if(isNumber(inputChar))
		{
			state = 3;
		}
		else
		{
			switch(inputChar)
			{
			case '=':
				state = 5;
				break;
			case '*':
				state = 6;
				break;
			case '/':
				state = 22;
				break;
			case '+':
				state = 7;
				break;
			case '-':
				state = 18;
				break;
			case '%':
				state = 30;
				break;
			case '>':
				state = 33;
				break;
			case '<':
				state = 39;
				break;
			case '&':
				state = 45;
				break;
			case '|':
				state = 49;
				break;
			case '^':
				state = 53;
				break;
			case '.':
				state = 56;
				break;
			case '\\':
				break;
			case '?':
				state = 57;
				break;
			case ':':
				state = 58;
				break;
			case '!':
				state = 59;
				break;
			case '~':
				state = 62;
				break;
			case '{':
				state = 11;
				break;
			case '}':
				state = 12;
				break;
			case '(':
				state = 13;
				break;
			case ')':
				state = 14;
				break;
			case '[':
				state = 15;
				break;
			case ']':
				state = 16;
				break;
			case ';':
				state = 17;
				break;
			case ',':
				state = 27;
				break;
			case '\'':
				state = 64;
				break;
			case '\"':
				state = 65;
				break;
			default:
				state = 100;
				break;
			}
		}
	}
	////////////////////////////////////////////////////////////
	if(state == 1)
	{
		while(isLetter(inputChar) || isNumber(inputChar))
		{
			inputChar = getNextChar(pBuffer, pScr);
		}
		state = 2;
	}
	////////////////////////////////////////////////////////////
	if(state == 2)
	{
		int n;
		if(pFinal > pBegin)
		{
			n = pFinal - pBegin + 1;
		}
		else
		{
			n = (2 * BUFFERSIZE) - (pBegin - pFinal) + 1;
		}
		aByte.value = (char *)malloc(n);
		setValue(pBuffer, aByte.value);
		if(judgeKey(aByte.value))
			aByte.attribute = KEYWORD;
		else
			aByte.attribute = SYMBOL;
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 3)
	{
		tempChar = inputChar;
		inputChar = getNextChar(pBuffer, pScr);
		if(tempChar == '0' && inputChar == 'x')
		{
			state = 71;
		}
		else if(inputChar == '.')
		{
			state = 73;
		}
		else
		{
			state = 72;
		}
	}
	if(state == 71)//十六进制数
	{
		int n;
		inputChar = getNextChar(pBuffer, pScr);
		while(is16Number(inputChar))
		{
			inputChar = getNextChar(pBuffer, pScr);
		}		
		if(inputChar == 'l' || inputChar == 'L' || inputChar == 'u' || inputChar == 'U')
		{
			goNextChar(pBuffer, pScr);
		}
		if(pFinal > pBegin)
		{
			n = pFinal - pBegin + 1;
		}
		else
		{
			n = (2 * BUFFERSIZE) - (pBegin - pFinal) + 1;
		}
		aByte.value = (char *)malloc(n);
		setValue(pBuffer, aByte.value);
		aByte.attribute = CONST_NUM;	
		return aByte;
	}
	if(state == 72)
	{
		while(isNumber(inputChar))
		{
			inputChar = getNextChar(pBuffer, pScr);
		}
		switch(inputChar)
		{
		case '.':
			state = 73;
			break;
		case 'e':
		case 'E':
			state = 74;
			break;
		default:
			state = 4;
			break;
		}
	}
	if(state == 73)
	{
		inputChar = getNextChar(pBuffer, pScr);
		while(isNumber(inputChar))
		{
			inputChar = getNextChar(pBuffer, pScr);
		}
		switch(inputChar)
		{
		case 'E':
		case 'e':
			state = 75;
			break;
		default:
			state = 4;
			break;
		}
	}
	if(state == 74)
	{
		inputChar = getNextChar(pBuffer, pScr);
		while(isNumber(inputChar))
		{
			inputChar = getNextChar(pBuffer, pScr);
		}
		state = 4;
	}
	if(state == 75)
	{
		inputChar = getNextChar(pBuffer, pScr);
		if(inputChar == '+' || inputChar == '-')
		{
			goNextChar(pBuffer, pScr);
		}
		inputChar = getNextChar(pBuffer, pScr);
		while(isNumber(inputChar))
		{
			inputChar = getNextChar(pBuffer, pScr);
		}
		state = 4;
	}
	if(state == 4)
	{
		int n;
		if(inputChar == 'f' || inputChar == 'F' || inputChar == 'l' || inputChar == 'L')
		{
			goNextChar(pBuffer, pScr);
		}
		if(pFinal > pBegin)
		{
			n = pFinal - pBegin + 1;
		}
		else
		{
			n = (2 * BUFFERSIZE) - (pBegin - pFinal) + 1;
		}
		aByte.value = (char *)malloc(n);
		setValue(pBuffer, aByte.value);
		aByte.attribute = CONST_NUM;
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 5)
	{
		inputChar = getNextChar(pBuffer, pScr);
		switch(inputChar)
		{
		case '=':
			state = 26;
			break;
		default:
			state = 25;
		}
	}
	if(state == 25)
	{
		aByte.attribute = OPERATION;
		aByte.value = "=";
		return aByte;
	}
	if(state == 26)
	{
		aByte.attribute = OPERATION;
		aByte.value = "==";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 6)
	{
		inputChar = getNextChar(pBuffer, pScr);
		switch(inputChar)
		{
		case '=':
			state = 29;
			break;
		default:
			state = 28;
		}
	}
	if(state == 28)
	{
		aByte.attribute = OPERATION;
		aByte.value = "*";
		return aByte;
	}
	if(state == 29)
	{
		aByte.attribute = OPERATION;
		aByte.value = "*=";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 7)
	{
		inputChar = getNextChar(pBuffer, pScr);
		switch(inputChar)
		{
		case '+':
			state = 9;
			break;
		case '=':
			state = 10;
			break;
		default:
			state = 8;
		}
	}
	if(state == 8)
	{
		aByte.attribute = OPERATION;
		aByte.value = "+";
		return aByte;
	}
	if(state == 9)
	{
		aByte.attribute = OPERATION;
		aByte.value = "++";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	if(state == 10)
	{
		aByte.attribute = OPERATION;
		aByte.value = "+=";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 11)
	{
		aByte.attribute = BOUNDARY;
		aByte.value = "{";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	if(state == 12)
	{
		aByte.attribute = BOUNDARY;
		aByte.value = "}";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 13)
	{
		aByte.attribute = BOUNDARY;
		aByte.value = "(";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	if(state == 14)
	{
		aByte.attribute = BOUNDARY;
		aByte.value = ")";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 15)
	{
		aByte.attribute = BOUNDARY;
		aByte.value = "[";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	if(state == 16)
	{
		aByte.attribute = BOUNDARY;
		aByte.value = "]";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 17)
	{
		aByte.attribute = BOUNDARY;
		aByte.value = ";";
		goNextChar(pBuffer, pScr);
		return aByte;
	}
	////////////////////////////////////////////////////////////
	if(state == 18)
	{
		inputChar = getNextChar(pBuffer, pScr);

⌨️ 快捷键说明

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