Fixed a pretty bad error in isNullable logic, I must have been tired. Also, edited grammer to support a[n].b, which was previously done with wrong operator precedence so that that construction was illegal. vector.krak still doesn't quite parse, but that's because of some error with if (!name) which I will fix later. Bedtime.

This commit is contained in:
Nathan Braswell
2014-07-02 01:18:27 -07:00
parent 03770028ad
commit 22fbd61360
5 changed files with 37 additions and 24 deletions

View File

@@ -75,8 +75,8 @@ comparator = "==" | "<=" | ">=" | "!=" | "<" | ">" ;
expression = expression WS "<<" WS term | expression WS ">>" WS shiftand | shiftand ;
shiftand = shiftand WS "-" WS term | shiftand WS "\+" WS term | term ;
term = term WS forward_slash WS factor | term WS "\*" WS factor | term WS "%" WS factor | factor ;
factor = "\+\+" WS unarad | unarad WS "\+\+" | "--" WS unarad | unarad WS "--" | "\+" WS unarad | "-" WS unarad | "!" WS unarad | "~" WS unarad | "\(" WS type WS "\)" WS unarad | "\*" WS unarad | "&" WS unarad | unarad WS "[" WS expression WS "]" | unarad ;
unarad = number | identifier | identifier WS template_inst | function_call | bool | string | character | "\(" WS boolean_expression WS "\)" | access_operation ;
factor = "\+\+" WS unarad | unarad WS "\+\+" | "--" WS unarad | unarad WS "--" | "\+" WS unarad | "-" WS unarad | "!" WS unarad | "~" WS unarad | "\(" WS type WS "\)" WS unarad | "\*" WS unarad | "&" WS unarad | unarad ;
unarad = number | identifier | identifier WS template_inst | function_call | bool | string | character | "\(" WS boolean_expression WS "\)" | access_operation | unarad WS "[" WS expression WS "]" ;
number = integer | float | double ;
access_operation = unarad "." identifier | unarad "->" identifier ;

View File

@@ -467,9 +467,21 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
return transform(children[0], scope, types, templateTypeReplacements, instantiateTemplates); //Just a promoted term, so do child
}
//Here's the order of ops stuff
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad" || name == "access_operation") { //unarad can ride through, it should always just be a promoted child
} else if (name == "expression" || name == "shiftand" || name == "term" || name == "unarad" || name == "access_operation") {
//If this is an actual part of an expression, not just a premoted child
if (children.size() > 2) {
/* else if (children.size() >= 4) { //Array brackets []
funcName = "[]";
std::vector<NodeTree<ASTData>*> transformedChildren;
transformedChildren.push_back(transform(children[0], scope, types, templateTypeReplacements, instantiateTemplates));
transformedChildren.push_back(transform(children[2], scope, types, templateTypeReplacements, instantiateTemplates));
NodeTree<ASTData>* function = doFunction(scope, funcName, transformedChildren, templateTypeReplacements);
if (function == NULL) {
std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl;
throw "LOOKUP ERROR: " + funcName;
}
return function;
}*/
NodeTree<ASTData>* lhs = transform(children[0], scope, std::vector<Type>(), templateTypeReplacements, instantiateTemplates); //LHS does not inherit types
NodeTree<ASTData>* rhs;
if (name == "access_operation") {
@@ -480,6 +492,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
rhs = transform(children[2], scope, types, templateTypeReplacements, instantiateTemplates);
std::string functionCallName = concatSymbolTree(children[1]);
if (functionCallName == "[")
functionCallName = "[]"; //fudge the lookup of brackets because only one is at children[1] (the other is at children[3])
//std::cout << "scope lookup from expression or similar" << std::endl;
std::vector<NodeTree<ASTData>*> transformedChildren; transformedChildren.push_back(lhs); transformedChildren.push_back(rhs);
newNode = doFunction(scope, functionCallName, transformedChildren, templateTypeReplacements);
@@ -491,8 +505,8 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
// //Set the value of this function call
if (newNode->getDataRef()->valueType == NULL && rhs->getDataRef()->valueType)
newNode->getDataRef()->valueType = rhs->getDataRef()->valueType;
else
newNode->getDataRef()->valueType = NULL;
//else
// newNode->getDataRef()->valueType = NULL;
std::cout << "function call to " << functionCallName << " - " << newNode->getName() << " is now " << newNode->getDataRef()->valueType << std::endl;
return newNode;
//skipChildren.insert(1);
@@ -522,17 +536,6 @@ NodeTree<ASTData>* ASTTransformation::transform(NodeTree<Symbol>* from, NodeTree
throw "LOOKUP ERROR: " + funcName;
}
return function;
} else if (children.size() >= 4) { //Array brackets []
funcName = "[]";
std::vector<NodeTree<ASTData>*> transformedChildren;
transformedChildren.push_back(transform(children[0], scope, types, templateTypeReplacements, instantiateTemplates));
transformedChildren.push_back(transform(children[2], scope, types, templateTypeReplacements, instantiateTemplates));
NodeTree<ASTData>* function = doFunction(scope, funcName, transformedChildren, templateTypeReplacements);
if (function == NULL) {
std::cout << "scope lookup error! Could not find " << funcName << " in factor " << std::endl;
throw "LOOKUP ERROR: " + funcName;
}
return function;
} else {
return transform(children[0], scope, types, templateTypeReplacements, instantiateTemplates); //Just a promoted child, so do it instead

View File

@@ -179,11 +179,21 @@ bool Parser::isNullableHelper(Symbol token, std::set<Symbol> done) {
done.insert(token);
if (tokenNullable.find(token) != tokenNullable.end())
return tokenNullable[token];
//Note that we only have to check the first token on the right side, as we would only get to the other tokens if it is nullable, which is what we're checking
for (std::vector<ParseRule*>::size_type i = 0; i < loadedGrammer.size(); i++)
if (token == loadedGrammer[i]->getLeftSide())
if (isNullableHelper(loadedGrammer[i]->getRightSide()[0], done))
for (std::vector<ParseRule*>::size_type i = 0; i < loadedGrammer.size(); i++) {
if (token == loadedGrammer[i]->getLeftSide()) {
auto rightSide = loadedGrammer[i]->getRightSide();
bool ruleNullable = true;
for (int j = 0; j < rightSide.size(); j++) {
if (!isNullableHelper(rightSide[j], done)) {
ruleNullable = false;
break;
}
}
if (ruleNullable)
return true;
}
}
return false;
}

View File

@@ -109,7 +109,7 @@ NodeTree<Symbol>* RNGLRParser::parseInput(std::string inputString) {
else
std::cout << input[j].toString() << " ";
std::cout << std::endl;
range = 3;
range = 1;
std::cout << "\n\n\nThe states in the GSS at last frontiers:" << std::endl;
for (int j = (i-range >= 0 ? i-range : 0); j < i; j++) {
std::cout << "Frontier:" << j << " (would get): " << input[j].toString() << std::endl;

View File

@@ -51,9 +51,9 @@ template <T> T* new() {
}
template <T> void delete(T* toDelete, int itemDestructCount) {
for (int i = 0; i < itemDestructCount; i++)
for (int i = 0; i < itemDestructCount; i++;)
toDelete[i].destruct();
delete(toDelete);
delete<T>(toDelete);
}
template <T> void delete(T* toDelete) {