-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPDA.java
More file actions
168 lines (130 loc) · 4.56 KB
/
PDA.java
File metadata and controls
168 lines (130 loc) · 4.56 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package pda_et;
import java.util.ArrayList;
import javax.swing.JOptionPane;
/* Programa: PDA
Equipo:
- Guzmán Cruz José Antonio
- Bautista Sanchéz Georgina Elena
Carrera: Ing. en Sistemas Computacionales
Curso: Lenguajes y Automatas II
Catedratico: Roberto Hernandez Peréz
Fecha: 27 de febrero de 2022
*/
public class PDA {
static int pda = 0; //-- variable de inicio esta Q0
private ArrayList<Object> pila = new ArrayList(); //-- Se crea el objeto pila
public void palindromo(String cad) { // -- Valida si la cadena es un palindromo
String cad1, rev = "";
cad1 = cad.replaceAll("\\s+", "").toLowerCase();
int length = cad1.length();
for (int i = length - 1; i >= 0; i--)
rev = rev + cad1.charAt(i);
if (cad1.equals(rev))
System.out.println("\nLa cadena <<" + cad + ">> es un palindromo");
else
System.out.println("\nLa cadena <<" + cad + ">> no es un palindromo");
}
public void push(Object o) { //-- agrega valor a lapila
pila.add(o);
}
public Object pop() { //-- muestra el ultimo valor y lo elimina
if (!(pila.isEmpty())) {
Object o = pila.get(pila.size() - 1);
pila.remove(pila.size() - 1);
return o;
} else {
return null;
}
}
public Object peek() { //-- muestra el ultimo valor
if (!(pila.isEmpty())) {
return pila.get(pila.size() - 1);
} else {
return null;
}
}
public Boolean empty() { //-- la pila esta llena o vacia
return pila.isEmpty();
}
static PDA stc = new PDA(); //-- Llamada a metodos
static void Q0(char c) { //-- Entrada estado Q0
stc.push("#");
if (c == 'a' && stc.peek() == "#"){
stc.push("E");
pda = 1;
} else if (c == 'b' && stc.peek() == "#") {
stc.push("T");
pda = 2;
}
}
static void Q1(char c) { //-- Entrada estado Q1
if (c == 'a'&& stc.peek() == "E") {
stc.push("E");
pda = 2;
} else if (c == 'b' && stc.peek() == "E") {
stc.pop();
pda = 3;
}
}
static void Q2(char c) { //-- Entrada estado Q2
if (c == 'a' && stc.peek() == "T") {
stc.pop();
pda = 3;
} else if (c == 'b' && stc.peek() == "T") {
stc.push("T");
pda = 2;
}
}
static void Q3(char c) { //-- Entrada estado Q3
if (c == 'a' && stc.peek() == "T") {
stc.pop();
pda = 3;
} else if (c == 'b' && stc.peek() =="E") {
stc.pop();
pda = 3;
}else if (c == ' ' && stc.peek() =="#") {
stc.pop();
pda = 3;
}else {
pda = -1; //-- en los otros estados es para rectificar si no hay malos datos en la cadena
}
}
static int isAccepted(char str[]) { //-- Valida si la cadena es aceptada
for (int i = 0; i < str.length; i++) {
if (pda == 0)
Q0(str[i]);
else if (pda == 1)
Q1(str[i]);
else if (pda == 2)
Q2(str[i]);
else if (pda == 3)
Q3(str[i]);
else
return 0;
}
if (pda == 3) //-- Señala estado final
return 1;
else
return 0;
}
// Clase principal
public static void main(String[] args) {
System.out.println("\nPROGRAMA PDA\n");
System.out.println("L = { w | w is a palindrome of a's and b's}\n");
System.out.println("P = (Q,Σ,Γ,Sigma,q0,Z0,F)");
System.out.println("Q = {Q0,Q1,Q2,Q3}\nΣ = {a,b}\nΓ = {E,T}\nSigma\nq0\nZ0 = {#}\nF = {Q3}");
System.out.println("P = ({Q0,Q1,Q2,Q3},{a,b},{E,T},Sigma,{q0},Z0 = #,{Q3})\n");
String cadena = JOptionPane.showInputDialog(null, "Dame la cadena");
PDA pila = new PDA();
pila.palindromo(cadena);
char str[] = cadena.toCharArray();
if (isAccepted(str) == 0)
System.out.printf("La cadena <<" + cadena + ">> es válida\n");
else
System.out.printf("La cadena <<" + cadena + ">> no es válida | Intenta nuevamente\n");
System.out.print("\nPila:");
while (pila.empty() == false) {
System.out.print("["+pila.pop()+"]\n");
}
}
}