Skip to content

Commit

Permalink
agregue programas de ejemplo
Browse files Browse the repository at this point in the history
  • Loading branch information
larchadrg committed Apr 14, 2024
1 parent 7ecee80 commit 5fb0d41
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 7 deletions.
4 changes: 3 additions & 1 deletion TP/automata/ndfa/NotDeterministicFiniteAutomata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ set<int> NotDeterministicFiniteAutomata :: calculateDelta(pair<int,int> key) {

set<int> NotDeterministicFiniteAutomata :: calculateWaysToGo(int from, int destination) {
set<int> ret;
for(int letter : this->E) {
set<int> symbols = this->E;
symbols.insert(LAMBDA);
for(int letter : symbols) {
pair<int,int> pair;
pair.first = from;
pair.second = letter;
Expand Down
8 changes: 8 additions & 0 deletions TP/ejemplos_documentados/automata1.dot
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
digraph{
inic[shape=point];inic -> 3;
3 -> 3 [label = "0,1"];
3 -> 4 [label = "0"];
4 -> 3 [label = "_"];
4 -> 5 [label = "1"];
5[shape=doublecircle];
}
31 changes: 31 additions & 0 deletions TP/ejemplos_documentados/ex1_file_creation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* En este programa se probará la función de cargar un automata
* por medio de la clase NotDeterministicFiniteAutomata y guardarlo en un archivo.
* El automata de ejemplo y las instrucciones de compilación está descrito en ex1_readme.txt
* El archivo de salida será automata1.dot
*/

#include <bits/stdc++.h>
#include "../automata/ndfa/NotDeterministicFiniteAutomata.h"
#include "../parser/Parser.h"

int main() {
const int lambda = -1;
string filename = "TP/ejemplos_documentados/automata1.dot";

NotDeterministicFiniteAutomata ndfa;
ndfa.setAlphabet({0,1}); //a, b
ndfa.setStates({3,4,5});
ndfa.setInitialState(3);
ndfa.addPath(3,0,4);
ndfa.addPath(3,0,3);
ndfa.addPath(3,1,3);
ndfa.addPath(4,lambda,3);
ndfa.addPath(4,1,5);
ndfa.addFinalState(5);

string content = Parser::ndfaToString(ndfa);
Parser::writeToFile(filename, content);
return 0;
}

60 changes: 60 additions & 0 deletions TP/ejemplos_documentados/ex1_file_read_and_string_validation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* En este programa se probará la función de cargar un automata
* por medio de un archivo de texto y comprobará si algunas cadenas pertenecen al lenguaje.
* El automata de ejemplo está descrito en ex1_readme.txt
* El archivo de entrada será automata1.dot
*/

#include <bits/stdc++.h>
#include "../automata/dfa/DeterministicFiniteAutomata.h"
#include "../automata/ndfa/NotDeterministicFiniteAutomata.h"
#include "../automata/convertion/ConvertionOfAutomatas.h"
#include "../parser/Parser.h"
using namespace std;

const string filename = "TP/ejemplos_documentados/automata1.dot";

int main() {
ifstream file(filename); // Abrir el archivo
Parser parser = *new Parser();

if (!file.is_open()) { // Verificar si el archivo se abrió correctamente
cerr << "No se pudo abrir el archivo." << endl;
return -1;
}

string line;

if (!getline(file, line)) { // Leer la primera línea
cerr << "No se pudo leer la primera línea." << endl;
return -1;
}
if(!Parser::validateFirstLine(line)){
cerr << "Formato invalido del archivo." << endl;
return -1;
}

while (getline(file, line)) {
parser.fileManagement(line);
}

file.close();
cout << "Automata leido correcamente";

NotDeterministicFiniteAutomata nda = parser.getNDA();
ConvertionOfAutomatas conversor;
conversor.setNDFA(nda);
DeterministicFiniteAutomata dfa;
conversor.setDFA(dfa);
conversor.convertFromNDFA();
dfa = conversor.getDFA();

vector<string> strings = {"001", "01", "1101101", "0001000"};
for(string s: strings){
if(dfa.checkValidString(s)){
cout << "La cadena " << s << " es aceptada por el automata." << endl;
} else {
cout << "La cadena " << s << " no es aceptada por el automata." << endl;
}
}
}
30 changes: 30 additions & 0 deletions TP/ejemplos_documentados/ex1_readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Se probará la entrada, salida y aceptación de cadenas con el siguiente
automata finito no deterministico (AFND):

A = < K, E, D, q0, F >
donde:
K = {3, 4, 5}
E = {0, 1}
D = {
d(3,0) = 4
d(3,0) = 3
d(3,1) = 3
d(4,_) = 3
d(4,1) = 5
}
q0 = 0
F = {5}
El automata lee expresiones regulares de la forma ((0|1)*0)+1.
Ejemplos de cadenas que acepta son:
- 001
- 01
- 1101101

----------------------------------------------
COMPILACIÓN
---------------------------------------------
(1) Para compilar ex1_file_creation.cpp, desde la carpeta del repositorio
g++ TP/ejemplos_documentados/ex1_file_creation.cpp TP/automata/dfa/DeterministicFiniteAutomata.cpp TP/automata/ndfa/NotDeterministicFiniteAutomata.cpp TP/parser/Parser.cpp TP/auxiliarmethods/CollectionsOperators.cpp

(2) Para compilar ex1_file_read_and_string_validation.cpp, desde el mismo directorio
g++ TP/ejemplos_documentados/ex1_file_read_and_string_validation.cpp TP/automata/dfa/DeterministicFiniteAutomata.cpp TP/automata/ndfa/NotDeterministicFiniteAutomata.cpp TP/parser/Parser.cpp TP/auxiliarmethods/CollectionsOperators.cpp TP/automata/convertion/ConvertionOfAutomatas.cpp
14 changes: 9 additions & 5 deletions TP/parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,25 @@ void Parser :: toStringFinalStateDFA(DeterministicFiniteAutomata dfa, string& re
ret.pop_back();
}
string Parser::ndfaToString(NotDeterministicFiniteAutomata ndfa) {
string ret = "digraph{\ninic[shape=point];\ninic -> ";
string ret = "digraph{\ninic[shape=point];inic -> ";
ret += to_string(ndfa.getInitialState()) + ";\n";
toStringStatesNDFA(ndfa, ret);
NDFATransitionsToString(ndfa, ret);
toStringFinalStateNDFA(ndfa, ret);
ret += "\n}";
return ret;
}
void Parser::toStringStatesNDFA(NotDeterministicFiniteAutomata ndfa, string& ret) {
void Parser::NDFATransitionsToString(NotDeterministicFiniteAutomata ndfa, string& ret) {
const int LAMBDA = -1;
for(int number : ndfa.getSates()) {
for(int number2 : ndfa.getSates()) {
set<int> label = ndfa.calculateWaysToGo(number,number2);
if(!label.empty()) {
ret += to_string(number) + " -> " + to_string(number2) + " [label = \"";
for(auto elem : label)
ret += to_string(elem) + ",";
for(int elem : label){
if(elem == LAMBDA){
ret += ("_,");
} else ret += to_string(elem) + ",";
}
ret.pop_back();
ret += "\"];\n";
}
Expand Down
2 changes: 1 addition & 1 deletion TP/parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Parser {
static bool checkDelta(const string& line);
NotDeterministicFiniteAutomata nda;
bool readLastLine;
static void toStringStatesNDFA(NotDeterministicFiniteAutomata ndfa, string& ret);
static void NDFATransitionsToString(NotDeterministicFiniteAutomata ndfa, string& ret);
static void toStringFinalStateNDFA(NotDeterministicFiniteAutomata ndfa, string& ret);
static void toStringStatesDFA(DeterministicFiniteAutomata dfa, string& ret);
static void toStringFinalStateDFA(DeterministicFiniteAutomata dfa, string& ret);
Expand Down

0 comments on commit 5fb0d41

Please sign in to comment.