예제 #1
0
NFAStateSet operationCONCAT ( NFAStateSet a, NFAStateSet b )
{
	/*
	 * CONCATs the states pointed by a and b, and then returns the NFA for
	 * a@b
	 */
	NFAStateSet output;
	for ( unsigned int i=0; i<a.size() && a[i][0] != -1; i++ )
		output.push_back( a[i] );
	for ( unsigned int i=0; i<b.size() && b[i][0] != -1; i++ )
		output.push_back( b[i] );
	int oldStart1;
	int oldStart2;
	int oldFinish1;
	int oldFinish2;
	if ( a[a.size()-1][0] == -1 ){
		oldStart1 = a[a.size()-1][1];
		oldFinish1 = a[a.size()-1][2];
	}
	else{
		oldStart1 = a[0][1];
		oldFinish1 = a[0][2];
	}
	if ( b[b.size()-1][0] == -1 ){
		oldStart2 = b[b.size()-1][1];
		oldFinish2 = b[b.size()-1][2];
	}
	else {
		oldStart2 = b[0][1];
		oldFinish2 = b[0][2];
	}
	int newStart = uniqueStateID;
	vector<int> startEpsilon, middleEpsilon;
	startEpsilon.push_back ( operators[EPSILON] );
	startEpsilon.push_back ( newStart );
	startEpsilon.push_back ( oldStart1 );
	
	middleEpsilon.push_back ( operators[EPSILON] );
	middleEpsilon.push_back ( oldFinish1 );
	middleEpsilon.push_back ( oldStart2 );
	
	uniqueStateID++;
	int newFinal = uniqueStateID;
	vector<int> finalEpsilon;
	finalEpsilon.push_back ( operators[EPSILON] );
	finalEpsilon.push_back ( oldFinish2 );
	finalEpsilon.push_back ( newFinal );
	uniqueStateID++;
	vector<int> startEndInfo;
	startEndInfo.push_back( -1 );
	startEndInfo.push_back( newStart );
	startEndInfo.push_back( newFinal );
	
	output.push_back ( startEpsilon );
	output.push_back ( middleEpsilon );
	output.push_back ( finalEpsilon );
	output.push_back ( startEndInfo );
	return output;
}
예제 #2
0
NFAStateSet allTermStates ( string fileName )
{
	ifstream fpCal;
	fpCal.open( strdup(fileName.c_str()) );
	if (!fpCal.is_open()){
		perror("Regular expression file doesn't exist or could not be opened\n");
		exit (1);
	}
	
	string terminalString;
	NFAStateSet singletonStates;
	
	getline (fpCal,terminalString );	
    stringstream ss(terminalString); // Insert the string into a stream
    /*
     * Push the EPSILON state first as state 1->2 on EPSILON
     */
	vector <int> epsilonState;
	epsilonState.push_back( (int)operators[EPSILON] );
	epsilonState.push_back( -1 );
	epsilonState.push_back( -1 );
	singletonStates.push_back(epsilonState);
	/*
	 * Now do the same for all other terminals
	 */
    while (ss){
		string sub;
		ss >> sub;
		vector <int> inputState;
		inputState.push_back((int)(sub.c_str())[0]);
		inputState.push_back(-1);
		inputState.push_back(-1);
        singletonStates.push_back(inputState);
    }
	singletonStates.pop_back();
	
	fpCal.close();
	return singletonStates;
}
예제 #3
0
NFAStateSet combineAllNFA ( vector < NFAStateSet> allRegexNFA )
{
	NFAStateSet combined;
	for ( unsigned int i=0; i<allRegexNFA.size(); i++){
		for ( unsigned int j=0; j<allRegexNFA[i].size(); j++){
			if ( allRegexNFA[i][j][0] != -1 )
				combined.push_back(allRegexNFA[i][j]);
			if ( allRegexNFA[i][j][0] == -1 ){
				vector <int> temp;
				temp.push_back( (int)operators[EPSILON] );
				temp.push_back( 0 );
				temp.push_back( allRegexNFA[i][j][1] );
				combined.push_back ( temp );

				vector <int> startEndInfo;
				startEndInfo.push_back( -1 );
				startEndInfo.push_back( 0 );
				startEndInfo.push_back( allRegexNFA[i][j][2] );
				combined.push_back ( startEndInfo );
			}
		}
	}
	return combined;
}
예제 #4
0
NFAStateSet operationSTAR ( NFAStateSet a )
{
	/*
	 * CONCATs the states pointed by a and b, and then returns the NFA for
	 * a*
	 */
	NFAStateSet output;
	for ( unsigned int i=0; i<a.size() && a[i][0] != -1; i++ )
		output.push_back( a[i] );
	int oldStart;
	int oldFinish;
	if ( a[a.size()-1][0] == -1 ){
		oldStart = a[a.size()-1][1];
		oldFinish = a[a.size()-1][2];
	}
	else{
		oldStart = a[0][1];
		oldFinish = a[0][2];
	}
	int newStart = uniqueStateID;
	uniqueStateID++;
	int newFinal = uniqueStateID;
	uniqueStateID++;

	vector<int> startEpsilon, startFinishEpsilon;
	startEpsilon.push_back ( operators[EPSILON] );
	startEpsilon.push_back ( newStart );
	startEpsilon.push_back ( oldStart );
	
	startFinishEpsilon.push_back ( operators[EPSILON] );
	startFinishEpsilon.push_back ( newStart );
	startFinishEpsilon.push_back ( newFinal );
	
	vector<int> loopBack, finishEpsilon;
	loopBack.push_back ( operators[EPSILON] );
	loopBack.push_back ( oldFinish );
	loopBack.push_back ( oldStart );
	
	finishEpsilon.push_back ( operators[EPSILON] );
	finishEpsilon.push_back ( oldFinish );
	finishEpsilon.push_back ( newFinal );
	
	vector<int> startEndInfo;
	startEndInfo.push_back( -1 );
	startEndInfo.push_back( newStart );
	startEndInfo.push_back( newFinal );
	
	output.push_back ( startEpsilon );
	output.push_back ( startFinishEpsilon );
	output.push_back ( loopBack );
	output.push_back ( finishEpsilon );
	output.push_back ( startEndInfo );
	return output;
}
예제 #5
0
NFAStateSet generateOpStack ( string postfix, string fileName )
{
	stack < NFAStateSet > stateStack;
	NFAStateSet inputState = allTermStates( fileName );
	bool isSingleton = true;
	for ( unsigned int i=0; i<postfix.length(); i++ ){
		if ( isOperator( postfix[i] ) == -1 ){
			//use the nfa for this state
			for ( unsigned int j = 0; j<inputState.size(); j++ ){
				if ( inputState[j][0] == (int)postfix[i] ){
					NFAStateSet tempToBePushed;
					/*
					 * Copy this to uidGiven to assign unique IDs
					 */
					vector <int> uidGiven = inputState[j];
					uidGiven[1]=uniqueStateID;
					uniqueStateID++;
					uidGiven[2]=uniqueStateID;
					uniqueStateID++;
					
					tempToBePushed.push_back(uidGiven);
					stateStack.push ( tempToBePushed );
					break;
				}
			}
		}
		if ( isOperator ( postfix[i] ) == OR ){
			//send the two preceding states to the function to OR
			NFAStateSet output;
			NFAStateSet operand2 = stateStack.top();
			stateStack.pop();
			NFAStateSet operand1 = stateStack.top();
			stateStack.pop();
			output = operationOR ( operand1, operand2 );
			stateStack.push(output);
			isSingleton = false;
		}
		if ( isOperator ( postfix[i] ) == CONCAT ){
			//send the two preceding states to the function to CONCAT
			NFAStateSet output;
			NFAStateSet operand2 = stateStack.top();
			stateStack.pop();
			NFAStateSet operand1 = stateStack.top();
			stateStack.pop();
			output = operationCONCAT ( operand1, operand2 );
			stateStack.push(output);
			isSingleton = false;
		}
		if ( isOperator ( postfix[i] ) == STAR ){
			//send the preceding state to the function to STAR
			NFAStateSet output;
			NFAStateSet operand = stateStack.top();
			stateStack.pop();
			output = operationSTAR ( operand );
			stateStack.push(output);
			isSingleton = false;
		}
	}
	if (!isSingleton){
		NFAStateSet finalStateSet = stateStack.top();
		if (!stateStack.empty())
			stateStack.pop();
		if (stateStack.empty())
			return finalStateSet;
		else{
			cout<<"ERROR: NFA stack didn't empty itself\n";
			exit(1);
		}
	}
	
	else if ( isSingleton ){
		NFAStateSet finalStateSet = stateStack.top();
		vector <int> startEndInfo;
		startEndInfo.push_back(-1);
		startEndInfo.push_back(finalStateSet[0][1]);
		startEndInfo.push_back(finalStateSet[0][2]);
		finalStateSet.push_back( startEndInfo );
	
		if (!stateStack.empty())
			stateStack.pop();
		if ( stateStack.empty() )
			return finalStateSet;
		else{
			cout<<"ERROR: Stack didn't empty itself\n";
			exit(1);
		}
	}
	NFAStateSet dummyReturnVal;
	return (dummyReturnVal);

}