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

@@ -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) + "\"";
}

View File

@@ -400,10 +400,10 @@ NodeTree* Parser::parseInput(Lexer* lexer) {
}
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++) {
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());
}

View File

@@ -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() {