Fixed all syntax errors, but NULL crops up where it shouldn't. Checks for it as stop-gap, creates very truncated trees. Does run, and still correctly accepts. Calling it a night.

This commit is contained in:
Nathan Braswell
2013-08-09 04:39:43 -04:00
parent 8570b8f641
commit 9f3370481e
3 changed files with 63 additions and 37 deletions

View File

@@ -22,6 +22,9 @@ class NodeTree {
NodeTree(std::string name, T inData); NodeTree(std::string name, T inData);
~NodeTree(); ~NodeTree();
bool const operator==(NodeTree &other);
bool const operator<(const NodeTree &other) const;
void setParent(NodeTree<T>* parent); void setParent(NodeTree<T>* parent);
void addParent(NodeTree<T>* parent); void addParent(NodeTree<T>* parent);
NodeTree<T>* getParent(); NodeTree<T>* getParent();
@@ -36,11 +39,10 @@ class NodeTree {
std::vector<NodeTree<T>*> getChildren(); std::vector<NodeTree<T>*> getChildren();
NodeTree<T>* get(int index); NodeTree<T>* get(int index);
std::string getName(); std::string getName();
void setName(std::string); void setName(std::string);
T getData(); T getData() const;
void setData(T data); void setData(T data);
int size(); int size();
@@ -83,6 +85,23 @@ NodeTree<T>::~NodeTree() {
parents.clear(); //? Will this segfault? parents.clear(); //? Will this segfault?
} }
template<class T>
const bool NodeTree<T>::operator==(NodeTree &other) {
if (!(data == other.data))
return false;
if (children.size() != other.getChildren().size())
return false;
for (typename std::vector<NodeTree<T>*>::size_type i = 0; i < children.size(); i++)
if (! (*(children[i]) == *(other.getChildren()[i])))
return false;
return true;
}
template<class T>
const bool NodeTree<T>::operator<(const NodeTree &other) const {
return data < other.getData();
}
template<class T> template<class T>
void NodeTree<T>::setParent(NodeTree<T>* parent) { void NodeTree<T>::setParent(NodeTree<T>* parent) {
parents.clear(); parents.clear();
@@ -180,7 +199,7 @@ void NodeTree<T>::setName(std::string name) {
} }
template<class T> template<class T>
T NodeTree<T>::getData() { T NodeTree<T>::getData() const {
return data; return data;
} }
@@ -198,7 +217,10 @@ template<class T>
std::string NodeTree<T>::DOTGraphStringHelper() { std::string NodeTree<T>::DOTGraphStringHelper() {
std::string ourDOTRelation = ""; std::string ourDOTRelation = "";
for (int i = 0; i < children.size(); i++) { for (int i = 0; i < children.size(); i++) {
if (children[i] != NULL)
ourDOTRelation += getDOTName() + " -> " + children[i]->getDOTName() + ";\n" + children[i]->DOTGraphStringHelper(); ourDOTRelation += getDOTName() + " -> " + children[i]->getDOTName() + ";\n" + children[i]->DOTGraphStringHelper();
else
ourDOTRelation += getDOTName() + " -> BAD_NULL_" + getDOTName();
} }
return(ourDOTRelation); return(ourDOTRelation);
} }

View File

@@ -9,6 +9,7 @@
#include "Parser.h" #include "Parser.h"
#include "Symbol.h" #include "Symbol.h"
#include "GraphStructuredStack.h" #include "GraphStructuredStack.h"
#include "util.h"
class RNGLRParser: public Parser { class RNGLRParser: public Parser {
public: public:
@@ -19,21 +20,21 @@ class RNGLRParser: public Parser {
private: private:
void reducer(int i); void reducer(int i);
void shifter(int i); void shifter(int i);
void addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Symbol*>*> children, int nullablePartsIndex); void addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Symbol*>*>* children, int nullablePartsIndex);
void addStates(std::vector< State* >* stateSets, State* state); void addStates(std::vector< State* >* stateSets, State* state);
bool reducesToNull(ParseRule* rule); bool reducesToNull(ParseRule* rule);
bool reducesToNull(ParseRule* rule, std::vector<Symbol*> avoidList); bool reducesToNull(ParseRule* rule, std::vector<Symbol*> avoidList);
bool belongsToFamily(NodeTree<Symbol*>* node, std::vector<NodeTree<Symbol*>*>* nodes); bool belongsToFamily(NodeTree<Symbol*>* node, std::vector<NodeTree<Symbol*>*>* nodes);
bool arePacked(std::vector<NodeTree<Symbol*>*>* nodes); bool arePacked(std::vector<NodeTree<Symbol*>*> nodes);
bool isPacked(NodeTree<Symbol*>* node); bool isPacked(NodeTree<Symbol*>* node);
void setPacked(NodeTree<Symbol*>* node, bool isPacked); void setPacked(NodeTree<Symbol*>* node, bool isPacked);
int getNullableIndex(ParseRule* rule); int getNullableIndex(ParseRule* rule);
NodeTree<Symbol*> getNullableParts(ParseRule* rule); NodeTree<Symbol*>* getNullableParts(ParseRule* rule);
NodeTree<Symbol*> getNullableParts(Symbol* symbol); NodeTree<Symbol*>* getNullableParts(Symbol* symbol);
NodeTree<Symbol*> getNullableParts(int index); NodeTree<Symbol*>* getNullableParts(int index);
std::vector<NodeTree<Symbol*>*> getPathEdges(std::vector<NodeTree<int>*> path); std::vector<NodeTree<Symbol*>*> getPathEdges(std::vector<NodeTree<int>*> path);
@@ -53,7 +54,7 @@ class RNGLRParser: public Parser {
std::vector<std::pair<NodeTree<Symbol*>*, int> > SPPFStepNodes; std::vector<std::pair<NodeTree<Symbol*>*, int> > SPPFStepNodes;
std::vector<NodeTree<Symbol*>*> nullableParts; std::vector<NodeTree<Symbol*>*> nullableParts;
std::map<NodeTree<Symbol*>*, bool> packedMap; std::map<NodeTree<Symbol*>, bool> packedMap;
}; };
#endif #endif

View File

@@ -20,7 +20,7 @@ NodeTree<Symbol*>* RNGLRParser::parseInput(std::string inputString) {
} }
if (accepting) { if (accepting) {
std::cout << "Accepted!" << std::endl; std::cout << "Accepted!" << std::endl;
return getNullableParts(stateSets[0]->getBasis()->operator[0]->getLeftSide()); return getNullableParts((*(stateSets[0]->getBasis()))[0]->getLeftSide());
} else { } else {
std::cout << "Rejected, no input (with no accepting state)" << std::endl; std::cout << "Rejected, no input (with no accepting state)" << std::endl;
} }
@@ -64,7 +64,7 @@ NodeTree<Symbol*>* RNGLRParser::parseInput(std::string inputString) {
if (firstActions[i]->action == ParseAction::SHIFT) if (firstActions[i]->action == ParseAction::SHIFT)
toShift.push(std::make_pair(v0,firstActions[i]->shiftState)); toShift.push(std::make_pair(v0,firstActions[i]->shiftState));
else if (firstActions[i]->action == ParseAction::REDUCE && firstActions[i]->reduceRule->getRightSide().size() == 0) { else if (firstActions[i]->action == ParseAction::REDUCE && firstActions[i]->reduceRule->getRightSide().size() == 0) {
Reduction newReduction = {v0, firstActions[i]->reduceRule->getLeftSide(), 0, getNullableIndex(firstActions[i]->reduceRule), new NodeTree<Symbol*>("null", nullSymbol)} Reduction newReduction = {v0, firstActions[i]->reduceRule->getLeftSide(), 0, getNullableIndex(firstActions[i]->reduceRule), new NodeTree<Symbol*>("null", nullSymbol)};
toReduce.push(newReduction); toReduce.push(newReduction);
//toReduce.push(std::make_pair(std::make_pair(v0, firstActions[i]->reduceRule->getLeftSide()), 0)); //toReduce.push(std::make_pair(std::make_pair(v0, firstActions[i]->reduceRule->getLeftSide()), 0));
} }
@@ -83,8 +83,8 @@ NodeTree<Symbol*>* RNGLRParser::parseInput(std::string inputString) {
} }
//Clear the vector of SPPF nodes created every step //Clear the vector of SPPF nodes created every step
for (std::vector<NodeTree<Symbol*>*>::size_type j = 0; j < SPPFStepNodes.size(); j++) // for (std::vector<NodeTree<Symbol*>*>::size_type j = 0; j < SPPFStepNodes.size(); j++)
SPPFStepNodes[j] = NULL; // SPPFStepNodes[j] = NULL;
SPPFStepNodes.clear(); SPPFStepNodes.clear();
while (toReduce.size() != 0) { while (toReduce.size() != 0) {
@@ -97,7 +97,7 @@ NodeTree<Symbol*>* RNGLRParser::parseInput(std::string inputString) {
std::cout << "GSS:\n" << gss.toString() << std::endl; std::cout << "GSS:\n" << gss.toString() << std::endl;
} }
std::cout << "Done with parsing loop, checking for acceptance" << std::endl; std::cout << "Done with parsing loop, checking for acceptance" << std::endl;
NodeTree<int>* accState = gss.frontierHasAccState(input.size()-1); NodeTree<int>* accState = gss.frontierGetAccState(input.size()-1);
if (accState) { if (accState) {
std::cout << "Accepted!" << std::endl; std::cout << "Accepted!" << std::endl;
return gss.getEdge(accState, v0); return gss.getEdge(accState, v0);
@@ -112,7 +112,7 @@ NodeTree<Symbol*>* RNGLRParser::parseInput(std::string inputString) {
void RNGLRParser::reducer(int i) { void RNGLRParser::reducer(int i) {
Reduction reduction = toReduce.front(); Reduction reduction = toReduce.front();
toReduce.pop(); toReduce.pop();
std::cout << "Doing reduction of length " << reduction.length << " from state " << reduction.from->getData() << " to symbol " << reduction.first.second->toString() << std::endl; std::cout << "Doing reduction of length " << reduction.length << " from state " << reduction.from->getData() << " to symbol " << reduction.symbol->toString() << std::endl;
int pathLength = reduction.length > 0 ? reduction.length -1 : 0; int pathLength = reduction.length > 0 ? reduction.length -1 : 0;
//Get every reachable path //Get every reachable path
std::vector<std::vector<NodeTree<int>*> >* paths = gss.getReachablePaths(reduction.from, pathLength); std::vector<std::vector<NodeTree<int>*> >* paths = gss.getReachablePaths(reduction.from, pathLength);
@@ -143,7 +143,7 @@ void RNGLRParser::reducer(int i) {
//Otherwise, we create the new label if we haven't already //Otherwise, we create the new label if we haven't already
int reachedFrontier = gss.getContainingFrontier(currentReached); int reachedFrontier = gss.getContainingFrontier(currentReached);
for (std::vector<std::pair<NodeTree<Symbol*>*, int> >::size_type k = 0; k < SPPFStepNodes.size(); k++) { for (std::vector<std::pair<NodeTree<Symbol*>*, int> >::size_type k = 0; k < SPPFStepNodes.size(); k++) {
if ( SPPFStepNodes[k].second == reachedFrontier && *(SPPFStepNodes[k].first->data) == *(reduction.symbol)) { if ( SPPFStepNodes[k].second == reachedFrontier && *(SPPFStepNodes[k].first->getData()) == *(reduction.symbol)) {
newLabel = SPPFStepNodes[k].first; newLabel = SPPFStepNodes[k].first;
break; break;
} }
@@ -164,7 +164,7 @@ void RNGLRParser::reducer(int i) {
std::vector<ParseAction*> actions = *(table.get(toState, input[i])); std::vector<ParseAction*> actions = *(table.get(toState, input[i]));
for (std::vector<ParseAction*>::size_type k = 0; k < actions.size(); k++) { for (std::vector<ParseAction*>::size_type k = 0; k < actions.size(); k++) {
if (actions[k]->action == ParseAction::REDUCE && actions[k]->reduceRule->getRightSize() != 0) { if (actions[k]->action == ParseAction::REDUCE && actions[k]->reduceRule->getRightSize() != 0) {
Reduction newReduction = {currentReached, actions[k]->reduceRule->getLeftSide(), actions[k]->reduceRule->getRightSize(), getNullableIndex(actions[k]->reduceRule), newLabel} Reduction newReduction = {currentReached, actions[k]->reduceRule->getLeftSide(), actions[k]->reduceRule->getRightSize(), getNullableIndex(actions[k]->reduceRule), newLabel};
toReduce.push(newReduction); toReduce.push(newReduction);
} }
} }
@@ -182,10 +182,10 @@ void RNGLRParser::reducer(int i) {
if (actions[k]->action == ParseAction::SHIFT) { if (actions[k]->action == ParseAction::SHIFT) {
toShift.push(std::make_pair(toStateNode, actions[k]->shiftState)); toShift.push(std::make_pair(toStateNode, actions[k]->shiftState));
} else if (actions[k]->action == ParseAction::REDUCE && actions[k]->reduceRule->getRightSize() == 0) { } else if (actions[k]->action == ParseAction::REDUCE && actions[k]->reduceRule->getRightSize() == 0) {
Reduction newReduction = {toStateNode, actions[k]->reduceRule->getLeftSide(), 0, getNullableIndex(actions[k]->reduceRule), new NodeTree<Symbol*>("null", nullSymbol)} Reduction newReduction = {toStateNode, actions[k]->reduceRule->getLeftSide(), 0, getNullableIndex(actions[k]->reduceRule), new NodeTree<Symbol*>("null", nullSymbol)};
toReduce.push(newReduction); toReduce.push(newReduction);
} else if (reduction.length != 0 && actions[k]->action == ParseAction::REDUCE && actions[k]->reduceRule->getRightSize() != 0) { } else if (reduction.length != 0 && actions[k]->action == ParseAction::REDUCE && actions[k]->reduceRule->getRightSize() != 0) {
Reduction newReduction = {currentReached, actions[k]->reduceRule->getLeftSide(), actions[k]->reduceRule->getRightSize(), getNullableIndex(actions[k]->reduceRule), newLabel} Reduction newReduction = {currentReached, actions[k]->reduceRule->getLeftSide(), actions[k]->reduceRule->getRightSize(), getNullableIndex(actions[k]->reduceRule), newLabel};
toReduce.push(newReduction); toReduce.push(newReduction);
} }
} }
@@ -198,7 +198,7 @@ void RNGLRParser::reducer(int i) {
void RNGLRParser::shifter(int i) { void RNGLRParser::shifter(int i) {
if (i != input.size()-1) { if (i != input.size()-1) {
std::queue< std::pair<NodeTree<int>*, int> > nextShifts; std::queue< std::pair<NodeTree<int>*, int> > nextShifts;
NodeTree<Symbol*> nextLabel = new NodeTree<Symbol*>("frontier: " + intToString(i), input[i]); NodeTree<Symbol*>* newLabel = new NodeTree<Symbol*>("frontier: " + intToString(i), input[i]);
while (!toShift.empty()) { while (!toShift.empty()) {
std::pair<NodeTree<int>*, int> shift = toShift.front(); std::pair<NodeTree<int>*, int> shift = toShift.front();
toShift.pop(); toShift.pop();
@@ -206,11 +206,11 @@ void RNGLRParser::shifter(int i) {
NodeTree<int>* shiftTo = gss.inFrontier(i+1, shift.second); NodeTree<int>* shiftTo = gss.inFrontier(i+1, shift.second);
if (shiftTo) { if (shiftTo) {
std::cout << "State already existed, just adding edge" << std::endl; std::cout << "State already existed, just adding edge" << std::endl;
gss.addEdge(shiftTo, shift.first, nextLabel); gss.addEdge(shiftTo, shift.first, newLabel);
std::vector<ParseAction*> actions = *(table.get(shift.second, input[i+1])); std::vector<ParseAction*> actions = *(table.get(shift.second, input[i+1]));
for (std::vector<ParseAction*>::size_type j = 0; j < actions.size(); j++) { for (std::vector<ParseAction*>::size_type j = 0; j < actions.size(); j++) {
if (actions[j]->action == ParseAction::REDUCE && actions[j]->reduceRule->getRightSize() != 0) { if (actions[j]->action == ParseAction::REDUCE && actions[j]->reduceRule->getRightSize() != 0) {
Reduction newReduction = {shift.first, actions[j]->reduceRule->getLeftSide(), actions[j]->reduceRule->getRightSize(), getNullableIndex(actions[j]->reduceRule), newLabel} Reduction newReduction = {shift.first, actions[j]->reduceRule->getLeftSide(), actions[j]->reduceRule->getRightSize(), getNullableIndex(actions[j]->reduceRule), newLabel};
toReduce.push(newReduction); toReduce.push(newReduction);
} }
} }
@@ -226,10 +226,10 @@ void RNGLRParser::shifter(int i) {
if (actions[j]->action == ParseAction::SHIFT) { if (actions[j]->action == ParseAction::SHIFT) {
nextShifts.push(std::make_pair(shiftTo, actions[j]->shiftState)); nextShifts.push(std::make_pair(shiftTo, actions[j]->shiftState));
} else if (actions[j]->action == ParseAction::REDUCE && actions[j]->reduceRule->getRightSize() != 0) { } else if (actions[j]->action == ParseAction::REDUCE && actions[j]->reduceRule->getRightSize() != 0) {
Reduction newReduction = {shift.first, actions[j]->reduceRule->getLeftSide(), actions[j]->reduceRule->getRightSize(), getNullableIndex(actions[j]->reduceRule), newLabel} Reduction newReduction = {shift.first, actions[j]->reduceRule->getLeftSide(), actions[j]->reduceRule->getRightSize(), getNullableIndex(actions[j]->reduceRule), newLabel};
toReduce.push(newReduction); toReduce.push(newReduction);
} else if (actions[j]->action == ParseAction::REDUCE && actions[j]->reduceRule->getRightSize() == 0) { } else if (actions[j]->action == ParseAction::REDUCE && actions[j]->reduceRule->getRightSize() == 0) {
Reduction newReduction = {shiftTo, actions[j]->reduceRule->getLeftSide(), 0, getNullableIndex(actions[j]->reduceRule), new NodeTree<Symbol*>("null", nullSymbol)} Reduction newReduction = {shiftTo, actions[j]->reduceRule->getLeftSide(), 0, getNullableIndex(actions[j]->reduceRule), new NodeTree<Symbol*>("null", nullSymbol)};
toReduce.push(newReduction); toReduce.push(newReduction);
} }
} }
@@ -241,7 +241,7 @@ void RNGLRParser::shifter(int i) {
void RNGLRParser::addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Symbol*>*>* children, int nullablePartsIndex) { void RNGLRParser::addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Symbol*>*>* children, int nullablePartsIndex) {
if (nullablePartsIndex != 0) if (nullablePartsIndex != 0)
children->push_back(getNullableParts(nullablePartsIndex); children->push_back(getNullableParts(nullablePartsIndex));
if (!belongsToFamily(parent, children)) { if (!belongsToFamily(parent, children)) {
if (parent->getChildren().size() == 0) { if (parent->getChildren().size() == 0) {
parent->addChildren(children); parent->addChildren(children);
@@ -249,7 +249,8 @@ void RNGLRParser::addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Sy
if (!arePacked(parent->getChildren())) { if (!arePacked(parent->getChildren())) {
NodeTree<Symbol*>* subParent = new NodeTree<Symbol*>(); NodeTree<Symbol*>* subParent = new NodeTree<Symbol*>();
setPacked(subParent, true); setPacked(subParent, true);
subParent->addChildren(&(parent->getChildren()); std::vector<NodeTree<Symbol*>*> tmp = parent->getChildren();
subParent->addChildren(&tmp);
parent->clearChildren(); parent->clearChildren();
parent->addChild(subParent); parent->addChild(subParent);
} }
@@ -262,11 +263,13 @@ void RNGLRParser::addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Sy
} }
bool RNGLRParser::belongsToFamily(NodeTree<Symbol*>* node, std::vector<NodeTree<Symbol*>*>* nodes) { bool RNGLRParser::belongsToFamily(NodeTree<Symbol*>* node, std::vector<NodeTree<Symbol*>*>* nodes) {
std::cout << "Checking " << node->getData()->toString() << "'s family" << std::endl;
std::vector<NodeTree<Symbol*>*> children = node->getChildren(); std::vector<NodeTree<Symbol*>*> children = node->getChildren();
for (std::vector<NodeTree<Symbol*>*>::size_type i = 0; i < nodes->size(); i++) { for (std::vector<NodeTree<Symbol*>*>::size_type i = 0; i < nodes->size(); i++) {
bool containsOne = false; bool containsOne = false;
for (std::vector<NodeTree<Symbol*>*>::size_type j = 0; j < children.size(); j++) { for (std::vector<NodeTree<Symbol*>*>::size_type j = 0; j < children.size(); j++) {
if ((*(*nodes)[i]) == *(children[j])) { //Not sure where null comes from. For right now, just check to be sure we don't segfault
if ((*nodes)[i] == children[j] || (*nodes)[i] != NULL && children[j] != NULL && (*(*nodes)[i]) == *(children[j])) {
containsOne = true; containsOne = true;
break; break;
} }
@@ -278,19 +281,19 @@ bool RNGLRParser::belongsToFamily(NodeTree<Symbol*>* node, std::vector<NodeTree<
return true; return true;
} }
bool RNGLRParser::arePacked(std::vector<NodeTree<Symbol*>*>* nodes) { bool RNGLRParser::arePacked(std::vector<NodeTree<Symbol*>*> nodes) {
bool packed = true; bool packed = true;
for (std::vector<NodeTree<Symbol*>*>::size_type i = 0; i < nodes->size(); i++) for (std::vector<NodeTree<Symbol*>*>::size_type i = 0; i < nodes.size(); i++)
packed &= packedMap[node]; packed &= packedMap[*(nodes[i])];
return packed; return packed;
} }
bool RNGLRParser::isPacked(NodeTree<Symbol*>* node) { bool RNGLRParser::isPacked(NodeTree<Symbol*>* node) {
return packedMap[node]; return packedMap[*node];
} }
void RNGLRParser::setPacked(NodeTree<Symbol*>* node, bool isPacked) { void RNGLRParser::setPacked(NodeTree<Symbol*>* node, bool isPacked) {
packedMap[node] = isPacked; packedMap[*node] = isPacked;
} }
@@ -408,15 +411,15 @@ int RNGLRParser::getNullableIndex(ParseRule* rule) {
return 1; return 1;
} }
NodeTree<Symbol*> RNGLRParser::getNullableParts(ParseRule* rule) { NodeTree<Symbol*>* RNGLRParser::getNullableParts(ParseRule* rule) {
return new NodeTree<Symbol*>("null", nullSymbol); return new NodeTree<Symbol*>("null", nullSymbol);
} }
NodeTree<Symbol*> RNGLRParser::getNullableParts(Symbol* symbol) { NodeTree<Symbol*>* RNGLRParser::getNullableParts(Symbol* symbol) {
return new NodeTree<Symbol*>("null", nullSymbol); return new NodeTree<Symbol*>("null", nullSymbol);
} }
NodeTree<Symbol*> RNGLRParser::getNullableParts(int index) { NodeTree<Symbol*>* RNGLRParser::getNullableParts(int index) {
if (index == 0) if (index == 0)
return new NodeTree<Symbol*>("not_null", nullSymbol); return new NodeTree<Symbol*>("not_null", nullSymbol);
return new NodeTree<Symbol*>("null", nullSymbol); return new NodeTree<Symbol*>("null", nullSymbol);
@@ -424,7 +427,7 @@ NodeTree<Symbol*> RNGLRParser::getNullableParts(int index) {
std::vector<NodeTree<Symbol*>*> RNGLRParser::getPathEdges(std::vector<NodeTree<int>*> path) { std::vector<NodeTree<Symbol*>*> RNGLRParser::getPathEdges(std::vector<NodeTree<int>*> path) {
std::vector<NodeTree<Symbol*>*> pathEdges; std::vector<NodeTree<Symbol*>*> pathEdges;
for (std::vector<NodeTree<int>*>::size_type i < path.size()-1; i++) for (std::vector<NodeTree<int>*>::size_type i = 0; i < path.size()-1; i++)
pathEdges.push_back(gss.getEdge(path[i], path[i+1])); pathEdges.push_back(gss.getEdge(path[i], path[i+1]));
return pathEdges; return pathEdges;
} }