2013-05-20 19:34:15 -04:00
|
|
|
#include "ParseRule.h"
|
|
|
|
|
|
|
|
|
|
ParseRule::ParseRule() {
|
|
|
|
|
pointerIndex = 0;
|
|
|
|
|
leftHandle = NULL;
|
2013-06-26 14:27:28 -04:00
|
|
|
lookahead = NULL;
|
2013-05-20 19:34:15 -04:00
|
|
|
}
|
|
|
|
|
|
2013-06-26 14:27:28 -04:00
|
|
|
ParseRule::ParseRule(Symbol* leftHandle, int pointerIndex, std::vector<Symbol*> &rightSide, std::vector<Symbol*>* lookahead) {
|
2013-05-20 23:26:15 -04:00
|
|
|
this->leftHandle = leftHandle;
|
|
|
|
|
this->pointerIndex = pointerIndex;
|
|
|
|
|
this->rightSide = rightSide;
|
2013-06-13 14:25:10 -04:00
|
|
|
this->lookahead = lookahead;
|
2013-05-20 23:26:15 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-20 19:34:15 -04:00
|
|
|
ParseRule::~ParseRule() {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-26 14:27:28 -04:00
|
|
|
const bool ParseRule::equalsExceptLookahead(const ParseRule &other) {
|
|
|
|
|
return(leftHandle == other.leftHandle && rightSide == other.rightSide && pointerIndex == other.pointerIndex);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 00:00:41 -04:00
|
|
|
const bool ParseRule::operator==(const ParseRule &other) {
|
2013-06-26 14:27:28 -04:00
|
|
|
return(equalsExceptLookahead(other) && (lookahead == NULL ? other.lookahead == NULL : (*lookahead) == *(other.lookahead)));
|
2013-05-24 00:00:41 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-26 22:12:47 -04:00
|
|
|
const bool ParseRule::operator!=(const ParseRule &other) {
|
|
|
|
|
return !(this->operator==(other));
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-20 23:26:15 -04:00
|
|
|
ParseRule* ParseRule::clone() {
|
2013-06-26 14:27:28 -04:00
|
|
|
return( new ParseRule(leftHandle, pointerIndex, rightSide, lookahead) );
|
2013-05-20 23:26:15 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-20 19:34:15 -04:00
|
|
|
void ParseRule::setLeftHandle(Symbol* leftHandle) {
|
|
|
|
|
this->leftHandle = leftHandle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ParseRule::appendToRight(Symbol* appendee) {
|
|
|
|
|
rightSide.push_back(appendee);
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 01:35:54 -04:00
|
|
|
Symbol* ParseRule::getLeftSide() {
|
|
|
|
|
return leftHandle;
|
|
|
|
|
}
|
|
|
|
|
|
2013-07-09 02:45:59 -04:00
|
|
|
void ParseRule::setRightSide(std::vector<Symbol*> &rightSide) {
|
|
|
|
|
this->rightSide = rightSide;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-23 01:35:54 -04:00
|
|
|
std::vector<Symbol*> ParseRule::getRightSide() {
|
|
|
|
|
return rightSide;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-26 22:12:47 -04:00
|
|
|
Symbol* ParseRule::getAtNextIndex() {
|
|
|
|
|
if (pointerIndex >= rightSide.size())
|
|
|
|
|
return NULL;
|
|
|
|
|
return rightSide[pointerIndex];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Symbol* ParseRule::getAtIndex() {
|
|
|
|
|
if (pointerIndex < 1)
|
|
|
|
|
return NULL;
|
|
|
|
|
return rightSide[pointerIndex-1];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int ParseRule::getRightSize() {
|
|
|
|
|
return rightSide.size();
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-24 00:00:41 -04:00
|
|
|
int ParseRule::getIndex() {
|
2013-06-26 14:27:28 -04:00
|
|
|
return pointerIndex;
|
2013-05-24 00:00:41 -04:00
|
|
|
}
|
|
|
|
|
|
2013-05-20 23:26:15 -04:00
|
|
|
bool ParseRule::advancePointer() {
|
|
|
|
|
if (pointerIndex < rightSide.size()) {
|
|
|
|
|
pointerIndex++;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-29 20:43:35 -04:00
|
|
|
bool ParseRule::isAtEnd() {
|
|
|
|
|
return pointerIndex == rightSide.size();
|
|
|
|
|
}
|
|
|
|
|
|
2013-06-26 14:27:28 -04:00
|
|
|
void ParseRule::setLookahead(std::vector<Symbol*>* lookahead) {
|
|
|
|
|
this->lookahead = lookahead;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::vector<Symbol*>* ParseRule::getLookahead() {
|
|
|
|
|
return lookahead;
|
|
|
|
|
}
|
|
|
|
|
|
2013-05-20 19:34:15 -04:00
|
|
|
std::string ParseRule::toString() {
|
|
|
|
|
std::string concat = leftHandle->toString() + " -> ";
|
|
|
|
|
for (int i = 0; i < rightSide.size(); i++) {
|
2013-05-20 23:26:15 -04:00
|
|
|
if (i == pointerIndex)
|
|
|
|
|
concat += "(*) ";
|
2013-05-20 19:34:15 -04:00
|
|
|
concat += rightSide[i]->toString() + " ";
|
|
|
|
|
}
|
2013-05-20 23:26:15 -04:00
|
|
|
if (pointerIndex >= rightSide.size())
|
|
|
|
|
concat += "(*)";
|
2013-06-26 14:27:28 -04:00
|
|
|
if (lookahead != NULL) {
|
|
|
|
|
concat += "**";
|
|
|
|
|
for (std::vector<Symbol*>::size_type i = 0; i < lookahead->size(); i++)
|
|
|
|
|
concat += (*lookahead)[i]->toString();
|
|
|
|
|
concat += "**";
|
|
|
|
|
}
|
2013-05-20 22:59:57 -04:00
|
|
|
return(concat);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
std::string ParseRule::toDOT() {
|
|
|
|
|
std::string concat = "";
|
|
|
|
|
for (int i = 0; i < rightSide.size(); i++) {
|
|
|
|
|
concat += leftHandle->toString() + " -> " + rightSide[i]->toString() + ";\n";
|
|
|
|
|
}
|
|
|
|
|
return(concat);
|
2013-05-20 19:34:15 -04:00
|
|
|
}
|
|
|
|
|
|