automatocom pilha pushdown automaton

57
1 Automato com Pilha Pushdown Automaton

Upload: others

Post on 31-Jul-2022

9 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Automatocom Pilha Pushdown Automaton

1

Automato com Pilha

Pushdown Automaton

Page 2: Automatocom Pilha Pushdown Automaton

2

Algoritmos Recursivos

e PilhasPrincípio Geral em Computação: Qualquer

algoritmo recursivo pode ser transformado emum não-recursivo usando-se uma pilha e um while-loop, que termina apenas quando a pilhaestá vazia.

EX: JVM mantém os registros de ativação de cadachamada de método. Considere:

long factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

Como JVM executa factorial(5)?

Page 3: Automatocom Pilha Pushdown Automaton

3

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

Compute 5!

Page 4: Automatocom Pilha Pushdown Automaton

4

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

f(5)=5·f(4)

Page 5: Automatocom Pilha Pushdown Automaton

5

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

f(4)=4·f(3)

f(5)=5·f(4)

Page 6: Automatocom Pilha Pushdown Automaton

6

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Page 7: Automatocom Pilha Pushdown Automaton

7

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Page 8: Automatocom Pilha Pushdown Automaton

8

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

f(1)=1·f(0)

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Page 9: Automatocom Pilha Pushdown Automaton

9

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

f(0)=

1�

f(1)=1·f(0)

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Page 10: Automatocom Pilha Pushdown Automaton

10

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

1·1=

1�

f(2)=2·f(1)

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Page 11: Automatocom Pilha Pushdown Automaton

11

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

2·1=

2�

f(3)=3·f(2)

f(4)=4·f(3)

f(5)=5·f(4)

Page 12: Automatocom Pilha Pushdown Automaton

12

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

3·2=

6�

f(4)=4·f(3)

f(5)=5·f(4)

Page 13: Automatocom Pilha Pushdown Automaton

13

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

4·6=

24�

f(5)=5·f(4)

Page 14: Automatocom Pilha Pushdown Automaton

14

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

5·24=

120�

Page 15: Automatocom Pilha Pushdown Automaton

15

Algoritmos Recursivos

e Pilhaslong factorial(int n){

if (n<=0) return 1;

return n*factorial(n-1);

}

Return 5!

= 120

Page 16: Automatocom Pilha Pushdown Automaton

16

De CFG’s para Autômatos com Pilha

CFG’s definem procedimentos recursivos:

boolean derives(strings x, y)

1. if (x==y) return true

2. for(all u⇒⇒⇒⇒y)

if derives(x,u) return true

3. return false

EX: S � # | aSa | bSb

Page 17: Automatocom Pilha Pushdown Automaton

17

De CFG’s para Máquinas de Pilha

Por princípios gerais, qualquer computação

recursiva pode ser executada usando-se

uma pilha. Isso pode ser feito em uma

versão simplificada de máquina com pilha

de registro de ativação, chamada Autômato

de Pilha (“Pushdown Automaton”– PDA)

Q: Qual é a linguagem gerada por

S � # | aSa | bSb ?

Page 18: Automatocom Pilha Pushdown Automaton

18

De CFG’s para Máquinas de Pilha

