Skip to content

Commit

Permalink
Merge branch 'phase1' into ANYCC-ci-build
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmedelsa3eed committed Dec 8, 2023
2 parents 3f72807 + 69d7445 commit f73f3e2
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
12 changes: 2 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.26)
cmake_minimum_required(VERSION 3.24)
project(anycc)

set(CMAKE_CXX_STANDARD 14)
Expand All @@ -7,22 +7,14 @@ include_directories(include)
include_directories(include/Lex)

add_executable(anycc
src/Lex/Input.cpp
src/Lex/InputReader.cpp
src/Lex/Lex.cpp
src/Lex/Rules.cpp
src/Lex/DFA.cpp
src/Lex/LexError.cpp
src/Lex/NFA.cpp
src/Lex/NFAGenerator.cpp
src/Lex/NFAState.cpp
src/Lex/Output.cpp
src/Lex/OutputReader.cpp
src/Lex/Regex.cpp
src/Lex/RegexGenerator.cpp
src/Lex/SymbolTable.cpp
src/Lex/Token.cpp
src/Lex/TransTable.cpp
src/anycc.cpp
src/Lex/Utilities.cpp
include/Lex/Operator.h
Expand All @@ -31,4 +23,4 @@ add_executable(anycc
include/Lex/FileReader.h
src/Lex/DeterministicTransitionDiagramCreator.cpp
src/Lex/TransitionDiagramMinimizer.cpp
)
)
2 changes: 2 additions & 0 deletions include/Lex/TransitionDiagram.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ class TransitionDiagram {
void print() const;
void clear();
void toDotFile(std::string file_name);
void toCSVFile(std::string file_name);
void toMDFile(std::string file_name);

private:
std::unordered_map<const NFAState*, std::unordered_map<char, std::vector<const NFAState*>>> table;
Expand Down
6 changes: 6 additions & 0 deletions src/Lex/Lex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,23 @@ void Lex::buildLex() {
std::cout << table->getEndStates().size() << "\n";
std::cout << table->getDeadStates().size() << "\n";
table->toDotFile("nfa.dot");
table->toCSVFile("nfa.csv");
table->toMDFile("nfa.md");
DeterministicTransitionDiagramCreator dfaCreator;
table = dfaCreator.subsetConstruction(table);
std::cout << "converted to dfa\n";
table->toDotFile("dfa.dot");
table->toCSVFile("dfa.csv");
table->toMDFile("dfa.md");
// table->print();
std::cout << table->getStates().size() << "\n";
std::cout << table->getEndStates().size() << "\n";
std::cout << table->getDeadStates().size() << "\n";
TransitionDiagramMinimizer minimizer;
table = minimizer.minimize(table);
table->toDotFile("min_dfa.dot");
table->toCSVFile("min_dfa.csv");
table->toMDFile("min_dfa.md");
std::cout << "minimized dfa\n";
// table->print();
std::cout << table->getStates().size() << "\n";
Expand Down
79 changes: 73 additions & 6 deletions src/Lex/TransitionDiagram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,97 @@ void TransitionDiagram::toDotFile(std::string file_name) {
return;
}

file << "digraph NFA {" << std::endl;
file << "digraph {" << std::endl;
file << "\trankdir=LR;" << std::endl; // Setting direction to Left-to-Right

for (auto& fromState : table) {
if(this->dead_states.find(fromState.first) != this->dead_states.end()) continue;
for (const auto& transition : fromState.second) {
for (auto& transition : fromState.second) {
for (auto& toState : transition.second) {
if(this->dead_states.find(toState) != this->dead_states.end()) continue;
file << "\t" << fromState.first->getStateId() << " -> " << toState->getStateId() << " [label=\"" << transition.first << "\"];" << std::endl;
}
}
}

// Mark specific nodes as end states and set their colors
file << "\t";
for (const auto& state : this->end_states) {
file << state->getStateId() << " [style=filled fillcolor=grey color=blue]; ";
for (auto& state : this->end_states) {
file << state->getStateId() << "[peripheries=2 style=filled fillcolor=yellow color=black]; ";
}
file << this->startState->getStateId() << " [style=filled fillcolor=green color=black];";
file << this->startState->getStateId() << " [arrowhead=normal style=filled fillcolor=green color=black];";
file << std::endl;
file << "}" << std::endl;
file.close();
}

void TransitionDiagram::toCSVFile(std::string file_name) {
std::ofstream file(file_name);
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return;
}

// Header row: inputs
file << ",";
for (const auto& input : table.begin()->second) {
file << "\"" << input.first << "\",";
}
file << std::endl;

// Data rows: states and transitions
for (auto& stateEntry : table) {
if(this->dead_states.find(stateEntry.first) != this->dead_states.end()) continue;
file << stateEntry.first->getStateId() << ",";
for (auto& input : stateEntry.second) {
auto& nextStates = input.second;
for (const auto& nextState : nextStates) {
if(this->dead_states.find(nextState) != this->dead_states.end()) continue;
file << nextState->getStateId();
}
file << ",";
}
file << std::endl;
}

file.close();
}

void TransitionDiagram::toMDFile(std::string file_name) {
std::ofstream file(file_name);
if (!file.is_open()) {
std::cerr << "Error opening file!" << std::endl;
return;
}

file << "| States |";
for (const auto& input : table.begin()->second) {
file << " " << input.first << " |";
}
file << std::endl;

file << "| --- |";
for (size_t i = 0; i < table.begin()->second.size(); ++i) {
file << " --- |";
}
file << std::endl;

for (const auto& stateEntry : table) {
if(this->dead_states.find(stateEntry.first) != this->dead_states.end()) continue;
file << "| " << stateEntry.first->getStateId() << " |";
for (const auto& input : stateEntry.second) {
const auto& nextStates = input.second;
for (const auto& nextState : nextStates) {
if(this->dead_states.find(nextState) != this->dead_states.end()) continue;
file << nextState->getStateId();
}
file << "|";
}
file << std::endl;
}

file.close();
}

void TransitionDiagram::fillTable(const NFAState* state, std::vector<const NFAState*> end_states, std::vector<std::string> tokens, std::unordered_map<const NFAState*, std::string> states_tokens_map, bool new_fill) {
this->startState = state;
this->tokens = tokens;
Expand Down

0 comments on commit f73f3e2

Please sign in to comment.