diff options
| author | Adrian Kummerländer | 2012-03-04 20:50:05 +0100 | 
|---|---|---|
| committer | Adrian Kummerländer | 2012-03-04 20:50:05 +0100 | 
| commit | 1482e7ede3133b8bf78c68c2e0618b1de90d328c (patch) | |
| tree | f3d48b2cba29ad8a23d5ac2de0026965d6bfb0a6 | |
| parent | c0933381497401a9439446d9470dd617b08b88ed (diff) | |
| download | SimpleParser-1482e7ede3133b8bf78c68c2e0618b1de90d328c.tar SimpleParser-1482e7ede3133b8bf78c68c2e0618b1de90d328c.tar.gz SimpleParser-1482e7ede3133b8bf78c68c2e0618b1de90d328c.tar.bz2 SimpleParser-1482e7ede3133b8bf78c68c2e0618b1de90d328c.tar.lz SimpleParser-1482e7ede3133b8bf78c68c2e0618b1de90d328c.tar.xz SimpleParser-1482e7ede3133b8bf78c68c2e0618b1de90d328c.tar.zst SimpleParser-1482e7ede3133b8bf78c68c2e0618b1de90d328c.zip | |
Got rid of boost dependency
| -rw-r--r-- | main.cpp | 3 | ||||
| -rw-r--r-- | parser.cpp | 96 | ||||
| -rw-r--r-- | parser.h | 5 | ||||
| -rw-r--r-- | tree.cpp | 6 | ||||
| -rw-r--r-- | tree.h | 3 | 
5 files changed, 58 insertions, 55 deletions
| @@ -1,5 +1,4 @@  #include <iostream> -  #include "parser.h"  int main(int argc, char *argv[]) @@ -16,7 +15,7 @@ int main(int argc, char *argv[])  	catch ( exception &e )  	{  		std::cerr << e.what() << std::endl; -		std::exit(1); +		exit(1);  	}  	return 0; @@ -16,7 +16,7 @@ int Parser::getPriority(char tmp)  		case '(':  			return 90;  		case ')': -			return 90;	 +			return 90;  		case ',':  			return -1;  		default: @@ -30,14 +30,14 @@ vector<string>* Parser::lexer(string term)  	int last_priority = 0;  	int level = 0;  	string tmp, tmp_num; -	 +  	vector<string> *output = new vector<string>(); -	 +  	string::iterator termIter; -	 +  	for ( termIter = term.begin(); termIter != term.end(); termIter++ ) {  		priority = this->getPriority( *termIter ); -		 +  		if ( priority == -1 || ( termIter == term.begin() && priority == 10 ) ) {  			if ( level > 0 ) {  				tmp += *termIter; @@ -51,19 +51,19 @@ vector<string>* Parser::lexer(string term)  				output->push_back( tmp_num );  				tmp_num = "";  			} -			 +  			switch ( *termIter ) {  				case '(': {  					if ( level > 0 ) {  						tmp += *termIter;  					} -					 +  					level++;  					break;  				}  				case ')': {  					level--; -					 +  					if ( level == 0 ) {  						output->push_back( tmp );  						tmp = ""; @@ -73,9 +73,12 @@ vector<string>* Parser::lexer(string term)  					}  					break;  				} -				default: {				 +				default: {  					if ( level == 0 ) { -						output->push_back( boost::lexical_cast<string>(*termIter) ); +						string helper; +						helper = *termIter; + +						output->push_back( helper );  					}  					else {  						tmp += *termIter; @@ -84,21 +87,21 @@ vector<string>* Parser::lexer(string term)  				}  			}  		} -		 -		last_priority = priority;	 + +		last_priority = priority;  	} -	 +  	if ( last_priority == -1 ) {  		output->push_back( tmp_num );  	}  	else if ( last_priority != 90 ) {  		throw operator_exception();  	} -	 +  	if (level != 0) {  		throw parenthese_exception();  	} -	 +  	return output;  } @@ -106,72 +109,77 @@ Node* Parser::buildTree(Tree **tree, string term)  {  	stack<Node*> *operandStack = new stack<Node*>();  	stack<Node*> *operatorStack = new stack<Node*>(); -	 +  	int priority; -	Node *newNode; -	 +  	vector<string> *tmpLexer; -	 +  	vector<string> *lexerOutput = lexer(term); -	 +  	for ( vector<string>::iterator termIter = lexerOutput->begin(); termIter != lexerOutput->end(); termIter++ ) {  		priority = this->getPriority( (*termIter)[0] ); -		 +  		if ( priority != -1 && (*termIter).size() == 1 ) {  			if ( !operatorStack->empty() ) {  				OperatorNode *lastNode = static_cast<OperatorNode*>( operatorStack->top() ); -			 +  				if ( this->getPriority( lastNode->function ) < priority ) { -					newNode = (*tree)->addOperator( NULL, (*termIter)[0] ); -					operatorStack->push( newNode ); +					operatorStack->push(  +						(*tree)->addOperator( NULL, (*termIter)[0] ) +					);  				}  				else {  					Node *currOperator = operatorStack->top();  					operatorStack->pop(); -					 +  					currOperator->rightChild = operandStack->top();  					operandStack->pop(); -					 +  					currOperator->leftChild = operandStack->top();  					operandStack->pop(); -					 +  					operandStack->push( currOperator ); -					 +  					termIter--;  				}  			}  			else { -				newNode = (*tree)->addOperator( NULL, (*termIter)[0] ); -				operatorStack->push( newNode ); +				operatorStack->push(  +					(*tree)->addOperator( NULL, (*termIter)[0] ) +				);  			}  		}  		else {  			tmpLexer = lexer( *termIter ); -			 +  			if ( tmpLexer->size() == 1 ) { -				newNode = (*tree)->addOperand( NULL, boost::lexical_cast<double>(*termIter) ); -				operandStack->push( newNode ); +				operandStack->push(  +					(*tree)->addOperand( NULL,  +						strtod( termIter->c_str(), NULL )  +					) +				);  			}  			else if ( tmpLexer->size() > 1 ) { -				newNode = buildTree(tree, *termIter); -				operandStack->push( newNode ); +				operandStack->push(  +					buildTree(tree, *termIter)  +				);  			}  		}  	} -	 +  	while ( !operatorStack->empty() ) {  		OperatorNode *currOperator = static_cast<OperatorNode*>( operatorStack->top() );  		operatorStack->pop(); -					 +  		currOperator->rightChild = operandStack->top();  		operandStack->pop(); -					 +  		currOperator->leftChild = operandStack->top();  		operandStack->pop(); -				 +  		operandStack->push( currOperator );  	} -	 +  	return operandStack->top();  } @@ -179,14 +187,14 @@ ParserResult Parser::calculate(string term, bool getTreeAsDot)  {  	Tree *termTree = new Tree();  	termTree->root = buildTree(&termTree, term); -	 +  	ParserResult returnVal; -	 +  	returnVal.result = termTree->root->solve(); -	 +  	if ( getTreeAsDot ) {  		returnVal.tree = termTree->print(term);  	} -	 +  	return returnVal;  } @@ -1,9 +1,6 @@ +#include <stdlib.h>  #include <stack> -#include <string> -#include <sstream>  #include <exception> -#include <boost/lexical_cast.hpp> -  #include "tree.h"  struct ParserResult  @@ -1,8 +1,6 @@  #include "tree.h"  #include <math.h> -#include <boost/lexical_cast.hpp> -  Node::Node()  { @@ -111,12 +109,12 @@ string Tree::print(string term)      	switch ( (*it)->type ) {  			case OPERAND_NODE: {  				OperandNode *tmp = static_cast<OperandNode*>( *it ); -				out << "node" << i << " [ label = \"" << boost::lexical_cast<string>(tmp->value) << "\"];" << endl; +				out << "node" << i << " [ label = \"" << tmp->value << "\"];" << endl;  				break;  			}  			case OPERATOR_NODE: {  				OperatorNode *tmp = static_cast<OperatorNode*>( *it ); -				out << "node" << i << " [ label = \"" << boost::lexical_cast<string>(tmp->function) << "\"];" << endl; +				out << "node" << i << " [ label = \"" << tmp->function << "\"];" << endl;  				for ( vector<Node*>::iterator iter = this->nodeCollection->begin(); iter != this->nodeCollection->end(); ++iter ) {  					if ( *iter == (*it)->leftChild ) { @@ -1,6 +1,7 @@  #include <vector>  #include <string> -#include <stdlib.h> +#include <sstream> +#include <math.h>  using namespace std; | 
