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
#include <util.h>
#include <Symbol.h>
#include <vector>
#include <string>
#include <iostream>
//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<NodeTree*> children;

View File

@@ -10,12 +10,16 @@
#include <vector>
#include <string>
//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;

View File

@@ -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;

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