Extended the Regular Expression class to now support * and ? as well as +. Next up, perens and alternation
This commit is contained in:
@@ -2,14 +2,65 @@
|
|||||||
|
|
||||||
RegEx::RegEx(std::string inPattern) {
|
RegEx::RegEx(std::string inPattern) {
|
||||||
pattern = inPattern;
|
pattern = inPattern;
|
||||||
RegExState* current;
|
std::vector<RegExState*> previousStates;
|
||||||
|
std::vector<RegExState*> currentStates;
|
||||||
begin = new RegExState();
|
begin = new RegExState();
|
||||||
current = begin;
|
currentStates.push_back(begin);
|
||||||
for (int i = 0; i < pattern.length(); i++) {
|
for (int i = 0; i < pattern.length(); i++) {
|
||||||
RegExState* next = new RegExState(pattern[i]);
|
switch (pattern[i]) {
|
||||||
current->addNext(next);
|
case '*':
|
||||||
current = next;
|
{
|
||||||
|
std::cout << "Star at " << i << " in " << pattern << std::endl;
|
||||||
|
for (std::vector<RegExState*>::size_type j = 0; j < currentStates.size(); j++)
|
||||||
|
for (std::vector<RegExState*>::size_type k = 0; k < currentStates.size(); k++)
|
||||||
|
currentStates[j]->addNext(currentStates[k]);
|
||||||
|
//add all previous states to current states to enable skipping over the starred item
|
||||||
|
currentStates.insert(currentStates.end(), previousStates.begin(), previousStates.end());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '+':
|
||||||
|
{
|
||||||
|
std::cout << "Plus at " << i << " in " << pattern << std::endl;
|
||||||
|
//OtherThingy
|
||||||
|
//current->addNext(current);
|
||||||
|
for (std::vector<RegExState*>::size_type j = 0; j < currentStates.size(); j++)
|
||||||
|
for (std::vector<RegExState*>::size_type k = 0; k < currentStates.size(); k++)
|
||||||
|
currentStates[j]->addNext(currentStates[k]);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '?':
|
||||||
|
{
|
||||||
|
std::cout << "Question at " << i << " in " << pattern << std::endl;
|
||||||
|
//add all previous states to current states to enable skipping over the questioned item
|
||||||
|
currentStates.insert(currentStates.end(), previousStates.begin(), previousStates.end());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case '|':
|
||||||
|
std::cout << "Alternation at " << i << " in " << pattern << std::endl;
|
||||||
|
//alternation
|
||||||
|
break;
|
||||||
|
case '(':
|
||||||
|
std::cout << "Begin peren at " << i << " in " << pattern << std::endl;
|
||||||
|
//perentheses
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
{
|
||||||
|
std::cout << "Regular" << std::endl;
|
||||||
|
//Ahh, it's regular
|
||||||
|
RegExState* next = new RegExState(pattern[i]);
|
||||||
|
for (std::vector<RegExState*>::size_type j = 0; j < currentStates.size(); j++)
|
||||||
|
currentStates[j]->addNext(next);
|
||||||
|
|
||||||
|
previousStates.clear();
|
||||||
|
previousStates.insert(previousStates.begin(), currentStates.begin(), currentStates.end());
|
||||||
|
currentStates.clear();
|
||||||
|
currentStates.push_back(next);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
//last one is goal state
|
||||||
|
for (std::vector<RegExState*>::size_type i = 0; i < currentStates.size(); i++)
|
||||||
|
currentStates[i]->addNext(NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
RegEx::~RegEx() {
|
RegEx::~RegEx() {
|
||||||
|
|||||||
@@ -29,21 +29,30 @@ bool RegExState::characterIs(char inCharacter) {
|
|||||||
std::vector<RegExState*>* RegExState::advance(char advanceCharacter) {
|
std::vector<RegExState*>* RegExState::advance(char advanceCharacter) {
|
||||||
std::vector<RegExState*>* advanceStates = new std::vector<RegExState*>();
|
std::vector<RegExState*>* advanceStates = new std::vector<RegExState*>();
|
||||||
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++) {
|
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++) {
|
||||||
if (nextStates[i]->characterIs(advanceCharacter))
|
if (nextStates[i] != NULL && nextStates[i]->characterIs(advanceCharacter))
|
||||||
advanceStates->push_back(nextStates[i]);
|
advanceStates->push_back(nextStates[i]);
|
||||||
}
|
}
|
||||||
return advanceStates;
|
return advanceStates;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool RegExState::isGoal() {
|
bool RegExState::isGoal() {
|
||||||
return inner == NULL && nextStates.size() == 0;
|
//return inner == NULL && nextStates.size() == 0;
|
||||||
|
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++)
|
||||||
|
if (nextStates[i] == NULL)
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string RegExState::toString() {
|
std::string RegExState::toString() {
|
||||||
std::string string = "";
|
std::string string = "";
|
||||||
string += std::string("\"") + character + "\"";
|
string += std::string("\"") + character + "\"";
|
||||||
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++)
|
for (std::vector<RegExState*>::size_type i = 0; i < nextStates.size(); i++)
|
||||||
string += "->" + nextStates[i]->toString() + " EC ";
|
if (nextStates[i] != this && nextStates[i] != NULL)
|
||||||
|
string += "->" + nextStates[i]->toString() + " EC ";
|
||||||
|
else if (nextStates[i] == NULL)
|
||||||
|
string += "-> GOAL ";
|
||||||
|
else
|
||||||
|
string += "->this";
|
||||||
//std::cout << "inner = " << inner << " nextStates size = " << nextStates.size() <<std::endl;
|
//std::cout << "inner = " << inner << " nextStates size = " << nextStates.size() <<std::endl;
|
||||||
return string;
|
return string;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user