automata de pila
TRANSCRIPT
![Page 1: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/1.jpg)
Lenguajes y Compiladores
Profesor: Carlos A. Ruiz De La Cruz Melo
Correo:[email protected]
Automata De Pila
![Page 2: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/2.jpg)
AUTOMATA DE PILA(AP)Definición
Autómata que extiende la potencia reconocedora de lenguajes del autómata finito añadiendo una memoria interna tipo pila (“contador de simbolos”).
El funcionamiento del AP se realiza de acuerdo con alguna forma de gramática.
Push(3)
3
Push(5)
53
Push(1)153
Pop
53
Pop
3
Push(2)
23
PILA
![Page 3: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/3.jpg)
AP
Definición formalSeptupla AP={ E, , p, , 0, P0, F }
E : conjunto finito de estados
: alfabeto del sistema
p : alfabeto de la pila
: función de transición
0 E : estado inicial
P0 p : estado inicial de la pila
F E : conjunto de estados finales
![Page 4: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/4.jpg)
APConfiguración de un AP
(i , w, a)
Donde:
i : estado actual del AP
w :Subcadena por analizar en el instante T
a :Contenido de la pila en el instante T
Observación:
w y a pueden ser la secuencia Si w = no hay nada mas por analizar Si a = la pila no tiene nada, esta vacía
![Page 5: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/5.jpg)
APMovimiento en un AP
Transición entre dos configuraciones
(i , aw, pa) (j , w, ua)
Movimiento válido si (j , u) (i , a, p) Donde:
i , j E a {}w * u p
![Page 6: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/6.jpg)
TIPOS DE AP Autómata de Pila determinista (APD) Cuando estando en una configuración se pasa solo a otra
configuración.
Autómata de Pila no determinista (APN) Cuando a partir de una configuración puede pasarse a una o
mas configuraciones
![Page 7: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/7.jpg)
SECUENCIA ACEPTADAConfiguración inicial (0 , w, P0) w
Configuración final (f , , a) f F a p*
Un AP acepta una secuencia w si puede llegarse de la configuración inicial a la configuración final:
(0 , w, P0) * (f , , a)
![Page 8: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/8.jpg)
LENGUAJE ACEPTADO
El lenguaje es aceptado por el AP cuando se llega al estado final que se denota por:
L(AP)={ w */ (0 , w, P0) * (f , , a), f F }
![Page 9: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/9.jpg)
Ejemplo 1
E = { 0 }
= { x, y }
p = { x }
0 E : estado inicial
x p : estado inicial de la pila
F = { 0 }
Construimos un AP :
AP={ E, , p, , 0, P0, F }
0
x, ; x
y, x ;
1.(0 , x, ; 0 , x )
2.(0 , y, x ; 0 , )
push
pop
![Page 10: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/10.jpg)
Ejemplo 1
(0 , xy, )
(0 , y, x)
(0 , , )
1
2
(0 , yxyx, x)
(0 , xyx, )
(0 , yx, x)
(0 , x, )
(0 , , x)
2
1
2
1
(0 , yxx, x)
(0 , xx, )
(0 , x, x)
(0 , , xx)
2
1
1
a) b) c)0
x, ; x
y, x ;
1.(0 , x, ; 0 , x )
2.(0 , y, x ; 0 , )
push
pop
![Page 11: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/11.jpg)
Ejemplo 1
(0 , xyyx, )
(0 , yyx, x)
(0 , yx, )
1
2
Secuencia que no pertenece al lenguaje
Observacion
No existe transición alguna para poder continuar y aceptar la secuencia como parte del lenguaje.
?
0
x, ; x
y, x ;
1.(0 , x, ; 0 , x )
2.(0 , y, x ; 0 , )
push
pop
![Page 12: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/12.jpg)
Ejemplo 1
Observación
El AP es un APD ya que para cada configuración solo existe una transición y nada mas que una hacia otra configuración.
autopila2
![Page 13: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/13.jpg)
class ARREGLO{ char vector[100]; char pila[100]; int i,tope; public: ARREGLO(char m[]){ tope=100;strcpy(pila,m);i=strlen(pila);} bool OCUPADO(){ if (i>=tope) return true; else return false; } bool VACIO(){ if(i<=0) return true; else return false; } void METER(char a){ char t[2]; t[0]=a;t[1]='\0'; strcat(pila,t); i=strlen(pila); } char SACAR(){ char a; int k=strlen(pila); a=pila[k-1]; pila[k-1]='\0'; i=strlen(pila); return a; }
bool reconocedor(char x[]){ strcpy(vector,x); int e=0; int j=0; char temp; while(j<strlen(vector)){ switch(e){ case 0: if(vector[j]=='x'){ METER('x'); } else { if(!VACIO()){ temp=SACAR(); if(vector[j]=='y' && temp=='x'){ ;} else e=3; } else e=3; } break; } j++; }//fin de while if(e==0) return true; else return false; } }; int main(){ char cadena[100]; char pila[100]; cout<<"\n INGRESE CADENA A RECONOCER: ";cin>>cadena; cout<<"\n INGRESE PILA : ";cin>>pila; if(!strcmp(pila,"v")) strcpy(pila,""); ARREGLO tira(pila); if(tira.reconocedor(cadena)) cout<<"\n verdadero"; else cout<<"\n falso"; getch();}
Ejemplo 1
Programa
![Page 14: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/14.jpg)
Ejemplo 2
Construir un AP que reconosca el siguiente lenguaje:
L(G)={ ={x, y} / w=xn w=yn donde n=2 }
Secuencias reconocidas por el lenguaje:
yy, xx
Secuencias no reconocidas:
x, y, yyxx, xyyx , xyyxx
![Page 15: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/15.jpg)
Ejemplo 2
E = { 0 }
= { x, y }
p = { x, y }
0 E : estado inicial
p : estado inicial de la pila
F = { 2 }
Construimos el AP :
AP={ E, , p, , 0, P0, F }
1.(0 , x, ; 1 , x )
2.(0 , y, ; 1 , y )
3.(1 , , x ; 1 , )
4.(1 , , y ; 1 , )
5.(1 , x, x ; 2 , )
6.(1 , y, y ; 2 , )
2
,x;
01
,y;
x, ;x
y,;y
x,x;
y,y;
push
pop
push
pop
pop
pop
![Page 16: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/16.jpg)
Ejemplo 2
(0 , xx, )
(1 , x, x)
(2 , , )
1
5
a)
En (a) y (b) el AP reconoce las secuencias, sin embargo no ocurre lo mismo en (c).
(0 , yy, )
(1 , y, y)
(2 , , )
2
6
b) (0 , xxyy, )
(1 , xyy, x)
(1 , xyy, )
1
3
c)
?
1.(0 , x, ; 1 , x )
2.(0 , y, ; 1 , y )
3.(1 , , x ; 1 , )
4.(1 , , y ; 1 , )
5.(1 , x, x ; 2 , )
6.(1 , y, y ; 2 , )
2
,x;
01
,y;
x, ;x
y,;y
x,x;
y,y;
![Page 17: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/17.jpg)
Ejemplo 2Observación
El AP es un APN
(0 , xx, )
(1 , x, x)
(2 , , )
1
5
(1 , x, )
3
Se llega a un estado de aceptación
No se puede continuar
2
,x;
01
,y;
x, ;x
y,;y
x,x;
y,y;
1.(0 , x, ; 1 , x )
2.(0 , y, ; 1 , y )
3.(1 , , x ; 1 , )
4.(1 , , y ; 1 , )
5.(1 , x, x ; 2 , )
6.(1 , y, y ; 2 , )
autopila
![Page 18: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/18.jpg)
class ARREGLO{ char vector[100]; char pila[100]; int i,tope; public: ARREGLO(char m[]){ tope=100;strcpy(pila,m); i=strlen(pila);} bool OCUPADO(){ if (i>=tope) return true; else return false; } bool VACIO(){ if(i<=0) return true; else return false; } void METER(char a){ char t[2]; t[0]=a;t[1]='\0'; strcat(pila,t); i=strlen(pila); } char SACAR(){ char a; int k=strlen(pila); a=pila[k-1]; pila[k-1]='\0'; i=strlen(pila); return a; }
bool reconocedor(char x[]){ strcpy(vector,x); int e=0; int j=0; char temp; while(j<strlen(vector) && e!=2){ switch(e){ case 0: if(vector[j]=='x' ){ e=1; METER('x'); } else if(vector[j]=='y' ){ e=1; METER('y'); } else e=3; break; case 1:if(!VACIO()){ temp=SACAR(); if( temp=='x'){ if(vector[j]=='x') e=2; else {e=1; j--;} } else if( temp=='y'){ if(vector[j]=='y')e=2; else {e=1; j--;} } else e=3; }else e=3; break; } j++; }//fin de while if(e==2 && (j==strlen(vector)) ) return true; else return false; } }; int main(){ char cadena[100]; char pila[100]; cout<<"\n INGRESE CADENA A RECONOCER: ";cin>>cadena; cout<<"\n INGRESE PILA : ";cin>>pila; if(!strcmp(pila,"v")) strcpy(pila,""); ARREGLO tira(pila); if(tira.reconocedor(cadena)) cout<<"\n verdadero"; else cout<<"\n falso"; getch();}
Ejemplo 2
Programa
![Page 19: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/19.jpg)
1.(0 , a, Z ; 0 , AZ )
2.(0 , a, A ; 0 , AA )
3.(0 , b, A ; 1 , )
4.(1 , b, A ; 1 , )
5.(1 , , Z ; 2 , )
Sea L = {a nb n : n>=0} sobre Σ= {a, b}.
Construir el automata de pila a partir del siguiente lenguaje
Ejemplo 3
2
b,A;
01
a,A;AA
a, Z;AZ
b,A; ,Z;
autopila3
push
push
pop
pop
pop
![Page 20: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/20.jpg)
2
b,A;
01
a,A;AA
a, Z;AZ
b,A; ,Z;
1.(0 , a, Z ; 0 , AZ )
2.(0 , a, A ; 0 , AA )
3.(0 , b, A ; 1 , )
4.(1 , b, A ; 1 , )
5.(1 , , Z ; 2 , )
push
push
pop
pop
pop
Ejemplo 3
201
a
201
b20
1
cadena pila estado
ab Z 0
cadena pila estado
b AZ 0
Cadena Pila Estado
Z 1
cadena pila estado
2
RECONOCER: ab
![Page 21: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/21.jpg)
class ARREGLO{ char vector[100]; char pila[100]; int i,tope; public: ARREGLO(char m[]){ tope=100;strcpy(pila,m); i=strlen(pila);} bool OCUPADO(){ if (i>=tope) return true; else return false; } bool VACIO(){ if(i<=0) return true; else return false; } void METER(char a){ char t[2]; t[0]=a;t[1]='\0'; strcat(pila,t); i=strlen(pila); } char SACAR(){ char a; int k=strlen(pila); a=pila[k-1]; pila[k-1]='\0'; i=strlen(pila); return a; }
bool reconocedor(char x[]){ strcpy(vector,x); int e=0; int j=0; char temp; while(j<=strlen(vector)){ switch(e){ case 0:if(!VACIO()){ temp=SACAR(); if(vector[j]=='a' && temp=='Z'){ e=0; METER('Z'); METER('A'); } else if(vector[j]=='a' && temp=='A' ){ e=0; METER('A');METER('A'); } else if(vector[j]=='b' && temp=='A' ) e=1; else e=3; }else e=3; break; case 1:if(!VACIO()){ temp=SACAR(); if( vector[j]=='b' && temp=='A') e=1; else if( vector[j]=='\0' && temp=='Z' ){ e=2;METER('Z'); } else e=3; }else e=3; break; } j++; }//fin de while if(e==2 ) return true; else return false; } }; int main(){ char cadena[100]; cout<<"\n INGRESE CADENA A RECONOCER: ";cin>>cadena; ARREGLO tira("Z"); if(tira.reconocedor(cadena)) cout<<"\n verdadero"; else cout<<"\n falso"; getch();}
Ejemplo 3
programa
![Page 22: Automata de Pila](https://reader033.vdocuments.site/reader033/viewer/2022061306/55148a90497959fd1d8b47b9/html5/thumbnails/22.jpg)
Ejercicios
Escriba el autómata de pila y su programa para los siguientes lenguajes
1.L={ancbm | n,m>0}.2.L={anbm | n m, n,m>0}.3.L={anbm | n m, n,m≥0}.4.L = {aibjck | i, j>0 y k=i+j}
El programa puede ser en java o C++