12.05.2013 Views

EJERCICIOS DE LA SECCIÓN 1.1 1.- Diseñe un diagrama de ...

EJERCICIOS DE LA SECCIÓN 1.1 1.- Diseñe un diagrama de ...

EJERCICIOS DE LA SECCIÓN 1.1 1.- Diseñe un diagrama de ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> <strong><strong>1.</strong>1</strong><br />

<strong>1.</strong>- <strong>Diseñe</strong> <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones para reconocer expresiones aritméticas <strong>de</strong> longitud arbitraria que compren<strong>de</strong>n enteros<br />

positivos separados por signos <strong>de</strong> suma, resta, multiplicación o división.<br />

Solución:<br />

operador<br />

1<br />

dígito<br />

letra<br />

dígito<br />

2<br />

operador = ‘+’, ‘-‘, ‘*’, ó ‘/’<br />

operador<br />

letra<br />

3<br />

error<br />

dígito<br />

operador<br />

letra letra<br />

4<br />

dígito<br />

operador<br />

Comentarios:<br />

1º No pue<strong>de</strong> comenzar recibiendo <strong>un</strong> operador, por dos razones:<br />

1ª Está especificado que los números son positivos.<br />

2ª Si se empieza admitiendo <strong>un</strong> operador, es estaría admitiendo que la ca<strong>de</strong>na empezara con * ó /.<br />

2º En el <strong>diagrama</strong> dado se llega al estado <strong>de</strong> aceptación tras leer dos y sólo dos números enteros <strong>de</strong> longitud arbitraria.<br />

Si el número <strong>de</strong> operaciones pudiera ser mayor que <strong>un</strong>a, tendríamos <strong>un</strong> arco <strong>de</strong>l estado 4 al 3 con etiqueta operador:<br />

operador<br />

1<br />

dígito<br />

letra<br />

dígito<br />

2<br />

operador = ‘+’, ‘-‘, ‘*’, ó ‘/’<br />

operador dígito<br />

letra<br />

3<br />

letra<br />

error<br />

operador<br />

operador<br />

letra<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 1<br />

4<br />

dígito<br />

operador<br />

José Garzía


2.- Escriba <strong>un</strong> analizador léxico directamente a partir <strong>de</strong>l siguiente <strong>diagrama</strong> <strong>de</strong> transiciones:<br />

Solución:<br />

1<br />

dígito<br />

letra<br />

dígito<br />

<br />

2<br />

5<br />

3<br />

letra<br />

=<br />

dígito<br />

Estado := 1;<br />

Leer el siguiente símbolo <strong>de</strong> entrada<br />

WHILE El último símbolo leído no sea el <strong>de</strong> Fin <strong>de</strong> Ca<strong>de</strong>na DO<br />

CASE Estado OF<br />

1: CASE Símbolo actual OF<br />

Letra : Estado := 2 ;<br />

‘:’ : Estado := 3 ;<br />

Dígito : Estado := 5 ;<br />

ELSE Salir a rutina <strong>de</strong> error ;<br />

END (* <strong>de</strong>l CASE interno *)<br />

2: CASE Símbolo actual OF<br />

Letra : Estado := 2 ;<br />

Dígito : Estado := 2;<br />

ELSE Salir a rutina <strong>de</strong> error ;<br />

END (* <strong>de</strong>l CASE interno *)<br />

4<br />

3: IF Símbolo actual ‘=‘ THEN Salir a rutina <strong>de</strong> error END ;<br />

4: IF Símbolo actual NIL THEN Salir a rutina <strong>de</strong> error END ;<br />

5: IF Símbolo actual dígito THEN Salir a rutina <strong>de</strong> error END ;<br />

ELSE Salir a rutina <strong>de</strong> error ;<br />

END (* <strong>de</strong>l CASE externo *)<br />

END (* <strong>de</strong>l WHILE *)<br />

IF ( Estado=1 OR Estado=3 ) THEN Salir a rutina <strong>de</strong> error END ;<br />

3.- Construya <strong>un</strong>a tabla <strong>de</strong> transiciones a partir <strong>de</strong>l <strong>diagrama</strong> <strong>de</strong>l ejercicio anterior y escriba <strong>un</strong> analizador léxico basado en la tabla.<br />

Solución:<br />

TAB<strong>LA</strong> <strong>DE</strong> TRANSICIONES<br />

ANALIZADOR LÉXICO<br />

Símbolo<br />

letra dígito ‘:’ ‘=‘ FDC<br />

Estado<br />

1 2 5 3 Error Error<br />

2 2 2 Error Error Aceptar<br />

3 Error Error Error 4 Error<br />

4 Error Error Error Error Aceptar<br />

5 Error 5 Error Error Aceptar<br />

Estado := 1 ;<br />

REPEAT<br />

Leer el siguiente símbolo <strong>de</strong> entrada ;<br />

CASE Símbolo OF<br />

0..9 : Entrada := dígito ;<br />

a..z, A..Z : Entrada := letra ;<br />

‘:’ : Entrada := ‘:’ ;<br />

‘=’ : Entrada := ‘=’ ;<br />

Marca <strong>de</strong> fin <strong>de</strong> ca<strong>de</strong>na : Entrada := FDC ;<br />

ELSE Salir a rutina <strong>de</strong> error ;<br />

END (* <strong>de</strong>l CASE *)<br />

Estado := Tabla( Estado, Entrada ) ;<br />

IF Estado=error THEN Salir a rutina <strong>de</strong> error END ;<br />

UNTIL Estado=aceptar ;<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 2


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

4.- Muestre que se pue<strong>de</strong> modificar <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones que contiene <strong>un</strong> arco rotulado por <strong>un</strong>a ca<strong>de</strong>na <strong>de</strong> símbolos <strong>de</strong><br />

longitud dos o más (lo cual significa que para recorrer el arco se requiere la ca<strong>de</strong>na completa y no <strong>un</strong> sólo símbolo) para que<br />

contenga sólo arcos rotulados con símbolos simples, pero <strong>de</strong> manera que siga aceptando las mismas ca<strong>de</strong>nas que antes.<br />

Solución:<br />

Inicialmente:<br />

I<br />

x1 x2 ... xn-1 xn<br />

Pero si añadimos nuevos estados intermedios <strong>de</strong> la forma:<br />

I<br />

x1<br />

S2<br />

x2<br />

S3<br />

Es <strong>de</strong>cir, <strong>de</strong> forma que:<br />

Si estando en el estado I recibe la entrada x1, cambia al estado S2.<br />

Si estando en el estado Si recibe la entrada xi, cambia al estado Si+1 (I=2,3, ..., n-1).<br />

Si estando en el estado Sn recibe la entrada xn, cambia al estado F.<br />

Entonces, ambos autómatas tienen idéntica respuesta ante la ca<strong>de</strong>na dada.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 3<br />

...<br />

xn-1<br />

Sn<br />

xn<br />

F<br />

F<br />

José Garzía


<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> <strong>1.</strong>2<br />

<strong>1.</strong>- Describa las ca<strong>de</strong>nas que acepta el autómata finito <strong>de</strong>terminista representado en el siguiente <strong>diagrama</strong> <strong>de</strong> transiciones:<br />

Solución:<br />

letra letra<br />

dígito dígito<br />

La expresión regular es: digito* letra (digito letra)*<br />

La <strong>de</strong>scripción es: ca<strong>de</strong>nas formadas por cero o más dígitos y al menos <strong>un</strong>a letra, estando esta (o estas) en cualquier posición.<br />

2.- Modifique el siguiente esqueleto <strong>de</strong> <strong>diagrama</strong> <strong>de</strong> transiciones <strong>de</strong> manera que esté completamente <strong>de</strong>finido y acepte las mismas<br />

ca<strong>de</strong>nas que antes.<br />

b a b<br />

a b<br />

Solución:<br />

b a b<br />

a<br />

a b a<br />

error<br />

a b<br />

3.- I<strong>de</strong>ntifique otro autómata finito <strong>de</strong>terminista <strong>de</strong> la vida diaria y dibuje su <strong>diagrama</strong> <strong>de</strong> transiciones.<br />

Solución:<br />

Un bolígrafo. Las entradas posibles al sistema son: x1=pulsar ó x2= no pulsar. La respuesta ante <strong>un</strong>a misma entrada no siempre es la<br />

misma. Depen<strong>de</strong> también <strong>de</strong> la historia <strong>de</strong> las entradas anteriores. Dicha historia <strong>de</strong> entradas anteriores se resume diciendo que el<br />

sistema está en <strong>un</strong> estado dado. Todo lo que pue<strong>de</strong> pasar en la vida útil <strong>de</strong> <strong>un</strong> bolígrafo es que su p<strong>un</strong>ta esté <strong>de</strong>ntro o esté fuera. La<br />

respuesta <strong>de</strong>pen<strong>de</strong> <strong>de</strong> la entrada aplicada y <strong>de</strong> si está <strong>de</strong>ntro o fuera. A<strong>de</strong>más, en la <strong>de</strong>finición <strong>de</strong> autómata finito <strong>de</strong>be haber <strong>un</strong> estado<br />

inicial. Po<strong>de</strong>mos elegir <strong>un</strong> bolígrafo que inicialmente tuviera la p<strong>un</strong>ta <strong>de</strong>ntro.<br />

entrada x1 =no pulsar x2 =pulsar<br />

estado<br />

s1=p<strong>un</strong>ta <strong>de</strong>ntro y1 = no se mueve y2 = sale<br />

s2 =p<strong>un</strong>ta fuera y1 = no se mueve y3 = entra<br />

no pulsar no se mueve pulsar sale no pulsar no se mueve<br />

p<strong>un</strong>ta <strong>de</strong>ntro<br />

pulsar entra<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 4<br />

b<br />

p<strong>un</strong>ta fuera


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> <strong>1.</strong>3<br />

<strong>1.</strong>- ¿Cómo pue<strong>de</strong> modificarse el autómata finito <strong>de</strong>terminista M=(S, , , , F) para obtener <strong>un</strong>a máquina que acepte el lenguaje *-<br />

L(M)?. Obsérvese que, si esto es posible, entonces, el complemento <strong>de</strong> <strong>un</strong> lenguaje regular respecto a * también es regular.<br />

Solución:<br />

Convirtiendo los estados <strong>de</strong> aceptación en estados <strong>de</strong> no aceptación; y viceversa, las ca<strong>de</strong>nas que antes eran aceptables ya no lo son;<br />

y viceversa.<br />

Francesc Xavier Torres aporta la expresión matemática:<br />

Sea M = (S,, , , F), entonces el M’ tal que L(M’) = *-L(M) es M’= (S, , , , S-F)<br />

Si el autómata <strong>de</strong>biera tener por lo menos <strong>un</strong> estado <strong>de</strong> aceptación (página 29 <strong>de</strong>l libro <strong>de</strong> texto) <strong>de</strong>beríamos contemplar esta<br />

excepción:<br />

Excepción: Cuando el autómata tiene <strong>un</strong> único estado.<br />

Figura <strong><strong>1.</strong>1</strong>4 a), autómata que acepta cualquier ca<strong>de</strong>na:<br />

x1<br />

xn<br />

Figura <strong><strong>1.</strong>1</strong>4 b), autómata que no acepta ning<strong>un</strong>a ca<strong>de</strong>na:<br />

x1<br />

xn<br />

x1<br />

xn<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 5<br />

José Garzía


2.- Elabore <strong>un</strong>a lista <strong>de</strong> los autómatas finitos <strong>de</strong>terministas basados en el alfabeto {x, y} que tengan <strong>un</strong> estado; y luego los que<br />

tengan dos estados.<br />

Solución:<br />

Recor<strong>de</strong>mos que <strong>un</strong> autómata finito <strong>de</strong>terminista cumple:<br />

- Existe <strong>un</strong> estado inicial.<br />

- De cada estado parte <strong>un</strong> arco (y sólo <strong>un</strong>o) por cada <strong>un</strong>a <strong>de</strong> las entradas posibles.<br />

A<strong>de</strong>más, contra los indicado el la página 29 <strong>de</strong>l texto base, admitiremos que <strong>un</strong> autómata pue<strong>de</strong> no tener ningún estado <strong>de</strong> aceptación.<br />

2.a) Autómatas con <strong>un</strong> sólo estado. Dos posibilida<strong>de</strong>s.<br />

01 x<br />

y<br />

02 x<br />

y<br />

El primero es el que acepta cualquier ca<strong>de</strong>na (figura <strong><strong>1.</strong>1</strong>4a).<br />

El seg<strong>un</strong>do es el que no acepta ning<strong>un</strong>a ca<strong>de</strong>na. Es posible gracias a que hemos admitido que el conj<strong>un</strong>to <strong>de</strong> estados <strong>de</strong> aceptación<br />

pue<strong>de</strong> ser vacío. Si el autómata <strong>de</strong>biera tener siempre al menos <strong>un</strong> estado <strong>de</strong> aceptación, para analizar el lenguaje vacío se <strong>de</strong>bería<br />

