Redid lookahead in a much much saner way. Now should be a legitimate parser somewhere between LALR(1) and LR(1).

This commit is contained in:
Nathan Braswell
2013-06-26 14:27:28 -04:00
parent ee9b8b8c39
commit 6a2977d12a
5 changed files with 78 additions and 109 deletions

View File

@@ -3,9 +3,10 @@
ParseRule::ParseRule() {
pointerIndex = 0;
leftHandle = NULL;
lookahead = NULL;
}
ParseRule::ParseRule(Symbol* leftHandle, int pointerIndex, std::vector<Symbol*> &rightSide, Symbol* lookahead) {
ParseRule::ParseRule(Symbol* leftHandle, int pointerIndex, std::vector<Symbol*> &rightSide, std::vector<Symbol*>* lookahead) {
this->leftHandle = leftHandle;
this->pointerIndex = pointerIndex;
this->rightSide = rightSide;
@@ -16,8 +17,12 @@ ParseRule::~ParseRule() {
}
const bool ParseRule::equalsExceptLookahead(const ParseRule &other) {
return(leftHandle == other.leftHandle && rightSide == other.rightSide && pointerIndex == other.pointerIndex);
}
const bool ParseRule::operator==(const ParseRule &other) {
return( leftHandle == other.leftHandle && rightSide == other.rightSide && pointerIndex == other.pointerIndex );
return(equalsExceptLookahead(other) && (lookahead == NULL ? other.lookahead == NULL : (*lookahead) == *(other.lookahead)));
}
const bool ParseRule::operator!=(const ParseRule &other) {
@@ -25,7 +30,7 @@ const bool ParseRule::operator!=(const ParseRule &other) {
}
ParseRule* ParseRule::clone() {
return( new ParseRule(leftHandle, pointerIndex, rightSide) );
return( new ParseRule(leftHandle, pointerIndex, rightSide, lookahead) );
}
void ParseRule::setLeftHandle(Symbol* leftHandle) {
@@ -61,7 +66,7 @@ int ParseRule::getRightSize() {
}
int ParseRule::getIndex() {
return pointerIndex-1;
return pointerIndex;
}
bool ParseRule::advancePointer() {
@@ -76,6 +81,14 @@ bool ParseRule::isAtEnd() {
return pointerIndex == rightSide.size();
}
void ParseRule::setLookahead(std::vector<Symbol*>* lookahead) {
this->lookahead = lookahead;
}
std::vector<Symbol*>* ParseRule::getLookahead() {
return lookahead;
}
std::string ParseRule::toString() {
std::string concat = leftHandle->toString() + " -> ";
for (int i = 0; i < rightSide.size(); i++) {
@@ -85,6 +98,12 @@ std::string ParseRule::toString() {
}
if (pointerIndex >= rightSide.size())
concat += "(*)";
if (lookahead != NULL) {
concat += "**";
for (std::vector<Symbol*>::size_type i = 0; i < lookahead->size(); i++)
concat += (*lookahead)[i]->toString();
concat += "**";
}
return(concat);
}