-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
113 lines (85 loc) · 3.61 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// I.S. Adolfo Solis Rosas
// 3/Abril/2018
// Maestria Ciencias de la Computación
// Automatas y Lenguajes Formales
// PROYECTO FINAL
/*
AUTOMATA BUILDER - CREADOR DE AUTOMATAS FINITOS
*/
// IMPORTS
var colors = require('colors');
var readlineSync = require('readline-sync');
console.log(colors.yellow("\n**********************************\n**\t\t\t\t**\n**\tAUTOMATA BUILDER\t**\n**\t\t\t\t**\n**********************************\n"))
class Automata {
constructor(name, estado, transitions) {
this.name = name;
this.estado = estado;
this.transitions = transitions;
}
}
var numStates = readlineSync.questionInt('Ingresa el número de estados del Automata: ')
var indexWhileStates = 0;
var automatasArray = [];
while (indexWhileStates < numStates) {
var nameState = readlineSync.question('\nIngresa el nombre del estado q' + indexWhileStates + ': ', {
defaultInput: 'q' + indexWhileStates
});
typeState = ['Initial', 'NonAccept', 'Final', 'Initial y Final'],
indexTypeState = readlineSync.keyInSelect(typeState, 'Define el tipo de estado: ');
var transitionsArray = [];
while (readlineSync.keyInYN('Agregar transición: ')) {
var toAutomata = readlineSync.questionInt("Transición del estado'" + nameState + "'al estado 'q': ");
var _transition = readlineSync.question("Transición: ");
transitionsArray.push({
toAutomata: toAutomata,
_transition: _transition
})
}
automatasArray.push(new Automata(nameState, typeState[indexTypeState], transitionsArray))
indexWhileStates++;
}
console.log(colors.underline.green("\nAutomata Creado Exitosamente!\n"));
typeState = ['Evaluar Cadena'],
indexTypeState = readlineSync.keyInSelect(typeState, 'Elige una opción: ');
while (typeState[indexTypeState] != undefined) {
var cadena = readlineSync.question('Ingresa la cadena a evaluar para el automata creado: ')
var automataAccepted = false;
var automataIndex = 0;
var automata = automatasArray[automataIndex];
console.log(colors.cyan("\nEvaluando: " + cadena + "\n"));
for (var i = 0; i < cadena.length; i++) {
var letra = cadena[i];
console.log(automata)
if (automata.transitions.length == 0)
automataAccepted = false;
for (j = 0; j < automata.transitions.length; j++) {
console.log(colors.green("Letra: " + letra + " -> transition: " + automata.transitions[j]._transition))
if (automata.transitions[j]._transition == letra) {
automataAccepted = true;
automataIndex = automata.transitions[j].toAutomata;
break;
} else {
automataAccepted = false;
}
}
automata = automatasArray[automataIndex];
console.log(colors.blue("\t |\n\t |\n\t |\n\t\\|/"))
if (!automataAccepted)
break;
}
if (automata.estado == "Final" && automataAccepted) {
console.log(colors.italic.underline.red("¡Cadena aceptada!"))
} else if (automata.estado == "Initial y Final" && automataAccepted) {
console.log(colors.italic.underline.red("¡Cadena aceptada!"))
} else {
automataAccepted = false;
console.log(colors.magenta("El estado no es final"))
}
if (automataAccepted)
console.log(colors.italic.underline.red("¡El automata acepta la Cadena: " + cadena))
else
console.log(colors.red("¡ERROR!"))
typeState = ['Evaluar Cadena'],
indexTypeState = readlineSync.keyInSelect(typeState, 'Elige una opción: ');
}
console.log(colors.yellow("Finalizó Programa..."));