Example #1
0
Automaton* Converter::convert()
{
    Automaton* result = new Automaton();
    
    populateStates();
    populateTransitions();
    
    for (int i = 0; i < this->totalStates; i++)
    {
        result->addState(*this->newStates[i]);
    }
    result->setStartState(this->newStates[1]);
    
    return result;
}
Example #2
0
Automaton * Automaton::getDeterministic() {
	Automaton * b = new Automaton;
	std::vector< std::vector< int > > mEmptyTransitions;
	std::vector< int > vEmptyIn;
	std::vector< int > vEmptyOut;

	int i, j, removed;

	b->vStates = vStates;
	b->vFinal = vFinal;
	b->vSymbols = vSymbols;
	b->mTransitions = mTransitions;

	//creates the mEmptyTransitions table
	vEmptyIn.resize(getNStates());
	vEmptyOut.resize(getNStates());
	mEmptyTransitions.resize(getNStates());
	for(i = 0; i < getNStates(); i++)
		mEmptyTransitions[i].resize(getNStates());

	//initialize mEmptyTransitions
	for(i = 0; i < mEmptyTransitions.size(); i++){
		vEmptyIn[i] = 0;
		vEmptyOut[i] = 0;
		for(j = 0; j < mEmptyTransitions[i].size(); j++)
			mEmptyTransitions[i][j] = 0;
	}

	//fills mEmptyTransitons
	j = findSymbolId("");
	if(j != -1) {
		for(int i = 0; i < getNStates(); i++) {
			for(std::list<int>::iterator it = mTransitions[i][j].begin(); it != mTransitions[i][j].end(); it++) {
				vEmptyIn[*it]++;
				vEmptyOut[i]++;
				mEmptyTransitions[i][*it]++;
			}	
		}
	}

	//finds a state 'i' with incoming empty transitions, but no outgoing empty transitions,
	//copy its transtions to states 'j' able to reach 'i' and removes these empty transitions
	do {
		removed = 0;

		for(i = 0; i < getNStates(); i++) {
			if((vEmptyIn[i] > 0) && (vEmptyOut[i] == 0)) {
				for(j = 0; j < getNStates(); j++) {
					if(i == j)
						continue;

					if(mEmptyTransitions[j][i] > 0) {
						b->copyOutgoingTransitions(i, j);
						b->removeIncomingEmptyTransitions(i);
						if(b->vFinal[i] == true)
							b->vFinal[j] = true;

						mEmptyTransitions[j][i]--;
						vEmptyIn[i]--;
						vEmptyOut[j]--;
						removed++;
					}
					
				}
				
			}
		}

	} while(removed > 0);
	

	//finds a state 'i' that has a non-determinism (two or more outputs for the same symbol),
	//creates a state representing all those symbols and change the transition to this one
	do {
		removed = 0;

		for(i = 0; i < b->getNStates(); i++) {
			for(j = 0; j < b->getNSymbols(); j++) {
				if(b->mTransitions[i][j].size() > 1) {
					int newStateId;
					std::string newStateName;
					bool isFinal;

					//fills newStateName and isFinal
					newStateName = b->vStates[mTransitions[i][j].front()];
					std::list<int>::iterator it = b->mTransitions[i][j].begin();
					isFinal = vFinal[*it];
					++it;
					for(; it != b->mTransitions[i][j].end(); it++) {
						newStateName += "_" + b->vStates[*it];
						isFinal = isFinal || vFinal[*it];
					}

					//guarantees the newStateName is unique
					while(b->findStateId(newStateName) != -1)
						newStateName += '_';

					//adds newStateName to b
					b->addState(newStateName, isFinal);
					newStateId = b->findStateId(newStateName);

					//copy the old state's outgoing transtions to the new state
					for(std::list<int>::iterator it = b->mTransitions[i][j].begin(); it != b->mTransitions[i][j].end(); it++)
						b->copyOutgoingTransitions(*it, newStateId);

					//removes the transitions going to the old states and adds a transtion going to the new one 
					b->mTransitions[i][j].clear();
					b->mTransitions[i][j].push_back(newStateId);

					removed++;
				}
			}
		}
	} while(removed > 0);
		

	return b;
}