utilizar el <strong>diagrama</strong> <strong>de</strong> la figura <strong><strong>1.</strong>1</strong>4b), el cual ya no es <strong>de</strong> <strong>un</strong> solo estado:<br />

x x<br />

1 2<br />

y y<br />

2.b) Autómatas con dos estados.<br />

Recor<strong>de</strong>mos que al ser <strong>de</strong>terminista, en cada estado <strong>de</strong>be haber <strong>un</strong> arco y sólo <strong>un</strong> arco para cada símbolo <strong>de</strong> alfabeto. El<br />

procedimiento para contar los autómatas es:<br />

actuales<br />

posibles<br />

estados <strong>de</strong> Nº<br />

Nº <strong>de</strong> autómatas Nº <strong>de</strong> arcos posibles en cada estado<br />

Como para <strong>un</strong> mismo símbolo (en cada estado) <strong>de</strong>be haber <strong>un</strong> único arco, el número <strong>de</strong> arcos posibles en cada estado es igual al<br />

número <strong>de</strong> arcos diferentes en cada estado.<br />

Nº <strong>de</strong> arcos diferentes N Nº<br />

<strong>de</strong> símbolos <strong>de</strong> alfabetoNº<br />

<strong>de</strong> estados siguientes posibles<br />

A<br />

por tanto:<br />

<br />

Nº <strong>de</strong> estados posibles actuales<br />

Nº <strong>de</strong> símbolos <strong>de</strong> alfabeto Nº <strong>de</strong> estados posibles siguientes<br />

2 22 16<br />

Nº <strong>de</strong> autómatas <br />

<br />

Nº x <strong>de</strong>l estado inicial y <strong>de</strong>l estado inicial x <strong>de</strong>l estado no inicial Y <strong>de</strong>l estado no inicial<br />

1<br />

2<br />

3<br />

4<br />

5<br />

6<br />

7<br />

8<br />

9<br />

10<br />

11<br />

12<br />

13<br />

14<br />

15<br />

16<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 6<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado no inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial<br />

Al estado inicial


2.b.i)<br />

01<br />

05<br />

09<br />

1<br />

1<br />

1<br />

x<br />

y<br />

x<br />

y<br />

x<br />

y<br />

x<br />

x<br />

y<br />

2<br />

2<br />

2<br />

x<br />

y<br />

y<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 7<br />

INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

y<br />

1<br />

1<br />

1<br />

y<br />

y<br />

x<br />

x<br />

x<br />

y<br />

x<br />

x<br />

2<br />

x<br />

y<br />

2<br />

y<br />

2<br />

13 x<br />

1<br />

y<br />

x<br />

2<br />

14<br />

1<br />

x<br />

y<br />

x<br />

2<br />

15 x<br />

1 y<br />

x<br />

2<br />

16<br />

1<br />

x<br />

y<br />

x<br />

2<br />

y<br />

y<br />

y<br />

y<br />

2.b.ii)<br />

Hay otras 16 combinaciones, con la única variación respecto a las <strong>de</strong> 2.b.i) <strong>de</strong> consi<strong>de</strong>rar como estado <strong>de</strong> aceptación sólo el <strong>1.</strong><br />

2.b.iii)<br />

17<br />

21<br />

25<br />

29<br />

1<br />

1<br />

x x<br />

x x<br />

y<br />

1<br />

y y<br />

x x<br />

y y<br />

x x<br />

1<br />

y<br />

y<br />

y<br />

2<br />

2<br />

2<br />

2<br />

02<br />

06<br />

10<br />

18<br />

22<br />

26<br />

30<br />

y<br />

1<br />

1<br />

1<br />

1<br />

y<br />

y<br />

y<br />

x<br />

x x<br />

x<br />

x<br />

y<br />

y<br />

x<br />

x<br />

2<br />

y<br />

y<br />

x<br />

2<br />

2<br />

2<br />

03<br />

07<br />

11<br />

19<br />

23<br />

27<br />

31<br />

x<br />

x<br />

1<br />

1<br />

1<br />

x<br />

1<br />

1<br />

1<br />

y<br />

y<br />

y<br />

y<br />

x<br />

x<br />

x<br />

2<br />

y<br />

2<br />

2<br />

y<br />

x x<br />

x x<br />

x x<br />

1<br />

x x<br />

y<br />

y<br />

y<br />

y<br />

y<br />

y<br />

y<br />

y<br />

2<br />

2<br />

2<br />

2<br />

04<br />

08<br />

12<br />

20<br />

24<br />

28<br />

32<br />

1<br />

1<br />

1<br />

x<br />

y<br />

x<br />

y<br />

x<br />

y<br />

1<br />

1<br />

1<br />

1<br />

x<br />

y<br />

x<br />

y<br />

x<br />

y<br />

x<br />

x<br />

x<br />

2<br />

y<br />

2<br />

y<br />

2<br />

y<br />

x x<br />

y<br />

x<br />

x<br />

y<br />

x<br />

y<br />

2<br />

y<br />

2<br />

2<br />

y<br />

2<br />

José Garzía


Hay otras 16 combinaciones, con la única variación respecto a las <strong>de</strong> 2.b.i) <strong>de</strong> consi<strong>de</strong>rar como estados <strong>de</strong> aceptación el 1 y el 2.<br />

33<br />

37<br />

41<br />

1 2<br />

y y<br />

2<br />

1<br />

2<br />

y y<br />

45 x x 46<br />

x<br />

x 47 x x<br />

48<br />

x<br />

x<br />

1 y 2<br />

1 y 2<br />

1 y 2<br />

1 y 2<br />

2.b.iv)<br />

y<br />

y<br />

y<br />

y<br />

Hay otras 16 combinaciones, con la única variación respecto a las <strong>de</strong> 2.b.i) <strong>de</strong> no consi<strong>de</strong>rar como estados <strong>de</strong> aceptación ni el 1 ni el 2.<br />

Estas 16 no existirían si en el autómata por lo menos <strong>un</strong>o fuera <strong>de</strong> aceptación.<br />

Comentarios:<br />

1<br />

x x<br />

x x<br />

1 2<br />

y y<br />

x x<br />

y<br />

y<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 8<br />

34<br />

38<br />

42<br />

y<br />

1<br />

x x<br />

x<br />

x<br />

1<br />

2<br />

y y<br />

x<br />

y<br />

x<br />

2<br />

35<br />

39<br />

43<br />

1<br />

x x<br />

1<br />

x x<br />

1<br />

x x<br />

y<br />

y<br />

y<br />

y<br />

2<br />

y<br />

2<br />

2<br />

y<br />

36<br />

40<br />

44<br />

1<br />

1<br />

1<br />

x<br />

y<br />

x<br />

y<br />

x x<br />

Posible duda: ¿No serían autómatas distintos, si el estado inicial fuera el 2, es <strong>de</strong>cir 96 en total (puesto que no se habla <strong>de</strong><br />

autómatas como reconocedores <strong>de</strong> lenguajes, <strong>de</strong> forma que son distintos a<strong>un</strong>que reconozcan el mismo lenguaje)? Si<br />

incluimos la variación en la que estado inicial es el 2, el autómata obtenido no es nuevo. Por ejemplo, partiendo <strong>de</strong>l autómata<br />

Nº 02:<br />

La x <strong>de</strong>l estado inicial va al estado no inicial<br />

La y <strong>de</strong>l estado inicial va al estado inicial<br />

La x <strong>de</strong>l estado no inicial va al estado no inicial<br />

La y <strong>de</strong>l estado no inicial va al estado no inicial<br />

Obtenemos este autómata:<br />

En el que:<br />

x x<br />

1 2<br />

y y<br />

La x <strong>de</strong>l estado inicial va al estado inicial<br />

La y <strong>de</strong>l estado inicial va al estado inicial<br />

La x <strong>de</strong>l estado no inicial va al estado inicial<br />

La y <strong>de</strong>l estado no inicial va al estado no inicial<br />

02<br />

y<br />

x<br />

x<br />

2<br />

y<br />

2<br />

y<br />

2<br />

y<br />

x x<br />

1 2<br />

y y<br />

El cual es el mismo (a<strong>un</strong>que los estados tengan nombres diferentes) que otro <strong>de</strong>l seg<strong>un</strong>do grupo <strong>de</strong> 16; en concreto el 21:<br />

21 x x<br />

1<br />

2<br />

y y<br />

Tengamos en cuenta que cambiar <strong>de</strong> nombre los estados no hace que los autómatas sean diferentes. De no ser así,<br />

podríamos llamarlos<br />

x x<br />

a<br />

y y<br />

a, p, r, Bieito, Xosé, Manel, Xoan, etc, y habría infinitos autómatas.<br />

p


2.b.v)<br />

INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

También po<strong>de</strong>mos tener en cuenta <strong>un</strong> estado <strong>de</strong> error. ¿Acaso el estado <strong>de</strong> captación global no es <strong>un</strong> estado posible?. Pues claro que<br />

sí. No se pue<strong>de</strong> tener en cuenta en los <strong>diagrama</strong>s <strong>de</strong> dos estados, pues entonces serían <strong>de</strong> tres estados. Pero sí se pue<strong>de</strong> tener en<br />

cuenta en el <strong>diagrama</strong> <strong>de</strong> <strong>un</strong> estado para formar <strong>diagrama</strong>s <strong>de</strong> dos estados.<br />

1<br />

x<br />

y<br />

x<br />

error<br />

y<br />

1<br />

x<br />

y<br />

x<br />

error<br />

y<br />

1<br />

x<br />

x<br />

error<br />

y y<br />

En conclusión, el número <strong>de</strong> autómatas <strong>de</strong> dos estados es 16 + 16 + 16 + 16 + 3 = 67<br />

Si <strong>de</strong>bieran tener al menos <strong>un</strong> estado <strong>de</strong> aceptación, excluiríamos los <strong>diagrama</strong>s <strong>de</strong> 2.b.iv); y el número <strong>de</strong> autómatas <strong>de</strong> dos estados<br />

sería 16 + 16 + 16 + 3 = 51<br />

El estudio <strong>de</strong> combinaciones ahora ha sido más simple, pues:<br />

1º Los arcos que salen <strong>de</strong>l estado <strong>de</strong> captación sólo pue<strong>de</strong>n tener como <strong>de</strong>stino el estado <strong>de</strong> captación.<br />

2º Los arcos que salen <strong>de</strong>l estado inicial no pue<strong>de</strong>n tener la combinación en la que ambos arcos vuelven al estado inicial<br />

pues este <strong>diagrama</strong><br />

x x<br />

1<br />

error<br />

y y<br />

es equivalente a este (el cual no es <strong>de</strong> dos estados):<br />

x<br />

y<br />

3.- Muestre que si <strong>un</strong> autómata finito <strong>de</strong>terminista es capaz <strong>de</strong> aceptar <strong>un</strong> número <strong>de</strong> ca<strong>de</strong>nas infinito, entonces <strong>de</strong>be aceptar <strong>un</strong>a<br />

ca<strong>de</strong>na que consista en la concatenación <strong>de</strong> tres segmentos tales que cualquier repetición <strong>de</strong>l segmento central (que es no vacío) dé<br />

como resultado otra ca<strong>de</strong>na aceptable. Esto se conoce como lema <strong>de</strong> bombeo (“pumping lemma”), pues indica que pue<strong>de</strong>n generarse<br />

otras ca<strong>de</strong>nas aceptables bombeando o ampliando <strong>un</strong>a ca<strong>de</strong>na aceptable.<br />

Solución:<br />

El autómata es finito El número n <strong>de</strong> estados es finito<br />

El número <strong>de</strong> ca<strong>de</strong>nas es infinito Para cualquier c<br />

1 <strong>de</strong> longitud l1, existe <strong>un</strong>a ca<strong>de</strong>na c2 <strong>de</strong> longitud l2>l<strong>1.</strong><br />

Estos dos hechos implican el que existan ca<strong>de</strong>nas <strong>de</strong> longitud l>n. Es <strong>de</strong>cir, existen ca<strong>de</strong>nas en las que algún estado es recorrido más<br />

<strong>de</strong> <strong>un</strong>a vez. Estas ca<strong>de</strong>nas pue<strong>de</strong>n ser <strong>de</strong> esta forma:<br />

...<br />

S1 Sk-1 Sk Sk+1<br />

Sn<br />

o <strong>de</strong> esta otra forma:<br />

...<br />

Segmento central<br />

S1 Si Sk Sj<br />

Sn<br />

Segmento central<br />

Y si nos fijamos bien, vemos que al llegar al extremo final <strong>de</strong>l segmento central, nada impi<strong>de</strong> que ocurra la entrada que provoca la<br />

vuelta al extremo inicial <strong>de</strong> dicho segmento. Por tanto, es posible que se repita cualquier número <strong>de</strong> veces; y posteriormente recorrer<br />