R: Palíndromos em {a,b,#}* contendo

exatamente um símbolo #.

Q: Usando uma pilha, como podemos

reconhecer tais strings?

Page 19: Automatocom Pilha Pushdown Automaton

19

De CFG’s para Máquinas de Pilha

A: Usamos um processo com três fases:

1. modo Push : Antes de ler “#”, emplihe qualquer símbolo lido.

2. Ao ler “#” troque de modo de operação.

3. modo Pop : Leia os símbolos restantes garantindo que cada novo símbolo lido é idêntico ao símbolo desempilhado.

Aceite se for capaz de concluir com a pilha vazia. Caso contrário, rejeite; e rejeite se não for possível desempilhar em algum caso.

Page 20: Automatocom Pilha Pushdown Automaton

20

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read ==a ou b ?Push it

ACCEPT

(2)read == # ?(ignore stack)

read == top ?Pop

else: CRASH!

empty stack?

Page 21: Automatocom Pilha Pushdown Automaton

21

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

Input:aaab#baa

Page 22: Automatocom Pilha Pushdown Automaton

22

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

aInput:aaab#baa

Page 23: Automatocom Pilha Pushdown Automaton

23

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

a aInput:aaab#baa

Page 24: Automatocom Pilha Pushdown Automaton

24

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

a a aInput:aaab#baa

Page 25: Automatocom Pilha Pushdown Automaton

25

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

b a a aInput:aaab#baa

Page 26: Automatocom Pilha Pushdown Automaton

26

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

b a a aInput:aaab#baa

Page 27: Automatocom Pilha Pushdown Automaton

27

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

a a aInput:aaab#baa

Page 28: Automatocom Pilha Pushdown Automaton

28

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

a aInput:aaab#baa

Page 29: Automatocom Pilha Pushdown Automaton

29

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

aInput:aaab#baa

REJECT (nonempty stack)

Page 30: Automatocom Pilha Pushdown Automaton

30

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

a aInput:aaab#baaa

Page 31: Automatocom Pilha Pushdown Automaton

31

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

aInput:aaab#baaa

Page 32: Automatocom Pilha Pushdown Automaton

32

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

Input:aaab#baaa

Pause input

Page 33: Automatocom Pilha Pushdown Automaton

33

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

Input:aaab#baaa

ACCEPT

Page 34: Automatocom Pilha Pushdown Automaton

34

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

a aInput:aaab#baaaa

Page 35: Automatocom Pilha Pushdown Automaton

35

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

aInput:aaab#baaaa

Page 36: Automatocom Pilha Pushdown Automaton

36

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

Input:aaab#baaaa

Pause input

Page 37: Automatocom Pilha Pushdown Automaton

37

De CFG’s para Máquinas de Pilha

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

Input:aaab#baaaa

CRASH

Page 38: Automatocom Pilha Pushdown Automaton

38

PDA’s à la Sipser

Para facilitar a análise, máquinas de pilha teóricas têmum conjunto restrito de operações. Cada livro-author tem sua própria versão. PDAs descritos em Sipsersão assim:

• Push/Pop agrupados em única operação: substituir o

símbolo no topo da pilha

• Nenhum teste intrínsico de pilha vazia

• Epsilon é usado para aumentar a funcionalidade: máquinas não deterministas.

Page 39: Automatocom Pilha Pushdown Automaton

39

Versão Sipser’s

Torna-se:

(1)

PUSH

(3)

POP

read a or b ?Push it

ACCEPT

(2)read # ?

(ignore stack)

read == peek ?Pop

Else: CRASH!

empty stack?

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

Page 40: Automatocom Pilha Pushdown Automaton

40

Versão Sipser’s

Significado da convenção do rótulo:

Se está no estado p e o próximo símbolo é x e o topo da

pilha é y,

então vá para q e substitua y por z na pilha.

• x = ε: ignore a entrada, não leia

• y = ε: ignore o topo da pilha e empilhe z

• z = ε: desempilhe y

p qx, y �z

Page 41: Automatocom Pilha Pushdown Automaton

41

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

push $

para detectar

pilha vazia

Page 42: Automatocom Pilha Pushdown Automaton

42

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

Input:aaab#baaa

Page 43: Automatocom Pilha Pushdown Automaton

43

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

$Input:aaab#baaa

Page 44: Automatocom Pilha Pushdown Automaton

44

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

a $Input:aaab#baaa

Page 45: Automatocom Pilha Pushdown Automaton

45

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

a a $Input:aaab#baaa

Page 46: Automatocom Pilha Pushdown Automaton

46

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

a a a $Input:aaab#baaa

Page 47: Automatocom Pilha Pushdown Automaton

47

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

b a a a $Input:aaab#baaa

Page 48: Automatocom Pilha Pushdown Automaton

48

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

b a a a $Input:aaab#baaa

Page 49: Automatocom Pilha Pushdown Automaton

49

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

a a a $Input:aaab#baaa

Page 50: Automatocom Pilha Pushdown Automaton

50

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

a a $Input:aaab#baaa

Page 51: Automatocom Pilha Pushdown Automaton

51

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

a $Input:aaab#baaa

Page 52: Automatocom Pilha Pushdown Automaton

52

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

$Input:aaab#baaa

Page 53: Automatocom Pilha Pushdown Automaton

53

Versão Sipser’s

ε , ε�$

a , ε�ab , ε�b

#, ε�ε ε , $�ε

a , a�ε

b , b�ε

Input:aaab#baaa

ACCEPT!

Page 54: Automatocom Pilha Pushdown Automaton

54

PDA

Definição Formal

DEF: Um autômato de pilha (PDA) é uma 6-

tupla M = (Q, Σ, Γ, δ, q0, F ) onde Q, Σ e q0, são

como para um AF. Γ é o alpfabeto da pilha. δ é

uma função:

Portanto, dado um estado p, uma símbolo lido x e

um símbolo de pilha y, δ(p,x,y) retorna todo (q,z)

tal que q é um estado alvo e z é um símbolo que

deve substituir y.

)(Σ:δεεε

Γ×→Γ×× QPQ

Page 55: Automatocom Pilha Pushdown Automaton

55

PDA

Definição Formal

Q: O que é δ(p,x,y) em cada caso?

1. δ(0,a,b)

2. δ(0,ε,ε)

3. δ(1,a,ε)

4. δ(3,ε,ε)

0 1 2

ε , ε�$3

a , ε�ab , ε�b

b, ε�$ ε , $�ε

a , a�ε

b , b�εa , ε� ε

Page 56: Automatocom Pilha Pushdown Automaton

56

PDA

Definição Formal

R:

1. δ(0,a,b) = ∅

2. δ(0,ε,ε) = {(1,$)}

3. δ(1,a,ε) = {(0,ε),(1,a)}

4. δ(3,ε,ε) = ∅

0 1 2

ε , ε�$3

a , ε�ab , ε�b

b, ε�$ ε , $�ε

a , a�ε

b , b�εa , ε� ε

Page 57: Automatocom Pilha Pushdown Automaton

PDA - Exercício

• Forneça PDA´s para reconhecer as seguintes

linguagens:

– {an bn | n ∈ ℵ}

– {an b2n | n ∈ ℵ}

– {a2n bn | n ∈ ℵ}

– {aibjck | i = j ou i=k}

– {w wR | w ∈ {0,1}* }

– {w ∈ {0,1} * | o número de 0´s é igual ao de 1´s }