Skip to content

Commit

Permalink
Cleaning code
Browse files Browse the repository at this point in the history
Cleaning code
  • Loading branch information
diegolrs authored Dec 2, 2022
2 parents 5392926 + b8a8cb3 commit 12ac655
Show file tree
Hide file tree
Showing 7 changed files with 231 additions and 155 deletions.
48 changes: 48 additions & 0 deletions color.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <iostream>

using namespace std;

#define RED_CONSOLE_COLOR 4
#define GREEN_CONSOLE_COLOR 2
#define WHITE_CONSOLE_COLOR 15

#define IS_ON_WINDOWS defined(WIN32) || defined(_WIN32) || defined(__WIN32) && !defined(__CYGWIN__)

#if IS_ON_WINDOWS
#include <windows.h> // Used to change color of text
void SetConsoleColor_Windows(int color)
{

HANDLE console_color = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(console_color, color);
}
#endif

void SetConsoleColor(int color)
{
#if IS_ON_WINDOWS
SetConsoleColor_Windows(color);
#endif
}

void ResetConsoleColor()
{
SetConsoleColor(WHITE_CONSOLE_COLOR);
}

// Driver Code
int main()
{
SetConsoleColor(RED_CONSOLE_COLOR);
cout << " Hello Geeks, " << "good night!!!";

SetConsoleColor(WHITE_CONSOLE_COLOR);
cout << " Hello Geeks, " << "good night!!!";

SetConsoleColor(GREEN_CONSOLE_COLOR);
cout << " Hello Geeks, " << "good night!!!";

ResetConsoleColor();

return 0;
}
88 changes: 39 additions & 49 deletions src/NFA_Machine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,6 @@ NFA_Machine::NFA_Machine(NFA_ReadedData data)
transitions->Push(_transition);
_init->AddTransition(_transition);
}

chain = new NaryTree<Transition*>();
chain->AddLeaf(new Transition(initialState, nullptr), nullptr);
}

std::string NFA_Machine::ToString()
Expand Down Expand Up @@ -126,39 +123,10 @@ int NFA_Machine::IndexOfSymbol(AlphabetSymbol* s)
return OUT_OF_INDEX;
}

bool NFA_Machine::IsOnFinalState()
{
int maxHeight = chain->GetMaxHeight();
MyList<NaryTree_Node<Transition*>*> lastsStates = chain->GetWithHeight(maxHeight);

for (int i = 0; i < lastsStates.Length(); i++)
{
if (lastsStates.At(i)->GetContent()->GetDestinationState()->IsAFinalState())
{
return true;
}
}

return false;
}

std::string NFA_Machine::GetEndOfProcessingMessage(MyList<State*> l)
{
for (int i = 0; i < l.Length(); i++)
{
if (l.At(i)->IsAFinalState())
{
return CHAIN_IS_ACCEPTED_MSG;
}
}

return CHAIN_IS_NOT_ACCEPTED_MSG;
}

void NFA_Machine::ProcessSymbol(AlphabetSymbol sim, int iterationIndex, int maxIndex)
void NFA_Machine::ProcessSymbol(AlphabetSymbol sim, int iterationIndex, NaryTree<Transition*>* processmentTree)
{
AlphabetSymbol* symbolRef = new AlphabetSymbol(sim.GetValue());
MyList<NaryTree_Node<Transition*>*> current = chain->GetWithHeight(iterationIndex-1);
MyList<NaryTree_Node<Transition*>*> current = processmentTree->GetWithHeight(iterationIndex-1);

NaryTree_Node<Transition*>* node;
State* oldState;
Expand All @@ -181,20 +149,23 @@ void NFA_Machine::ProcessSymbol(AlphabetSymbol sim, int iterationIndex, int maxI
for (int j = 0; j < _states.Length(); j++)
{
Transition* _chainProcessed = new Transition(_states.At(j), symbolRef);
chain->AddLeaf(_chainProcessed, node, node->GetHeight()+1);
processmentTree->AddLeaf(_chainProcessed, node, node->GetHeight()+1);
}
}
else
{
Transition* _chainProcessed = new Transition(crashState, symbolRef);
chain->AddLeaf(_chainProcessed, node, CRASH_STATE_HEIGHT);
processmentTree->AddLeaf(_chainProcessed, node, CRASH_STATE_HEIGHT);
}
}
}