<strong>de</strong>s<strong>de</strong> Sj hasta Sn, con lo que son admisibles ca<strong>de</strong>nas con <strong>un</strong> número cualquiera <strong>de</strong> repeticiones <strong>de</strong>l segmento central.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 9<br />

...<br />

Sergio Gálvez Rojas aporta <strong>un</strong> en<strong>un</strong>ciado más formal y general; y <strong>un</strong>a conclusión:<br />

...<br />

...<br />

Si L es <strong>un</strong> lenguaje regular sobre T, entonces:<br />

Existe p>0 / para todo w perteneciente a L, con |w|>=p, existe <strong>un</strong>a <strong>de</strong>scomposición <strong>de</strong> la forma w=xyz con x,y,z pertenecientes a T* con<br />

|y|>0, <strong>de</strong> manera que el bombeo <strong>de</strong> y (y i , para todo i0), en su entorno, verifica:<br />

xy i z pertenece a L, para todo i>=0.<br />

Es más, el lema nos dice que esta es la única forma <strong>de</strong> obtener ca<strong>de</strong>nas arbitrariamente largas (<strong>de</strong> longitud superior a <strong>un</strong> p dado. Si<br />

hay alg<strong>un</strong>a otra forma, entonces el lenguaje no es regular, como ocurre con el mencionado L={0 n 1 n , n0} en el que no hay ningún trozo<br />

intermedio que, repitiéndose varias veces, siga perteneciendo al lenguaje.<br />

4.- Muestre que no existe <strong>un</strong> autómata finito <strong>de</strong>terminista M tal que L(M)={x n y n z n : n N}.<br />

...<br />

José Garzía


Solución:<br />

El autómata es finito El número m <strong>de</strong> estados es finito.<br />

La longitud n <strong>de</strong> x n y n z n pue<strong>de</strong> ser todo lo gran<strong>de</strong> que queramos.<br />

Estos dos hechos implican el que en x n y n z n , n pue<strong>de</strong> ser mayor que m. Dicho <strong>de</strong> otra manera, algún estado o segmento <strong>de</strong> estados<br />

<strong>de</strong>be ser recorrido más <strong>de</strong> <strong>un</strong>a vez.<br />

Supongamos que a ese estado o segmento <strong>de</strong> estados se llega con la entrada x. Recorriéndolo <strong>un</strong> <strong>de</strong>terminado número <strong>de</strong> veces,<br />

se tiene la secuencia x n y n z n . Nada impi<strong>de</strong> que ese estado o segmento se recorra <strong>un</strong> número mayor <strong>de</strong> vces, con lo que la secuencia<br />

sería x p y n z n , con p>n.<br />

Si hubiéramos supuesto que se llegaba con entrada y ó z, se tendrían secuencias x n y p z n ó x n y n z p , con p>n. Con cualquiera <strong>de</strong><br />

estas suposiciones se tiene <strong>un</strong> autómata cuyo lenguaje no es {x n y n z n : n N}.<br />

<br />

5.- Muestre que pue<strong>de</strong> utilizarse <strong>un</strong> autómata finito <strong>de</strong>terminista para reconocer <strong>un</strong>a ca<strong>de</strong>na <strong>de</strong> paréntesis anidados y equilibrados si<br />

se asegura que la prof<strong>un</strong>didad <strong>de</strong> anidamiento no exce<strong>de</strong>rá <strong>de</strong> <strong>un</strong> nivel preestablecido.<br />

Este autómata lee ca<strong>de</strong>nas <strong>de</strong> paréntesis anidados hasta <strong>un</strong> nivel n, y los acepta sólo cuando están todos equilibrados.<br />

Solución:<br />

Este autómata permite ca<strong>de</strong>nas <strong>de</strong>l tipo ( ( ) ( ) ). Se vuelven a abrir paréntesis <strong>de</strong>spués <strong>de</strong> aparecer el primero que cierra.<br />

S0<br />

)<br />

( ( ( ( ... ( (<br />

S1<br />

)<br />

) ) ) ) )<br />

S2<br />

S3<br />

error<br />

Francesc Xavier Torres opina que las ca<strong>de</strong>nas <strong>de</strong>ben ser <strong>de</strong>l tipo ( ( ( ) ) ). Una vez aparece el primero que cierra, no se pue<strong>de</strong>n abrir<br />

más. Propone este autómata:<br />

S0<br />

(<br />

Sn-1<br />

( ( ( ( ... ( (<br />

S1<br />

) )<br />

Sf<br />

)<br />

)<br />

S2<br />

S1’<br />

error<br />

)<br />

S3<br />

S2’<br />

Sn-1<br />

) ... ) )<br />

( ( (<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 10<br />

(<br />

)<br />

)<br />

Sn-2’<br />

(<br />

Sn<br />

)<br />

Sn<br />

Sn-1


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> <strong>1.</strong>4<br />

<strong>1.</strong>- I<strong>de</strong>ntifique los p<strong>un</strong>tos don<strong>de</strong> existe no <strong>de</strong>terminismo en el siguiente <strong>diagrama</strong> <strong>de</strong> transiciones:<br />

Solución:<br />

2.- Utilizando métodos intuitivos y sui generis, modifique el <strong>diagrama</strong> <strong>de</strong> transiciones <strong>de</strong> la siguiente figura (fig. <strong><strong>1.</strong>1</strong>7) para que<br />

acepte las mismas ca<strong>de</strong>nas que antes, pero ahora sin ramificaciones no <strong>de</strong>terministas.<br />

Solución:<br />

1<br />

5<br />

dígito<br />

dígito<br />

dígito<br />

2 <br />

letra<br />

dígito<br />

letra<br />

dígito<br />

Acepta dos clases <strong>de</strong> ca<strong>de</strong>nas:<br />

1ª: (dígito) n (p<strong>un</strong>to) (dígito) m , m, n 1<br />

2ª: (dígito) r<br />

r 1<br />

La primera clase podría reducirse a la seg<strong>un</strong>da, si modificamos el <strong>diagrama</strong> original <strong>de</strong> forma que el estado al que llega tras (dígito) n ,<br />

(el 2) sea <strong>de</strong> aceptación. De esta forma no es necesaria la rama <strong>de</strong>l estado 5:<br />

dígito<br />

1<br />

dígito <br />

2<br />

3<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 11<br />

3<br />

letra<br />

1<br />

dígito<br />

1<br />

dígito<br />

letra<br />

dígito<br />

4<br />

2<br />

3<br />

2<br />

3<br />

letra<br />

dígito<br />

letra<br />

dígito<br />

letra<br />

dígito<br />

letra<br />

dígito<br />

dígito dígito<br />

4<br />

dígito<br />

3.- Muestre que cada autómata finito <strong>de</strong>terminista (S, , , , F), es <strong>un</strong> autómata finito no <strong>de</strong>terminista, <strong>de</strong>mostrando que la f<strong>un</strong>ción<br />

<strong>de</strong> transición pue<strong>de</strong> representarse como <strong>un</strong> subconj<strong>un</strong>to <strong>de</strong> SxxS.<br />

Solución:<br />

En los <strong>de</strong>terministas, es <strong>un</strong>a f<strong>un</strong>ción:<br />

S x <br />

<br />

S<br />

A cada elemento <strong>de</strong> Sx le correspon<strong>de</strong> <strong>un</strong>o sólo <strong>de</strong> S. Por eso, es <strong>un</strong> subconj<strong>un</strong>to <strong>de</strong> SxxS formado por todos los pares posibles<br />

<strong>de</strong> Sx, j<strong>un</strong>to con <strong>un</strong> sólo elemento <strong>de</strong> S.<br />

Al ser <strong>un</strong> subconj<strong>un</strong>to <strong>de</strong> SxxS, cumple las condiciones <strong>de</strong> la <strong>de</strong>finición <strong>de</strong> autómata finito <strong>de</strong>terminista.<br />

José Garzía


4.- Empleando las técnicas presentadas en la <strong>de</strong>mostración <strong>de</strong>l teorema <strong>1.</strong>3, dibuje <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones para <strong>un</strong> autómata<br />

finito <strong>de</strong>terminista que acepte las mismas ca<strong>de</strong>nas que el autómata finito no <strong>de</strong>terminista representado en el siguiente <strong>diagrama</strong> <strong>de</strong><br />

transiciones:<br />

b<br />

Solución:<br />

1<br />

b<br />

a<br />

3<br />

a<br />

2<br />

1<br />

a<br />

a<br />

1, 3<br />

a<br />

b<br />

1, 2<br />

a b<br />

1, 2, 3<br />

3<br />

a<br />

vacío<br />

2, 3<br />

a b b<br />

a b<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 12<br />

b<br />

b<br />

a<br />

b<br />

2


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> <strong>1.</strong>5<br />

<strong>1.</strong>- Elabore <strong>un</strong>a lista con con todas las frases generadas por esta gramática:<br />

Solución:<br />

<br />

<br />

María<br />

Juan<br />

<br />

<br />

patinar<br />

golpear<br />

quiere<br />

a <br />

.<br />

María patina. Juan patina.<br />

María golpea a Juan. Juan golpea a Juan.<br />

María golpea a María. Juan golpea a María.<br />

María quiere a Juan. Juan quiere a Juan.<br />

María quiere a María. Juan quiere a María.<br />

2.- Muestre que es posible modificar la gramática que se presenta a continuación (con el símbolo inicial S) para formar <strong>un</strong>a<br />

gramática regular, sin cambiar el lenguaje que genera. ¿Cuál <strong>de</strong> los ejercicios <strong>de</strong> alg<strong>un</strong>a sección anterior es en esencia el mismo<br />

problema, a<strong>un</strong>que en <strong>un</strong> contexto diferente?.<br />

S y X<br />

X x x X<br />

X y Y<br />

Y <br />

Solución:<br />

Frases que genera: y (x x) n y ; n 0<br />

Para que sea regular, <strong>de</strong>bemos cambiar la regla X x x X, manteniendo el mismo lenguaje generado. Para ello, sustituimos la<br />

antedicha regla por estas otras dos:<br />

X x Z<br />

Z x X<br />

En consecuencia, la gramática modificada es:<br />

S y X<br />

X x Z<br />

Z x X<br />

X y Y<br />

Y <br />

Este problema es, en esencia, el mismo que el 4º <strong>de</strong> la sección <strong><strong>1.</strong>1</strong>. En ambos se <strong>de</strong>scompone <strong>un</strong>a ca<strong>de</strong>na <strong>de</strong> longitud mayor que la<br />

<strong>un</strong>idad, en varias <strong>de</strong> longitud <strong>un</strong>idad.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 13<br />

José Garzía


3.- Convierta la siguiente gramática regular en otra gramática regular que no contenga reglas <strong>de</strong> reescritura cuyos lados <strong>de</strong>rechos<br />

consistan en <strong>un</strong> sólo terminal, pero que a la vez genere el mismo lenguaje. Describa el lenguaje generado.<br />

S x S<br />

S y<br />

S z<br />

Solución:<br />

Lenguaje generado: { y, z, xy, xz, xxy, xxz, xxxy, xxxz, ..., x...xy, x...xz }, en forma compacta: {x m y, x n z: m,n 0, m, n N }<br />

Gramática modificada:<br />

S x S<br />

S y Y<br />

Y <br />

S z Z<br />

Z <br />

4.- Dibuje <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones para <strong>un</strong> autómata finito que acepte el lenguaje generado por la gramática regular (con<br />

símbolo inicial S) que se presenta a continuación. Describa el lenguaje generado.<br />

S <br />

S x X<br />

S y Y<br />

Y y Y<br />

Y <br />

X x X<br />

X <br />

Solución:<br />

Lenguaje generado: { , x m , x n : m, n N<br />

}<br />

Diagrama <strong>de</strong> transiciones:<br />

S<br />

x<br />

y<br />

X<br />

Y<br />

x<br />

y<br />

5.- Presente <strong>un</strong>a gramática regular que genere el lenguaje aceptado por el autómata finito cuyo <strong>diagrama</strong> <strong>de</strong> transiciones se muestra<br />

a continuación:<br />

b<br />

Solución:<br />

S a M<br />

S b N<br />

M a P<br />

M b M<br />

M <br />

N a N<br />

N b P<br />

N <br />

P a P<br />

P b P<br />

S<br />

a a<br />

M<br />

b<br />

N<br />

a<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 14<br />

b<br />

P<br />

a<br />

b<br />

Nota: Las reglas 3, 7, 9 y 10 pue<strong>de</strong>n ser suprimidas. Son improductivas. Si se cae en el estado P,<br />

luego no se pue<strong>de</strong> llegar a ningún estado <strong>de</strong> aceptación. A<strong>un</strong>que la gramática resultante <strong>de</strong> suprimir<br />

estas reglas sea diferente, tiene el mismo lenguaje. Démonos cuenta que se pi<strong>de</strong> “<strong>un</strong>a gramática”, no<br />

“la gramática”.


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> <strong>1.</strong>6<br />

<strong>1.</strong>- ¿Cuáles <strong>de</strong> los lenguajes <strong>de</strong>scritos en las siguientes expresiones regulares para el alfabeto { x, y, z } son infinitos?. Describa, en<br />

<strong>un</strong>a sóla frase, el contenido <strong>de</strong> cada <strong>un</strong>o <strong>de</strong> estos lenguajes infinitos; y elabore <strong>un</strong>a lista exhaustiva <strong>de</strong> las ca<strong>de</strong>nas <strong>de</strong> los lenguajes<br />

finitos.<br />

a) (x (y z*) ) b) (x* (y z)) c) ((z y) x) d) (x y)*<br />

