Changed Table to store vectors of actions, changed Parser appropriately.

This commit is contained in:
Nathan Braswell
2013-07-28 23:48:10 -04:00
parent 6d7b38a03b
commit 733fe1c08d
8 changed files with 142 additions and 72 deletions

View File

@@ -23,7 +23,7 @@ void Table::add(int stateNum, Symbol* tranSymbol, ParseAction* action) {
//std::cout << "table size is " << table.size() <<std::endl;
while (stateNum >= table.size()) {
//std::cout << "Pushing back table" << std::endl;
table.push_back(new std::vector<ParseAction*>);
table.push_back(new std::vector<std::vector< ParseAction*>* >());
}
//find out what index this symbol is on
@@ -57,18 +57,21 @@ void Table::add(int stateNum, Symbol* tranSymbol, ParseAction* action) {
if ( (*(table[stateNum]))[symbolIndex] == NULL ) {
//std::cout << "Null, adding " << action->toString() << std::endl;
(*(table[stateNum]))[symbolIndex] = action;
std::vector<ParseAction*>* actionList = new std::vector<ParseAction*>();
actionList->push_back(action);
(*(table[stateNum]))[symbolIndex] = actionList;
}
//If the slot is not empty and does not contain ourself, then it is a conflict
else if ( !(*(table[stateNum]))[symbolIndex]->equalsExceptLookahead(*action)) {
//else if ( !(*(table[stateNum]))[symbolIndex]->equalsExceptLookahead(*action)) {
else {
//std::cout << "not Null!" << std::endl;
std::cout << "State: " << stateNum << " Conflict between old: " << (*(table[stateNum]))[symbolIndex]->toString() << " and new: " << action->toString() << " on " << tranSymbol->toString() << std::endl;
//Don't overwrite
//(*(table[stateNum]))[symbolIndex] = action;
//std::cout << "State: " << stateNum << " Conflict between old: " << (*(table[stateNum]))[symbolIndex]->toString() << " and new: " << action->toString() << " on " << tranSymbol->toString() << std::endl;
(*(table[stateNum]))[symbolIndex]->push_back(action);
}
}
ParseAction* Table::get(int state, Symbol* token) {
std::vector<ParseAction*>* Table::get(int state, Symbol* token) {
int symbolIndex = -1;
for (std::vector<Symbol*>::size_type i = 0; i < symbolIndexVec.size(); i++) {
if ( *(symbolIndexVec[i]) == *token) {
@@ -79,17 +82,25 @@ ParseAction* Table::get(int state, Symbol* token) {
//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)
if (state == 1 && symbolIndex == 0)
return(new ParseAction(ParseAction::ACCEPT));
std::vector<ParseAction*>* action = (*(table[state]))[symbolIndex];
if (state == 1 && symbolIndex == 0) {
if (action == NULL)
action = new std::vector<ParseAction*>();
action->push_back(new ParseAction(ParseAction::ACCEPT));
}
//If ourside the symbol range of this state (same as NULL), reject
if ( symbolIndex >= table[state]->size() )
return(new ParseAction(ParseAction::REJECT));
if ( symbolIndex >= table[state]->size() ) {
action = new std::vector<ParseAction*>();
action->push_back(new ParseAction(ParseAction::REJECT));
}
ParseAction* action = (*(table[state]))[symbolIndex];
//If null, reject. (this is a space with no other action)
if (action == NULL)
return(new ParseAction(ParseAction::REJECT));
if (action == NULL) {
action = new std::vector<ParseAction*>();
action->push_back(new ParseAction(ParseAction::REJECT));
}
//Otherwise, we have something, so return it
return (action);
@@ -101,13 +112,16 @@ std::string Table::toString() {
concat += "\t" + symbolIndexVec[i]->toString();
concat += "\n";
for (std::vector< std::vector< ParseRule* > >::size_type i = 0; i < table.size(); i++) {
for (std::vector< std::vector< std::vector< ParseRule* >* >* >::size_type i = 0; i < table.size(); i++) {
concat += intToString(i) + "\t";
for (std::vector< ParseRule* >::size_type j = 0; j < table[i]->size(); j++) {
if ( (*(table[i]))[j] != NULL)
concat += (*(table[i]))[j]->toString() + "\t";
else
for (std::vector< std::vector< ParseRule* >* >::size_type j = 0; j < table[i]->size(); j++) {
if ( (*(table[i]))[j] != NULL) {
for (std::vector< ParseRule* >::size_type k = 0; k < (*(table[i]))[j]->size(); k++) {
concat += (*((*(table[i]))[j]))[k]->toString() + "\t";
}
} else {
concat += "NULL\t";
}
}
concat += "\n";
}