Docsity

Pushdown Automata, Slides de Informática

Toggle sidebar
bg1
1
Section 12.2 Pushdown Automata
A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations
pop, push, and nop. PDAs always start with one designated symbol on the stack. A state
transition depends on the input symbol and the top of the stack. The machine then performs
a stack operation and enters the next state.
Representation can be graphical or with sets of 5-tuples. For example:
i j
€
b, C
pop
or (i, b, C, pop, j)
Execution: If the machine is in state i and the input letter is b and C is on top of the stack,
then pop the stack and enter state j.
Nondeterminism can occur in two ways, as in the following examples.
i
j
€
b, C
pop
k
€
b, C
push(D)
i
j
€
Λ, C
pop
k
€
b, C
push(D)
Acceptance: A string w is accepted by a PDA if there is a path from the start state to a
final state such that the input symbols on the path edges concatenate to w. Otherwise, w
is rejected.
pf3
pf4
pf5
pf8
pf9

Pré-visualização parcial do texto

Baixe Pushdown Automata e outras Slides em PDF para Informática, somente na Docsity!

1 Section 12.2 Pushdown Automata A pushdown automaton (PDA) is a finite automaton with a stack that has stack operations pop, push, and nop. PDAs always start with one designated symbol on the stack. A state transition depends on the input symbol and the top of the stack. The machine then performs a stack operation and enters the next state. Representation can be graphical or with sets of 5-tuples. For example: i j € b , C pop or ( i , b, C, pop, j ) Execution : If the machine is in state i and the input letter is b and C is on top of the stack, then pop the stack and enter state j. Nondeterminism can occur in two ways, as in the following examples. i j € b , C pop k € b , C push( D ) i j € Λ, C pop k € b , C push( D ) Acceptance : A string w is accepted by a PDA if there is a path from the start state to a final state such that the input symbols on the path edges concatenate to w. Otherwise, w is rejected.

2 Example. A PDA to accept the language { anbn^ | n > 0} as a graph and as a set of 5-tuples. €^0 a , X push( a ) , a , a push( a ) € b , a pop Start X 1 2 € b , a pop € Λ, X nop (0, a , X , push( a ), 0) (0, a , a , push( a ), 0) (0, b , a , pop, 1) (1, b , a , pop, 1) (1, Λ, X , nop, 2). Quiz. How would you modify the machine to accept { anbn^ | n ∈ N }? Answer: Add the instruction (0, Λ, X , nop, 2). Example/Quiz. Find a PDA to accept the language { a^2 nbn^ | n ∈ N }. €^0 a , X push( a ) , a , a push( a ) € b , a pop Start X 1 € b , a pop € Λ, X nop

€ Λ, X nop € Λ, a pop ID for input aaaabb : (0, aaaabb, X ) (0, aaabb, aX ) (0, aabb, aaX ) (0, abb, aaaX ) (0, bb, aaaaX ) (1, b, aaaX ) (2, b, aaX ) (1, Λ , aX ) (2, Λ , X ) (3, Λ, X ). A solution:

4 Transform empty-stack acceptance to final-state acceptance: Introduce a new start state s , a new start symbol Y , a new final state ƒ, and construct edges and actions as shown in the diagram below. Quiz : If the empty-stack PDA is deterministic, then the transformed final-state PDA is also deterministic. Why? Answer : Because the instructions of the empty-stack PDA do not use stack symbol Y. So the only time a new instruction can be executed is when Y is on top of the stack, meaning that the empty-stack PDA has emptied it’s original stack and can’t move. Theorem : The context-free languages are exactly the languages accepted by PDAs. Proof : We’ll see two algorithms, one to transform a C-F grammar into an empty-stack PDA, and one to transform an empty-stack PDA into a C-F grammar. QED. € Λ, Y push( X ) Start X € Λ, Y nop s Y Start empty-stack PDA ƒ € Λ, Y €^ nop Λ, Y nop

