meljun cortes automata lecture equivalence of pda and cfg part 1 1

Upload: meljun-cortes-mbampa

Post on 07-Aug-2018

221 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    1/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 1 of 19

    TOPIC TITLE: Equivalence of PDA and CFG (Part 1)

    Specific Objectives:

     At the end of the topic session, the students are expected to:

    Cognit ive:

    1. Explain the relationship between pushdown automata andcontext-free grammars.

    2. Convert any context-free grammar into its equivalent pushdownautomaton.

    Affective:

    1. Listen to others with respect.2. Participate in class discussions actively.

    MATERIALS/EQUIPMENT:

    o  topic slides

    o  OHP

    TOPIC PREPARATION:

    o  Have the students review related topics that discussed inprevious courses.

    o  Prepare the slides to be presented in class.o  It is imperative for the instructor to incorporate various kinds of

    teaching strategies while discussing the suggested topics. o  Prepare additional examples on the topic to be presented. 

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    2/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 2 of 19

    Equivalence of PDA and CFGPage 1 of 31

    Equivalence of Pushdown Automata and Context-Free Grammars

    Pushdown Automata and Context-Free Grammars

    Introduction

    Based on previous discussions, a context-free grammar can generate the strings ofthe context-free language it represents. While a pushdown automaton canrecognize a context-free language since it can determine if a certain string belongsto that language. Technically speaking, a PDA and a CFG are then equal inpower.

    This section will formally explore the relationship between pushdown automata andcontext-free grammars. Specifically, the discussions will now try to establishwhether there is equivalence between the two.

    Equivalence of PDA and CFGPage 2 of 31

    Recall that a context-free language is any language whose strings can begenerated by some context-free grammar.

     And since PDAs recognize context-free languages, can it then be claimed that

     A language is context-free if and only if somepushdown automaton recognizes it?

    To answer the question, it must be shown that:

    1. If a language is context free, then some pushdown automaton recognizesit.

     Assume that there is a context-free language L. Since it is context-free,there is some context-free grammar G  that describes it. To show thatthere is a pushdown automaton that recognizes L, a procedure must beestablished to construct a PDA for language L based on grammar G.

    This will be discussed in this section.

    2. If a pushdown automaton recognizes some language, then it is context-free.

     Assume that there is a PDA P  which recognizes a certain language L. Todetermine if language L is a context-free language, it must be shown thatthere is some context-free grammar that describes it. There must then bea procedure established for constructing a CFG based on PDA P .

    This will be discussed in the next section.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    3/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 3 of 19

    Equivalence of PDA and CFGPage 3 of 31

    Context-Free Grammar to Pushdown Automaton

    The following discussions will show that a language is context-free if and only ifsome pushdown automaton recognizes it.

     Assume that there is a context-free language L. Since it is context-free, there issome context-free grammar G that can be used to generate or derive the strings ofthe language.

    To show that context-free language L  has some pushdown automaton thatrecognizes it, a procedure must be established to convert context-free grammar G of language L into its equivalent pushdown automaton P .

    Equivalence of PDA and CFGPage 4 of 31

    To facilitate the discussion, a case study will be used. Assume a certain context-free language L1 is represented by context-free grammar G1:

    S → AB 

     A → 0A 0

    B → 0B1 ε  

    This grammar actually represents the language L1 = {0i 1 j   i  > j }. Take note that this

    language is composed of strings that start with a block of consecutive 0s followedby a block of consecutive 1s where the total number of consecutive 0s is greaterthan the total number of consecutive 1s.

    Recall how a grammar is used to generate a string of the language. As a review,the leftmost derivation of string 0001 using grammar G1 is shown below:

    S → AB → 0AB  substituted A with 0 A → 00B  substituted A with 0→ 000B1  substituted B with 0B1→ 0001  substituted B with ε  

    Recall that a string can be derived from a grammar by performing a series ofsubstitutions wherein the right-hand side variables are replaced by variables and/orterminals using the appropriate rules of the grammar until no variables are left.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    4/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 4 of 19

    Equivalence of PDA and CFGPage 5 of 31

    To show that language L1 has a pushdown automaton that recognizes it, a PDA P 1 must be constructed based on context-free grammar G1. In other words, thecontext-free grammar must be converted to a pushdown automaton.

    The PDA P 1 that will be constructed from grammar G1 should be able to determine

    if an input string belongs to context-free language L1.

    To do this, P 1 must have the capability to determine whether there is a series ofsubstitutions using the rules of grammar G1 that can generate the input string. P 1 should be able to “simulate” the leftmost derivation of the input string. If there issuch a derivation, the string will be accepted. Otherwise, it will be rejected.

    In the simulation of the leftmost derivation of a string, the stack of the PDA can beused to store or record the steps of the derivation.

    Equivalence of PDA and CFGPage 6 of 31

    Here is an example of how the stack of a PDA is used in simulating the leftmostderivation of the string 0001 using grammar G1:

    1. First push the start variable S onto the stack. The stack contents will be

    Stop of the

    stack  

    This step is simply showing that the derivation begins with the startvariable S.

    2. The next step in the derivation process is to replace the variable S withthe right-hand side string of a rule for S. Using the rule S →  AB, removeS from the stack and replace it with the string AB. The stack contents willnow be

    A

    B

    top of the

    stack

     

    Since this is the leftmost derivation, the right-hand side string should bepushed onto the stack such that the leftmost part of the string is at the topof the stack. This is done so that the PDA can easily access the leftmostvariable for subsequent substitutions. If this were using the rightmostderivation, then the rightmost part of the string should be at the top of thestack.

     At this point in time, the derivation string is AB.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    5/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 5 of 19

    Equivalence of PDA and CFGPage 7 of 31

    3. The next step is to replace the leftmost variable A of the derivation stringwith the right-hand side string of a rule for  A. Using the rule  A → 0 A, remove  A  from the stack and replace it with the string 0 A. The stackcontents will now be

    A

    B

    top of thestack

    0

     

    The derivation string at this point is the string 0 AB.

    4. Since the leftmost part of the derivation string is a terminal (0), this will bethe first symbol of the string being derived. The next step is then toremove 0 from the stack. Therefore, the string derived so far is 0 and thestack contents will now be

    A

    B

    top of thestack

    stringderived = 0

     

    The derivation string at this point is the string AB.

    Equivalence of PDA and CFGPage 8 of 31

    5. The next step is to replace the leftmost variable A of the derivation stringwith the right-hand side string of a rule for  A. Using the rule  A → 0, remove A  from the stack and replace it with 0. The stack contents willnow be

    0

    B

    top of thestack

    stringderived = 0

     

    The derivation string at this point is the string 0B.

    6. Since the leftmost part of the derivation string is a terminal (0), this will bethe second symbol of the string being derived. The next step now is toremove 0 from the stack. Therefore, the string derived so far is 00 and

    the stack contents will now be

    Btop of the

    stack

    stringderived = 0 0

     

    The derivation string at this point is the string B.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    6/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 6 of 19

    Equivalence of PDA and CFGPage 9 of 31

    7. The next step is to replace the leftmost variable B of the derivation stringwith the right-hand side string of a rule for B. Using the rule B → 0B1, remove B  from the stack and replace it with the string 0B1. The stackcontents will now be

    B

    1

    top of thestack

    0 stringderived = 0 0

     

    The derivation string at this point is the string 0B1.

    8. Since the leftmost part of the derivation string is a terminal (0), this will bethe third symbol of the string being derived. The next step now is toremove 0 from the stack. Therefore, the string derived so far is 000 andthe stack contents will now be

    B

    1

    top of thestack

    stringderived = 0 0 0

     

    The derivation string at this point is the string B1.

    Equivalence of PDA and CFGPage 10 of 31

    9. The next step is to replace the leftmost variable B of the derivation stringwith the right-hand side string of a rule for B. Using the rule B →  , remove B from the stack (no need to push anything onto the stack sinceB is being replaced by the empty string). The stack contents will now be

    1top of the

    stack

    stringderived = 0 0 0

     

    The derivation string at this point is the string 1.

    10. Since the leftmost part of the derivation string is a terminal (1), this will bethe fourth symbol of the string being derived. The next step now is toremove 1 from the stack. Therefore, the string derived so far is 0001 and

    the stack contents will now be

    top of thestack

    stringderived = 0 0 0 1

     

    Since the stack is now empty, the PDA was therefore able to simulate theleftmost derivation of the string 0001 using grammar G1.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    7/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 7 of 19

    Equivalence of PDA and CFGPage 11 of 31

    Hence, to determine if an input string is to be accepted, the PDA P 1  that will beconstructed should be able to perform the following actions:

    1. Before any computation starts, P 1 pushes the stack empty symbol $  onto

    the stack.

    2. P 1 then pushes the start symbol of the grammar onto the stack.

    3. The following actions are then repeated:

    a. If the top of stack is a variable, pop that variable from the stack.Then, select one of the rules for that variable and push the right-hand side string of that rule onto the stack.

    Equivalence of PDA and CFGPage 12 of 31

    b. If the top of stack is a terminal, pop that terminal from the stack.Then, compare that terminal with the next symbol of the inputstring. If they match, repeat step 3 and processing continues. Ifthey do not match, reject the string.

    c. If the top of stack is the stack empty symbol $   (the stack isempty) and there are no more input symbols. Accept the inputstring (the PDA goes to a final state).

    If the stack is empty and there are input symbols remaining, or ifthe stack is not empty and there are no more input symbols,reject the input string.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    8/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 8 of 19

    Equivalence of PDA and CFGPage 13 of 31

    Based on the series of actions given, the following will now be performed by P 1 inorder to determine if the string 0001 belongs to language L1:

    1. P 1 will push the stack empty symbol $  onto the stack. The stack contents

    will now be

    $top of the

    stack  

    2. P 1 will push the start symbol S  onto the stack. The stack contents willnow be

    S

    $

    top of thestack

     

    Equivalence of PDA and CFGPage 14 of 31

    3. Since the symbol at the top of the stack is a variable (S), pop it out fromthe stack and   push the right-hand side string of a rule for S  (S →  AB)onto the stack. The stack contents will now be

    B

    $

    top of thestack

    A

     

    4. Since the symbol at the top of the stack is a variable ( A), pop it from thestack and push the right-hand side string of a rule for A ( A → 0 A) onto thestack. The stack contents will now be

    B

    $

    top of thestack

    A

    0

     

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    9/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 9 of 19

    Equivalence of PDA and CFGPage 15 of 31

    5. Since the symbol at the top of the stack is a terminal (0), pop this from thestack and compare it with the first incoming input symbol (0). Since theymatch, the computation continues. The stack contents will now be

    B

    $

    top of thestack

    A

     

    6. Since the symbol at the top of the stack is a variable ( A), pop it from thestack and push the right-hand side string of a rule for  A ( A → 0) onto thestack. The stack contents will now be

    B

    $

    top of the

    stack

    0

     

    Proof of InductionPage 16 of 31

    7. Since the symbol at the top of the stack is a terminal (0), pop this from thestack and compare it with the second incoming input symbol (0). Sincethey match, the computation continues. The stack contents will now be

    B

    $

    top of thestack

     

    8. Since the symbol at the top of the stack is a variable (B), pop it out fromthe stack and push the right-hand side string of a rule for B  (B → 0B1)onto the stack. The stack contents will now be

    1

    $

    top of thestack

    B

    0

     

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    10/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 10 of 19

    Equivalence of PDA and CFGPage 17 of 31

    9. Since the symbol at the top of the stack is a terminal (0), pop this from thestack and compare it with the third incoming input symbol (0). Since theymatch, the computation continues. The stack contents will now be

    1

    $

    top of thestack

    B

     

    10. Since the symbol at the top of the stack is a variable (B), pop it from thestack and push the right-hand side string of a rule for B (B →  ) onto thestack. But since the right-hand side string of the selected rule is theempty string, there is no need to push anything. The stack contents willnow be

    1

    $

    top of thestack

     

    Equivalence of PDA and CFGPage 18 of 31

    11. Since the symbol at the top of the stack is a terminal (1), pop this from thestack and compare it with the fourth incoming input symbol (1). Sincethey match, the computation continues. The stack contents will now be

    $top of the

    stack  

    12. Since the stack is empty and there are no more input symbols left, thestring is accepted by PDA P 1.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    11/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 11 of 19

    Equivalence of PDA and CFGPage 19 of 31

    One problem in the series of actions outlined is that there will be situations wherethere will be several choices on which rule to use in substituting a certain variable. As an example, take note that there are two rules for variable A  in grammar G1.One is the rule A → 0 A while the other one is the rule  A → 0. Hence, if the symbolat the top of the stack is A, the PDA has two options:

    1. One option is to pop variable A  from the stack and then push the right-hand side string of the rule A → 0 A onto the stack. In other words, pop A and then push 0 A.

    2. The other option is to pop variable A  from the stack and then push theright-hand side string of the rule  A → 0 onto the stack. In other words,pop A and then push 0.

    The same case is also true for variable B.

    The question now is which option will the PDA choose? One option may cause thePDA to accept the string while the other may not.

    The answer lies in the nondeterministic nature of PDAs. The PDA will makenondeterministic guesses on which rule to use by doing several computations atthe same time. In the given example, the PDA will make two copies of itself. Onecopy will proceed with the computation using the rule  A → 0 A while the secondcopy will continue processing the input string using the rule A → 0.

    In order words, the PDA will try to use all possible rules for substituting a certainvariable by making nondeterministic guesses. If the string is indeed part of thelanguage of the PDA, then one or more copies of the PDA will end up in a finalstate. If the string does not belong to the language, then all copies of the PDA will just die.

    Equivalence of PDA and CFGPage 20 of 31

    Observe that the PDA to be constructed requires that an entire string is pushedonto the stack.

    Example:

    In step 8 of the previous example, when the symbol at the top of the stack was B, itwas popped out of the stack and the string 0B1 was pushed onto the stack. This isshown in the figure below:

    1

    $

    B

    0

    B

    $

    before after   

    Take note again that the leftmost symbol of the string to be pushed should be atthe top of the stack. In this example, push first the symbol 1, then B, and thenfinally 0.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    12/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 12 of 19

    Equivalence of PDA and CFGPage 21 of 31

    If this action is represented by a state diagram, it would look like:

    The transition edge from state qi   to state q j   is , B → 0B1. Recall that this labelmeans that even without an input symbol, if the symbol at the top the stack is B,then the PDA will pop B from the stack and then move from state qi  to state q j , andat the same time push the string 0B1 onto the stack. This transition can be writtenas

    (qi , , B) = (q j , 0B1)

    This transition equation means that if the PDA is at state qi  and there is no inputsymbol, and the symbol at the top of the stack is B, the PDA will move to state q j  and push 0B1 onto the stack. Remember that this implies that the symbol at thetop of the stack was popped before moving to the new state.

    However, PDAs can push only one symbol onto the stack as it moves from onestate to another. There is no provision for the PDA to push an entire string in onlyone state transition.

    Equivalence of PDA and CFGPage 22 of 31

    The pushing of an entire string onto the stack can be implemented by introducingintermediate states between states qi and q j. Instead of having the PDA move fromstate q

    i  to state q

     j  and push the string 0B1 onto the stack, the PDA will perform the

    following:

    1. If the symbol at the top of the stack is B and there is no input symbol,move from state qi   to an intermediate state q x  and then push 1 onto thestack. In other words, (qi , , B) = (q x , 1).

    2. Then, from intermediate state q x , move right away to another intermediatestate qy  while pushing B onto the stack. In other words, (q x   , ) = (qy ,B).

    3. Last, from intermediate state qy , move right away to the destinationstate q j  while pushing 0 onto the stack. In other words, (qy   , ) = (q j , 0).

    The state diagram can now be revised so that only one symbol is pushed onto thestack for each state transition. This is seen in the state diagram below:

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    13/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 13 of 19

    Equivalence of PDA and CFGPage 23 of 31

    Recall the procedure presented earlier on how the PDA to be constructedprocesses input strings.

    1. Before any computation starts, P 1 pushes the stack empty symbol $  ontothe stack.

    2. P 1 pushes the start symbol of the grammar onto the stack.3. Repeat the following actions:

    a. If the top of stack is a variable, pop that variable from the stack.Then, select one of the rules for that variable and push the right-hand side string of that rule onto the stack.

    b. If the top of stack is a terminal, pop that terminal from the stack.Then, compare that terminal with the next symbol from the input.If they match, repeat step 3 and processing continues. If theydo not match, reject the string.

    c. If the top of stack is the stack empty symbol $   (the stack isempty) and there are no more input symbols, accept the inputstring (the PDA goes to a final state).

    This PDA will therefore have three major states representing the actions it has to

    perform in determining whether a string belongs to a certain language:

    1. qstart  – this is the start state where the stack empty symbol $  and then thestart variable S of the grammar will be pushed onto the stack. This takescare of steps 1 and 2 listed above.

    2. qloop  – this state is where the major processing occurs. This takes care ofsteps 3a, 3b, and 3c listed above

    3. qfinal  –  this is the final or accept state. The PDA goes to this state fromstate qloop when the stack is empty and there are no more input symbols.

    Equivalence of PDA and CFGPage 24 of 31

    The transition function of the PDA to be constructed should be as follows:

    The start state qstart will have a transition edge to state qloop with the label

    (qstart, , ) = (qloop, S$ )

    This transition simply states that when the PDA is at state qstart, it will moveright away to state qloop  and at the same time push the string S$   onto thestack. The stack empty symbol $  goes inside the stack first followed by thestart variable S of the grammar.

    The transition edges for state qloop  should be able to handle the followingsituations:

    1. The symbol at the top of the stack is a variable (step 3a).2. The symbol at the top of the stack is a terminal (step 3b).3. The symbol at the top of the stack is the stack empty symbol $  

    indicating that the stack is already empty (step 3c).

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    14/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 14 of 19

    Equivalence of PDA and CFGPage 25 of 31

    Case 1:

    If the top of the stack symbol is a variable A, there should be a transition edgewith label

    (qloop, , A) = (qloop, w )

    where w  is the right-hand side string of the rule A → w  used to replace A.

    Hence, if the symbol at the top of the stack is a variable  A, the PDA movesfrom state qloop to qloop and pushes the right-hand side string (w ) of a rule forvariable A. As mentioned earlier, if there are several rules for variable A, thenondeterministic nature of the PDA will allow it to try out all possible rules.Hence, there must be a transition edge of this type for each rule for eachvariable.

    Case 2:

    If the top of the stack symbol is a terminal a, there should be a transition edgewith label

    (qloop, a, a) = (qloop, )

    Hence, if the symbol at the top of the stack is a terminal a, the PDA must popthis terminal and compare it to next incoming input symbol. If the inputsymbol is also a (they match), then the PDA simply moves from qloop  to qloop without pushing anything onto the stack. Since there is a match, processingtherefore continues. If the terminal at the top of the stack does not match theincoming input symbol, the PDA cannot go anywhere and processing thendies. The input string is therefore rejected. Take note that there must be atransition edge of this type for each terminal in the grammar.

    Case 3:

    If the top of the stack symbol is the stack empty symbol $ , there should be atransition edge with label

    (qloop, , $ ) = (qfinal, )

    If the symbol at the top of the stack is $  (the stack is empty) and there are nomore input symbols, the PDA moves from qloop to qfinal. The string is thereforeaccepted.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    15/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 15 of 19

    Equivalence of PDA and CFGPage 26 of 31

    The “generic” state diagram for the PDA P   to be constructed from context-freegrammar G will be

    The following are the transition labels of the given PDA:

    1. From qstart to qloop: , → S$  

    This allows the PDA to move right away from qstart  to qloop and push thestring S$  onto the stack.

    2. From qloop to qloop: , A → w  

    If the symbol at the top of the stack is a variable A, this allows the PDA tomove right away from qloop  to qloop  and substitute variable A  with thestring w  using the rule  A → w . In other words, the variable A  is poppedout of the stack and the right-hand string of the selected rule is pushedonto the stack. In the PDA to be constructed, there should be a transitionedge (from qloop  to qloop) with this type of label for each rule for each

    variable in the grammar to be converted. In converting grammar G1  toPDA P 1, the following transition edges must be added:

    , S → AB  for the rule S → AB 

    , A → 0 A  for the rule A → 0 A , A → 0 for the rule A → 0

    , B → 0B1 for the rule B → 0B1, B → for the rule B →

    3. From qloop to qloop: a, a →

    If the symbol at the top of the stack is a terminal a, this allows the PDA tocompare this terminal with the next incoming input symbol. The PDApops the terminal from the stack and compares it with the input symbol. If

    they are the same, the PDA moves from qloop  to qloop  and processingcontinues. Otherwise, processing dies. In the PDA to be constructed,there should be a transition edge (from qloop to qloop) with this type of labelfor each terminal symbol in the grammar to be converted. In convertinggrammar G1 to PDA P 1, the following transition edges must be added:

    0, 0 → for the terminal 0

    1, 1 → for the terminal 1

    4. From qloop to qfinal: , $  →

    If the symbol at the top of the stack is $  (stack is empty) and there are nomore input symbols, the PDA moves to the final state qfinal and the inputstring is accepted.

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    16/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 16 of 19

    Equivalence of PDA and CFGPage 27 of 31

    Therefore, converting the context-free grammar G1  into PDA P 1  will result in thefollowing state diagram:

    Equivalence of PDA and CFGPage 28 of 31

    However, the PDA presented still has state transitions wherein an entire string ispushed onto the stack instead of just one symbol. The transitions being referred toare the ones with the labels

    , → S$

    , S → AB

    , A → 0 A

    , B → 0B1

    Revising PDA P 1  so that intermediate states are used to ensure that only onesymbol is pushed onto the stack per state transition will result in the following statediagram:

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    17/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 17 of 19

    Equivalence of PDA and CFGPage 29 of 31

     As a second example, convert the following context-free grammar G2 to a PDA P 2:

    S → 0T1 1

    T → T0 ε  

    The state diagram of the initial PDA P 2 is given below:

    Equivalence of PDA and CFGPage 30 of 31

    However, the PDA still has state transitions wherein an entire string, instead of justone symbol, is pushed onto the stack. The transitions being referred to are theones with the labels

    , → S$

    , S → 0T 1 

    , T  → T0 

    Revising PDA P 2  so that intermediate states are used to ensure that only one

    symbol is pushed onto the stack per state transition results in

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    18/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STIPage 18 of 19

    Equivalence of PDA and CFGPage 31 of 31

    Since there is now a procedure to convert a CFG to a PDA, it can therefore beconcluded that if a language is context-free, then some pushdown automatonrecognizes it.

    Exercises:

    Convert the following context-free grammars to PDAs:

    1. Grammar G3 

    S → AB 

     A → aAb ε  

    B → cB ε  

    2. Grammar G4 

    S → SS (S) ε  

    3. Grammar G5 

    S → ε 0 1 0S0 1S1

    Solution for # 1:

    Initial PDA: Revised PDA:

  • 8/21/2019 MELJUN CORTES Automata Lecture Equivalence of Pda and Cfg Part 1 1

    19/19

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 1) *Property o f STI

    Solution for # 2:

    Initial PDA: Revised PDA:

    Solution for # 3:

    Initial PDA: Revised PDA:

    [Equivalence of PDA and CFG, Pages 1 –31 of 31]