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

@@ -0,0 +1,23 @@
#include <iostream>
#include <vector>
#include "GSSNode.h"
#ifndef GRAPH_STRUCTURED_STACK
#define GRAPH_STRUCTURED_STACK
class GraphStructuredStack {
public:
GraphStructuredStack();
~GraphStructuredStack();
GSSNode* newNode(int stateNum);
void addToFrontier(int frontier, GSSNode* node);
bool inFrontier(int frontier, int state);
bool frontierIsEmpty(int frontier);
bool frontierHasAccState(int frontier);
std::vector<GSSNode*>* getReachable(GSSNode* start, int lenght);
bool hasEdge(GSSNode* start, GSSNode* end);
void addEdge(GSSNode* start, GSSNode* end);
private:
std::vector<std::vector<GSSNode*>*> gss;
//
};

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

View File

@@ -33,7 +33,7 @@ class Parser {
int stateNum(State* state);
std::string stateSetToString();
NodeTree* parseInput(std::string inputString);
NodeTree<Symbol*>* parseInput(std::string inputString);
std::string grammerToString();
std::string grammerToDOT();
@@ -60,7 +60,7 @@ class Parser {
std::stack<Symbol*> symbolStack;
Symbol* getOrAddSymbol(std::string symbolString, bool isTerminal);
NodeTree* reduceTreeCombine(Symbol* newSymbol, std::vector<Symbol*> &symbols);
NodeTree<Symbol*>* reduceTreeCombine(Symbol* newSymbol, std::vector<Symbol*> &symbols);
};
#endif

17
include/RNGLRParser.h Normal file
View File

@@ -0,0 +1,17 @@
#include <iostream>
class RNGLRParser {
public:
parseInput(std::string inputString);
reducer(int i);
shifter(int i);
private:
Lexer lexer;
std::vector<Symbol*> input;
GraphStructuredStack gss;
//start node, lefthand side of the reduction, reduction length
std::queue<std::pair< std::pair<GSSNode*, Symbol*>, int > toReduce;
//Node coming from, state going to
std::queue< std::pair<GSSNode*, int> > toShift;
};

View File

@@ -11,26 +11,26 @@
#include <string>
//Circular references
class NodeTree;
//class NodeTree;
class Symbol {
public:
Symbol(std::string name, bool isTerminal);
Symbol(std::string name, bool isTerminal, std::string value);
Symbol(std::string name, bool isTerminal, NodeTree* tree);
Symbol(std::string name, bool isTerminal, NodeTree<Symbol*>* tree);
~Symbol();
bool const operator==(const Symbol &other);
std::string getName();
std::string toString();
Symbol* clone();
void setSubTree(NodeTree* tree);
NodeTree* getSubTree();
void setSubTree(NodeTree<Symbol*>* tree);
NodeTree<Symbol*>* getSubTree();
bool isTerminal();
private:
std::string name;
std::string value;
bool terminal;
NodeTree* subTree;
NodeTree<Symbol*>* subTree;
};
#endif