Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

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 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, )

5 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

6 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 after popping B 1 B 2 …B n Solution: Nonterminals will have the form [s,A,p], Where s and p are in Q and A is in  Roughly, [s,A,p] indicates that we can reach state p from state s after popping A from the stack

7 Special Case If ((s,a,A),(q,e)) is in  then add the rule: [s,A,q]  a

8 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

9 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:

10 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

11 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))


Download ppt "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."

Similar presentations


Ads by Google