直接上代码 :
支持:左右结合性
// MyCompiler.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <fstream>
#include <list>
#include <map>
#include <string>
#include <iostream>
#include <vector>
//把Token转换为字符串
#define ATokenToString(l, r) if(l == r)return #r;
using namespace std;
namespace tokens{
enum TOKEN
{
INVALID_TYPE,
WHITE_SPACE,
NAME,
NUMBER,
END,
PLUS,
MINUS,
MUL,
DIV,
PRINT,
ASSIGN,
LP,
RP,
};
static string MyTokenToString(TOKEN token)
{
ATokenToString(token, WHITE_SPACE)
ATokenToString(token, NAME)
ATokenToString(token, NUMBER)
ATokenToString(token, END)
ATokenToString(token, PLUS)
ATokenToString(token, MINUS)
ATokenToString(token, MUL)
ATokenToString(token, DIV)
ATokenToString(token, PRINT)
ATokenToString(token, ASSIGN)
ATokenToString(token, LP)
ATokenToString(token, RP)
return "";
}
}
class tokenizer
{
int mTokenTypes[256];
string mCurrentStrToken;
tokens::TOKEN mCurrentTokenType;
istream& mIfStream;
public:
tokenizer(istream& infile):mIfStream(infile)
{
memset(mTokenTypes, 0, 256);
mTokenTypes['+'] = tokens::PLUS;
mTokenTypes['-'] = tokens::MINUS;
mTokenTypes['*'] = tokens::MUL;
mTokenTypes['/'] = tokens::DIV;
mTokenTypes[';'] = tokens::PRINT;
mTokenTypes['='] = tokens::ASSIGN;
mTokenTypes['('] = tokens::LP;
mTokenTypes[')'] = tokens::RP;
mTokenTypes['\t'] = tokens::WHITE_SPACE;
mTokenTypes[' '] = tokens::WHITE_SPACE;
char ch = 0;
for (ch = 'a'; ch <= 'z'; ch ++)
{
mTokenTypes[ch] = tokens::NAME;
}
for(ch = 'A'; ch <= 'Z'; ch ++)
{
mTokenTypes[ch] = tokens::NAME;
}
for(ch = '0'; ch <= '9'; ch ++)
{
mTokenTypes[ch] = tokens::NUMBER;
}
mTokenTypes['.'] = tokens::NUMBER;
}
bool hasNext()
{
char temptoken = 0;
if(mIfStream.get(temptoken))
{
mIfStream.putback(temptoken);
return true;
}
return false;
}
const string getCurrent()
{
return mCurrentStrToken;
}
tokens::TOKEN getTokenType()
{
return mCurrentTokenType;
}
const string getNext()
{
string strValue;
char ch = 0;
mIfStream>>ch;
mCurrentTokenType = (tokens::TOKEN)mTokenTypes[ch];