e) (y y)* f) (x* y*) g) ((x x) z) h) ((z y) x)<br />

Solución:<br />

a) Por tener la operación estrella <strong>de</strong> Kleene, es infinito. Las frases son <strong>de</strong> la forma: xy, xyz, xyzz, xyzzz,..., xyz...z.<br />

b) Infinito. Las frases son <strong>de</strong> la forma: yz, xyz, xxyz, xxxyz,..., x...xyz.<br />

c) Finito. El conj<strong>un</strong>to <strong>de</strong> frases generado es: {zx, yx}<br />

d) Infinito. El conj<strong>un</strong>to <strong>de</strong> frases es <strong>de</strong> la forma (x y) (x y) (x y)... es <strong>de</strong>cir: ({x, y})*<br />

e) Infinito. El conj<strong>un</strong>to <strong>de</strong> frases es {y 2n , n N}<br />

f) Infinito. El conj<strong>un</strong>to <strong>de</strong> frases es <strong>de</strong> la forma x, xx, xxx,..., x...x, y, yy, yyy, ..., y...y, es <strong>de</strong>cir: { x m , y n ; m,n N}<br />

= {x*, y*}<br />

g) Finito. El conj<strong>un</strong>to <strong>de</strong> frases es: {xx, z}<br />

h) Finito. El conj<strong>un</strong>to <strong>de</strong> frases es: {x, y, z}<br />

2.- Dibuje <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones que acepte la estrella <strong>de</strong> Kleene <strong>de</strong>l lenguaje aceptado por el siguiente <strong>diagrama</strong> <strong>de</strong><br />

transiciones:<br />

a<br />

c<br />

Solución:<br />

1<br />

b<br />

2<br />

3<br />

1 er paso: Dibujamos <strong>un</strong> nuevo estado inicial (<strong>de</strong> aceptación) y lo conectamos al <strong>diagrama</strong> original. Todos los estados que en el original<br />

eran <strong>de</strong>stino <strong>de</strong>l anterior inicial, <strong>de</strong>ben ser <strong>de</strong>stino <strong>de</strong> este nuevo inicial, con las mismas etiquetas:<br />

a<br />

1 2<br />

b<br />

2º paso: Des<strong>de</strong> cada estado <strong>de</strong> aceptación <strong>de</strong>l original, se aña<strong>de</strong> <strong>un</strong> arco a cada estado que era <strong>de</strong>stino <strong>de</strong>l inicial original.<br />

a<br />

3.- Dibuje <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones que acepte la <strong>un</strong>ión <strong>de</strong> los lenguajes aceptados por los siguientes <strong>diagrama</strong>s:<br />

a) a<br />

Solución:<br />

1<br />

2<br />

3<br />

b<br />

Lenguajes generados:<br />

a) Frases <strong>de</strong> la forma a, aba, ababa, abababa, etc.<br />

b) Frases <strong>de</strong> la forma x, xyx, xyxyx, xyxyxyx, etc.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 15<br />

S<br />

a<br />

x<br />

b)<br />

S<br />

S<br />

1<br />

3<br />

a<br />

a<br />

1 2<br />

b<br />

a<br />

b<br />

x<br />

y<br />

x<br />

y<br />

2<br />

4<br />

4<br />

c<br />

c<br />

a<br />

3<br />

3<br />

José Garzía


4.- Dibuje <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones que acepte la concatenación <strong>de</strong>l lenguaje aceptado por<br />

seguido <strong>de</strong>l lenguaje aceptado por<br />

x y<br />

3<br />

Solución:<br />

Lenguajes generados:<br />

1<br />

a<br />

b<br />

a) Frases <strong>de</strong> la forma a, abb, abbb, ..., ab...b, b, bb, bbb, b...b etc.<br />

b) Frases <strong>de</strong> la forma x m y n , m, n N, m0, n>0<br />

<br />

1<br />

a b<br />

b<br />

2<br />

4<br />

b<br />

y<br />

2<br />

x<br />

3<br />

y<br />

x y<br />

5.- Construya <strong>un</strong>a expresión regular que <strong>de</strong>scriba el lenguaje aceptado por el autómata <strong>de</strong>l siguiente <strong>diagrama</strong> <strong>de</strong> transiciones:<br />

1<br />

a a<br />

2<br />

b<br />

Solución:<br />

(a b*) (a b*) a (b a*) (b a*) b<br />

<br />

<br />

b<br />

3<br />

a<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 16<br />

b<br />

4<br />

<br />

a<br />

b<br />

5<br />

4<br />

y


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 2.1<br />

<strong>1.</strong>- <strong>Diseñe</strong> <strong>un</strong> autómata <strong>de</strong> pila M tal que L(M) = { x n y m x n : m, n N }<br />

Solución:<br />

, ; #<br />

x, ; x y, ; x, x ; <br />

, ; <br />

, ; <br />

i 1 2<br />

3<br />

, # ; <br />

Es el autómata pedido. Es no <strong>de</strong>terminista. Según el teorema 2.1, siempre se pue<strong>de</strong> adaptar <strong>de</strong> forma que vacíe su pila antes <strong>de</strong><br />

aceptar la ca<strong>de</strong>na. De hecho, ya lo hace.<br />

Si se pidiera <strong>un</strong> autómata <strong>de</strong>terminista para L(M) = { x n y m x n : m, n N }, podría tenerse este:<br />

, ; #<br />

x, ; x y, ; x, x ; <br />

y, ; <br />

x, x ; <br />

i 1<br />

2<br />

3<br />

Su f<strong>un</strong>cionamiento es el pedido.<br />

Cuando m=0 y n=0, se acaba en el estado <strong>1.</strong><br />

Cuando m=0 y n0, se acaba en el estado 2.<br />

Cuando m0 y n=0, se acaba en el estado <strong>1.</strong><br />

Cuando m0 y n0, se acaba en el estado 4.<br />

, # ; <br />

Es <strong>de</strong>terminista, pero si acaba en los estados 1 ó 2 no vacía su pila al aceptar la ca<strong>de</strong>na. El teorema 2.1 es aplicable, pues los<br />

<strong>de</strong>terministas están incluidos en los no <strong>de</strong>terministas. Pero el resultado (<strong>de</strong>bido al paso 2 <strong>de</strong> la <strong>de</strong>mostración <strong>de</strong>l teorema, página 81)<br />

es <strong>un</strong> autómata <strong>de</strong> pila no <strong>de</strong>terminista.<br />

Se trata <strong>de</strong> <strong>un</strong> ejemplo similar al <strong>de</strong> la figura 2.22: <strong>un</strong> autómata <strong>de</strong> pila <strong>de</strong>terminista que no pue<strong>de</strong> modificarse para que (sin alterar el<br />

lenguaje aceptado) vacíe su pila al aceptar la ca<strong>de</strong>na. El razonamiento para <strong>de</strong>mostrarlo es idéntico al presentado allí.<br />

Estos dos ejemplos muestran que el conj<strong>un</strong>to <strong>de</strong> lenguajes <strong>de</strong>terministas se subdivi<strong>de</strong>n en dos: los que <strong>de</strong>ben vaciar su pila al terminar<br />

y los que pue<strong>de</strong>n no vaciarla.<br />

LENGUAJES IN<strong>DE</strong>PENDIENTES <strong>DE</strong>L CONTEXTO <strong>DE</strong>TERMINISTA<br />

Aceptados por:<br />

autómatas <strong>de</strong> pila <strong>de</strong>terministas que pue<strong>de</strong>n vaciar o no vaciar su pila antes <strong>de</strong> aceptar <strong>un</strong>a ca<strong>de</strong>na<br />

analizadores sintácticos LR(k); los cuales son equivalentes a los antedichos autómatas (página 133)<br />

n<br />

+<br />

n<br />

n<br />

{ x : n N } U { x y : n N } Página 110, figura 2.22.<br />

otro ejemplo: {xnyx | n N + } {xnyxn | n N + }<br />

+<br />

LENGUAJES ACEPTADOS POR AUTÓMATAS <strong>DE</strong> PI<strong>LA</strong> <strong>DE</strong>TERMINISTAS QUE <strong>DE</strong>BEN VACIAR SU PI<strong>LA</strong> ANTES<br />

<strong>DE</strong> ACEPTAR UNA CA<strong>DE</strong>NA<br />

n n +<br />

{ x y : n N } No es regular, pues podría ser n mayor que el número <strong>de</strong> estados <strong>de</strong>l autómata (teorema <strong>1.</strong>2)<br />

otros ejemplos: {x m y n x n | m, n N + }, {x n y n z| n N + }, {x n yx n | n N + }.<br />

Por supuesto (<strong>de</strong>bido a que <strong>un</strong> conj<strong>un</strong>to está incluido en el otro), pue<strong>de</strong> haber alg<strong>un</strong>os (pero sólo alg<strong>un</strong>os) autómatas <strong>de</strong> pila<br />

<strong>de</strong>terministas que puedan modificarse para que vacíen su pila al aceptar la ca<strong>de</strong>na.<br />

Si se pidiera <strong>un</strong> autómata <strong>de</strong>terminista para L(M) = { x n y m x n : m, n N+ }, podría tenerse este:<br />

, ; #<br />

x, ; x y, ; x, x ; <br />

y, ; <br />

x, x ; <br />

i 1 2<br />

3<br />

, # ; <br />

Su f<strong>un</strong>cionamiento es el pedido.<br />

Como m0 y n0, sólo se acaba en el estado 4; y en este estado la pila está vacía.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 17<br />

4<br />

4<br />

4<br />

José Garzía


2.- ¿Cuál es el lenguaje que acepta el autómata <strong>de</strong> pila cuyo <strong>diagrama</strong> <strong>de</strong> transiciones es el siguiente?<br />

Solución:<br />

x, ; x<br />

y, x ; <br />

Acepta la ca<strong>de</strong>na vacía.<br />

Según la rama superior, toda x que se reciba, se registrará en la pila; y el estado siguiente será <strong>de</strong> aceptación.<br />

Según la rama inferior, sólo se aceptará la entrada y si se pue<strong>de</strong> sacar <strong>un</strong>a x <strong>de</strong> la pila.<br />

Conclusión:<br />

Acepta cualquier ca<strong>de</strong>na que (empezando a contar por la izquierda) contenga <strong>un</strong> número <strong>de</strong> ‘y’ menor o igual a que <strong>de</strong> ‘x’. Pue<strong>de</strong>n<br />

estar intercaladas<br />

Ejemplos: xyx, xxy, xxyxyx, etc.<br />

3.- Modifique el <strong>diagrama</strong> <strong>de</strong> transiciones <strong>de</strong>l ejercicio 2 para que el autómata <strong>de</strong> pila acepte el mismo conj<strong>un</strong>to <strong>de</strong> ca<strong>de</strong>nas, pero<br />

con su pila vacía.<br />

Solución:<br />

i<br />

, ; #<br />

x, ; x<br />

1<br />

y, x ; <br />

, ; <br />

, x ; <br />

p<br />

, # ; <br />

4.- Muestre cómo pue<strong>de</strong>n combinarse dos autómatas <strong>de</strong> pila M 1 y M 2 para formar <strong>un</strong> sólo autómata <strong>de</strong> pila que acepte el lenguaje<br />

L(M 1) U L(M 2).<br />

Solución:<br />

Exactamente igual que en la sección <strong>1.</strong>6:<br />

- Creación <strong>de</strong> <strong>un</strong> nuevo estado inicial y eliminación <strong>de</strong> la condición <strong>de</strong> inicial en los originales.<br />

Si alg<strong>un</strong>o <strong>de</strong> los originales era <strong>de</strong> aceptación, el nuevo <strong>de</strong>be ser <strong>de</strong> aceptación.<br />

- Hasta cada estado que era <strong>de</strong>stino <strong>de</strong>l original inicial, se lleva <strong>un</strong> arco <strong>de</strong>s<strong>de</strong> el nuevo inicial, con la misma rotulación.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 18<br />

q


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 2.2<br />

<strong>1.</strong>- Muestre que toda ca<strong>de</strong>na <strong>de</strong>rivada por la izquierda <strong>de</strong> <strong>un</strong>a gramática in<strong>de</strong>pendiente <strong>de</strong>l contexto pue<strong>de</strong> <strong>de</strong>rivarse también por la<br />