5 Transform a C-F grammar to empty-stack PDA The idea is to construct a PDA whose execution corresponds to a leftmost derivation. The PDA has one state, but we allow multiple stack operations. The stack symbols are the terminals and nonterminals of the grammar. The designated starting stack symbol is the grammar start symbol. We’ll illustrate the algorithm with an example. Example. A leftmost derivation of abbcb : S ⇒ aSb ⇒ aBCb ⇒ abBCb ⇒ abbCb ⇒ abbcCb ⇒ abbcb. (0, abbcb, S ) (0, abbcb, aSb ) (0, bbcb, Sb ) (0, bbcb, BCb ) (0, bbcb, bBCb ) (0, bcb, BCb ) (0, bcb, bCb ) (0, cb, Cb ) (0, cb, cCb ) (0, b, Cb ) (0, b, b ) (0, Λ, Λ). Example. ID for input string abbcb : Terminals Corresponding PDA Instructions a (0, a , a , pop, 0) b (0, b , b , pop, 0) c (0, c , c , pop, 0) Productions S → aSb (0, Λ, S , 〈pop, push( b ), push( S ), push( a )〉, 0) S → BC (0, Λ, S , 〈pop, push( C ), push( B )〉, 0) B → bB (0, Λ, B , 〈pop, push( B ), push( b )〉, 0) B → b (0, Λ, B , 〈pop, push( b )〉, 0) C → cC (0, Λ, C , 〈pop, push( C ), push( c )〉, 0) C → Λ (0, Λ, C , pop, 0) Grammar S → aSb | BC B → bB | b C → cC | Λ.

7 Example/Quiz. Use the algorithm to transform the following empty-stack PDA into a C-F grammar. (^) Start (^0) € a , X push( A ) € a , A nop X 1 € a , X pop , a , A pop Quiz: Simplify the preceding grammar by deleting productions that do not derive terminal strings and productions that are not reachable from the start symbol. Solution: The start state 0 with X on the stack gives: S^ →^ X 00 |^ X 01 The pop operation (1, a, X , pop, 1) gives: X 11 →^ a The pop operation (1, a, A , pop, 1) gives: A 11 →^ a The push operation (0, a , X , push( A ), 0) gives: X 00 →^ aA 00 X 00 |^ aA 01 X 10 X 01 → aA 00 X 01 | aA 01 X 11 The nop operation (0, a , A , nop, 1) gives: A 00 →^ aA 10 A 01 → aA 11 Solution : S → X 01 X 01 → aA 01 X 11 A 01 → aA 11 X 11 → a A 11 → a or, S → aaaa.

8 Example. We’ll try to find a grammar for the language L = { w ∈ { a, b }* | na ( w ) = nb ( w )} by constructing an empty-stack PDA to accept L and then transforming it into a C-F grammar. Consider the following single-state empty-stack PDA. € Λ, X pop , b , A pop , a , B pop Start (^0) X € a , X push( A ) , a , A push( A ) € b , X push( B ) , b , B push( B ) Idea of Proof: Assume, WLOG, that the input string begins with a. Then A is pushed and this action continues for each consecutive a. When the first b occurs, A is popped, which means that an a-b pair has been counted. The only way to get back to X on top is if the string has an equal number of a ’s and b ’s so far. If the input is exhausted, then pop X and accept the string. Otherwise, the process continues as described. PDA Transformed into C-F grammar : S → X 00 X 00 → Λ | aA 00 X 00 | bB 00 X 00 A 00 → b | aA 00 A 00 B 00 → a | bB 00 B 00. A Simplified C-F grammar : S → Λ | aAS | bBS A → b | aAA B → a | bBB. Example Derivation of aababb : S ⇒ aAS ⇒ aaAAS ⇒ aabAS ⇒ aabaAAS ⇒ aababAS ⇒ aababbS ⇒ aababb.