aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerländer2013-10-19 20:11:56 +0200
committerAdrian Kummerländer2013-10-19 20:11:56 +0200
commit9a95b5d24b2b2f5111e1862875d4136964a59548 (patch)
tree9ac96fdf846ae19e2278f9540593823fa8ba3b21
parentaca18e1803b3d54e6c9d7444e8b9c1bf09d12f52 (diff)
downloadSimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.gz
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.bz2
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.lz
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.xz
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.tar.zst
SimpleParser-9a95b5d24b2b2f5111e1862875d4136964a59548.zip
Improvement: Replaced "priority" with TokenType
* Made implementation more expressive by replacing the integer priority with an strictly typed enum called TokenType ** Made removal of character comparisons from tree construction and lexer possible * As a side effect distinct numbers had to be assigned to each token ** Operators of same priority do not have identical numbers anymore
-rw-r--r--src/tree.cc10
-rw-r--r--src/utils.cc53
-rw-r--r--src/utils.h13
3 files changed, 44 insertions, 32 deletions
diff --git a/src/tree.cc b/src/tree.cc
index 1dcfca6..0f5c2d8 100644
--- a/src/tree.cc
+++ b/src/tree.cc
@@ -111,21 +111,19 @@ Node* Tree::buildTree(std::string term) {
std::vector<std::string> tmpLexer;
std::vector<std::string> lexerOutput = lexer(term);
- int8_t priority;
-
for ( auto termIter = lexerOutput.begin();
termIter != lexerOutput.end();
termIter++ ) {
- std::string& currTerm = (*termIter);
- priority = getPriority(currTerm[0]);
+ const std::string& currTerm = (*termIter);
+ const TokenType token = getTokenType(currTerm[0]);
- if ( priority != -1 && (*termIter).size() == 1 ) {
+ if ( token != TokenType::VALUE_NUMBER && (*termIter).size() == 1 ) {
if ( !operatorStack.empty() ) {
OperatorNode* lastNode(
static_cast<OperatorNode*>(topNodeFrom(operatorStack))
);
- if ( getPriority(lastNode->getFunction()) < priority ) {
+ if ( token > getTokenType(lastNode->getFunction()) ) {
operatorStack.push(
this->addOperator(nullptr, currTerm[0])
);
diff --git a/src/utils.cc b/src/utils.cc
index 7457ae2..112bc20 100644
--- a/src/utils.cc
+++ b/src/utils.cc
@@ -5,26 +5,26 @@
namespace SimpleParser {
-int8_t getPriority(char tmp) {
+TokenType getTokenType(char tmp) {
switch ( tmp ) {
case '-':
- return 10;
+ return TokenType::OPERATOR_PLUS;
case '+':
- return 10;
+ return TokenType::OPERATOR_MINUS;
case '/':
- return 20;
+ return TokenType::OPERATOR_DIVIDE;
case '*':
- return 20;
+ return TokenType::OPERATOR_MULTIPLY;
case '^':
- return 30;
+ return TokenType::OPERATOR_POWER;
case '(':
- return 90;
+ return TokenType::PARENTHESES_OPEN;
case ')':
- return 90;
+ return TokenType::PARENTHESES_CLOSE;
case ',':
- return -1;
+ return TokenType::VALUE_NUMBER;
default:
- return -1;
+ return TokenType::VALUE_NUMBER;
}
}
@@ -32,31 +32,33 @@ std::vector<std::string> lexer(std::string term) {
std::string tmp;
std::string tmpNum;
std::vector<std::string> output;
+ TokenType token;
+ TokenType lastToken;
- int8_t priority = 0;
- int8_t lastPriority = 0;
- uint32_t level = 0;
+ uint32_t level = 0;
for ( auto termIter = term.begin();
termIter != term.end();
termIter++ ) {
- priority = getPriority(*termIter);
+ token = getTokenType(*termIter);
- if ( priority == -1 || ( termIter == term.begin() &&
- priority == 10 ) ) {
+ if ( token == TokenType::VALUE_NUMBER ||
+ ( token == TokenType::OPERATOR_MINUS &&
+ termIter == term.begin() ) ) {
if ( level > 0 ) {
tmp += *termIter;
} else {
tmpNum += *termIter;
}
} else {
- if ( lastPriority == -1 && level == 0 ) {
+ if ( lastToken == TokenType::VALUE_NUMBER &&
+ level == 0 ) {
output.push_back(tmpNum);
tmpNum.clear();
}
- switch ( *termIter ) {
- case '(': {
+ switch ( token ) {
+ case TokenType::PARENTHESES_OPEN: {
if ( level > 0 ) {
tmp += *termIter;
}
@@ -65,7 +67,7 @@ std::vector<std::string> lexer(std::string term) {
break;
}
- case ')': {
+ case TokenType::PARENTHESES_CLOSE: {
level--;
if ( level == 0 ) {
@@ -92,20 +94,21 @@ std::vector<std::string> lexer(std::string term) {
}
}
- lastPriority = priority;
+ lastToken = token;
}
- if ( lastPriority == -1 ) {
+ if ( lastToken == TokenType::VALUE_NUMBER ) {
output.push_back(tmpNum);
- } else if ( lastPriority != 90 ) {
+ } else if ( lastToken != TokenType::PARENTHESES_CLOSE ) {
throw operator_exception();
}
- if (level != 0) {
+ if ( level != 0 ) {
throw parenthese_exception();
}
- if ( lastPriority == 90 && output.size() == 1 ) {
+ if ( lastToken == TokenType::PARENTHESES_CLOSE &&
+ output.size() == 1 ) {
output = lexer(output[0]);
}
diff --git a/src/utils.h b/src/utils.h
index f71c835..6e43605 100644
--- a/src/utils.h
+++ b/src/utils.h
@@ -8,7 +8,18 @@
namespace SimpleParser {
-int8_t getPriority(char);
+enum class TokenType : int8_t {
+ OPERATOR_PLUS = 10,
+ OPERATOR_MINUS = 11,
+ OPERATOR_DIVIDE = 20,
+ OPERATOR_MULTIPLY = 21,
+ OPERATOR_POWER = 30,
+ PARENTHESES_OPEN = 90,
+ PARENTHESES_CLOSE = 91,
+ VALUE_NUMBER = -1,
+};
+
+TokenType getTokenType(char);
std::vector<std::string> lexer(std::string);
}