Skip to content

Commit

Permalink
fixig afn output
Browse files Browse the repository at this point in the history
  • Loading branch information
diegolrs committed Dec 2, 2022
1 parent 0cc3c01 commit ccadeb5
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 25 deletions.
12 changes: 12 additions & 0 deletions 4b.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
alfabeto=0,1
estados=q0,q1,q2,q3
inicial=q0
final=q3
transicoes
q0,q0,0
q0,q0,1
q0,q1,1
q1,q2,1
q2,q3,0
q3,q3,1
q3,q3,0
12 changes: 12 additions & 0 deletions 4c.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
alfabeto=0,1
estados=q0,q1,q2,q3
inicial=q0
final=q3
transicoes
q0,q0,0
q0,q0,1
q0,q1,1
q1,q2,1
q2,q3,0
q3,q3,1
q3,q3,0
12 changes: 12 additions & 0 deletions 4d.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
alfabeto=0,1
estados=q0,q1,q2,q3
inicial=q0
final=q3
transicoes
q0,q0,0
q0,q0,1
q0,q1,1
q1,q2,1
q2,q3,0
q3,q3,1
q3,q3,0
29 changes: 15 additions & 14 deletions src/NFA_Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ NFA_Machine::NFA_Machine(NFA_ReadedData data)
}

currentState = new MyList<State*>();
chain = new NaryTree<State*>();
chain = new NaryTree<Transition*>();

currentState->Push(initialState);
chain->AddLeaf(initialState, nullptr);
chain->AddLeaf(new Transition(initialState, nullptr), nullptr);
}

std::string NFA_Machine::ToString()
Expand Down Expand Up @@ -172,13 +172,13 @@ void NFA_Machine::ProcessSymbol(AlphabetSymbol sim, int iterationIndex, int maxI
AlphabetSymbol* symbolRef = new AlphabetSymbol(sim.GetValue());
State* oldState;

NaryTree_Node<State*>* node;
MyList<NaryTree_Node<State*>*> current = chain->GetWithHeight(iterationIndex-1);
NaryTree_Node<Transition*>* node;
MyList<NaryTree_Node<Transition*>*> current = chain->GetWithHeight(iterationIndex-1);

for(int i = 0; i < current.Length(); i++)
{
node = current.At(i);
oldState = node->GetContent();
oldState = node->GetContent()->GetDestinationState();

// Trying to process crashed state
if (oldState->IsEquals(crashState))
Expand All @@ -192,13 +192,14 @@ void NFA_Machine::ProcessSymbol(AlphabetSymbol sim, int iterationIndex, int maxI

for (int j = 0; j < _states.Length(); j++)
{
afnStates->Push(_states.At(j));
chain->AddLeaf(_states.At(j), node, node->GetHeight()+1);
Transition* _chainProcessed = new Transition(_states.At(j), symbolRef);
chain->AddLeaf(_chainProcessed, node, node->GetHeight()+1);
}
}
else
{
chain->AddLeaf(crashState, node, CRASH_STATE_HEIGHT);
Transition* _chainProcessed = new Transition(crashState, symbolRef);
chain->AddLeaf(_chainProcessed, node, CRASH_STATE_HEIGHT);
}
}
}
Expand All @@ -207,13 +208,13 @@ void NFA_Machine::ProcessEpsilon(int iterationIndex, int maxIndex)
{
AlphabetSymbol* epsilon = new AlphabetSymbol();

NaryTree_Node<State*>* node;
MyList<NaryTree_Node<State*>*> current = chain->GetWithHeight(iterationIndex-1);
NaryTree_Node<Transition*>* node;
MyList<NaryTree_Node<Transition*>*> current = chain->GetWithHeight(iterationIndex-1);

for(int i = 0; i < current.Length(); i++)
{
node = current.At(i);
State* curState = node->GetContent();
State* curState = node->GetContent()->GetDestinationState();

if (curState->IsEquals(crashState) || !curState->CanProcessSymbol(epsilon))
{
Expand All @@ -227,14 +228,14 @@ void NFA_Machine::ProcessEpsilon(int iterationIndex, int maxIndex)
if(!IsACurrentState(_states.At(j)))
{
// adiciona estado novo na msm altura que o estado pai
chain->AddLeaf(_states.At(j), node, node->GetHeight());
currentState->Push(_states.At(j));
Transition* _chainProcessed = new Transition(_states.At(j), epsilon);
chain->AddLeaf(_chainProcessed, node, node->GetHeight());
}
}
}
}