bool NFA_Machine::IsACurrentState(State* s){
int maxHeight = chain->GetMaxHeight();
MyList<NaryTree_Node<Transition*>*> lastsStates = chain->GetWithHeight(maxHeight);
// Verify if state is being processed right now
bool NFA_Machine::IsACurrentState(State* s, NaryTree<Transition*>* processmentTree)
{
int maxHeight = processmentTree->GetMaxHeight();
MyList<NaryTree_Node<Transition*>*> lastsStates = processmentTree->GetWithHeight(maxHeight);

for (int i = 0; i < lastsStates.Length(); i++)
{
if (lastsStates.At(i)->GetContent()->GetDestinationState()->IsEquals(s))
Expand All @@ -205,16 +176,14 @@ bool NFA_Machine::IsACurrentState(State* s){
return false;
}

void NFA_Machine::ProcessEpsilon(int iterationIndex, int maxIndex)
void NFA_Machine::ProcessEpsilon(int iterationIndex, NaryTree<Transition*>* processmentTree)
{
AlphabetSymbol* epsilon = new AlphabetSymbol();
MyList<NaryTree_Node<Transition*>*> current = chain->GetWithHeight(iterationIndex-1);

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

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

if (curState->IsEquals(crashState) || !curState->CanProcessSymbol(epsilon))
Expand All @@ -226,17 +195,38 @@ void NFA_Machine::ProcessEpsilon(int iterationIndex, int maxIndex)

for (int j = 0; j < _states.Length(); j++)
{
if(!IsACurrentState(_states.At(j)))
if(!IsACurrentState(_states.At(j), processmentTree))
{
// adiciona estado novo na msm altura que o estado pai
// add new state at the same level as the parent node in the processment tree
Transition* _chainProcessed = new Transition(_states.At(j), epsilon);
chain->AddLeaf(_chainProcessed, node, node->GetHeight());
processmentTree->AddLeaf(_chainProcessed, node, node->GetHeight());
}
}
}
}

NaryTree<Transition*>* NFA_Machine::GetChain()
NaryTree<Transition*>* NFA_Machine::StartProcessment(NFA_Machine* machine, MyList<AlphabetSymbol> *chain)
{
return chain;
NaryTree<Transition*>* processmentTree = new NaryTree<Transition*>();

// Adding initial state
processmentTree->AddLeaf(new Transition(initialState, nullptr), nullptr, 0);

int maxInterations = chain->Length();
int curInteration = 0;
int firstEpsilonProcessingHeight = 1;

// Processing epsilon before initiate all symbols processing
machine->ProcessEpsilon(firstEpsilonProcessingHeight, processmentTree);
curInteration++;

// Processing all chain symbols
for (int i = 0; i < chain->Length(); i++)
{
machine->ProcessSymbol(chain->At(i).GetValue(), curInteration, processmentTree);
machine->ProcessEpsilon(curInteration, processmentTree);
curInteration++;
}

return processmentTree;
}
27 changes: 9 additions & 18 deletions src/NFA_Machine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,23 @@ class NFA_Machine
static const int CRASH_STATE_HEIGHT = -4; // altura para setar nos estados que deram crash

NFA_Machine(NFA_ReadedData data);
std::string ToString();

bool IsOnFinalState();
std::string GetEndOfProcessingMessage(MyList<State*> l);

void ProcessSymbol(AlphabetSymbol sim, int iterationIndex, int maxIndex);
void ProcessEpsilon(int iterationIndex, int maxIndex);

NaryTree<Transition*>* GetChain();
std::string ToString();
NaryTree<Transition*>* StartProcessment(NFA_Machine* machine, MyList<AlphabetSymbol> *chain);
private:
const std::string TRAP_STATE_NAME = "Trap State";
const std::string CRASH_STATUS_NAME = "CRASH";
const std::string CHAIN_IS_ACCEPTED_MSG = "A cadeia processada eh aceita. \n";
const std::string CHAIN_IS_NOT_ACCEPTED_MSG = "A cadeia processada nao eh aceita. \n";

MyList<AlphabetSymbol*>* alphabet;
MyList<State*>* states;

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

MyList<Transition*>* transitions;
MyList<AlphabetSymbol*>* alphabet; // All alphabet symbols
MyList<State*>* states; // All states
MyList<Transition*>* transitions; // All states transitions

bool IsACurrentState(State* s);
bool IsACurrentState(State* s, NaryTree<Transition*>* processmentTree);
bool ContainsState(State* s);
int IndexOfState(State* s);
int IndexOfSymbol(AlphabetSymbol* s);

void ProcessSymbol(AlphabetSymbol sim, int iterationIndex, NaryTree<Transition*>* processmentTree);
void ProcessEpsilon(int iterationInde, NaryTree<Transition*>* processmentTreex);
};
52 changes: 52 additions & 0 deletions src/NFA_Printer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include "NFA_Printer.hpp"

void NFA_Printer::PrintProcessmentTree(NaryTree<Transition*>* processmentTree)
{
// Get all crashed chains and all chain at the deepest level (bigger height value)
MyList<NaryTree_Node<Transition*>*> crashedsChains, finishedsChains;
crashedsChains = processmentTree->GetWithHeight(NFA_Machine::CRASH_STATE_HEIGHT);
finishedsChains = processmentTree->GetWithHeight(processmentTree->GetMaxHeight());

PrintProcessmentList(crashedsChains);
PrintProcessmentList(finishedsChains);
}

void NFA_Printer::PrintProcessmentList(MyList<NaryTree_Node<Transition*>*> node)
{
AlphabetSymbol* symbol;

for(int i = 0; i < node.Length(); i++)
{
NaryTree_Node<Transition*>* s = node.At(i);
MyList<Transition*> chainList = MyList<Transition*>();

while(s != nullptr)
{
chainList.Push(s->GetContent());
s = s->GetParent();
}

Transition* _transition = chainList.GetLast();
std::cout << _transition->GetDestinationState()->GetName();

for(int j = chainList.Length()-2; j >= 0; j--)
{
Transition* _transition = chainList.At(j);

symbol = _transition->GetTransitionSymbol();

if(symbol != nullptr)
std::cout << " -> " << symbol->GetValue() << " -> " ;

std::cout << _transition->GetDestinationState()->GetName();
}

// Print the status of the last state in the chain
if(chainList.At(0)->GetDestinationState()->IsAFinalState())
std::cout << " " << NFA_Printer::CHAIN_IS_ACCEPTED_SYMBOL;
else
std::cout << " " << NFA_Printer::CHAIN_IS_NOT_ACCEPTED_SYMBOL;

std::cout << "\n";
}
}
17 changes: 17 additions & 0 deletions src/NFA_Printer.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include <iostream>
#include "Transition.hpp"
#include "Utils/NaryTree.hpp"
#include "NFA_Machine.hpp"

namespace NFA_Printer
{
const char CHAIN_IS_ACCEPTED_SYMBOL = 'V';
const char CHAIN_IS_NOT_ACCEPTED_SYMBOL = 'X';
const std::string CHAIN_IS_ACCEPTED_MSG = "A cadeia processada eh aceita. \n";
const std::string CHAIN_IS_NOT_ACCEPTED_MSG = "A cadeia processada nao eh aceita. \n";

void PrintProcessmentTree(NaryTree<Transition*>* processmentTree);
void PrintProcessmentList(MyList<NaryTree_Node<Transition*>*> node);
}
23 changes: 22 additions & 1 deletion src/Utils/MyList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class MyList
int Length();
int GetIndexOf(T value);

T At(int pos);
T At(int pos);
T GetLast();
void SetAt(int pos, T value);

void PushOnBeginning(T value);
Expand Down Expand Up @@ -97,6 +98,26 @@ T MyList<T>::At(int pos)
return aux->getContent();
}

template <typename T>
T MyList<T>::GetLast()
{
if (IsEmpty() || head == nullptr)
{
return DEFAULT_RETURN;
}

Node<T> *last;
Node<T> *aux = head;

while(aux != nullptr)
{
last = aux;
aux = aux->getNextNode();
}

return last->getContent();
}

template <typename T>
void MyList<T>::SetAt(int pos, T value)
{
Expand Down
Loading

0 comments on commit 12ac655

Please sign in to comment.