Download presentation
Presentation is loading. Please wait.
Published byRosaline Jordan Modified over 8 years ago
1
Equivalence of Pushdown Automata and Context-Free Grammars (1) Theorem. Given a context-free grammar CG = ( ,NT,R,S), then there is a pushdown automaton PA = (Q, , , ,s,F) such that L(CG) = L(PA) Construction: Q = {s,f}, F ={f}, = , = NT Make a rule pushing S in the stack: ((s,e,e),(f,S)) For each rule C w in R: ((f,e,C),(f,w)) For each terminal in ((f, , ),(f,e))
2
Sketch of the Proof S 1 T 1 1 where 1, 1 are in * and T 1 is in ( NT)* 1 2 T 2 2 1 where 2 is in in * and T 2 is in ( NT)* (taking the leftmost non terminal in T 1 ) … (always taking the leftmost non terminal) 1 2… n n … 1 (s, 1 2 … n n 1, e) (f, 1 2 … n n 1, S) (f, 1 2 … n n 1, 1 T 1 1 ) * (f, 2 … n n 1, T 1 1 ) (f, 2 … n n 1, 2 T 2 2 1 ) * (f, 3 … n n 2 1,T 2 2 1 ) * (f, e,e)
3
Equivalence of Pushdown Automata and Context-Free Grammars (2) Theorem. Given a pushdown automata PA = (Q, , , ,s,F) then, there exists a context-free grammar CG = ( ,NT,R,S) such that L(PA) = L(CG) Steps: 1. Obtain an equivalent pushdown automata accepting by empty stack 2. Obtain an equivalent simple pushdown automata (Q, , ,, ,s) 3. Use (Q, , ,, ,s) to obtain a context-free grammar CG = ( ,NT,R,S) such that L(SAE) = L(CG)
4
Acceptance by Empty Stack A pushdown automata accepting by empty stack is a 5-tuple PAE = (Q, , , ,s). The language accepted by a PAE is the set of all words w such that starting with the configuration: (s,w,e) ends in the configuration (q,e,e), where q is any state in Q. Theorem. Given a pushdown automaton PA = (Q, , , ,s,F), then there exists a pushdown automata accepting by empty stack PAE = (Q’, , ’, ’,s’) such that L(PA) = L(PAE)
5
Proof The book says: Add a new initial state and push a new symbol: For every state that was favorable pop the new symbol: PA > e e PAPAE e e Both points are wrong!
6
New Proof We say: Assume that the initial configuration is: (s,, e) For every state that was favorable make a transition to a new state q: PAPAE e e q
7
Formally We have: PA = (Q, , , ,s,F) We need: PAE = (Q’, , ’, ’,s’) Q’ = ’ = ’ = s’= important! !
8
Simple Pushdown Automata A pushdown automata SA is simple if for each ((q, , ), (q’, )) in then and SA accepts by empty stack Theorem. Given a pushdown automata accepting a language by empty stack PAE = (Q, , , ,s) there is a simple pushdown automata SPA = (Q’, ’, ’,, ’,s’) accepting by empty stack such that L(SAE) = L(PAE) (that means no pop of empty word or more than one character) Assumptions: For any word w we start with the configuration (s’,w, )
9
Proof Replace each transition ((q, ,w), (q’, )), where w is a word with at least 2 characters, w = 1 2 … n with: ((q,e, 1 ),(q 1,e)) ((q 1,e, 2 ),(q 2,e)) … ((q n-1,e, n ),(q’, )) where q 1,.., q n-1 are new states For every state q we add the transition: ((q, e, ), (q,e)) Replace each transformation ((q, ,e), (q’, )) with a transformation ((q, , ), (q’, )) Idea: always maintain on top of the stack
10
Last step: From a Simple Pushdown Automaton to a Context-Free Grammar Suppose that we have the transition: ((s,a,A),(q,B 1 B 2 …B n )) Problem with first attempt: 1. Doesn’t consider the states s or q First attempt: add the rule A a B 1 B 2 …B n 2. Doesn’t consider the state where it is going to end Solution: Nonterminals will have the form [s,A,p], Where s and p are in Q and A is in
11
Special Case If ((s,a,A),(q,e)) is in then add the rule: [s,A,q] a
12
General Case If Nonterminals are having the form [s,A,p] and we have the transition: ((s,a,A),(q,B 1 B 2 …B n )) Our rule has the form: [s,A,?] a[q, B 1,?] [?, B 2,?]… [?, B n,?] With which states should we fill the question marks? A s a B1B1 q p2p2 B2B2 p3p3 … BnBn p Problem: we don’t know the states p 2, …, p n-1, p
13
General Case (2) Solution: take all possible p 2, …, p n-1, p A s a B1B1 q P2P2 B2B2 P3P3 … BnBn p ((s,a,A),(q,B 1 B 2 …B n )) We add several rules: [s,A,p] a[q, B 1, p 2 ] [p 2, B 2, p 3 ]… [p n-1, B n, p] One for each possible combination p 2, …, p n-1, p For each transition:
14
Resulting Context-Free Grammar Grammar: For every p Q we add the rule: S [s,, p] If ((s,a,A),(q,e)) is in then add the rule: [s,A,q] a If ((s,a,A),(q,B 1 B 2 …B n )) is in then we add the rules: [s,A,p] a[q, B 1, p 2 ] [p 2, B 2, p 3 ]… [p n-1, B n, p] one for each possible combination p 2, …, p n-1, p
15
Homework for Friday Consider the grammar from Example 3.1.5 of the book (Page 74) –Use the grammar to generate the word: (v + (v/(v*v))) –Convert it into a pushdown automaton. –Show that the word (v + (v/(v*v))) is accepted by the pushdown automaton Consider the pushdown automaton from Example 3.3.1 of the book (Page 83) –What is the language accepted by this pushdown automaton –Convert it into an equivalent pushdown automaton accepting by empty stack Consider the following pushdown automaton P accepting by empty stack: –What is the language accepted by it? –Convert P to a simple pushdown automaton. Note: you will need to figure out yourself how to make the transformation. The procedure in the book and the one in the slides has some errors. Assume that the initial configuration is: ((s,a,e),(s,a)) ((s,e,e),(f,e)) ((f,b,aa),(f,e))
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.