From 8554ffd7fb674195f9338e54fc50998e3344a52f Mon Sep 17 00:00:00 2001 From: Valk Richard Li <48872266+ValKmjolnir@users.noreply.github.com> Date: Mon, 29 Jul 2019 21:46:55 +0800 Subject: [PATCH] bugs fixed --- lexical_analysis.cpp | 156 ++++++++++++++++++++++++++++--------------- 1 file changed, 104 insertions(+), 52 deletions(-) diff --git a/lexical_analysis.cpp b/lexical_analysis.cpp index b84836f..7cfbcf4 100644 --- a/lexical_analysis.cpp +++ b/lexical_analysis.cpp @@ -2,34 +2,36 @@ #include #include -#define IDENTR -1 //界符 or 运算符 -#define NUMBER -2 //数字 -#define RESERV -3 //关键字 -#define FAIL -4 -#define SCANEND -5 -#define ERROROCC -6 -#define SELFD -7 //自定义标识符 +#define SELFDEFINE -1 //自定义标识符 +#define IDENTIFIER -2 //界符 or 运算符 +#define NUMBER -3 //数字 +#define RESERVEWORD -4//关键字 +#define FAIL -5 //失败 +#define SCANEND -6 //扫描完成 +#define ERRORFOUND -7 //异常错误 + // \n 换行 // \t tab // \r 回车 // \\ 反斜线 // \' 单引号 // \" 双引号 -std::string ReserveWord[25]= +std::string ReserveWord[27]= { "for","foreach","forindex","while", "var","func","break","continue","return", "if","else","elsif","nil","and","or", "print","cmp","append","setsize","subvec","pop", - "sort","contains","delete","keys" + "sort","contains","delete","keys","typeof","id" }; -std::string OperatorOrDelimiter[33]= +std::string OperatorOrDelimiter[40]= { "+","-","*","/","=","+=","-=","*=","/=", "\n","\t","\r","\\","\'","\"",".", - "<","<=",">",">=","==","!=","!","~", - ",",";","(",")","[","]","{","}","#" + "<","<=",">",">=","==","!=","~=","!","~", + ",",";","(",")","[","]","{","}","#","?",":", + "&","|","%","^" }; std::string IdentifierTable[1000]={""}; @@ -37,7 +39,7 @@ char ResourcePrograme[16384]; int isReserveWord(std::string &p) { - for(int i=0;i<25;++i) + for(int i=0;i<27;++i) if(ReserveWord[i]==p) return i+1; return FAIL; @@ -45,7 +47,7 @@ int isReserveWord(std::string &p) int isOperatorOrDelimiter(std::string &p) { - for(int i=0;i<33;++i) + for(int i=0;i<40;++i) if(OperatorOrDelimiter[i]==p) return i+1; return FAIL; @@ -61,9 +63,16 @@ bool isNumber(char t) return ('0'<=t && t<='9'); } -void InputFile(const char *FileName) +void InputFile(std::string &FileName) { std::ifstream fin(FileName); + if(fin.fail()) + { + std::cout<<"[Error] Failed to load file: "<127) { ++ptr; temp=Source[ptr]; } token=""; - if(isLetter(temp)) + if(isLetter(temp) || temp=='_') { token+=temp; ++ptr; temp=Source[ptr]; - while(isLetter(temp) || isNumber(temp) || temp==':' || temp=='?' || temp=='`' || temp=='.') + while(isLetter(temp) || isNumber(temp) || temp=='_') { token+=temp; ++ptr; @@ -113,40 +122,50 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr) } Syn=isReserveWord(token); if(Syn==FAIL) - Syn=SELFD; + Syn=SELFDEFINE; else - Syn=RESERV; + Syn=RESERVEWORD; } else if(isNumber(temp)) { int PointCnt=0; - token+=temp; - ++ptr; - temp=Source[ptr]; while(isNumber(temp)) { token+=temp; ++ptr; temp=Source[ptr]; - if(temp=='.') + if(temp=='.' && !PointCnt) + { ++PointCnt; - else if(temp!='.' && !isNumber(temp)) - break; - if(PointCnt>1) - break; + token+=temp; + ++ptr; + temp=Source[ptr]; + } } Syn=NUMBER; } - else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' || temp=='}' || temp=='~' || temp==',' || temp==';' || temp=='\"') + else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' || + temp=='}' || temp==',' || temp==';' || temp=='\"' || temp==':' || + temp=='?' || temp=='.' || temp=='`' || temp=='\'' || temp=='&' || + temp=='|') { token+=temp; ++ptr; - Syn=IDENTR; + Syn=IDENTIFIER; } - else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || - temp=='/' || temp=='=' || temp=='\\' || temp=='.' || temp=='<' || - temp=='>' - ) + else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~') + { + Syn=IDENTIFIER; + token+=temp; + ++ptr; + temp=Source[ptr]; + if(temp=='=') + { + token+=temp; + ++ptr; + } + } + else if(temp=='\\') { /* "+","-","*","/","=","+=","-=","*=","/=", @@ -154,7 +173,7 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr) "<","<=",">",">=","==","!=","!","~", ",",";","(",")","[","]","{","}","#" */ - Syn=IDENTR; + Syn=IDENTIFIER; token+=temp; ++ptr; temp=Source[ptr]; @@ -171,35 +190,68 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr) } else { - Syn=SCANEND; + Syn=FAIL; + std::cout<<"[Error] Unexpected error occurred: "<> exit: exit the programe."<> clear: clean the screen."<> help: find help."<> input the file name to scan."<> "; + std::cin>>FileNameOrCommand; + + if(FileNameOrCommand=="exit") + break; + else if(FileNameOrCommand=="clear") + { + system("cls"); + continue; + } + else if(FileNameOrCommand=="help") + { + help(); + continue; + } + //std::ofstream fout("Data.txt"); + InputFile(FileNameOrCommand); + while(Syn!=SCANEND && Syn!=ERRORFOUND) + { + Scanner(Syn,ResourcePrograme,token,Ptr); + if(Syn==IDENTIFIER) + std::cout<<"( "<> Complete scanning \""<