More work towards RNGLR. First, NodeTree is now a template. Second, I've started writing the actual GLR parser and GSS and other things, but am still in the first write process.

This commit is contained in:
Nathan Braswell
2013-07-28 19:45:08 -04:00
parent 726ead0455
commit 6d7b38a03b
14 changed files with 360 additions and 146 deletions

View File

@@ -6,19 +6,20 @@
#endif
#include <util.h>
#include <Symbol.h>
//#include <Symbol.h>
#include <vector>
#include <string>
#include <iostream>
//Circular references
class Symbol;
//class Symbol;
template<class T>
class NodeTree {
public:
NodeTree();
NodeTree(std::string name, Symbol* inSymbol);
NodeTree(std::string name, T inData);
~NodeTree();
void setParent(NodeTree* parent);
@@ -34,8 +35,8 @@ class NodeTree {
std::string getName();
void setName(std::string);
Symbol* getSymbol();
void setSymbol(Symbol* symbol);
T getData();
void setData(T data);
int size();
std::string DOTGraphString();
@@ -44,7 +45,7 @@ class NodeTree {
std::string DOTGraphStringHelper();
std::string getDOTName();
std::string name;
Symbol* symbol;
T data;
NodeTree* parent;
std::vector<NodeTree*> children;
@@ -52,4 +53,129 @@ class NodeTree {
int id;
};
template<class T>
int NodeTree<T>::idCounter;
template<class T>
NodeTree<T>::NodeTree() {
parent = NULL;
name = "UnnamedNode";
data = NULL;
id = idCounter++;
}
template<class T>
NodeTree<T>::NodeTree(std::string name, T inData) {
parent = NULL;
data = NULL;
this->name = name;
this->data = inData;
id = idCounter++;
}
template<class T>
NodeTree<T>::~NodeTree() {
children.clear();
}
template<class T>
void NodeTree<T>::setParent(NodeTree<T>* parent) {
if (this->parent != NULL) {
this->parent->removeChild(this);
}
this->parent = parent;
}
template<class T>
NodeTree<T>* NodeTree<T>::getParent() {
return parent;
}
template<class T>
void NodeTree<T>::addChild(NodeTree<T>* child) {
if (findChild(child) == -1)
children.push_back(child);
}
template<class T>
int NodeTree<T>::findChild(NodeTree<T>* child) {
for (int i = 0; i < children.size(); i++) {
if (children[i] == child) {
return i;
}
}
return -1;
}
template<class T>
void NodeTree<T>::removeChild(int index) {
children[index] = NULL;
children.erase(children.begin()+index);
}
template<class T>
void NodeTree<T>::removeChild(NodeTree<T>* child) {
int index = findChild(child);
if (index != 0) {
removeChild(index);
}
}
template<class T>
int NodeTree<T>::size() {
int count = 0;
for (int i = 0; i < children.size(); i++) {
count += children[i]->size();
}
return 1+count;
}
template<class T>
NodeTree<T>* NodeTree<T>::get(int index) {
return children[index];
}
template<class T>
std::string NodeTree<T>::getName() {
return name;
}
template<class T>
void NodeTree<T>::setName(std::string name) {
this->name = name;
}
template<class T>
T NodeTree<T>::getData() {
return data;
}
template<class T>
void NodeTree<T>::setData(T data) {
this->data = data;
}
template<class T>
std::string NodeTree<T>::DOTGraphString() {
return( "digraph Kraken { \n" + DOTGraphStringHelper() + "}");
}
template<class T>
std::string NodeTree<T>::DOTGraphStringHelper() {
std::string ourDOTRelation = "";
for (int i = 0; i < children.size(); i++) {
ourDOTRelation += getDOTName() + " -> " + children[i]->getDOTName() + ";\n" + children[i]->DOTGraphStringHelper();
}
return(ourDOTRelation);
}
template<class T>
std::string NodeTree<T>::getDOTName() {
if (data != NULL)
return "\"" + name + "-" + data->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) + "\"";
}
#endif