NaryTree<State*>* NFA_Machine::GetChain()
NaryTree<Transition*>* NFA_Machine::GetChain()
{
return chain;
}
4 changes: 2 additions & 2 deletions src/NFA_Machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class NFA_Machine
void ProcessSymbol(AlphabetSymbol sim, int iterationIndex, int maxIndex);
void ProcessEpsilon(int iterationIndex, int maxIndex);

NaryTree<State*>* GetChain();
NaryTree<Transition*>* GetChain();
private:
const std::string TRAP_STATE_NAME = "Trap State";
const std::string CRASH_STATUS_NAME = "CRASH";
Expand All @@ -36,7 +36,7 @@ class NFA_Machine

State* initialState;
MyList<State*>* currentState;
NaryTree<State*>* chain;
NaryTree<Transition*>* chain;
State* crashState;

MyList<Transition*>* transitions;
Expand Down
29 changes: 20 additions & 9 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using namespace std;
using namespace NFA_FileReader;

const std::string FILE_ADDRESS = "../4a.txt";
const std::string FILE_ADDRESS = "../4b.txt";

void TestTransitions()
{
Expand Down Expand Up @@ -54,42 +54,53 @@ void TestDFAReader(MyList<AlphabetSymbol> *c)
// else
// cout << "\n\nA cadeia fornecida nao eh reconhecida pelo automato" << endl;

NaryTree<State*>* chain = machine->GetChain();
MyList<NaryTree_Node<State*>*> crashed = chain->GetWithHeight(NFA_Machine::CRASH_STATE_HEIGHT);
NaryTree<Transition*>* chain = machine->GetChain();
MyList<NaryTree_Node<Transition*>*> crashed = chain->GetWithHeight(NFA_Machine::CRASH_STATE_HEIGHT);
AlphabetSymbol* symbol;

for(int i = 0; i < crashed.Length(); i++)
{
NaryTree_Node<State*>* s = crashed.At(i);
NaryTree_Node<Transition*>* s = crashed.At(i);

while(s != nullptr)
{
cout << s->GetContent()->GetName();
cout << s->GetContent()->GetDestinationState()->GetName();
symbol = s->GetContent()->GetTransitionSymbol();
s = s->GetParent();

if(s != nullptr)
{
cout << " <- (" << symbol->GetValue() << ")";
cout << " <- ";
}

}
cout << "\n";
}

MyList<NaryTree_Node<State*>*> endeds = chain->GetWithHeight(chain->GetMaxHeight());
MyList<NaryTree_Node<Transition*>*> endeds = chain->GetWithHeight(chain->GetMaxHeight());

for(int i = 0; i < endeds.Length(); i++)
{
NaryTree_Node<State*>* s = endeds.At(i);
NaryTree_Node<Transition*>* s = endeds.At(i);

if(s->GetContent()->IsAFinalState())
if(s->GetContent()->GetDestinationState()->IsAFinalState())
cout << "V ";
else
cout << "X ";

while(s != nullptr)
{
cout << s->GetContent()->GetName();
cout << s->GetContent()->GetDestinationState()->GetName();
symbol = s->GetContent()->GetTransitionSymbol();

s = s->GetParent();

if(s != nullptr)
{
cout << " <- (" << symbol->GetValue() << ")";
cout << " <- ";
}
}
cout << "\n";
}
Expand Down

0 comments on commit ccadeb5

Please sign in to comment.