NodeTree now also has a link to the Symbol it represents.

This commit is contained in:
Nathan Braswell
2013-06-27 23:45:38 -04:00
parent 6a2977d12a
commit c2520ec2c4
6 changed files with 37 additions and 18 deletions

View File

@@ -6,15 +6,19 @@
#endif #endif
#include <util.h> #include <util.h>
#include <Symbol.h>
#include <vector> #include <vector>
#include <string> #include <string>
#include <iostream> #include <iostream>
//Circular references
class Symbol;
class NodeTree { class NodeTree {
public: public:
NodeTree(); NodeTree();
NodeTree(std::string name); NodeTree(std::string name, Symbol* inSymbol);
~NodeTree(); ~NodeTree();
void setParent(NodeTree* parent); void setParent(NodeTree* parent);
@@ -26,10 +30,13 @@ class NodeTree {
void removeChild(int index); void removeChild(int index);
NodeTree* get(int index); NodeTree* get(int index);
std::string getName();
std::string getName();
void setName(std::string); void setName(std::string);
Symbol* getSymbol();
void setSymbol(Symbol* symbol);
int size(); int size();
std::string DOTGraphString(); std::string DOTGraphString();
@@ -37,6 +44,7 @@ class NodeTree {
std::string DOTGraphStringHelper(); std::string DOTGraphStringHelper();
std::string getDOTName(); std::string getDOTName();
std::string name; std::string name;
Symbol* symbol;
NodeTree* parent; NodeTree* parent;
std::vector<NodeTree*> children; std::vector<NodeTree*> children;

View File

@@ -10,12 +10,16 @@
#include <vector> #include <vector>
#include <string> #include <string>
//Circular references
class NodeTree;
class Symbol { class Symbol {
public: public:
Symbol(std::string name, bool isTerminal); Symbol(std::string name, bool isTerminal);
Symbol(std::string name, bool isTerminal, NodeTree* tree); Symbol(std::string name, bool isTerminal, NodeTree* tree);
~Symbol(); ~Symbol();
bool const operator==(const Symbol &other); bool const operator==(const Symbol &other);
std::string getName();
std::string toString(); std::string toString();
Symbol* clone(); Symbol* clone();
void setSubTree(NodeTree* tree); void setSubTree(NodeTree* tree);
@@ -23,6 +27,7 @@ class Symbol {
bool isTerminal(); bool isTerminal();
private: private:
std::string name; std::string name;
std::string value;
bool terminal; bool terminal;
NodeTree* subTree; NodeTree* subTree;

View File

@@ -29,15 +29,6 @@ int main(int argc, char* argv[]) {
return(1); 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 //Read the input file into a string
std::string programInputFileString, grammerInputFileString; std::string programInputFileString, grammerInputFileString;
std::string line; std::string line;

View File

@@ -5,14 +5,16 @@ int NodeTree::idCounter;
NodeTree::NodeTree() { NodeTree::NodeTree() {
parent = NULL; parent = NULL;
name = "UnnamedNode"; name = "UnnamedNode";
symbol = NULL;
id = idCounter++; id = idCounter++;
} }
NodeTree::NodeTree(std::string name) { NodeTree::NodeTree(std::string name, Symbol* inSymbol) {
parent = NULL; parent = NULL;
symbol = NULL;
this->name = name; this->name = name;
this->symbol = inSymbol;
id = idCounter++; id = idCounter++;
} }
@@ -77,6 +79,14 @@ void NodeTree::setName(std::string name) {
this->name = name; this->name = name;
} }
Symbol* NodeTree::getSymbol() {
return symbol;
}
void NodeTree::setSymbol(Symbol* symbol) {
this->symbol = symbol;
}
std::string NodeTree::DOTGraphString() { std::string NodeTree::DOTGraphString() {
return( "digraph Kraken { \n" + DOTGraphStringHelper() + "}"); return( "digraph Kraken { \n" + DOTGraphStringHelper() + "}");
} }
@@ -90,7 +100,7 @@ std::string NodeTree::DOTGraphStringHelper() {
} }
std::string NodeTree::getDOTName() { std::string NodeTree::getDOTName() {
if (name.at(0) == '\"') if (symbol != NULL)
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 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) + "\""; return "\"" + name + "_" + intToString(id) + "\"";
} }

View File

@@ -400,10 +400,10 @@ NodeTree* Parser::parseInput(Lexer* lexer) {
} }
NodeTree* Parser::reduceTreeCombine(Symbol* newSymbol, std::vector<Symbol*> &symbols) { NodeTree* Parser::reduceTreeCombine(Symbol* newSymbol, std::vector<Symbol*> &symbols) {
NodeTree* newTree = new NodeTree(newSymbol->toString()); NodeTree* newTree = new NodeTree(newSymbol->getName(), newSymbol);
for (std::vector<Symbol*>::size_type i = 0; i < symbols.size(); i++) { for (std::vector<Symbol*>::size_type i = 0; i < symbols.size(); i++) {
if (symbols[i]->isTerminal()) if (symbols[i]->isTerminal())
newTree->addChild(new NodeTree(symbols[i]->toString())); newTree->addChild(new NodeTree(symbols[i]->getName(), symbols[i]));
else else
newTree->addChild(symbols[i]->getSubTree()); newTree->addChild(symbols[i]->getSubTree());
} }

View File

@@ -4,6 +4,7 @@ Symbol::Symbol(std::string name, bool isTerminal) {
this->name = name; this->name = name;
this->terminal = isTerminal; this->terminal = isTerminal;
this->subTree = NULL; this->subTree = NULL;
value = "HAHAHA VALUE";
} }
Symbol::Symbol(std::string name, bool isTerminal, NodeTree* tree) { 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); return( name == other.name && terminal == other.terminal);
} }
std::string Symbol::getName() {
return(name);
}
std::string Symbol::toString() { std::string Symbol::toString() {
return(name); //+ "(" + (terminal ? "T" : "NT") + ")"); return(name + (terminal ? " " + value : ""));
} }
Symbol* Symbol::clone() { Symbol* Symbol::clone() {