Still in progress

This commit is contained in:
Nathan Braswell
2013-08-22 15:41:30 -04:00
parent 2eaf640855
commit 35a0677d07
6 changed files with 33 additions and 8 deletions

View File

@@ -11,6 +11,7 @@
#include "NodeTree.h" #include "NodeTree.h"
#include "Table.h" #include "Table.h"
#include <queue>
#include <map> #include <map>
#include <vector> #include <vector>
#include <algorithm> #include <algorithm>
@@ -37,7 +38,7 @@ class Parser {
std::vector<Symbol*>* firstSet(Symbol* token, std::vector<Symbol*> avoidList); std::vector<Symbol*>* firstSet(Symbol* token, std::vector<Symbol*> avoidList);
std::vector<Symbol*>* incrementiveFollowSet(ParseRule* rule); std::vector<Symbol*>* incrementiveFollowSet(ParseRule* rule);
virtual void closure(State* state); virtual void closure(State* state);
virtual void addStates(std::vector< State* >* stateSets, State* state); virtual void addStates(std::vector< State* >* stateSets, State* state, std::queue<State*>* toDo);
int stateNum(State* state); int stateNum(State* state);

View File

@@ -22,7 +22,7 @@ class RNGLRParser: public Parser {
void shifter(int i); void shifter(int i);
void addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Symbol*>*>* children, NodeTree<Symbol*>* nullableParts); void addChildren(NodeTree<Symbol*>* parent, std::vector<NodeTree<Symbol*>*>* children, NodeTree<Symbol*>* nullableParts);
void addStates(std::vector< State* >* stateSets, State* state); void addStates(std::vector< State* >* stateSets, State* state, std::queue<State*>* toDo);
void addStateReductionsToTable(State* state); void addStateReductionsToTable(State* state);
bool fullyReducesToNull(ParseRule* rule); bool fullyReducesToNull(ParseRule* rule);
bool reducesToNull(ParseRule* rule); bool reducesToNull(ParseRule* rule);

View File

@@ -27,6 +27,7 @@ class State {
std::vector<ParseRule*>* getTotal(); std::vector<ParseRule*>* getTotal();
bool containsRule(ParseRule* rule); bool containsRule(ParseRule* rule);
void addRuleCombineLookahead(ParseRule* rule); void addRuleCombineLookahead(ParseRule* rule);
void addRuleCombineLookaheadWithLookahead(ParseRule* rule);
std::string toString(); std::string toString();
void combineStates(State &other); void combineStates(State &other);

View File

@@ -87,13 +87,17 @@ void Parser::createStateSet() {
std::vector<Symbol*>* goalRuleLookahead = new std::vector<Symbol*>(); std::vector<Symbol*>* goalRuleLookahead = new std::vector<Symbol*>();
goalRuleLookahead->push_back(EOFSymbol); goalRuleLookahead->push_back(EOFSymbol);
goalRule->setLookahead(goalRuleLookahead); goalRule->setLookahead(goalRuleLookahead);
stateSets.push_back( new State(0, goalRule)); State* zeroState = new State(0, goalRule);
stateSets.push_back(zeroState);
std::queue<State*>* toDo = new std::queue<State*>();
toDo->push(zeroState);
//std::cout << "Begining for main set for loop" << std::endl; //std::cout << "Begining for main set for loop" << std::endl;
for (std::vector< State* >::size_type i = 0; i < stateSets.size(); i++) { while (toDo->front()) {
//closure //closure
closure(stateSets[i]); closure(toDo->front());
//Add the new states //Add the new states
addStates(&stateSets, stateSets[i]); addStates(&stateSets, toDo->front(), toDo);
toDo->pop();
} }
table.remove(1, EOFSymbol); table.remove(1, EOFSymbol);
} }
@@ -239,7 +243,7 @@ void Parser::closure(State* state) {
} }
//Adds state if it doesn't already exist. //Adds state if it doesn't already exist.
void Parser::addStates(std::vector< State* >* stateSets, State* state) { void Parser::addStates(std::vector< State* >* stateSets, State* state, std::queue<State*>* toDo) {
std::vector< State* > newStates; std::vector< State* > newStates;
//For each rule in the state we already have //For each rule in the state we already have
std::vector<ParseRule*>* currStateTotal = state->getTotal(); std::vector<ParseRule*>* currStateTotal = state->getTotal();
@@ -302,6 +306,7 @@ void Parser::addStates(std::vector< State* >* stateSets, State* state) {
if (!stateAlreadyInAllStates) { if (!stateAlreadyInAllStates) {
//If the state does not already exist, add it and add it as the shift/goto in the action table //If the state does not already exist, add it and add it as the shift/goto in the action table
stateSets->push_back(newStates[i]); stateSets->push_back(newStates[i]);
toDo->push(newStates[i]);
table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, stateSets->size()-1)); table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, stateSets->size()-1));
} }
} }

