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 "Table.h"
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
@@ -37,7 +38,7 @@ class Parser {
std::vector<Symbol*>* firstSet(Symbol* token, std::vector<Symbol*> avoidList);
std::vector<Symbol*>* incrementiveFollowSet(ParseRule* rule);
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);

View File

@@ -22,7 +22,7 @@ class RNGLRParser: public Parser {
void shifter(int i);
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);
bool fullyReducesToNull(ParseRule* rule);
bool reducesToNull(ParseRule* rule);

View File

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

View File

@@ -87,13 +87,17 @@ void Parser::createStateSet() {
std::vector<Symbol*>* goalRuleLookahead = new std::vector<Symbol*>();
goalRuleLookahead->push_back(EOFSymbol);
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;
for (std::vector< State* >::size_type i = 0; i < stateSets.size(); i++) {
while (toDo->front()) {
//closure
closure(stateSets[i]);
closure(toDo->front());
//Add the new states
addStates(&stateSets, stateSets[i]);
addStates(&stateSets, toDo->front(), toDo);
toDo->pop();
}
table.remove(1, EOFSymbol);
}
@@ -239,7 +243,7 @@ void Parser::closure(State* state) {
}
//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;
//For each rule in the state we already have
std::vector<ParseRule*>* currStateTotal = state->getTotal();
@@ -302,6 +306,7 @@ void Parser::addStates(std::vector< State* >* stateSets, State* state) {
if (!stateAlreadyInAllStates) {
//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]);
toDo->push(newStates[i]);
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
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;
//For each rule in the state we already have
std::vector<ParseRule*>* currStateTotal = state->getTotal();
@@ -314,6 +314,7 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state) {
symbolAlreadyInState = true;
//Add rule to state, combining with idenical rule except lookahead if exists
newStates[j]->addRuleCombineLookahead(advancedRule);
//newStates[j]->addRuleCombineLookaheadWithLookahead(advancedRule);
//We found a state with the same symbol, so stop searching
break;
}
@@ -334,6 +335,9 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state) {
if (newStates[i]->basisEqualsExceptLookahead(*((*stateSets)[j]))) {
stateAlreadyInAllStates = true;
//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]));
addStateReductionsToTable((*stateSets)[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 the state does not already exist, add it and add it as the shift/goto in the action table
stateSets->push_back(newStates[i]);
toDo->push(newStates[i]);
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);
}
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 concat = "";
concat += "State " + intToString(number) + " with " + intToString(parents.size()) + " parents:\n";