// Test_2.cpp : 定义控制台应用程序的入口点。
//
//#include "stdafx.h"
#include "map"
#include "string"
#include "fstream"
#include "iostream"
#include "cctype"
using namespace std;
class Translate
{
public:
int CreateMap(fstream &Keyfile);
int CreateMap();
int Analyze(fstream &srcfile);
private:
map<string, string> OperatorMap;
map<string, string> KeyMap;
};
int Translate::CreateMap()
{
//Create Operator Map
OperatorMap.insert(make_pair( "+" , "13" ));
OperatorMap.insert(make_pair( "-" , "14" ));
OperatorMap.insert(make_pair( "*" , "15" ));
OperatorMap.insert(make_pair( "/" , "16" ));
OperatorMap.insert(make_pair( ":" , "17" ));
OperatorMap.insert(make_pair( ":=" , "18" ));
OperatorMap.insert(make_pair( "<" , "20" ));
OperatorMap.insert(make_pair( "<>" , "21" ));
OperatorMap.insert(make_pair( "<=" , "22" ));
OperatorMap.insert(make_pair( ">" , "23" ));
OperatorMap.insert(make_pair( ">=" , "24" ));
OperatorMap.insert(make_pair( "=" , "25" ));
OperatorMap.insert(make_pair( ";" , "26" ));
OperatorMap.insert(make_pair( "(" , "27" ));
OperatorMap.insert(make_pair( ")" , "28" ));
OperatorMap.insert(make_pair( "#" , "0" ));
//Create KeyWord Map
KeyMap.insert(make_pair( "begin", "1" ));
KeyMap.insert(make_pair( "if" , "2" ));
KeyMap.insert(make_pair( "then" , "3" ));
KeyMap.insert(make_pair( "while", "4" ));
KeyMap.insert(make_pair( "do" , "5" ));
KeyMap.insert(make_pair( "end" , "6" ));
return 0;
}
int Translate::CreateMap(fstream &Keyfile)
{
string::iterator tempite;
string str1, str2;
while(Keyfile>>str1>>str2)
{
tempite=str1.begin();
if(isalpha(*tempite))
{
KeyMap.insert(make_pair(str1,str2));
}
else
{
OperatorMap.insert(make_pair(str1,str2));
}
}
return 0;
}
int Translate::Analyze(fstream &srcfile)
{
string tempstr="";
string tempoperator="";
char tempch;
bool wordover=false;
bool isanum=false;
bool isoperator=false;
fstream outfile;
outfile.open("out.txt",fstream::out);
while(srcfile.get(tempch))
{
if(!isspace(tempch))
{
if(!isalnum(tempch))
{
isoperator=true;
if(isanum)
{
wordover=true;
isoperator=false;
tempoperator.push_back(tempch);
}
else
{
tempoperator.push_back(tempch);
if(tempstr!="")
{
isanum=true;
isoperator=false;
wordover=true;
}
}
}
else
{
isanum=true;
if(isoperator)
{
wordover=true;
isanum=false;
tempstr.push_back(tempch);
}
else
{
tempstr.push_back(tempch);
if(tempoperator!="")
{
isoperator=true;
isanum=false;
wordover=true;
}
}
}
}
else
{
wordover=true;
}
if(wordover)
{
map<string,string>::iterator tempite;
if(isspace(tempch))
{
if(tempstr!="")
{
bool isnum=true;
string::iterator strite;
strite=tempstr.begin();
if(isdigit(*strite))//第一个字母为数字
{
while(strite!=tempstr.end())
{
if(!isdigit(*strite))
{
isnum=false;
break;
}
strite++;
}
if(isnum)
{
outfile<<"("<<tempstr<<","<<11<<")"<<endl;
}
else
{
outfile<<"("<<"非法字符"<<","<<-1<<")"<<endl;
}
}
else
{
tempite=KeyMap.find(tempstr);
if(tempite!=KeyMap.end())
{
outfile<<"("<<tempite->first<<","<<tempite->second<<")"<<endl;
}
else
{
outfile<<"("<<tempstr<<","<<10<<")"<<endl;
}
}
tempstr.clear();
isanum=false;
}
if(tempoperator!="")
{
tempite=OperatorMap.find(tempoperator);
if(tempite!=OperatorMap.end())
{
outfile<<"("<<tempite->first<<","<<tempite->second<<")"<<endl;
}
else
{
outfile<<"("<<"非法字符"<<","<<-1<<")"<<endl;
}
tempoperator.clear();
isoperator=false;
}
}
if(isanum)
{ bool isnum=true;
string::iterator strite;
strite=tempstr.begin();
if(isdigit(*strite))//第一个字母为数字
{
while(strite!=tempstr.end())
{
if(!isdigit(*strite))
{
isnum=false;
break;
}
strite++;
}
if(isnum)
{
outfile<<"("<<tempstr<<","<<11<<")"<<endl;
}
else
{
outfile<<"("<<"非法字符"<<","<<-1<<")"<<endl;
}
}
else
{
tempite=KeyMap.find(tempstr);
if(tempite!=KeyMap.end())
{
outfile<<"("<<tempite->first<<","<<tempite->second<<")"<<endl;
}
else
{
outfile<<"("<<tempstr<<","<<10<<")"<<endl;
}
}
tempstr.clear();
isanum=false;
}
if(isoperator)
{
tempite=OperatorMap.find(tempoperator);
if(tempite!=OperatorMap.end())
{
outfile<<"("<<tempite->first<<","<<tempite->second<<")"<<endl;
}
else
{
outfile<<"("<<"非法字符"<<","<<-1<<")"<<endl;
}
tempoperator.clear();
isoperator=false;
}
wordover=false;
}
}
outfile<<"("<<"#"<<","<<0<<")"<<endl;
outfile.close();
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
fstream srcfile;
if(srcfile.open("Test.txt",fstream::in),!srcfile)
{
cout<<"Open the file failed!"<<endl;
getchar();
exit(EXIT_FAILURE);
}
Translate test;
cout<<"Do you want to create Key map from file ? (y or n)"<<endl;
char ch;
cin>>ch;
if(ch=='y')
{
fstream keyfile;
if(keyfile.open("Keyfile.txt",fstream::in),!keyfile)
{
cout<<"Open the file failed!"<<endl;
getchar();
exit(EXIT_FAILURE);
}
test.CreateMap(keyfile);
keyfile.close();
cout<<"From Keyfile"<<endl;
}
else
{
test.CreateMap();
}
test.Analyze(srcfile);
srcfile.close();
cout<<"Translate Over!"<<endl;
getchar();
getchar();
return 0;
}