<strong>de</strong>recha.<br />

Solución:<br />

z<br />

a<br />

M<br />

M<br />

z<br />

a<br />

S<br />

b<br />

N<br />

M<br />

z<br />

b<br />

z<br />

El lado por don<strong>de</strong> se empieza a construir (por la <strong>de</strong>recha o por la izquierda) sólo tiene relevancia en las etapas intermedias <strong>de</strong> la<br />

construcción <strong>de</strong>l árbol. Pero con la construcción terminada, no es relevante el lado por don<strong>de</strong> se empezó. La ca<strong>de</strong>na es la misma en<br />

ambos casos.<br />

2.- Demuestre que la gramática presentada a continuación es ambigua, mostrando que la ca<strong>de</strong>na “ictictses” tiene <strong>de</strong>rivaciones que<br />

producen distintos árboles <strong>de</strong> análisis sintáctico.<br />

S icts<br />

S ictSeS<br />

S s<br />

Solución:<br />

Po<strong>de</strong>mos mostrar simultáneamente <strong>un</strong>a analogía con <strong>un</strong> esquema <strong>de</strong> control <strong>de</strong> flujo típico <strong>de</strong> lenguajes <strong>de</strong> programación. Para ello<br />

haremos las siguientes i<strong>de</strong>ntificaciones:<br />

i if<br />

c (condicion)<br />

t {<br />

e else<br />

s en<strong>un</strong>ciado<br />

Con ellas, la primera regla sirve para <strong>de</strong>scribir el esquema simple:<br />

if (condicion)<br />

{ en<strong>un</strong>ciado<br />

y la seg<strong>un</strong>da regla sirve para <strong>de</strong>scribir el esquema:<br />

if (condicion)<br />

{ en<strong>un</strong>ciado 1<br />

else<br />

{ en<strong>un</strong>ciado 2<br />

Los árboles sintácticos son:<br />

1º.- Cuando el else está asociado al if sin else más inmediatamente anterior (al if interno):<br />

S if (condicion)<br />

{<br />

i c t S<br />

if (condicion)<br />

{<br />

i c t S e S<br />

}<br />

else<br />

en<strong>un</strong>ciado<br />

s s<br />

{<br />

}<br />

en<strong>un</strong>ciado<br />

}<br />

2º.- Cuando el else está asociado al primer if (al if externo):<br />

S<br />

i c t S e S<br />

i c t S<br />

s<br />

s<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 19<br />

if (condicion)<br />

{<br />

if (condicion)<br />

{<br />

en<strong>un</strong>ciado<br />

}<br />

}<br />

else<br />

{<br />

en<strong>un</strong>ciado<br />

}<br />

José Garzía


4.- Demuestre que la <strong>un</strong>ión <strong>de</strong> dos lenguajes in<strong>de</strong>pendientes <strong>de</strong>l contexto también es in<strong>de</strong>pendiente <strong>de</strong>l contexto, mostrando cómo<br />

pue<strong>de</strong>n combinarse dos gramáticas in<strong>de</strong>pendientes <strong>de</strong>l contexto <strong>de</strong> dos lenguajes originales para formar <strong>un</strong>a gramática in<strong>de</strong>pendiente<br />

<strong>de</strong>l contexto que genere la <strong>un</strong>ión.<br />

Solución:<br />

GA<br />

SA wA1<br />

...<br />

SA wAp<br />

N1 wA1<br />

...<br />

Nq wAq<br />

GB<br />

SB wB1<br />

...<br />

SB wBr<br />

M1 wB1<br />

...<br />

Ms wBs<br />

- Se crea <strong>un</strong> nuevo estado inicial<br />

- Se aña<strong>de</strong>n <strong>un</strong>a regla <strong>de</strong> sustitución <strong>de</strong>s<strong>de</strong> el nuevo inicial hasta cada <strong>un</strong>o <strong>de</strong> los que eran <strong>de</strong>stino <strong>de</strong> alg<strong>un</strong>o <strong>de</strong> los iniciales<br />

originales.<br />

G<br />

S wA1<br />

...<br />

S wAp<br />

S wB1<br />

...<br />

SB wBr<br />

N1 wA1<br />

...<br />

Nq wAq<br />

M1 wB1<br />

...<br />

Ms wBs<br />

5.- Convierta la siguiente gramática, con el signo inicial S, en <strong>un</strong>a gramática con forma normal <strong>de</strong> Chomsky que genere el mismo<br />

lenguaje.<br />

S x S y<br />

S w N z<br />

N S<br />

N <br />

Solución:<br />

a) Preparación previa: Eliminación <strong>de</strong> la regla tal como se explica en los párrafos anteriores al teorema 2.4<br />

S x S y<br />

S w N z<br />

S w z<br />

N S<br />

b) Cambio <strong>de</strong> los terminales no aislados, según se explica en el primer paso <strong>de</strong>l teorema 2.4.<br />

S X S Y<br />

S W N Z<br />

S W Z<br />

N S<br />

X x<br />

Y y<br />

W w<br />

Z z<br />

Esto produce <strong>un</strong>a gramática G’ in<strong>de</strong>pendiente <strong>de</strong>l contexto en la cual el lado <strong>de</strong>recho <strong>de</strong> cada regla <strong>de</strong> escritura es <strong>un</strong> solo terminal o<br />

<strong>un</strong>a ca<strong>de</strong>na <strong>de</strong> no terminales<br />

c) Separación <strong>de</strong> dos no terminales, según se explica en el seg<strong>un</strong>do paso <strong>de</strong>l teorema 2.4.<br />

S X R<br />

R S Y<br />

S W T<br />

T N Z<br />

S W Z<br />

N S<br />

X x<br />

Y y<br />

W w<br />

Ejercicios <strong>de</strong> ZTeoría z<strong>de</strong><br />

Autómatas I 20


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

Al llegar a esta etapa tenemos <strong>un</strong>a gramática G’ in<strong>de</strong>pendiente <strong>de</strong>l contexto, para la cual el lado <strong>de</strong>recho <strong>de</strong> cada regla <strong>de</strong> reescritura<br />

es <strong>un</strong> solo terminal, dos no terminales o <strong>un</strong> solo terminal.<br />

d) Sustitución <strong>de</strong> las reglas con <strong>un</strong> único no terminal en el lado <strong>de</strong>recho, según se explica en el tercer paso <strong>de</strong>l teorema 2.4.<br />

Las secuencias<br />

Nn Nn-1<br />

...<br />

N1 Fin1<br />

N1 Fin2<br />

...<br />

N1 Finm<br />

Se sustituyen por<br />

Nn Fin1<br />

Nn Fin2<br />

...<br />

Nn Finm<br />

Francesc Xavier Torres consigue <strong>un</strong>a gramática con menos reglas, al emplear <strong>un</strong>a estrategia alternativa:<br />

Las reglas<br />

Nn Nn-1<br />

Nn-1 Nn-2<br />

...<br />

N2 N1<br />

Son aplicadas en todos los lugares don<strong>de</strong> aparezcan Nn, Nn-1, ..., ó N2<br />

En nuestro caso tenemos (NS). La aparición <strong>de</strong> la N en (TN Z) <strong>de</strong>l paso c) es sustituida, obteniendo (TS Z).<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 21<br />

Resulta:<br />

S X R<br />

R S Y<br />

S W T<br />

T N Z<br />

S W Z<br />

N X R<br />

N W T<br />

N W Z<br />

X x<br />

Y y<br />

W w<br />

Z z<br />

Resulta:<br />

S X R<br />

R S Y<br />

S W T<br />

T S Z<br />

S W Z<br />

X x<br />

Y y<br />

W w<br />

Z z<br />

José Garzía


<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 2.3<br />

<strong>1.</strong>- Aplique el teorema 2.5 para <strong>de</strong>mostrar que el lenguaje { x m y n x m y n x m y n : m, n N<br />

+ } no es in<strong>de</strong>pendiente <strong>de</strong>l contexto.<br />

Solución:<br />

<br />

{ x m y n x m y n x m y n +<br />

: m, n N } tiene infinitas ca<strong>de</strong>nas.<br />

{ x <br />

m y n x m y n x m y n : m, n N + } no contiene ca<strong>de</strong>nas <strong>de</strong> la forma x m y n v p y n w p y n : m, n, p N<br />

+ p p<br />

, p m, es <strong>de</strong>cir, s v u w t<br />

Como no cumple la conclusión <strong>de</strong>l teorema 2.5, es absurdo pensar que pueda cumplir la hipótesis; es <strong>de</strong>cir, no es in<strong>de</strong>pendiente <strong>de</strong>l<br />

contexto.<br />

2.- Proporcione ejemplos <strong>de</strong> los siguientes lenguajes:<br />

a) Un lenguaje no in<strong>de</strong>pendiente <strong>de</strong>l contexto.<br />

c) Un lenguaje in<strong>de</strong>pendiente <strong>de</strong>l contexto pero no <strong>de</strong>terminista.<br />

c) Un lenguaje in<strong>de</strong>pendiente <strong>de</strong>l contexto <strong>de</strong>terminista pero que no es aceptado por <strong>un</strong> autómata <strong>de</strong> pila <strong>de</strong>terminista que<br />

tenga que vaciar su pila<br />

d) Un lenguaje que es aceptado por <strong>un</strong> autómata <strong>de</strong> pila <strong>de</strong>terminista que tiene que vaciar su pila, pero que no es regular.<br />

Solución:<br />

a) { x n y n z n : n N<br />

+ } Es consecuencia <strong>de</strong>l teorema 2.5, (página 104).<br />

b) { x n y n : n N + } U { x n y 2n : n N + } Es consecuencia <strong>de</strong>l teorema 2.6.<br />

c) { x n : n N + } U { x n y n : n N<br />

+ } Página 110, figura 2.22.<br />

d) { x n y n : n N + } No es regular, pues podría ser n mayor que el número <strong>de</strong> estados <strong>de</strong>l autómata.<br />

3.- Trace la porción pertinente <strong>de</strong> <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones para <strong>un</strong> autómata <strong>de</strong>terminista que pasará <strong>de</strong>l estado p al q leyendo<br />

<strong>un</strong>a x, extrayendo <strong>un</strong>a y e insertando <strong>un</strong>a z, si la cima <strong>de</strong> la pila contiene <strong>un</strong>a y; o pasará al estado q leyendo <strong>un</strong>a x, sin extraer nada e<br />

insertando <strong>un</strong>a z, si la pila no tiene <strong>un</strong>a y en su cima.<br />

Solución:<br />

Este <strong>diagrama</strong> :<br />

x, y ; z<br />

p q<br />

x, ; z<br />

No es <strong>de</strong>terminista. Deberemos diseñarlo <strong>de</strong> otra forma. La transición (p, x, ; q, z) hay que sustituirla por todas las que sean<br />

necesarias para eliminar la in<strong>de</strong>terminación. Sustituimos l <strong>un</strong>a vez por cada <strong>un</strong>o <strong>de</strong> los símbolos <strong>de</strong> la pila, pero <strong>de</strong> forma que<br />

globalmente no tengan ningún efecto:<br />

x, y ; z<br />

4.- I<strong>de</strong>ntifique las situaciones que provocarían incertidumbres en la ejecución <strong>de</strong> <strong>un</strong>a autómata <strong>de</strong> pila que contuviera las<br />

transiciones (p, , y; q, z) y (p, x, ; r, w).<br />

Solución:<br />

p<br />

p<br />

x, x ; x<br />

x, z ; z<br />

, y ; z<br />

x, # ; #<br />

x, ; w<br />

, ; z<br />

a) (p, , y; q, z) Aplicable sea cual sea la entrada, cuando en la cima <strong>de</strong> la pila hay <strong>un</strong>a y.<br />

b) (p, x, ; r, w) Aplicable cuando la entrada es x, sea cual sea la cima <strong>de</strong> la pila.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 22<br />

q<br />

r<br />

r<br />

En conclusión: Existe <strong>un</strong> único caso en el que son aplicables ambas transiciones; cuando la entrada es x y la cima <strong>de</strong> la pila es y. En el<br />

resto <strong>de</strong> casos, siempre habrá alg<strong>un</strong>a transición <strong>de</strong> las dos que no se pueda aplicar.<br />

q


Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 23<br />

INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 2.4<br />

<strong>1.</strong>- Reescriba el segmento <strong>de</strong> la figura 2.30 utilizando <strong>un</strong>a estructura REPEAT_UNTIL en lugar <strong>de</strong> <strong>un</strong>a estructura WHILE.<br />

Insertar(S) ;<br />

Leer(Símbolo) ;<br />

WHILE Pila no vacía DO<br />

CASE CimaPila OF<br />

Terminal:<br />

IF CimaPila = Símbolo<br />

THEN<br />

Extraer <strong>de</strong> la pila ;<br />

Leer(Símbolo) ;<br />

ELSE<br />

