add type string | change output format

This commit is contained in:
Valk Richard Li 2019-07-30 20:56:07 +08:00 committed by GitHub
parent 3c95e93dfd
commit 6f76331ae0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 67 additions and 33 deletions

View File

@ -2,13 +2,14 @@
#include <fstream> #include <fstream>
#include <cstring> #include <cstring>
#define SELFDEFINE -1 //自定义标识符 #define IDENTIFIER -1 //自定义标识符
#define IDENTIFIER -2 //界符 or 运算符 #define OPERATOR -2 //界符 or 运算符
#define NUMBER -3 //数字 #define NUMBER -3 //数字
#define RESERVEWORD -4//关键字 #define RESERVEWORD -4 //关键字
#define FAIL -5 //失败 #define STRING -5 //字符串类型
#define SCANEND -6 //扫描完成 #define FAIL -6 //失败
#define ERRORFOUND -7 //异常错误 #define SCANEND -7 //扫描完成
#define ERRORFOUND -8 //异常错误
// \n 换行 // \n 换行
// \t tab // \t tab
@ -16,13 +17,13 @@
// \\ 反斜线 // \\ 反斜线
// \' 单引号 // \' 单引号
// \" 双引号 // \" 双引号
std::string ReserveWord[27]= std::string ReserveWord[26]=
{ {
"for","foreach","forindex","while", "for","foreach","forindex","while",
"var","func","break","continue","return", "var","func","break","continue","return",
"if","else","elsif","nil","and","or", "if","else","elsif","nil","and","or",
"print","cmp","append","setsize","subvec","pop", "print","cmp","append","setsize","subvec","pop",
"sort","contains","delete","keys","typeof","id" "sort","contains","delete","keys","typeof"
}; };
std::string OperatorOrDelimiter[40]= std::string OperatorOrDelimiter[40]=
@ -35,11 +36,11 @@ std::string OperatorOrDelimiter[40]=
}; };
std::string IdentifierTable[1000]={""}; std::string IdentifierTable[1000]={""};
char ResourcePrograme[16384]; char ResourcePrograme[16777216];
int isReserveWord(std::string &p) int isReserveWord(std::string &p)
{ {
for(int i=0;i<27;++i) for(int i=0;i<26;++i)
if(ReserveWord[i]==p) if(ReserveWord[i]==p)
return i+1; return i+1;
return FAIL; return FAIL;
@ -55,12 +56,12 @@ int isOperatorOrDelimiter(std::string &p)
bool isLetter(char t) bool isLetter(char t)
{ {
return ('a'<=t && t<='z' || 'A'<=t && t<='Z'); return (('a'<=t) && (t<='z') || ('A'<=t) && (t<='Z'));
} }
bool isNumber(char t) bool isNumber(char t)
{ {
return ('0'<=t && t<='9'); return (('0'<=t) && (t<='9'));
} }
void InputFile(std::string &FileName) void InputFile(std::string &FileName)
@ -122,7 +123,7 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
} }
Syn=isReserveWord(token); Syn=isReserveWord(token);
if(Syn==FAIL) if(Syn==FAIL)
Syn=SELFDEFINE; Syn=IDENTIFIER;
else else
Syn=RESERVEWORD; Syn=RESERVEWORD;
} }
@ -145,17 +146,17 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
Syn=NUMBER; Syn=NUMBER;
} }
else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' || else if(temp=='(' || temp==')' || temp=='[' || temp==']' || temp=='{' ||
temp=='}' || temp==',' || temp==';' || temp=='\"' || temp==':' || temp=='}' || temp==',' || temp==';' || temp=='|' || temp==':' ||
temp=='?' || temp=='.' || temp=='`' || temp=='\'' || temp=='&' || temp=='?' || temp=='.' || temp=='`' || temp=='\'' || temp=='&'||
temp=='|') temp=='%' || temp=='$' || temp=='^')
{ {
token+=temp; token+=temp;
++ptr; ++ptr;
Syn=IDENTIFIER; Syn=OPERATOR;
} }
else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~') else if(temp=='=' || temp=='+' || temp=='-' || temp=='*' || temp=='!' || temp=='/' || temp=='<' || temp=='>' || temp=='~')
{ {
Syn=IDENTIFIER; Syn=OPERATOR;
token+=temp; token+=temp;
++ptr; ++ptr;
temp=Source[ptr]; temp=Source[ptr];
@ -167,13 +168,7 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
} }
else if(temp=='\\') else if(temp=='\\')
{ {
/* Syn=OPERATOR;
"+","-","*","/","=","+=","-=","*=","/=",
"\n","\t","\r","\\","\'","\"",".",
"<","<=",">",">=","==","!=","!","~",
",",";","(",")","[","]","{","}","#"
*/
Syn=IDENTIFIER;
token+=temp; token+=temp;
++ptr; ++ptr;
temp=Source[ptr]; temp=Source[ptr];
@ -183,6 +178,43 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
++ptr; ++ptr;
} }
} }
else if(temp=='\"')
{
Syn=STRING;
token+=temp;
++ptr;
temp=Source[ptr];
while(temp!='\"')
{
if(temp=='\\')
{
token+=temp;
++ptr;
temp=Source[ptr];
token+=temp;
++ptr;
temp=Source[ptr];
}
else
{
token+=temp;
++ptr;
temp=Source[ptr];
}
if(temp=='@' || temp=='\n')
break;
}
//add the last char \"
if(temp=='\"')
{
token+=temp;
++ptr;
}
else
token+=" __missing_end_of_string";
}
else if(temp=='@') else if(temp=='@')
{ {
Syn=SCANEND; Syn=SCANEND;
@ -192,10 +224,10 @@ void Scanner(int &Syn,const char Source[],std::string &token,int &ptr)
{ {
Syn=FAIL; Syn=FAIL;
std::cout<<"[Error] Unexpected error occurred: "<<temp<<std::endl; std::cout<<"[Error] Unexpected error occurred: "<<temp<<std::endl;
system("pause");
++ptr; ++ptr;
return; return;
} }
if(token=="") if(token=="")
{ {
Syn=ERRORFOUND; Syn=ERRORFOUND;
@ -241,14 +273,16 @@ int main()
while(Syn!=SCANEND && Syn!=ERRORFOUND) while(Syn!=SCANEND && Syn!=ERRORFOUND)
{ {
Scanner(Syn,ResourcePrograme,token,Ptr); Scanner(Syn,ResourcePrograme,token,Ptr);
if(Syn==IDENTIFIER) if(Syn==OPERATOR)
std::cout<<"( "<<token<<" , ---- )"<<std::endl; std::cout<<"( Operator | "<<token<<" )"<<std::endl;
else if(Syn==SELFDEFINE) else if(Syn==IDENTIFIER)
std::cout<<"( 标识符 , "<<token<<" )"<<std::endl; std::cout<<"( Identifier | "<<token<<" )"<<std::endl;
else if(Syn==NUMBER) else if(Syn==NUMBER)
std::cout<<"( Number , "<<token<<" )"<<std::endl; std::cout<<"( Number | "<<token<<" )"<<std::endl;
else if(Syn==RESERVEWORD) else if(Syn==RESERVEWORD)
std::cout<<"( Reserve Word , "<<token<<" )"<<std::endl; std::cout<<"( ReserveWord | "<<token<<" )"<<std::endl;
else if(Syn==STRING)
std::cout<<"( String | "<<token<<" )"<<std::endl;
} }
std::cout<<">> Complete scanning \""<<FileNameOrCommand<<"\"."<<std::endl; std::cout<<">> Complete scanning \""<<FileNameOrCommand<<"\"."<<std::endl;
//fout.close(); //fout.close();