COMP20121 The Implementation and Power of Computer Languages ‘Power’ Part http://www.cs.man.ac.uk/ ∼ petera/2121/index.html . Peter Aczel room: CS2.52, tel: 56155 petera@cs.man.ac.uk email: Department of Computer Science, University of Manchester COMP20121 - Section 0 – p.1/307
COMP20121, ‘power’ part: section 2 lecture 5: Pushdown Automata LECTURE SIX From Grammars to PDAs COMP20121 - Section 2 – p.123/307
From grammars to PDAs Given a grammar, create a PDA: COMP20121 - Section 2 – p.124/307
From grammars to PDAs Given a grammar, create a PDA: • Three states, 0 (start state), 1 and 2 (accepting state), with transitions COMP20121 - Section 2 – p.124/307
From grammars to PDAs Given a grammar, create a PDA: • Three states, 0 (start state), 1 and 2 (accepting state), with transitions ( , ) �→ push ( S ) ( EOF , EOS ) �→ • 0 → • 1 → 1 , 2 , COMP20121 - Section 2 – p.124/307
From grammars to PDAs Given a grammar, create a PDA: • Three states, 0 (start state), 1 and 2 (accepting state), with transitions ( , ) �→ push ( S ) ( EOF , EOS ) �→ • 0 → • 1 → 1 , 2 , ( ,R ) �→ pop ; push ( X n ) ··· push ( X 1 ) • 1 → 1 for every production R → X 1 X 2 · · · X n . COMP20121 - Section 2 – p.124/307
From grammars to PDAs Given a grammar, create a PDA: • Three states, 0 (start state), 1 and 2 (accepting state), with transitions ( , ) �→ push ( S ) ( EOF , EOS ) �→ • 0 → • 1 → 1 , 2 , ( ,R ) �→ pop ; push ( X n ) ··· push ( X 1 ) • 1 → 1 for every production R → X 1 X 2 · · · X n . ( x,x ) �→ pop ; advance • 1 → 1 , for each x ∈ Σ . COMP20121 - Section 2 – p.124/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Build a PDA: Three states 0 1 2 COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Build a PDA: Getting started. 0 1 2 ( , ) �→ push ( S ) COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Build a PDA: When finished. ( EOF , EOS ) �→ 0 1 2 ( , ) �→ push ( S ) COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Build a PDA: S → aSb . ( EOF , EOS ) �→ 0 1 2 ( , ) �→ push ( S ) ( , S ) �→ pop ; push ( b ); push ( S ); push ( a ) COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Build a PDA: S → ǫ . ( , S ) �→ pop ( EOF , EOS ) �→ 0 1 2 ( , ) �→ push ( S ) ( , S ) �→ pop ; push ( b ); push ( S ); push ( a ) COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Build a PDA: Recognize a . ( , S ) �→ pop ( a, a ) �→ pop ; advance ( EOF , EOS ) �→ 0 1 2 ( , ) �→ push ( S ) ( , S ) �→ pop ; push ( b ); push ( S ); push ( a ) COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Build a PDA: Recognize b . ( , S ) �→ pop ( a, a ) �→ pop ; advance ( EOF , EOS ) �→ 0 1 2 ( , ) �→ push ( S ) ( b, b ) �→ pop ; advance ( , S ) �→ pop ; push ( b ); push ( S ); push ( a ) COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; Finished PDA ( , S ) �→ pop ( a, a ) �→ pop ; advance ( EOF , EOS ) �→ 0 1 2 ( , ) �→ push ( S ) ( b, b ) �→ pop ; advance ( , S ) �→ pop ; push ( b ); push ( S ); push ( a ) COMP20121 - Section 2 – p.125/307
Example: From grammar to PDA Consider the grammar S → aSb | ǫ ; which (with exception of the empty word) accepts the same language as the PDA ( , S ) �→ pop ( a, a ) �→ pop ; advance ( , S ) �→ ( , ) �→ push ( S ) ( EOF , EOS ) �→ 0 1 2 3 ( , a ) �→ ( , b ) �→ ( b, b ) �→ pop ; advance ( , S ) �→ pop ; push ( b ); push ( S ); push ( a ) COMP20121 - Section 2 – p.125/307
From PDAs to grammars • Given a (non-deterministic) PDA, it is possible to devise a context-free grammar such that the language generated by the grammar is the language accepted by the automaton. COMP20121 - Section 2 – p.126/307
From PDAs to grammars • Given a (non-deterministic) PDA, it is possible to devise a context-free grammar such that the language generated by the grammar is the language accepted by the automaton. • We will not prove this statement here. See any of the recommended books. COMP20121 - Section 2 – p.126/307
Context-free languages and PDAs The following result connects context-free languages and PDAs. Theorem 2.1 A language is context-free if and only if there is a non-deterministic pushdown automaton that accepts it. COMP20121 - Section 2 – p.127/307
Determinism versus non-determinism • Non-deterministic PDAs are more powerful than deterministic PDAs. COMP20121 - Section 2 – p.128/307
Determinism versus non-determinism • Non-deterministic PDAs are more powerful than deterministic PDAs. • There are context-free grammars for which there is no deterministic pushdown automaton. See Exercise 19 for an example. COMP20121 - Section 2 – p.128/307
Determinism versus non-determinism • Non-deterministic PDAs are more powerful than deterministic PDAs. • There are context-free grammars for which there is no deterministic pushdown automaton. See Exercise 19 for an example. (This example is not examinable.) COMP20121 - Section 2 – p.128/307
Long words in context-free languages If α is a ‘long enough’ word then in the parse tree for this word, at least one of the non-terminal symbols, say R , will appear twice. COMP20121 - Section 2 – p.129/307
Long words in context-free languages If α is a ‘long enough’ word then in the parse tree for this word, at least one of the non-terminal symbols, say R , will appear twice. It is not easy to define what ‘long enough’ means this time. COMP20121 - Section 2 – p.129/307
Long words in context-free languages Parse tree for α : S R R COMP20121 - Section 2 – p.130/307
Long words in context-free languages Breaking up α : S R R κ λ µ ν ξ COMP20121 - Section 2 – p.130/307
Leaving out parts of α Given a parse tree for α , with α broken up S R R as before, κ λ µ ν ξ COMP20121 - Section 2 – p.131/307
Leaving out parts of α we can replace the big parse tree at R S R R κ λ µ ν ξ COMP20121 - Section 2 – p.131/307
Leaving out parts of α we can replace the big parse tree at R S R R by the small parse tree at R κ λ µ ν ξ S R R κ λ µ ν ξ COMP20121 - Section 2 – p.131/307
Leaving out parts of α we can replace the big parse tree at R S R R by the small parse tree at R κ λ µ ν ξ S R R to get the parse tree for κµξ κ λ µ ν ξ S R µ κ ξ COMP20121 - Section 2 – p.131/307
Repeating parts of α Given a parse tree for α , with α broken up S R R as before, κ λ µ ν ξ COMP20121 - Section 2 – p.132/307
Repeating parts of α we can replace the small parse tree at R S R R κ λ µ ν ξ COMP20121 - Section 2 – p.132/307
Repeating parts of α we can replace the small parse tree at R S R R κ λ µ ν ξ by the big parse tree at R S R R κ λ µ ν ξ COMP20121 - Section 2 – p.132/307
Repeating parts of α to get the parse tree for κλλµννξ S R R R λ ν κ ξ λ µ ν COMP20121 - Section 2 – p.133/307
The Pumping Lemma II We can formalize this observation: Lemma 2.2 If L is a context-free language then there is a number n such that any word α of L that has length at least n can be divided into five pieces COMP20121 - Section 2 – p.134/307
The Pumping Lemma II α = κλµνξ such that • the length of λν is greater than 0 ; • the length of λµν is at most n ; • all words of the form κλ k µν k ξ , where k ∈ N , belong to L . COMP20121 - Section 2 – p.134/307
The Pumping Lemma II α = κλµνξ such that • the length of λν is greater than 0 ; • the length of λµν is at most n ; • all words of the form κλ k µν k ξ , where k ∈ N , belong to L . Note that if L is finite, it may not have any words of length n or longer! COMP20121 - Section 2 – p.134/307
Applying Pumping: An example • The language L = { a i b i c i | i ∈ N } is not context-free. COMP20121 - Section 2 – p.135/307
Applying Pumping: An example • The language L = { a i b i c i | i ∈ N } is not context-free. • To see this, suppose it were! Choose n by the Pumping Lemma. Let α = a n b n c n . Write α = κλµνξ as in the lemma. COMP20121 - Section 2 – p.135/307
Applying Pumping: An example Then one of λ, ν is non-empty. Case 1: Each only contain one kind of character. Then κµξ �∈ L . COMP20121 - Section 2 – p.136/307
Applying Pumping: An example Then one of λ, ν is non-empty. Case 1: Each only contain one kind of character. Then κµξ �∈ L . Case 2: One of λ, ν contains more than one kind of character. Then κλ 2 µν 2 ξ �∈ L . COMP20121 - Section 2 – p.136/307
Recommend
More recommend