FINALLY fixed an error that took weeks. Turned out that the ParseRule was shallow copied, and the lookahead was not copied correctly. So it got extended and thus skipped over the state when it should have been redone.
This commit is contained in:
@@ -66,52 +66,40 @@ std::string ASTData::ASTTypeToString(ASTType type) {
|
||||
switch (type) {
|
||||
case translation_unit:
|
||||
return "translation_unit";
|
||||
break;
|
||||
case interpreter_directive:
|
||||
return "interpreter_directive";
|
||||
break;
|
||||
case identifier:
|
||||
return "identifier";
|
||||
break;
|
||||
case import:
|
||||
return "import";
|
||||
break;
|
||||
case function:
|
||||
return "function";
|
||||
break;
|
||||
case code_block:
|
||||
return "code_block";
|
||||
break;
|
||||
case typed_parameter:
|
||||
return "typed_parameter";
|
||||
break;
|
||||
case expression:
|
||||
return "expression";
|
||||
break;
|
||||
case boolean_expression:
|
||||
return "boolean_expression";
|
||||
break;
|
||||
case statement:
|
||||
return "statement";
|
||||
break;
|
||||
case if_statement:
|
||||
return "if_statement";
|
||||
break;
|
||||
case while_loop:
|
||||
return "while_loop";
|
||||
case for_loop:
|
||||
return "for_loop";
|
||||
case return_statement:
|
||||
return "return_statement";
|
||||
break;
|
||||
case assignment_statement:
|
||||
return "assignment_statement";
|
||||
break;
|
||||
case declaration_statement:
|
||||
return "declaration_statement";
|
||||
break;
|
||||
case function_call:
|
||||
return "function_call";
|
||||
break;
|
||||
case value:
|
||||
return "value";
|
||||
break;
|
||||
default:
|
||||
return "unknown_ASTType";
|
||||
}
|
||||
|
||||
@@ -61,23 +61,15 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||
} else {
|
||||
return transform(children[0]); //Just a promoted bool_exp, so do child
|
||||
}
|
||||
} else if (name == "expression") {
|
||||
//If this is an actual part of an expression, not just a premoted term
|
||||
//Here's the order of ops stuff
|
||||
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad") {
|
||||
//If this is an actual part of an expression, not just a premoted child
|
||||
if (children.size() > 1) {
|
||||
std::string functionCallName = concatSymbolTree(children[1]);
|
||||
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
||||
skipChildren.insert(1);
|
||||
} else {
|
||||
return transform(children[0]); //Just a promoted term, so do child
|
||||
}
|
||||
} else if (name == "term") {
|
||||
//If this is an actual part of an expression, not just a premoted factor
|
||||
if (children.size() > 1) {
|
||||
std::string functionCallName = concatSymbolTree(children[1]);
|
||||
newNode = new NodeTree<ASTData>(functionCallName, ASTData(function_call, Symbol(functionCallName, true)));
|
||||
skipChildren.insert(1);
|
||||
} else {
|
||||
return transform(children[0]); //Just a promoted factor, so do child
|
||||
return transform(children[0]); //Just a promoted child, so do it instead
|
||||
}
|
||||
} else if (name == "factor") {
|
||||
return transform(children[0]); //Just a premoted number or function call or something, so use it instead
|
||||
@@ -85,10 +77,28 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from) {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(statement));
|
||||
} else if (name == "if_statement") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(if_statement));
|
||||
} else if (name == "while_loop") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(while_loop));
|
||||
} else if (name == "for_loop") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(for_loop));
|
||||
} else if (name == "return_statement") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(return_statement));
|
||||
} else if (name == "assignment_statement") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(assignment_statement));
|
||||
std::string assignFuncName = concatSymbolTree(children[1]);
|
||||
if (assignFuncName == "=") {
|
||||
newNode->addChild(transform(children[0]));
|
||||
newNode->addChild(transform(children[2]));
|
||||
} else {
|
||||
//For assignments like += or *=, expand the syntatic sugar.
|
||||
NodeTree<ASTData>* lhs = transform(children[0]);
|
||||
NodeTree<ASTData>* childCall = new NodeTree<ASTData>(assignFuncName, ASTData(function_call, Symbol(assignFuncName, true)));
|
||||
childCall->addChild(lhs);
|
||||
childCall->addChild(transform(children[2]));
|
||||
newNode->addChild(lhs);
|
||||
newNode->addChild(childCall);
|
||||
}
|
||||
return newNode;
|
||||
} else if (name == "declaration_statement") {
|
||||
newNode = new NodeTree<ASTData>(name, ASTData(declaration_statement));
|
||||
NodeTree<ASTData>* newIdentifier = transform(children[1]); //Transform the identifier
|
||||
|
||||
@@ -27,10 +27,8 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
||||
break;
|
||||
case import:
|
||||
return "#include <" + data.symbol.getName() + ">\n";
|
||||
break;
|
||||
case identifier:
|
||||
return data.symbol.getName();
|
||||
break;
|
||||
case function:
|
||||
output += "\n" + ValueTypeToCType(data.valueType) + " " + data.symbol.getName() + "(";
|
||||
for (int i = 0; i < children.size()-1; i++) {
|
||||
@@ -40,7 +38,6 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
||||
}
|
||||
output+= ")\n" + generate(children[children.size()-1]);
|
||||
return output;
|
||||
break;
|
||||
case code_block:
|
||||
output += "{\n";
|
||||
tabLevel++;
|
||||
@@ -49,22 +46,23 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
||||
tabLevel--;
|
||||
output += tabs() + "}";
|
||||
return output;
|
||||
break;
|
||||
case expression:
|
||||
output += " " + data.symbol.getName() + ", ";
|
||||
break;
|
||||
case boolean_expression:
|
||||
output += " " + data.symbol.getName() + " ";
|
||||
break;
|
||||
case statement:
|
||||
return tabs() + generate(children[0]) + ";\n";
|
||||
break;
|
||||
case if_statement:
|
||||
output += "if (" + generate(children[0]) + ")\n\t" + generate(children[1]);
|
||||
if (children.size() > 2)
|
||||
output += " else " + generate(children[2]);
|
||||
return output;
|
||||
break;
|
||||
case while_loop:
|
||||
output += "if (" + generate(children[0]) + ")\n\t" + generate(children[1]);
|
||||
return output;
|
||||
case for_loop:
|
||||
output += "if (" + generate(children[0]) + ")\n\t" + generate(children[1]);
|
||||
return output;
|
||||
case return_statement:
|
||||
return "return " + generate(children[0]);
|
||||
case assignment_statement:
|
||||
@@ -76,7 +74,7 @@ std::string CGenerator::generate(NodeTree<ASTData>* from) {
|
||||
//Handle operators specially for now. Will later replace with
|
||||
//Inlined functions in the standard library
|
||||
std::string name = data.symbol.getName();
|
||||
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=") {
|
||||
if (name == "+" || name == "-" || name == "*" || name == "/" || name == "==" || name == ">=" || name == "<=" || name == "!=" || name == "%") {
|
||||
return "((" + generate(children[0]) + ")" + name + "(" + generate(children[1]) + "))";
|
||||
}
|
||||
output += data.symbol.getName() + "(";
|
||||
|
||||
@@ -29,7 +29,12 @@ const bool ParseRule::operator!=(const ParseRule &other) {
|
||||
}
|
||||
|
||||
ParseRule* ParseRule::clone() {
|
||||
return( new ParseRule(leftHandle, pointerIndex, rightSide, lookahead) );
|
||||
std::vector<Symbol>* newLookahead = NULL;
|
||||
if (lookahead) {
|
||||
newLookahead = new std::vector<Symbol>();
|
||||
*newLookahead = *lookahead;
|
||||
}
|
||||
return( new ParseRule(leftHandle, pointerIndex, rightSide, newLookahead) );
|
||||
}
|
||||
|
||||
void ParseRule::setLeftHandle(Symbol leftHandle) {
|
||||
|
||||
@@ -88,7 +88,7 @@ void Parser::createStateSet() {
|
||||
std::queue<State*>* toDo = new std::queue<State*>();
|
||||
toDo->push(zeroState);
|
||||
//std::cout << "Begining for main set for loop" << std::endl;
|
||||
while (toDo->front()) {
|
||||
while (toDo->size()) {
|
||||
//closure
|
||||
closure(toDo->front());
|
||||
//Add the new states
|
||||
@@ -181,7 +181,7 @@ std::vector<Symbol>* Parser::incrementiveFollowSet(ParseRule* rule) {
|
||||
}
|
||||
}
|
||||
followSet->insert(followSet->end(), symbolFirstSet->begin(), symbolFirstSet->end());
|
||||
//delete symbolFirstSet;
|
||||
delete symbolFirstSet;
|
||||
rule->advancePointer();
|
||||
}
|
||||
if (rule->isAtEnd()) {
|
||||
@@ -209,10 +209,13 @@ void Parser::closure(State* state) {
|
||||
std::vector<ParseRule*>* stateTotal = state->getTotal();
|
||||
for (std::vector<ParseRule*>::size_type i = 0; i < stateTotal->size(); i++) {
|
||||
ParseRule* currentStateRule = (*stateTotal)[i];
|
||||
//If it's at it's end, move on. We can't advance it.
|
||||
if(currentStateRule->isAtEnd())
|
||||
continue;
|
||||
for (std::vector<ParseRule*>::size_type j = 0; j < loadedGrammer.size(); j++) {
|
||||
//If the current symbol in the rule is not null (rule completed) and it equals a grammer's left side
|
||||
ParseRule* currentGramRule = loadedGrammer[j]->clone();
|
||||
if ( !currentStateRule->isAtEnd() && currentStateRule->getAtNextIndex() == currentGramRule->getLeftSide()) {
|
||||
if (currentStateRule->getAtNextIndex() == currentGramRule->getLeftSide()) {
|
||||
//std::cout << (*stateTotal)[i]->getAtNextIndex()->toString() << " has an applicable production " << loadedGrammer[j]->toString() << std::endl;
|
||||
//Now, add the correct lookahead. This followSet is built based on the current rule's lookahead if at end, or the next Symbol's first set.
|
||||
//std::cout << "Setting lookahead for " << currentGramRule->toString() << " in state " << state->toString() << std::endl;
|
||||
@@ -225,6 +228,7 @@ void Parser::closure(State* state) {
|
||||
//std::cout << (*stateTotal)[k]->toString() << std::endl;
|
||||
(*stateTotal)[k]->addLookahead(currentGramRule->getLookahead());
|
||||
isAlreadyInState = true;
|
||||
delete currentGramRule;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -311,7 +315,7 @@ void Parser::addStates(std::vector< State* >* stateSets, State* state, std::queu
|
||||
std::string Parser::stateSetToString() {
|
||||
std::string concat = "";
|
||||
for (std::vector< State *>::size_type i = 0; i < stateSets.size(); i++) {
|
||||
concat += stateSets[i]->toString();
|
||||
concat += intToString(i) + " is " + stateSets[i]->toString();
|
||||
}
|
||||
return concat;
|
||||
}
|
||||
|
||||
@@ -81,7 +81,10 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
|
||||
std::cout << "Nearby is:" << std::endl;
|
||||
int range = 5;
|
||||
for (int j = (i-range >= 0 ? i-range : 0); j < (i+range < input.size() ? i+range : input.size()); j++)
|
||||
std::cout << input[j].toString() << " ";
|
||||
if (j == i)
|
||||
std::cout << "||*||*||" << input[j].toString() << "||*||*|| ";
|
||||
else
|
||||
std::cout << input[j].toString() << " ";
|
||||
std::cout << std::endl;
|
||||
break;
|
||||
}
|
||||
@@ -339,11 +342,13 @@ void RNGLRParser::addStates(std::vector< State* >* stateSets, State* state, std:
|
||||
//if (newStates[i]->basisEquals(*((*stateSets)[j]))) {
|
||||
stateAlreadyInAllStates = true;
|
||||
//If it does exist, we should add it as the shift/goto in the action table
|
||||
//std::cout << "newStates[" << i << "] == stateSets[" << j << "]" << std::endl;
|
||||
|
||||
if (!((*stateSets)[j]->basisEquals(*(newStates[i]))))
|
||||
toDo->push((*stateSets)[j]);
|
||||
|
||||
(*stateSets)[j]->combineStates(*(newStates[i]));
|
||||
//std::cout << j << "\t Hay, doing an inside loop state reductions!" << std::endl;
|
||||
addStateReductionsToTable((*stateSets)[j]);
|
||||
table.add(stateNum(state), currStateSymbol, new ParseAction(ParseAction::SHIFT, j));
|
||||
|
||||
@@ -368,8 +373,9 @@ void RNGLRParser::addStateReductionsToTable(State* state) {
|
||||
//Also, this really only needs to be done for the state's basis, but we're already iterating through, so...
|
||||
std::vector<Symbol>* lookahead = (*currStateTotal)[i]->getLookahead();
|
||||
if ((*currStateTotal)[i]->isAtEnd()) {
|
||||
for (std::vector<Symbol>::size_type j = 0; j < lookahead->size(); j++)
|
||||
for (std::vector<Symbol>::size_type j = 0; j < lookahead->size(); j++) {
|
||||
table.add(stateNum(state), (*lookahead)[j], new ParseAction(ParseAction::REDUCE, (*currStateTotal)[i]));
|
||||
}
|
||||
//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;
|
||||
|
||||
@@ -79,12 +79,9 @@ void State::combineStates(State &other) {
|
||||
|
||||
std::vector<ParseRule*>* State::getTotal() {
|
||||
total.clear();
|
||||
for (std::vector<ParseRule*>::size_type i = 0; i < basis.size(); i++) {
|
||||
total.push_back(basis[i]);
|
||||
}
|
||||
for (std::vector<ParseRule*>::size_type i = 0; i < remaining.size(); i++) {
|
||||
total.push_back(remaining[i]);
|
||||
}
|
||||
//std::cout << "Vector will be " << basis.size() << " + " << remaining.size() << std::endl;
|
||||
total.insert(total.begin(), basis.begin(), basis.end());
|
||||
total.insert(total.end(), remaining.begin(), remaining.end());
|
||||
return(&total);
|
||||
}
|
||||
std::vector<ParseRule*>* State::getBasis() {
|
||||
@@ -111,6 +108,7 @@ void State::addRuleCombineLookahead(ParseRule* rule) {
|
||||
if (rule->equalsExceptLookahead(*(total[i]))) {
|
||||
total[i]->addLookahead(rule->getLookahead());
|
||||
alreadyIn = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!alreadyIn)
|
||||
|
||||
@@ -128,7 +128,7 @@ std::vector<ParseAction*>* Table::get(int state, Symbol token) {
|
||||
action->push_back(new ParseAction(ParseAction::ACCEPT));
|
||||
}
|
||||
|
||||
//If ourside the symbol range of this state (same as NULL), reject
|
||||
//If outside the symbol range of this state (same as NULL), reject
|
||||
if ( symbolIndex >= table[state]->size() ) {
|
||||
action = new std::vector<ParseAction*>();
|
||||
action->push_back(new ParseAction(ParseAction::REJECT));
|
||||
@@ -141,7 +141,7 @@ std::vector<ParseAction*>* Table::get(int state, Symbol token) {
|
||||
}
|
||||
|
||||
//Otherwise, we have something, so return it
|
||||
return (action);
|
||||
return action;
|
||||
}
|
||||
|
||||
ParseAction* Table::getShift(int state, Symbol token) {
|
||||
@@ -163,8 +163,9 @@ std::string Table::toString() {
|
||||
concat += "\n";
|
||||
|
||||
for (std::vector< std::vector< std::vector< ParseRule* >* >* >::size_type i = 0; i < table.size(); i++) {
|
||||
concat += intToString(i) + "\t";
|
||||
concat += intToString(i) + " is the state\t";
|
||||
for (std::vector< std::vector< ParseRule* >* >::size_type j = 0; j < table[i]->size(); j++) {
|
||||
concat += "for " + symbolIndexVec[j].toString() + " do ";
|
||||
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";
|
||||
|
||||
Reference in New Issue
Block a user