Salir a rutina <strong>de</strong> error ;<br />

END ;<br />

No Terminal:<br />

IF Tabla[CimaPila, Símbolo] Error<br />

THEN<br />

Reemplazar CimaPila por Tabla[CimaPila, Símbolo] ;<br />

ELSE<br />

Salir a rutina <strong>de</strong> error ;<br />

END ;<br />

END ;<br />

END ;<br />

IF Símbolo FDC THEN Salir a rutina <strong>de</strong> error; END ;<br />

Solución:<br />

Insertar(S) ;<br />

Leer(Símbolo) ;<br />

REPEAT<br />

CASE CimaPila OF<br />

Terminal:<br />

IF CimaPila = Símbolo<br />

THEN<br />

Extraer <strong>de</strong> la pila ;<br />

Leer(Símbolo) ;<br />

ELSE<br />

Salir a rutina <strong>de</strong> error ;<br />

END ;<br />

No Terminal:<br />

IF Tabla[ CimaPila, Símbolo] Error<br />

THEN<br />

Reemplazar CimaPila por Tabla[CimaPila, Símbolo] ;<br />

ELSE<br />

Salir a rutina <strong>de</strong> error ;<br />

END ;<br />

END (* Del CASE *)<br />

UNTIL Pila vacía ;<br />

IF Símbolo FDC THEN Salir a rutina <strong>de</strong> error; END<br />

José Garzía


2.- Traduzca el <strong>diagrama</strong> <strong>de</strong> transiciones mostrado a continuación, directamente a <strong>un</strong> segmento <strong>de</strong> programa que analice el lenguaje<br />

en cuestión. ¿ Cuáles son las incertidumbres que <strong>de</strong>ben resolverse para obtener <strong>un</strong> autómata <strong>de</strong>terminista ?.<br />

Solución:<br />

a) Segmento <strong>de</strong> programa:<br />

Estado := i ;<br />

Insertar(#) ;<br />

Estado := p ;<br />

Insertar(S) ;<br />

Estado := q ;<br />

Leer(Símbolo) ;<br />

i<br />

, ; #<br />

x, x ; <br />

, S ; <br />

, ; S<br />

y, y ; <br />

, S ; xSz<br />

REPEAT<br />

CASE CimaPila OF<br />

S:<br />

CASE Símbolo OF<br />

x:<br />

Extraer(S) ;<br />

Insertar(xSz) ;<br />

y:<br />

Extraer(S) ;<br />

Insertar(ySz) ;<br />

END ;<br />

x:<br />

IF Símbolo x<br />

THEN<br />

Salir a rutina <strong>de</strong> error ;<br />

ELSE<br />

Extraer(x) ;<br />

Leer(Símbolo) ;<br />

END ;<br />

y:<br />

IF Símbolo y<br />

THEN<br />

Salir a rutina <strong>de</strong> error ;<br />

ELSE<br />

Extraer(y) ;<br />

Leer(Símbolo) ;<br />

END ;<br />

z:<br />

IF Símbolo z<br />

THEN<br />

Salir a rutina <strong>de</strong> error ;<br />

ELSE<br />

Extraer(z) ;<br />

Leer(Símbolo) ;<br />

END ;<br />

END ; (* Del CASE *)<br />

UNTIL Pila vacía<br />

Extraer(#) ;<br />

IF Símbolo FDC THEN Salir a rutina <strong>de</strong> error; END<br />

Estado := f ;<br />

b) Incertidumbres:<br />

S <br />

S x S z<br />

S y S z<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 24<br />

p<br />

q<br />

, S ; ySz<br />

, # ; <br />

z, z ; <br />

f


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

3.- <strong>Diseñe</strong> <strong>un</strong>a tabla <strong>de</strong> análisis sintáctico LL(1) para la siguiente gramática:<br />

S x S z<br />

S y S z<br />

S <br />

Solución:<br />

x y FDC<br />

S xSz ySz <br />

4.- ¿ Cuántos símbolos <strong>de</strong> preanálisis requeriría <strong>un</strong> analizador sintáctico LL al analizar la sintaxis <strong>de</strong> ca<strong>de</strong>nas basadas en la<br />

gramática siguiente ?.<br />

S x S y<br />

S x y<br />

<strong>Diseñe</strong> la tabla <strong>de</strong> análisis sintáctico correspondiente.<br />

Solución:<br />

A pesar <strong>de</strong> que las ca<strong>de</strong>nas admisibles son <strong>de</strong> la forma x n y n ; ( n 1),<br />

es <strong>de</strong>cir, que pue<strong>de</strong>n tener cualquier longitud finita, bastan dos<br />

símbolos <strong>de</strong> preanálisis:<br />

Tabla <strong>de</strong> preanálisis:<br />

xx xy FDC<br />

S xSy xy error<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 25<br />

José Garzía


<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 2.5<br />

<strong>1.</strong>- <strong>Diseñe</strong> el <strong>diagrama</strong> <strong>de</strong> transiciones <strong>de</strong>l autómata <strong>de</strong> pila correspondiente al analizador sintáctico LR(k) <strong>de</strong> la siguiente gramática:<br />

S x S z<br />

S y S z<br />

S <br />

Solución:<br />

, ; S<br />

, xSz ; S<br />

, ySz ; S<br />

, ; #<br />

, S ; <br />

i p<br />

q<br />

x, ; x<br />

y, ; y<br />

z, ; z<br />

, # ; <br />

Por la regla S existiría la tentación <strong>de</strong> permitir que el estado inicial fuera <strong>de</strong> aceptación. Hay dos razones para no proce<strong>de</strong>r así:<br />

1ª Se <strong>de</strong>be posibilitar la “reducción” yz en yz; y posteriormente en ySz (recuér<strong>de</strong>se que es LR).<br />

2ª El diseño <strong>de</strong>l analizador sintáctico será más fácil si sólo <strong>de</strong>be comprobar la posible llegada a <strong>un</strong> único estado <strong>de</strong> aceptación<br />

(f); en lugar <strong>de</strong> estar obligado a comprobar más estados <strong>de</strong> aceptación.<br />

2.- I<strong>de</strong>ntifique la <strong>de</strong>rivación obtenida por el autómata <strong>de</strong> pila construido en el anterior ejercicio al analizar la ca<strong>de</strong>na yxyzzz<br />

Solución:<br />

Nº <strong>de</strong> fila Estado actual Contenido <strong>de</strong> la pila Resto <strong>de</strong> la entrada<br />

01 p # yxyzzz<br />

02 p #y xyzzz<br />

03 p #yx yzzz<br />

04 p #yxy zzz<br />

05 p #yxyz zz<br />

06 p #yxS zz<br />

07 p #yxSz z<br />

08 p #yS z<br />

09 p #ySz <br />

10 p #S <br />

11 q # <br />

12 f <br />

3.- Construya <strong>un</strong>a tabla <strong>de</strong> análisis sintáctico LR(1) para la siguiente gramática:<br />

S x S y<br />

S <br />

Solución:<br />

1<br />

x<br />

S<br />

x<br />

2<br />

5<br />

S<br />

3<br />

y<br />

4<br />

f<br />

y x y z z z<br />

x y FDC S<br />

1 <strong>de</strong>splazar 2 aceptar 5<br />

2 <strong>de</strong>splazar 2 S 3<br />

3 <strong>de</strong>splazar 4<br />

4 S x S y 3<br />

5 aceptar<br />

Ejemplo: ca<strong>de</strong>na x x x y y y<br />

Contenido <strong>de</strong> la pila Resto <strong>de</strong> la entrada<br />

# (1)<br />

# (1) x (2)<br />

# (1) x (2) x (2)<br />

# (1) x (2) x (2) x (2)<br />

# (1) x (2) x (2) x (2) S (3)<br />

# (1) x (2) x (2) x (2) S (3) y (4)<br />

# (1) x (2) x (2) S (3)<br />

# (1) x (2) x (2) S (3) y (4)<br />

# (1) x (2) S (3)<br />

# (1) x (2) S (3) y (4)<br />

# (1) S (5)<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 26<br />

S<br />

S<br />

S<br />

x x x y y y<br />

x x y y y<br />

x y y y<br />

y y y<br />

y y y<br />

y y<br />

y y<br />

y<br />

y


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 3.1<br />

<strong>1.</strong>- ¿ Con qué configuración <strong>de</strong> cinta se <strong>de</strong>tendrá la máquina <strong>de</strong> Turing mostrada a continuación si comienza con la cinta configurada<br />

como xxx... ?<br />

x / R 2<br />

Solución:<br />

/ <br />

Estado <strong>de</strong> la máquina Estado <strong>de</strong> la cinta<br />

1 xxx...<br />

2 xxx...<br />

1 xxx...<br />

2 xxx...<br />

1 xxx...<br />

3 xxx...<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 27<br />

1<br />

x / R<br />

/ R<br />

3<br />

2.- <strong>Diseñe</strong> <strong>un</strong>a máquina <strong>de</strong> Turing con símbolos <strong>de</strong> cinta x, y y que busque en la cinta el patrón xyxy y se <strong>de</strong>tenga si y sólo si<br />

encuentra ese patrón.<br />

x / x<br />

Solución:<br />

x / R<br />

y / y<br />

x / x<br />

i<br />

x / R<br />

1<br />

y / R<br />

2<br />

x / R<br />

3<br />

/ <br />

/ <br />

/ <br />

José María Olmedo Llera completó y comentó el <strong>diagrama</strong>:<br />

Los arcos <strong>de</strong> la ruta central son los que siguen el patrón dado.<br />

El arco x / R que sale y regresa al estado inicial es para permitir que el patrón buscado esté a partir <strong>de</strong> cualquier posición, no sólo empezando <strong>de</strong>s<strong>de</strong> la primera.<br />

Si estando en el estado 1 se lee algo diferente <strong>de</strong> y, se escribe ese algo (sin avanzar en la cinta) y se vuelve al estado inicial. Será en ese estado inicial don<strong>de</strong> se<br />

avance la cinta.<br />

Í<strong>de</strong>m en el estado 2 con respecto al símbolo x y el estado 3 con respecto al símbolo y.<br />

3.- <strong>Diseñe</strong> <strong>un</strong>a máquina <strong>de</strong> Turing que, al iniciar con la cabeza sobre la celda <strong>de</strong>l extremo izquierdo <strong>de</strong> la cinta, tenga <strong>un</strong>a<br />

terminación anormal si y sólo si hay <strong>un</strong>a x registrada en algún lugar <strong>de</strong> la cinta. Si aplicara su máquina a <strong>un</strong>a cinta que no contiene<br />

ning<strong>un</strong>a x, ¿<strong>de</strong>tectaría la máquina esta situación?.<br />

y / R y / L<br />

Solución: Suponiendo que el alfabeto <strong>de</strong> la cinta es {x, y, }:<br />

/ R / L<br />

Si no existe ning<strong>un</strong>a x, siempre se estaría avanzando hacia la <strong>de</strong>recha, pero en cada instante no se conoce lo que hay más a la<br />

<strong>de</strong>recha, por lo que no se <strong>de</strong>tecta la ausencia <strong>de</strong> x.<br />

4.- Muestre que las capacida<strong>de</strong>s computacionales <strong>de</strong> <strong>un</strong>a máquina <strong>de</strong> Turing no aumentarían si se permitiera que tuvieran más <strong>de</strong> <strong>un</strong><br />

estado <strong>de</strong> parada, ya que esta máquina se podría simular con <strong>un</strong>a máquina <strong>de</strong> Turing con <strong>un</strong> sólo estado <strong>de</strong> parada.<br />

Solución:<br />

Con varios estados <strong>de</strong> parada:<br />

Resto <strong>de</strong><br />

la<br />

h1<br />

Resto <strong>de</strong><br />

la<br />

máquina máquina<br />

h2<br />

<br />

hn<br />

i<br />

y / R<br />

h1<br />

h2<br />

hn<br />

x / L<br />

Con <strong>un</strong> único estado <strong>de</strong> parada:<br />

<br />

h<br />

Un arco por cada símbolo<br />

<strong>de</strong> pila, escribiendo el<br />

mismo símbolo que se lee<br />

h<br />

José Garzía


<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 3.2<br />

<strong>1.</strong>- Combine los <strong>diagrama</strong>s <strong>de</strong> transiciones <strong>de</strong> las siguientes máquinas<br />

y / R<br />

x / y<br />

M1<br />

x / x<br />

/ <br />

M2<br />

y / y<br />

/ <br />

para formar el <strong>diagrama</strong> <strong>de</strong> transiciones <strong>de</strong> la máquina compuesta por:<br />

Solución:<br />

M1<br />

x<br />

M2<br />

Siguiendo los pasos <strong>de</strong> las páginas 149 y 150:<br />

M<br />

El f<strong>un</strong>cionamiento <strong>de</strong> la máquina es:<br />

1<br />

y / R<br />

x / x<br />

/ <br />

/ <br />

<strong>1.</strong>- Busca el primer símbolo distinto <strong>de</strong> y.<br />

2.- Si ese símbolo es x:, lo cambia por <strong>un</strong>a y<br />

3.- Regresa a <strong>1.</strong><br />

2 3<br />

h<br />

x / y<br />

La salida <strong>de</strong>l bucle se produce al encontrar <strong>un</strong> espacio en blanco en cualquiera <strong>de</strong> estos tres estados: 3, 4, 1 ó 2. Obsérvese que va<br />

pasando por esta secuencia <strong>de</strong> estados hasta llegar al h.<br />

Este f<strong>un</strong>cionamiento se pue<strong>de</strong> representar <strong>de</strong> <strong>un</strong>a forma más simple, sin seguir el procedimiento general:<br />

M<br />

y / R<br />

1<br />

/ <br />

h<br />

x / y<br />

4<br />

A<strong>un</strong>que en realidad, yo no lo he hecho directamente; sino partiendo <strong>de</strong>l resultado <strong>de</strong>l procedimiento general, suprimiendo estados<br />

innecesarios.<br />

2.- Utilizando los bloques <strong>de</strong> construcción presentados en esta sección, construya <strong>un</strong>a máquina <strong>de</strong> Turing que reemplace la ca<strong>de</strong>na<br />

<strong>de</strong> ceros y <strong>un</strong>os inmediatamente a la <strong>de</strong>recha <strong>de</strong> la cabeza por el complemento <strong>de</strong> la ca<strong>de</strong>na y luego regrese la cabeza a su posición<br />

original. Suponga que el extremo <strong>de</strong>recho <strong>de</strong> la ca<strong>de</strong>na está marcado con <strong>un</strong> espacio en blanco.<br />

Solución:<br />

0, 1<br />

<br />

<br />

1 0<br />

<br />

R<br />

0<br />

1<br />

Nótese que se marca el bit <strong>de</strong> partida con , para luego encontrarlo y colocar la cabeza lectora en dicho lugar <strong>de</strong> partida.<br />

3.- ¿ En qué condiciones se presentaría <strong>un</strong>a terminación anormal durante la ejecución <strong>de</strong> la máquina S R ?.<br />

Solución:<br />

x1 x2 x3 ...<br />

Las dos primeras instrucciones son L, con lo que quedaría así:<br />

x2 x3 ..<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 28<br />

L<br />

<br />

x / y<br />

y / y<br />

/ <br />

4


R <br />

* <br />

INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

4.- Analice los cálculos efectuados por la siguiente máquina <strong>de</strong> Turing :<br />

Solución:<br />

<br />

L<br />

<strong>1.</strong>- Escribe <strong>un</strong> ‘*’.<br />

2.- Avanza hacia la <strong>de</strong>recha.<br />

IF no es espacio en blanco<br />

THEN Escribe <strong>un</strong> espacio en blanco ;<br />

Vuelve a 2 ; // Todos los símbolos diferentes <strong>de</strong> ‘‘, los sustituye por ‘‘<br />

ELSE Retroce<strong>de</strong> hasta encontrar el ‘*’<br />

Sustituye el ‘*’ por ‘‘<br />

END ;<br />

El efecto es:<br />

. Todos los caracteres <strong>de</strong>s<strong>de</strong> el comienzo hasta el primer ‘‘ los sustituye por ‘‘.<br />

Por ejemplo, si la configuración inicial <strong>de</strong> la ca<strong>de</strong>na es:<br />

x y z x y z x x<br />

La configuración final es:<br />

x y z x x<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 29<br />

José Garzía


<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 3.3<br />

<strong>1.</strong>- a) <strong>Diseñe</strong> <strong>un</strong>a máquina <strong>de</strong> Turing que acepte el lenguaje *, don<strong>de</strong> ={x, y}.<br />

