#include <iostream>
#include <string>
#include <cctype>
#include <map>
#include <vector>
#include <sstream>

using namespace std;

static std::map<std::string, int> keyMap;

class Parser
{
public:
	Parser();
	~Parser();

public:
	// 是不是 数字
	static bool isDigit(char c){
		return std::isdigit(c);
	}
	// 是不是字母
	static bool isAlpha(char c){
		return std::isalpha(c);
	}
	// 是不是关键字
	static bool isKeyWords(std::string word){
		return keyMap.count(word);
	}
private:
};


Parser::~Parser()
{
}



void initMap(){
	vector<string> vecKey;
	vecKey.push_back("False");
	vecKey.push_back("def");
	vecKey.push_back("if");
	vecKey.push_back("raise");
	vecKey.push_back("None");
	vecKey.push_back("del");
	vecKey.push_back("import");
	vecKey.push_back("return");
	vecKey.push_back("True");
	vecKey.push_back("elif");
	vecKey.push_back("int");
	vecKey.push_back("try");
	vecKey.push_back("and");
	vecKey.push_back("else");
	vecKey.push_back("is");
	vecKey.push_back("while");
	vecKey.push_back("as");
	vecKey.push_back("expect");
	vecKey.push_back("lambda");
	vecKey.push_back("with");
	vecKey.push_back("assert");
	vecKey.push_back("finally");
	vecKey.push_back("yield");
	vecKey.push_back("break");
	vecKey.push_back("for");
	vecKey.push_back("not");
	vecKey.push_back("class");
	vecKey.push_back("from");
	vecKey.push_back("or");
	vecKey.push_back("continue");
	vecKey.push_back("global");
	vecKey.push_back("pass");
	vecKey.push_back("ID");
	vecKey.push_back("NUM");



	for (auto it = vecKey.begin(); it != vecKey.end(); ++it){
		keyMap.insert(make_pair(*it, std::distance(vecKey.begin(), it) + 1));
	}

	std::vector<string> vecOp;
	vecOp.push_back("+");
	vecOp.push_back("-");
	vecOp.push_back("*");
	vecOp.push_back("/");
	vecOp.push_back("%");
	vecOp.push_back("//");
	vecOp.push_back("+=");
	vecOp.push_back("-=");
	vecOp.push_back("*=");
	vecOp.push_back("/=");
	vecOp.push_back(">");
	vecOp.push_back("<");
	vecOp.push_back(">=");
	vecOp.push_back("<=");
	vecOp.push_back("*=");
	vecOp.push_back("/=");
	vecOp.push_back(":");
	vecOp.push_back("(");
	vecOp.push_back("{");
	vecOp.push_back("}");

	for (auto it = vecOp.begin(); it != vecOp.end(); ++it){
		keyMap.insert(make_pair(*it, std::distance(vecOp.begin(), it) + 1 + vecKey.size()));
	}


	for (auto it = keyMap.begin(); it != keyMap.end(); ++it){
		cout << it->first << ":" << it->second << endl;
	}
}

// 格式化输出
string format_pair(initializer_list<string> ele){
	stringstream ss;
	ss << "(";
	for (auto & e : ele){
		ss << e << ",";
	}
	ss << ")";
	string res;
	ss >> res;
	return res;
}

int Scan(string str, int num){
	auto c = str[num];
	while (c == ' ' || c == '\t')
	{
		num++;
		if (num >= str.size())
			return num;
		c = str[num];
	}
	string token = "";
	if (Parser::isAlpha(c)){
		token += c;
		num++;
		while (Parser::isAlpha(str[num]) ||Parser::isDigit(str[num]))
		{
			token += str[num];
			num++;
		}
		// 关键字

		if (Parser::isKeyWords(token)){
			cout << format_pair({ token, to_string(keyMap[token]) }) << endl;
		}
		else{
			cout << format_pair({ "ID", to_string(keyMap[string("ID")]) });
			return num;
		}
	}
	else if (Parser::isDigit(c)){
		token += c;
		num++;
		while (Parser::isDigit(str[num]))
		{
			token += str[num];
			num++;
		}
		// 连续数字
		format_pair({ to_string(keyMap[string("NUM")]), token });

	}
	else if (c == '<'){
		num++;
		if (str[num] == '='){
			format_pair({ to_string(keyMap[string("<=")]), token });
			return ++num;
		}
		else{
			format_pair({ to_string(keyMap[string("<")]), token });
			return num;
		}

	}
	else if (c == '>'){
		num++;
		if (str[num] == '='){
			format_pair({ to_string(keyMap[string(">=")]), token });
			return ++num;
		}
		else{
			format_pair({ to_string(keyMap[string(">")]), token });
			return num;
		}
	}
	else if (c == ':'){
		num++;
		format_pair({ to_string(keyMap[string("c")]), token });
		return num;
	}
	else if (c == '+' || c == '-' || c == '*' || c == '/' c=='%' ){
		token += c;
		format_pair({ to_string(keyMap[string(token)]), token });
		return ++num;
	}
	else{
		cout << "compile error:" << num << endl;
	}
	return 0;
}






int main(int argc, char** argv){
	initMap();

	// 词法分析


	__asm{
		jmp $;
	}
	return 0;
}
上一篇 下一篇