Fixed the bug! Probabally other bugs too.
This commit is contained in:
@@ -27,7 +27,6 @@ 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);
|
||||
|
||||
8
main.cpp
8
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;
|
||||
|
||||
@@ -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<ParseRule*>* currStateTotal = state->getTotal();
|
||||
for (std::vector<ParseRule*>::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<Symbol*> oldRightSide = nullRule->getRightSide();
|
||||
// oldRightSide.erase(oldRightSide.begin()+nullRule->getIndex(), oldRightSide.end());
|
||||
// nullRule->setRightSide(oldRightSide);
|
||||
for (std::vector<Symbol*>::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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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";
|
||||
|
||||
@@ -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<ParseAction*>::size_type i = 0; i < (*(table[stateNum]))[symbolIndex]->size(); i++)
|
||||
if (*((*((*(table[stateNum]))[symbolIndex]))[i]) == *action)
|
||||
@@ -104,16 +106,19 @@ std::vector<ParseAction*>* 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<ParseAction*>* 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<ParseAction*>* 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)
|
||||
|
||||
Reference in New Issue
Block a user