#include "ParseRule.h" ParseRule::ParseRule() { pointerIndex = 0; } ParseRule::ParseRule(Symbol leftHandle, int pointerIndex, std::vector &rightSide, std::vector lookahead) { this->leftHandle = leftHandle; this->pointerIndex = pointerIndex; this->rightSide = rightSide; this->lookahead = lookahead; } ParseRule::~ParseRule() { } const bool ParseRule::equalsExceptLookahead(const ParseRule &other) const { return(leftHandle == other.leftHandle && rightSide == other.rightSide && pointerIndex == other.pointerIndex); } const bool ParseRule::operator==(const ParseRule &other) const { return(equalsExceptLookahead(other) && (lookahead == other.lookahead)); } const bool ParseRule::operator!=(const ParseRule &other) const { return !(this->operator==(other)); } const bool ParseRule::operator<(const ParseRule &other) const { //Used for ordering so we can put ParseRule's in sets, and also so that ParseActions will have an ordering if (leftHandle != other.leftHandle) return leftHandle < other.leftHandle; if (rightSide != other.rightSide) return rightSide < other.rightSide; if (lookahead != other.lookahead) { return lookahead < other.lookahead; } return false; } ParseRule* ParseRule::clone() { return( new ParseRule(leftHandle, pointerIndex, rightSide, lookahead) ); } void ParseRule::setLeftHandle(Symbol leftHandle) { this->leftHandle = leftHandle; } void ParseRule::appendToRight(Symbol appendee) { rightSide.push_back(appendee); } Symbol ParseRule::getLeftSide() { return leftHandle; } void ParseRule::setRightSide(std::vector rightSide) { this->rightSide = rightSide; } std::vector ParseRule::getRightSide() { return rightSide; } Symbol ParseRule::getAtNextIndex() { if (pointerIndex >= rightSide.size()) return Symbol(); return rightSide[pointerIndex]; } Symbol ParseRule::getAtIndex() { if (pointerIndex < 1) return Symbol(); return rightSide[pointerIndex-1]; } int ParseRule::getRightSize() { return rightSide.size(); } int ParseRule::getIndex() { return pointerIndex; } bool ParseRule::advancePointer() { if (pointerIndex < rightSide.size()) { pointerIndex++; return true; } return false; } bool ParseRule::isAtEnd() { return pointerIndex == rightSide.size(); } void ParseRule::setLookahead(std::vector lookahead) { this->lookahead = lookahead; } void ParseRule::addLookahead(std::vector lookahead) { for (std::vector::size_type i = 0; i < lookahead.size(); i++) { bool alreadyIn = false; for (std::vector::size_type j = 0; j < this->lookahead.size(); j++) { if (lookahead[i] == this->lookahead[j]) { alreadyIn = true; break; } } if (!alreadyIn) this->lookahead.push_back(lookahead[i]); } } std::vector ParseRule::getLookahead() { return lookahead; } std::string ParseRule::toString(bool printLookahead) { std::string concat = leftHandle.toString() + " -> "; for (int i = 0; i < rightSide.size(); i++) { if (i == pointerIndex) concat += "(*) "; concat += rightSide[i].toString() + " "; } if (pointerIndex >= rightSide.size()) concat += "(*)"; if (printLookahead && lookahead.size()) { concat += "**"; for (std::vector::size_type i = 0; i < lookahead.size(); i++) concat += lookahead[i].toString(); concat += "**"; } 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); }