diff --git a/include/NodeTree.h b/include/NodeTree.h index d24627d..edeec53 100644 --- a/include/NodeTree.h +++ b/include/NodeTree.h @@ -6,15 +6,19 @@ #endif #include +#include #include #include #include +//Circular references +class Symbol; + class NodeTree { public: NodeTree(); - NodeTree(std::string name); + NodeTree(std::string name, Symbol* inSymbol); ~NodeTree(); void setParent(NodeTree* parent); @@ -26,10 +30,13 @@ class NodeTree { void removeChild(int index); NodeTree* get(int index); - std::string getName(); + std::string getName(); void setName(std::string); + Symbol* getSymbol(); + void setSymbol(Symbol* symbol); + int size(); std::string DOTGraphString(); @@ -37,6 +44,7 @@ class NodeTree { std::string DOTGraphStringHelper(); std::string getDOTName(); std::string name; + Symbol* symbol; NodeTree* parent; std::vector children; diff --git a/include/Symbol.h b/include/Symbol.h index 0971e71..5831f6e 100644 --- a/include/Symbol.h +++ b/include/Symbol.h @@ -10,12 +10,16 @@ #include #include +//Circular references +class NodeTree; + class Symbol { public: Symbol(std::string name, bool isTerminal); Symbol(std::string name, bool isTerminal, NodeTree* tree); ~Symbol(); bool const operator==(const Symbol &other); + std::string getName(); std::string toString(); Symbol* clone(); void setSubTree(NodeTree* tree); @@ -23,6 +27,7 @@ class Symbol { bool isTerminal(); private: std::string name; + std::string value; bool terminal; NodeTree* subTree; diff --git a/main.cpp b/main.cpp index e46ad53..c6392c0 100644 --- a/main.cpp +++ b/main.cpp @@ -29,15 +29,6 @@ int main(int argc, char* argv[]) { return(1); } - NodeTree root; - root.setName("Root"); - root.addChild(new NodeTree("SomeChild")); - root.addChild(new NodeTree("SomeOtherChild")); - root.get(0)->addChild(new NodeTree("Grandchildren")); - - //outFile << root.DOTGraphString() << std::endl; - - //Read the input file into a string std::string programInputFileString, grammerInputFileString; std::string line; diff --git a/src/NodeTree.cpp b/src/NodeTree.cpp index 11225cc..bc54d8e 100644 --- a/src/NodeTree.cpp +++ b/src/NodeTree.cpp @@ -5,14 +5,16 @@ int NodeTree::idCounter; NodeTree::NodeTree() { parent = NULL; name = "UnnamedNode"; + symbol = NULL; id = idCounter++; } -NodeTree::NodeTree(std::string name) { +NodeTree::NodeTree(std::string name, Symbol* inSymbol) { parent = NULL; + symbol = NULL; this->name = name; - + this->symbol = inSymbol; id = idCounter++; } @@ -77,6 +79,14 @@ void NodeTree::setName(std::string name) { this->name = name; } +Symbol* NodeTree::getSymbol() { + return symbol; +} + +void NodeTree::setSymbol(Symbol* symbol) { + this->symbol = symbol; +} + std::string NodeTree::DOTGraphString() { return( "digraph Kraken { \n" + DOTGraphStringHelper() + "}"); } @@ -90,7 +100,7 @@ std::string NodeTree::DOTGraphStringHelper() { } std::string NodeTree::getDOTName() { - if (name.at(0) == '\"') - return truncateEnd(name) + "_" + intToString(id) + "\""; //Note that terminals already have a quote in the front of their name, so we don't need to add one + if (symbol != NULL) + return "\"" + name + "-" + symbol->toString() + "_" + intToString(id) + "\""; //Note that terminals already have a quote in the front of their name, so we don't need to add one return "\"" + name + "_" + intToString(id) + "\""; } \ No newline at end of file diff --git a/src/Parser.cpp b/src/Parser.cpp index a5e1908..9a26168 100644 --- a/src/Parser.cpp +++ b/src/Parser.cpp @@ -400,10 +400,10 @@ NodeTree* Parser::parseInput(Lexer* lexer) { } NodeTree* Parser::reduceTreeCombine(Symbol* newSymbol, std::vector &symbols) { - NodeTree* newTree = new NodeTree(newSymbol->toString()); + NodeTree* newTree = new NodeTree(newSymbol->getName(), newSymbol); for (std::vector::size_type i = 0; i < symbols.size(); i++) { if (symbols[i]->isTerminal()) - newTree->addChild(new NodeTree(symbols[i]->toString())); + newTree->addChild(new NodeTree(symbols[i]->getName(), symbols[i])); else newTree->addChild(symbols[i]->getSubTree()); } diff --git a/src/Symbol.cpp b/src/Symbol.cpp index 35350c0..4b0e3d5 100644 --- a/src/Symbol.cpp +++ b/src/Symbol.cpp @@ -4,6 +4,7 @@ Symbol::Symbol(std::string name, bool isTerminal) { this->name = name; this->terminal = isTerminal; this->subTree = NULL; + value = "HAHAHA VALUE"; } Symbol::Symbol(std::string name, bool isTerminal, NodeTree* tree) { @@ -20,8 +21,12 @@ const bool Symbol::operator==(const Symbol &other) { return( name == other.name && terminal == other.terminal); } +std::string Symbol::getName() { + return(name); +} + std::string Symbol::toString() { - return(name); //+ "(" + (terminal ? "T" : "NT") + ")"); + return(name + (terminal ? " " + value : "")); } Symbol* Symbol::clone() {