Solución:<br />

R<br />

x, y<br />

<br />

<br />

Nótese que avanza al primer símbolo <strong>de</strong> la ca<strong>de</strong>na, lo rescribe sea cual sea; y se <strong>de</strong>tiene sin seguir leyendo, sea cual sea el resto.<br />

b) <strong>Diseñe</strong> <strong>un</strong>a máquina <strong>de</strong> Turing que acepte el lenguaje <br />

Solución:<br />

Nótese que siempre está avanzando hacia la <strong>de</strong>recha. N<strong>un</strong>ca llega a <strong>un</strong> estado <strong>de</strong> parada.<br />

2.- Muestre que <strong>un</strong>a máquina <strong>de</strong> Turing se pue<strong>de</strong> modificar para que evite <strong>un</strong>a terminación anormal, pero que a la vez acepte las<br />

mismas ca<strong>de</strong>nas que antes.<br />

Solución:<br />

Original<br />

X1<br />

i<br />

n 1<br />

. M0 2<br />

.<br />

. 3<br />

4<br />

X2<br />

X3<br />

La única modificación en M0 con respecto a M1 es que da cada estado <strong>de</strong> M1 sale la rama<br />

#<br />

R<br />

R<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 30<br />

...<br />

Xm<br />

Modificada<br />

# X1 X2 X3 ... Xm<br />

i<br />

n 1<br />

. M1 2<br />

.<br />

. 3<br />

4<br />

En lugar <strong>de</strong> terminar anormalmente, hace avanzar in<strong>de</strong>finidamente hacia la <strong>de</strong>recha la ca<strong>de</strong>na lectora.<br />

3.- Muestre que si en <strong>un</strong>a máquina <strong>de</strong> Turing se permite que las transiciones individuales afecten a más <strong>de</strong> <strong>un</strong>a cinta, entonces no<br />

aumenta el potencial <strong>de</strong> la máquina. En otras palabras, muestre que cualquier transición que opera sobre más <strong>de</strong> <strong>un</strong>a cinta se pue<strong>de</strong><br />

simular con <strong>un</strong>a secuencia <strong>de</strong> transiciones que operan cada <strong>un</strong>a en <strong>un</strong>a sola cinta.<br />

Solución:<br />

Los pasos B1, B2 y B3 <strong>de</strong>l teorema 3.1 son para el supuesto <strong>de</strong> que haya que operar sólo sobre la cinta j. Si hubiera que operar sobre<br />

las cintas i, j, k, ... ; se ejecutaría esta secuencia <strong>de</strong> pasos :<br />

B1i B2i B3i B1j B2j B3j B1k B2k B3k ...<br />

4.- Muestre que para cualquier cálculo <strong>de</strong> <strong>un</strong>a máquina <strong>de</strong> Turing cuya cinta se extien<strong>de</strong> infinitamente hacia la izquierda y hacia la<br />

<strong>de</strong>recha se pue<strong>de</strong> simular con <strong>un</strong>a máquina <strong>de</strong> Turing <strong>de</strong> dos cintas; y por lo tanto, con <strong>un</strong>a máquina <strong>de</strong> Turing tradicional.<br />

Solución:<br />

l3 l2<br />

l1<br />

Hay que tener cuidado con varios <strong>de</strong>talles:<br />

r1<br />

i<br />

n 1<br />

.<br />

. M0 2<br />

.<br />

3<br />

r2 r3 r1 r2<br />

Se pue<strong>de</strong> simular con :<br />

<br />

l1<br />

l2<br />

r3<br />

l3<br />

i<br />

n 1<br />

.<br />

. M1 2<br />

.<br />

3<br />

Sólo hay <strong>un</strong>a cabeza lectora, compartida por las dos cintas, esto es, en cada instante sólo <strong>un</strong>a cinta tiene el foco <strong>de</strong> la cabeza.<br />

En el diseño <strong>de</strong> M1, las referencias a li, las L <strong>de</strong>ben cambiarse por R y viceversa.<br />

El paso <strong>de</strong> la cabeza <strong>de</strong>s<strong>de</strong> r1 a l1 <strong>de</strong>be provocar el paso <strong>de</strong>s<strong>de</strong> el extremo izquierdo <strong>de</strong> la primera al <strong>de</strong> la seg<strong>un</strong>da cinta.<br />

El paso <strong>de</strong> la cabeza <strong>de</strong>s<strong>de</strong> l1 a r1 <strong>de</strong>be provocar el paso <strong>de</strong>s<strong>de</strong> el extremo izquierdo <strong>de</strong> la seg<strong>un</strong>da al <strong>de</strong> la primera cinta.


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 3.4<br />

<strong>1.</strong>- Dibuje <strong>un</strong> <strong>diagrama</strong> <strong>de</strong> transiciones para todas las máquinas <strong>de</strong> Turing con símbolos <strong>de</strong> cinta x y que sólo tengan dos estados<br />

(<strong>un</strong>o inicial y otro <strong>de</strong> parada). ¿ Cómo se relacionan su capacidad para llevar a cabo esta tarea y el hecho <strong>de</strong> que sea contable la<br />

colección <strong>de</strong> todas las máquinas <strong>de</strong> Turing cuyo conj<strong>un</strong>to <strong>de</strong> símbolos <strong>de</strong> cinta es {x, } ?.<br />

Solución :<br />

x / R<br />

/ R<br />

x / R<br />

/ L<br />

x / R<br />

/ x<br />

x / R<br />

/ <br />

x / R<br />

/ R<br />

x / R<br />

/ L<br />

x / R<br />

/ x<br />

x / R<br />

/ <br />

/ R<br />

x / R<br />

/ R<br />

x / L<br />

/ R<br />

x / x<br />

/ R<br />

x / <br />

x / L<br />

/ R<br />

x / L<br />

/ L<br />

x / L<br />

/ x<br />

x / L<br />

/ <br />

x / L<br />

/ R<br />

x / L<br />

/ L<br />

x / L<br />

/ x<br />

x / L<br />

/ <br />

/ L<br />

x / R<br />

/ L<br />

x / L<br />

/ L<br />

x / x<br />

/ L<br />

x / <br />

Esta tarea se pue<strong>de</strong> llevar a cabo <strong>de</strong> forma sistemática, obteniéndose <strong>un</strong> número finito <strong>de</strong> máquinas <strong>de</strong> dos estados. También para el<br />

caso en que el número <strong>de</strong> estados sea cualquier n finito. Como el conj<strong>un</strong>to <strong>de</strong> las máquinas T <strong>de</strong> S={x, } pue<strong>de</strong>n clasificarse según el<br />

número <strong>de</strong> estados, se concluye que dicho conj<strong>un</strong>to es contable.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 31<br />

x / x<br />

/ R<br />

x / x<br />

/ L<br />

x / x<br />

/ x<br />

x / x<br />

/ <br />

x / x<br />

/ R<br />

x / x<br />

/ L<br />

x / x<br />

/ x<br />

x / x<br />

/ <br />

/ x<br />

x / R<br />

/ x<br />

x / L<br />

/ x<br />

x / x<br />

/ x<br />

x / <br />

x / <br />

/ R<br />

x / <br />

/ L<br />

x / <br />

/ x<br />

x / <br />

/ <br />

x / <br />

/ R<br />

x / <br />

/ L<br />

x / <br />

/ x<br />

x / <br />

/ <br />

/ <br />

x / R<br />

/ <br />

x / L<br />

/ <br />

x / x<br />

/ <br />

x / <br />

José Garzía


2.- Muestre que al agregar <strong>un</strong>a seg<strong>un</strong>da pila a <strong>un</strong> autómata <strong>de</strong> pila, obtenemos <strong>un</strong>a clase <strong>de</strong> máquinas con el mismo po<strong>de</strong>r <strong>de</strong><br />

aceptación <strong>de</strong> lenguajes que las máquinas <strong>de</strong> Turing.<br />

Solución:<br />

Situación inicial:<br />

X1<br />

X2<br />

i<br />

n 1<br />

.<br />

. M0 2<br />

.<br />

3<br />

X3<br />

vacía<br />

Pila L<br />

Situación <strong>de</strong> puesta en marcha <strong>de</strong> la simulación:<br />

i<br />

n 1<br />

.<br />

. M0 2<br />

.<br />

3<br />

Ya no es necesaria la cinta<br />

vacía<br />

Pila L<br />

vacía<br />

Pila R<br />

X1<br />

X2<br />

X3<br />

...<br />

Pila R<br />

A partir <strong>de</strong> este momento:<br />

. Un movimiento hacia la <strong>de</strong>recha <strong>de</strong> la cabeza <strong>de</strong> lectura en la máquina <strong>de</strong> Turing es equivalente a <strong>un</strong>a extracción <strong>de</strong> la Pila R e<br />

inserción <strong>de</strong> lo extraído en la Pila L..<br />

. Un movimiento hacia la izquierda <strong>de</strong> la cabeza <strong>de</strong> lectura en la máquina <strong>de</strong> Turing es equivalente a <strong>un</strong>a extracción <strong>de</strong> la Pila L e<br />

inserción <strong>de</strong> lo extraído en la Pila R.<br />

. Una operación <strong>de</strong> escritura en la cinta <strong>de</strong> la máquina <strong>de</strong> Turing es equivalente a <strong>un</strong>a extracción en la Pila R, e inserción en la<br />

