📄 compile.cpp
字号:
#include "ComplieHead.h"
#include "malloc.h"
#include "string.h"
#define BUFFERSIZE 10
#define KEYNUM 32
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, bool* pIsNeedFree); //设置状态
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("input.txt", "r");
pDes = initFile("output.txt", "w");
pBegin = pFinal = buffer;
pFstHalf = buffer;
pSecHalf = buffer + BUFFERSIZE;
end = false;
loadFromFileToBuffer(pScr, pFstHalf, BUFFERSIZE); //读取源文件
while(!end)
{
bool isNeedFree = false;
aByte = changeState(buffer, pScr, pDes, &isNeedFree);
putAttributeToFile(pDes, &aByte);
pBegin = pFinal;
if(isNeedFree)
{
free(aByte.value);
}
}
closeFile(pScr);
closeFile(pDes);
}
AttributeByte changeState(char* pBuffer, FILE* pScr, FILE* pDes, bool* pIsNeedFree)
{
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;
*pIsNeedFree = true;
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;
*pIsNeedFree = true;
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;
*pIsNeedFree = true;
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);
switch(inputChar)
{
case '-':
state = 20;
break;
case '=':
state = 21;
break;
case '>':
state = 63;
break;
default:
state = 19;
}
}
if(state == 19)//-
{
aByte.attribute = OPERATION;
aByte.value = "-";
return aByte;
}
if(state == 20)//--
{
aByte.attribute = OPERATION;
aByte.value = "--";
goNextChar(pBuffer, pScr);
return aByte;
}
if(state == 21)//-=
{
aByte.attribute = OPERATION;
aByte.value = "-=";
goNextChar(pBuffer, pScr);
return aByte;
}
if(state == 63)//->
{
aByte.attribute = OPERATION;
aByte.value = "->";
goNextChar(pBuffer, pScr);
return aByte;
}
////////////////////////////////////////////////////////////
if(state == 22)
{
inputChar = getNextChar(pBuffer, pScr);
switch(inputChar)
{
case '=':
state = 24;
break;
case '/':
state = 70;
break;
case '*':
state = 71;
break;
default:
state = 23;
break;
}
}
if(state == 23)
{
aByte.attribute = OPERATION;
aByte.value = "/";
goNextChar(pBuffer, pScr);
return aByte;
}
if(state == 70)//单行注释
{
inputChar = getNextChar(pBuffer, pScr);
while(inputChar != '\n')
{
inputChar = getNextChar(pBuffer, pScr);
}
goNextChar(pBuffer, pScr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -