From d1f2ba5fc8e01559f6502bed943a385496e3eff8 Mon Sep 17 00:00:00 2001 From: Nathan Braswell Date: Mon, 26 Aug 2013 15:37:49 -0400 Subject: [PATCH] Fixed the bug! Probabally other bugs too. --- include/State.h | 1 - main.cpp | 8 ++++---- src/RNGLRParser.cpp | 12 ++++-------- src/State.cpp | 13 ------------- src/Table.cpp | 13 +++++++++---- 5 files changed, 17 insertions(+), 30 deletions(-) diff --git a/include/State.h b/include/State.h index c638170..7a6a52c 100644 --- a/include/State.h +++ b/include/State.h @@ -27,7 +27,6 @@ class State { std::vector* getTotal(); bool containsRule(ParseRule* rule); void addRuleCombineLookahead(ParseRule* rule); - void addRuleCombineLookaheadWithLookahead(ParseRule* rule); std::string toString(); void combineStates(State &other); diff --git a/main.cpp b/main.cpp index c013e1d..8488c74 100644 --- a/main.cpp +++ b/main.cpp @@ -53,10 +53,10 @@ int main(int argc, char* argv[]) { //std::cout << "finished State Set from Main" << std::endl; //std::cout << "Doing stateSetToString from Main" << std::endl; // std::cout << "\n\n\n\n\n\n\n\n\n\nState Set toString" << std::endl; - std::cout << parser.stateSetToString() << std::endl; - std::cout << "finished stateSetToString from Main" << std::endl; - std::cout << "\n\n\n\n\n\n\n\n\n\nTable" << std::endl; - std::cout << parser.tableToString() << std::endl; + // std::cout << parser.stateSetToString() << std::endl; + // std::cout << "finished stateSetToString from Main" << std::endl; + // std::cout << "\n\n\n\n\n\n\n\n\n\nTable" << std::endl; + // std::cout << parser.tableToString() << std::endl; // std::cout << "\n\n\n\n\n\n\n\n\n\nGrammer Input File" << std::endl; // std::cout << grammerInputFileString << std::endl; // std::cout << "\n\n\n\n\n\n\n\n\n\nGrammer toString" << std::endl; diff --git a/src/RNGLRParser.cpp b/src/RNGLRParser.cpp index 268c51d..947a9c4 100644 --- a/src/RNGLRParser.cpp +++ b/src/RNGLRParser.cpp @@ -314,7 +314,6 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state, std: 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; } @@ -333,6 +332,7 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state, std: currStateSymbol = (*(newStates[i]->getBasis()))[0]->getAtIndex(); for (std::vector< State * >::size_type j = 0; j < stateSets->size(); j++) { if (newStates[i]->basisEqualsExceptLookahead(*((*stateSets)[j]))) { + //if (newStates[i]->basisEquals(*((*stateSets)[j]))) { stateAlreadyInAllStates = true; //If it does exist, we should add it as the shift/goto in the action table @@ -342,6 +342,7 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state, std: (*stateSets)[j]->combineStates(*(newStates[i])); addStateReductionsToTable((*stateSets)[j]); table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, j)); + break; } } @@ -352,10 +353,10 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state, std: table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, stateSets->size()-1)); } } - //Also add any completed rules as reduces in the action table addStateReductionsToTable(state); } + void RNGLRParser::addStateReductionsToTable(State* state) { std::vector* currStateTotal = state->getTotal(); for (std::vector::size_type i = 0; i < currStateTotal->size(); i++) { @@ -368,16 +369,11 @@ void RNGLRParser::addStateReductionsToTable(State* state) { //If this has an appropriate ruduction to null, get the reduce trees out } else if (reducesToNull((*currStateTotal)[i])) { //std::cout << (*currStateTotal)[i]->toString() << " REDUCES TO NULL" << std::endl; - //If is a rule that produces only NULL, add in the approprite reduction, but use a new rule with a right side that is equal to + //It used to be that if is a rule that produces only NULL, add in the approprite reduction, but use a new rule with a right side that is equal to //the part that we've already gone through in the rule. (so we don't pop extra off stack) //Now we use the same rule and make sure that the index location is used - //ParseRule* nullRule = (*currStateTotal)[i]->clone(); - // std::vector oldRightSide = nullRule->getRightSide(); - // oldRightSide.erase(oldRightSide.begin()+nullRule->getIndex(), oldRightSide.end()); - // nullRule->setRightSide(oldRightSide); for (std::vector::size_type j = 0; j < lookahead->size(); j++) table.add(stateNum(state), (*lookahead)[j], new ParseAction(ParseAction::REDUCE, (*currStateTotal)[i])); - //table.add(stateNum(state), (*lookahead)[j], new ParseAction(ParseAction::REDUCE, nullRule)); } } } diff --git a/src/State.cpp b/src/State.cpp index ca62820..b77caf1 100644 --- a/src/State.cpp +++ b/src/State.cpp @@ -117,19 +117,6 @@ void State::addRuleCombineLookahead(ParseRule* rule) { basis.push_back(rule); } -void State::addRuleCombineLookaheadWithLookahead(ParseRule* rule) { - getTotal(); - bool alreadyIn = false; - for (std::vector::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"; diff --git a/src/Table.cpp b/src/Table.cpp index 18587cc..abe6104 100644 --- a/src/Table.cpp +++ b/src/Table.cpp @@ -68,6 +68,8 @@ void Table::add(int stateNum, Symbol* tranSymbol, ParseAction* action) { //std::cout << "State: " << stateNum << " Conflict between old: " << (*(table[stateNum]))[symbolIndex]->toString() << " and new: " << action->toString() << " on " << tranSymbol->toString() << std::endl; //Check to see if this action is already in the list + + //(*(table[stateNum]))[symbolIndex]->push_back(action); bool alreadyIn = false; for (std::vector::size_type i = 0; i < (*(table[stateNum]))[symbolIndex]->size(); i++) if (*((*((*(table[stateNum]))[symbolIndex]))[i]) == *action) @@ -104,16 +106,19 @@ std::vector* Table::get(int state, Symbol* token) { return NULL; } - //std::cout << "Get for state: " << state << ", and Symbol: " << token->toString() << std::endl; + std::cout << "Get for state: " << state << ", and Symbol: " << token->toString() << std::endl; if (state < 0 || state >= table.size()) { std::cout << "State bad: " << state << std::endl; return NULL; } + + std::vector* action = NULL; + if (symbolIndex < 0 || symbolIndex >= table[state]->size()) { - std::cout << "Symbol bad for this state: " << token->toString() << std::endl; - return NULL; + std::cout << "Symbol bad for this state: " << token->toString() << ". This is a reject." << std::endl; + } else { + action = (*(table[state]))[symbolIndex]; } - std::vector* action = (*(table[state]))[symbolIndex]; //This is the accepting state, as it is the 1th's state's reduction on EOF, which is 0 in the symbolIndexVec //(This assumes singular goal assignment, a simplification for now)