misma pila <strong>de</strong>l valor que se escribe.<br />

Como vemos, todas las operaciones <strong>de</strong> la máquina <strong>de</strong> Turing para gestionar la cinta se pue<strong>de</strong>n simular con <strong>un</strong> autómata <strong>de</strong> dos pilas.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 32


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

3.- Desarrolle <strong>un</strong>a gramática estructurada por frases que acepte el mismo lenguaje aceptado por la máquina <strong>de</strong> Turing cuyo<br />

<strong>diagrama</strong> <strong>de</strong> transiciones se muestra a continuación. Con la gramática que obtenga, busque <strong>un</strong>a <strong>de</strong>rivación <strong>de</strong> la ca<strong>de</strong>na x.<br />

Solución:<br />

a)<br />

b)<br />

i) Estas son fijas:<br />

S [ h Y ] (1)<br />

] ]<br />

<br />

ii) Para cada transición <strong>de</strong> la forma (p, x) = (q, y) introducimos la regla q y p x<br />

s Y r x (3)<br />

iii) Para cada transición <strong>de</strong> la forma (p, x) = (q, R) introducimos la regla x q p x<br />

(<strong>de</strong> (i, ) = (p, R) ) : p i (6)<br />

(<strong>de</strong> (p, x) = (q, R) ) : x q p x (5)<br />

(<strong>de</strong> (s, Y) = (h, R) ) : i h s Y<br />

Esta última, en realidad no <strong>de</strong>bería existir. Si la incluyo es <strong>de</strong>bido a <strong>un</strong>a errata <strong>de</strong>l libro. El último arco <strong>de</strong>be ser Y/L, no Y/R.<br />

iv) Para cada transición <strong>de</strong> la forma (p, x) = (q, L) y cada símbolo y <strong>de</strong> la cinta, introducimos la regla q y x y p x<br />

(<strong>de</strong> (q, ) = (r, L) ), como los símbolos <strong>de</strong> la cinta son x y , tendremos:<br />

r x x q (4) h x Y x s Y<br />

r q h Y s Y (2)<br />

r Y Y q h Y Y Y s Y<br />

v) Estas también son fijas:<br />

[ i (7)<br />

] ]<br />

] (8)<br />

() 1<br />

( 2)<br />

( 3)<br />

( 4)<br />

() 5<br />

( 6)<br />

( 7)<br />

( 8)<br />

s [h Y ] [ s Y ] [ r x ] [ x q ] [ p x ] [i x ] x ] x A veces es conveniente comprobar en el <strong>diagrama</strong> <strong>de</strong> transiciones que la ca<strong>de</strong>na x es admisible por la máquina <strong>de</strong> Turing:<br />

[i x ]<br />

[ p x ]<br />

[ x q ]<br />

[ r x ]<br />

[ s Y ]<br />

[h Y ]<br />

4.- Muestre que el lenguaje { x n y 2n z 4n : n N } es aceptado por <strong>un</strong>a máquina <strong>de</strong> Turing.<br />

Solución:<br />

#<br />

i<br />

/ R<br />

x / R<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 33<br />

p<br />

q<br />

/ L<br />

r<br />

x / Y<br />

<br />

R<br />

x<br />

SL<br />

y<br />

y<br />

SL SL<br />

z<br />

SL z SL z SL z SL L<br />

y, z<br />

x x<br />

R<br />

y<br />

y y<br />

R<br />

z<br />

#<br />

, z , z y , x , x z z z<br />

R<br />

Nótese que al llegar a la esquina superior <strong>de</strong>recha, la ca<strong>de</strong>na x n y 2n z 4n , la ha reducido a x n-1 y 2n-2 z 4n-4 . La consecuencia <strong>de</strong> esto, es que<br />

el lenguaje x n y 2n z 4n pue<strong>de</strong> ser generado por <strong>un</strong>a gramática in<strong>de</strong>pendiente <strong>de</strong>l contexto.<br />

L#<br />

s<br />

Y / L<br />

h<br />

José Garzía


<strong>EJERCICIOS</strong> <strong>DE</strong> <strong>LA</strong> <strong>SECCIÓN</strong> 3.5<br />

<strong>1.</strong>- <strong>Diseñe</strong> <strong>un</strong>a máquina <strong>de</strong> Turing que acepte exactamente aquellas ca<strong>de</strong>nas <strong>de</strong> ceros y <strong>un</strong>os que sean representaciones válidas <strong>de</strong><br />

máquinas <strong>de</strong> Turing <strong>de</strong>terministas, <strong>de</strong> acuerdo con el sistema <strong>de</strong> codificación <strong>de</strong>scrito en esta sección.<br />

Solución:<br />

El sistema <strong>de</strong> codificación para cada transición <strong>de</strong> la máquina es:<br />

1[Np] 1 [Nx] 1[Nq] 1 [Ny] 1<br />

<br />

Número <strong>de</strong> ceros que codifican el estado actual.<br />

Np<br />

Nx Número <strong>de</strong> ceros que codifican la entrada actual.<br />

<br />

Número <strong>de</strong> ceros que codifican el estado siguiente.<br />

Nq<br />

Ny Número <strong>de</strong> ceros que codifican R, L ó el símbolo escrito en la cinta.<br />

Para comprobar que la máquina codificada es <strong>de</strong>terminista, basta comprobar que no se repite ningún par (Np, Nx). Para ello es<br />

conveniente tener or<strong>de</strong>nadas las transiciones, primero las que parten <strong>de</strong>l estado 0, <strong>de</strong>spués las que parten <strong>de</strong>l estado 000, luego las<br />

que parten <strong>de</strong>l estado 0000; y así sucesivamente.<br />

Si se <strong>de</strong>sea utilizar <strong>un</strong>a única cinta, la implementación <strong>de</strong> la comprobación pue<strong>de</strong> hacerse con cualquier algoritmo <strong>de</strong> recorrido y<br />

comparación. Pero si tenemos la libertad <strong>de</strong> usar varias cintas (más que el número <strong>de</strong> símbolos <strong>de</strong> cinta):<br />

1 Np 1 Nx1<br />

i<br />

n 1<br />

.<br />

. M0 2<br />

.<br />

3<br />

Nx1<br />

Nx2<br />

Nx3<br />

...<br />

Nxn<br />

1<br />

1 Np 1 Nx2 1<br />

Las siguientes columnas son <strong>un</strong>a para cada <strong>un</strong>o <strong>de</strong> los siguientes estados a p<br />

el algoritmo sería más simple:<br />

1º Llenar las n últimas cintas tal como se muestra en la figura<br />

2º La máquina codificada es <strong>de</strong>terminista si y sólo si en ning<strong>un</strong>a columna existen valores repetidos.<br />

Nq1<br />

2.- Localice la paradoja que existe en el siguiente en<strong>un</strong>ciado:<br />

1<br />

“ El cocinero <strong>de</strong> <strong>un</strong>a com<strong>un</strong>idad aislada cocina para aquellas personas; y sólo para aquellas personas que no cocinan para sí<br />

mismas “.<br />

¿ Cómo se relaciona esta preg<strong>un</strong>ta con esta sección ?.<br />

Solución:<br />

. Si el cocinero come <strong>de</strong> su comida, se contradice el en<strong>un</strong>ciado, pues cocina para sí mismo.<br />

. Si el cocinero no come <strong>de</strong> su comida, hay alguien que cocina para él. No es el único cocinero. El en<strong>un</strong>ciado, para ser correcto,<br />

<strong>de</strong>bería comenzar así: “Un cocinero <strong>de</strong> <strong>un</strong>a com<strong>un</strong>idad aislada .. “.<br />

Definiciones<br />

P0 : { Personas que no cocinan para sí mismas }. Es <strong>un</strong> subconj<strong>un</strong>to <strong>de</strong> los habitantes <strong>de</strong> la isla.<br />

C : Persona que cocina para todas las personas <strong>de</strong>l conj<strong>un</strong>to P0.<br />

El problema es análogo al expuesto en la página 183:<br />

L0 : { Ca<strong>de</strong>nas w que no son aceptadas por su máquina M|w| asociada con la f<strong>un</strong>ción más arriba <strong>de</strong>finida }<br />

M : Máquina <strong>de</strong> Turing que acepta L0.<br />

Allí se <strong>de</strong>muestra la inexistencia <strong>de</strong> M. Por analogía, aplicado al teorema actual, también se <strong>de</strong>muestra la inexistencia <strong>de</strong> C.<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 34<br />

Ny1<br />

Nq2<br />

1<br />

Ny2


INGENIERÍA TÉCNICA en INFORMÁTICA <strong>de</strong> SISTEMAS y <strong>de</strong> GESTIÓN <strong>de</strong> la UNED<br />

ASIGNATURA: TEORÍA <strong>DE</strong> AUTÓMATAS I<br />

Tutoría <strong>de</strong>l Centro Asociado <strong>de</strong> Plasencia<br />

3.- <strong>Diseñe</strong> <strong>un</strong>a máquina <strong>de</strong> Turing que acepte las ca<strong>de</strong>nas <strong>de</strong>l lenguaje L={w n : w = xy } al <strong>de</strong>tenerse con su cinta configurada como<br />

Y ... y rechace las ca<strong>de</strong>nas que no se hallan en L al <strong>de</strong>tenerse con la configuración N ...<br />

Explique por qué no pue<strong>de</strong> construirse esta máquina en el caso <strong>de</strong> todos los lenguajes aceptados por máquinas <strong>de</strong> Turing.<br />

Solución:<br />

R SR R * L L<br />

Marca los extremos<br />

inicial y final con # y *,<br />

respectivamente<br />

R<br />

x<br />

R<br />

y<br />

y x<br />

<br />

SL<br />

y<br />

Reduce pares xy<br />

SL<br />

#*<br />

Salida “Si”<br />

R *<br />

Salida “No”<br />

En los lenguajes no <strong>de</strong>cidibles con máquinas <strong>de</strong> Turing no es posible <strong>de</strong>tectar alg<strong>un</strong>a <strong>de</strong> estas posibilida<strong>de</strong>s: ca<strong>de</strong>na aceptable ó<br />

ca<strong>de</strong>na rechazable. El <strong>diagrama</strong> <strong>de</strong> la máquina <strong>de</strong> Turing carece <strong>de</strong> alg<strong>un</strong>a <strong>de</strong> las salidas. Por este motivo, en dichos lenguajes es<br />

imposible crear la máquina pedida.<br />

4.- Exponga <strong>un</strong> argumento <strong>de</strong> que cualquier lenguaje que contenga <strong>un</strong> número finito <strong>de</strong> ca<strong>de</strong>nas es <strong>de</strong>cidible con máquinas <strong>de</strong><br />

Turing.<br />

Solución:<br />

Cuando nos dispongamos a construir <strong>un</strong>a máquina M <strong>de</strong> Turing para analizar la pertenencia o no <strong>de</strong> ciertas ca<strong>de</strong>nas a <strong>un</strong> lenguaje L<br />

cuyo número <strong>de</strong> ca<strong>de</strong>nas sea n finito, podremos construir M <strong>de</strong> forma que tenga n+1 cintas. Almacenamos cada <strong>un</strong>a <strong>de</strong> las n ca<strong>de</strong>nas<br />

<strong>de</strong> L en <strong>un</strong>a cinta. En la otra cinta almacenamos la ca<strong>de</strong>na en estudio. Por comparación entre esta ca<strong>de</strong>na y las <strong>de</strong>más, podremos<br />

discernir si pertenece o no al lenguaje L.<br />

5.- Amplíe la tabla <strong>de</strong> la figura 3.26 para contestar a la preg<strong>un</strong>ta <strong>de</strong> si la ca<strong>de</strong>na xxyyyxy es generable con la gramática dada.<br />

Solución:<br />

R *<br />

L<br />

L<br />

#<br />

#<br />

#<br />

RYL<br />

#<br />

RNL<br />

S M N P<br />

1 x y x<br />

y<br />

2 La <strong>de</strong> MN La <strong>de</strong> MP La <strong>de</strong> PN<br />

3 La <strong>de</strong> MPN Las <strong>de</strong> MPP Las <strong>de</strong> PPN<br />

4 Las <strong>de</strong> MPPN Las <strong>de</strong> MPPP Las <strong>de</strong> PPPN<br />

5 Las <strong>de</strong> MPPPN Las <strong>de</strong> MPPPP Las <strong>de</strong> PPPPN<br />

6 Las <strong>de</strong> MPPPPN Las <strong>de</strong> MPPPPP Las <strong>de</strong> PPPPPN<br />

7 Las <strong>de</strong> MPPPPPN Las <strong>de</strong> MPPPPPP Las <strong>de</strong> PPPPPPN<br />

xxyyyxy<br />

está entre ellas<br />

Ejercicios <strong>de</strong> Teoría <strong>de</strong> Autómatas I 35<br />

José Garzía

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!