View File

@@ -294,7 +294,7 @@ void RNGLRParser::setPacked(NodeTree<Symbol*>* node, bool isPacked) {
//Have to use own add states function in order to construct RN table instead of LALR table //Have to use own add states function in order to construct RN table instead of LALR table
void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state) { void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state, std::queue<State*>* toDo) {
std::vector< State* > newStates; std::vector< State* > newStates;
//For each rule in the state we already have //For each rule in the state we already have
std::vector<ParseRule*>* currStateTotal = state->getTotal(); std::vector<ParseRule*>* currStateTotal = state->getTotal();
@@ -314,6 +314,7 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state) {
symbolAlreadyInState = true; symbolAlreadyInState = true;
//Add rule to state, combining with idenical rule except lookahead if exists //Add rule to state, combining with idenical rule except lookahead if exists
newStates[j]->addRuleCombineLookahead(advancedRule); newStates[j]->addRuleCombineLookahead(advancedRule);
//newStates[j]->addRuleCombineLookaheadWithLookahead(advancedRule);
//We found a state with the same symbol, so stop searching //We found a state with the same symbol, so stop searching
break; break;
} }
@@ -334,6 +335,9 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state) {
if (newStates[i]->basisEqualsExceptLookahead(*((*stateSets)[j]))) { if (newStates[i]->basisEqualsExceptLookahead(*((*stateSets)[j]))) {
stateAlreadyInAllStates = true; stateAlreadyInAllStates = true;
//If it does exist, we should add it as the shift/goto in the action table //If it does exist, we should add it as the shift/goto in the action table
if (*((*stateSets)[j]) != *(newStates[i]))
toDo->push((*stateSets)[j]);
(*stateSets)[j]->combineStates(*(newStates[i])); (*stateSets)[j]->combineStates(*(newStates[i]));
addStateReductionsToTable((*stateSets)[j]); addStateReductionsToTable((*stateSets)[j]);
table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, j)); table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, j));
@@ -343,6 +347,7 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state) {
if (!stateAlreadyInAllStates) { if (!stateAlreadyInAllStates) {
//If the state does not already exist, add it and add it as the shift/goto in the action table //If the state does not already exist, add it and add it as the shift/goto in the action table
stateSets->push_back(newStates[i]); stateSets->push_back(newStates[i]);
toDo->push(newStates[i]);
table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, stateSets->size()-1)); table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, stateSets->size()-1));
} }
} }

View File

@@ -117,6 +117,19 @@ void State::addRuleCombineLookahead(ParseRule* rule) {
basis.push_back(rule); basis.push_back(rule);
} }
void State::addRuleCombineLookaheadWithLookahead(ParseRule* rule) {
getTotal();
bool alreadyIn = false;
for (std::vector<ParseRule*>::size_type i = 0; i < total.size(); i++) {
if (*rule == (*(total[i]))) {
total[i]->addLookahead(rule->getLookahead());
alreadyIn = true;
}
}
if (!alreadyIn)
basis.push_back(rule);
}
std::string State::toString() { std::string State::toString() {
std::string concat = ""; std::string concat = "";
concat += "State " + intToString(number) + " with " + intToString(parents.size()) + " parents:\n"; concat += "State " + intToString(number) + " with " + intToString(parents.size()) + " parents:\n";