180 likes | 474 Views
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.
E N D
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))
S 1T11 where 1, 1 are in * and T1 is in ( NT)* 12T2 21 12… n n …1 where 2 is in in * and T2 is in ( NT)* … (always taking the leftmost non terminal) (s, 12…n n 1, e) (f, 12…n n 1, S) (f, 12… n n 1, 1T11) * * * (f, 2…n n 1, T11) (f, 2…n n 1, 2T2 21) (f, 3… n n 2 1,T2 21) Sketch of the Proof (taking the leftmost non terminal in T1) (f, e,e)
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)
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)
e e PA > e e PA PAE Proof • The book says: • Add a new initial state and push a new symbol: • For every state that was favorable pop the new symbol: Both points are wrong!
We say: • Assume that the initial configuration is: (s, , e) • For every state that was favorable make a transition to a new state q: q e e PA PAE New Proof
important! ! Formally We have: PA = (Q,,,,s,F) We need: PAE = (Q’,,’,’,s’) • Q’ = • ’ = • ’ = • s’=
Assumptions: • • For any word w we start with the configuration (s’,w, ) Simple Pushdown Automata A pushdown automata SA is simple if for each ((q, ,), (q’,)) in then and SA accepts by empty stack (that means no pop of empty word or more than one character) 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)
For every state q we add the transition: • ((q, e, ), (q,e)) Proof Idea: always maintain on top of the stack • Replace each transformation ((q, ,e), (q’,)) with a transformation ((q, , ), (q’, )) • Replace each transition ((q, ,w), (q’,)), where w is a word with at least 2 characters, w = 1 2 … n • with: • ((q,e, 1),(q1,e)) • ((q1,e, 2),(q2,e)) • … • ((qn-1,e, n),(q’, )) • where q1,.., qn-1 are new states
Last step: From a Simple Pushdown Automaton to a Context-Free Grammar Suppose that we have the transition: ((s,a,A),(q,B1B2…Bn)) First attempt: add the rule A a B1B2…Bn Problem with first attempt: 1. Doesn’t consider the states s or q 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
Special Case If ((s,a,A),(q,e)) is in then add the rule: [s,A,q] a
A B1 B2 Bn … q p s p2 p3 a General Case If Nonterminals are having the form [s,A,p] and we have the transition: ((s,a,A),(q,B1B2…Bn)) Our rule has the form: [s,A,?] a[q, B1,?] [?, B2,?]… [?, Bn,?] With which states should we fill the question marks? Problem: we don’t know the states p2, …, pn-1, p
For each transition: ((s,a,A),(q,B1B2…Bn)) We add several rules: [s,A,p] a[q, B1, p2] [p2, B2, p3]… [pn-1, Bn, p] One for each possible combination p2, …, pn-1, p A B1 B2 Bn … q p s P2 P3 a General Case (2) Solution: take all possible p2, …, pn-1, p
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,B1B2…Bn)) is in then we add the rules: • [s,A,p] a[q, B1, p2] [p2, B2, p3]… [pn-1, Bn, p] • one for each possible combination p2, …, pn-1, p
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))