1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
|
#ifndef PARSING_H
#define PARSING_H
#include <string>
#include <sstream>
#include <map>
#include <vector>
#include <stack>
namespace anl
{
class Token
{
public:
enum ETokenTypes
{
COMMA,
CLOSEPARENS,
OPENPARENS,
NUMBER,
FUNCTION,
OPERATOR,
UNARYOPERATOR,
VAR,
INVALID,
NONE,
};
Token();
Token(ETokenTypes t, const std::string token);
Token(const Token &rhs);
virtual ~Token();
ETokenTypes GetType() const;
const std::string &GetToken() const;
protected:
ETokenTypes type_;
std::string token_;
};
class Tokenizer
{
public:
Tokenizer(const std::string expr, const std::map<std::string, int> &fmap, const std::vector<std::string> &vars);
bool HasNext();
Token NextToken();
protected:
std::string expression_;
std::string::iterator pos_;
Token lastToken_;
std::map<std::string, int> functions_;
std::vector<std::string> vars_;
bool IsValidOperator(char ch);
bool IsDigit(char ch);
bool IsAlphabetic(char ch);
Token ParseNumberToken(char ch);
Token ParseComma(char ch);
Token ParseParentheses(bool which);
Token ParseOperator(char ch);
Token ParseFunctionOrVariable(char ch);
bool IsNumeric(char ch, bool lastCharE);
bool IsSpecialToken(const std::string &t);
bool IsFunctionName(const std::string &t);
};
class ExpressionToPostfix
{
public:
ExpressionToPostfix(const std::string &expr, const std::map<std::string, int> &f, const std::vector<std::string> &v);
std::vector<Token> ToPostfix();
protected:
std::string expr_;
const std::map<std::string, int> &f_;
const std::vector<std::string> &vars_;
int GetNumOperands(const Token &tk);
bool IsLeftAssociative(const Token &tk);
int GetPrecedence(const Token &tk);
};
};
#endif
|