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

Upload: meljun-cortes-mbampa

Post on 07-Aug-2018

224 views

Category:

Documents


0 download

TRANSCRIPT

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

    1/13

     

    Theory of Computation (With Automata Theory)

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

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

    Specific Objectives:

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

    Cognit ive:

    1. Convert a pushdown automaton into its equivalent context-freegrammar.

    Affective:

    1. Listen to others with respect.

    2. Participate in class discussions actively.

    MATERIALS/EQUIPMENT:

    o  topic slideso  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 ofteaching 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 2

    2/13

     

    Theory of Computation (With Automata Theory)

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

    Equivalence of PDA and CFGPage 1 of 23

    Equivalence of Pushdown Automata and Context-Free Grammars

    Context-Free Grammars to Pushdown Automata 

    Introduction

    Recall in the previous lesson that to prove that a language is context-free if andonly if some pushdown automaton recognizes it, it must be shown that:

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

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

    The previous lesson established that if a language is context-free, then somepushdown automaton recognizes it. This was accomplished by presenting aprocedure for converting any context-free grammar into an equivalent pushdown

    automaton.

    The discussions in this lesson will now prove that if a pushdown automatonrecognizes some language, then that language is context-free.

    Equivalence of PDA and CFGPage 2 of 23

     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 that there issome context-free grammar that describes it. A procedure should therefore becreated for constructing a CFG G based on PDA P .

    In other words, there must be a procedure for converting the pushdownautomaton P   into a corresponding context-free grammar G  such that G  cangenerate or derive all the strings that are accepted by P . Take note that stringsthat are accepted by P  causes it to go from its start state to one of its accept states.

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

    3/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 3 of 13

    Equivalence of PDA and CFGPage 3 of 23

    To simplify the conversion of a PDA P   into its corresponding CFG G, P  must bemodified so that it will have the following properties:

    1. It must have only one final state.

    2. The stack becomes empty when P  goes to the final state. In other words,when P  moves to the final state, the last symbol on the stack must bepopped out.

    3. As P  moves from one state to another, it either pushes a symbol onto thestack or pops a symbol from the stack. For each state transition, thePDA must perform exactly one stack operation (either a push or a pop).It cannot perform both, nor can it not do any.

    Equivalence of PDA and CFGPage 4 of 23

    For Property 1:

    If the PDA has more than one final state, change these states to non-finalstates. Then, create a new final state called qfinal and connect all the original

    final states of the PDA to this state using  -transitions ( ,   →  ). This isshown in the figure below:

    Hence, if the PDA reaches any of its original final states, it moves immediatelyto the new final state qfinal. This modification does not change the originalPDA.

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

    4/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 4 of 13

    Equivalence of PDA and CFGPage 5 of 23

    For Property 2:

    If the stack is not empty when the PDA reaches a final state, the followingmodifications should be made:

    1. Change that final state into a non-final state.

    2. From that state, add transitions going back to itself (loopbacktransitions) that pops the remaining contents of the stack except forthe stack empty symbol $ .

    3. Then, create a new final state called qfinal  and connect the originalfinal state to this new state using a transition edge with the label

     , $  →  . Take note the $  symbol is popped as the PDA moves fromthe original final state to the new final state.

    Equivalence of PDA and CFGPage 6 of 23

    For example, assume that there is a PDA whose final state is q4. When thePDA reaches this state, the contents of its stack are:

    a

    b

    c

    $

    stackcontents

    top of thestack

     

    The following will be the modifications that should be done to ensure that thePDA can reach its final state with an empty stack:

    The addition of the transition edge going from state q4 back to itself with thelabels  , a →  ,  , b →  , and  , c  →    ensures that the PDA can pop theremaining symbols inside the stack before it reaches the new final state qfinal.Observe that the $  symbol is popped as the PDA moves from the original finalstate q4 to the new final state qfinal.

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

    5/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 5 of 13

    Equivalence of PDA and CFGPage 7 of 23 For Property 3:

    Consider first the case where the PDA has a state transition as shown below:

    In the state diagram shown, when the input symbol is a, the PDA pops andreads the symbol at the top of the stack which is b. It then moves fromstate qi   to state q j   while pushing the symbol c   onto the stack. This is anexample of a situation where the PDA performs both a pop and a pushoperation in one state transition since the PDA popped b and pushed c  as itmoved from state qi  to state q j .

    Equivalence of PDA and CFGPage 8 of 23

    To ensure that the PDA will perform only one operation (push or pop) for eachstate transition, the PDA will be modified as follows:

    In the modified PDA, a temporary state qt  was placed between states qi  andq j . The transition edge from qi  to qt  with label a, b →   means that if the inputsymbol is a, the PDA pops and reads the symbol at the top of the stack which

    is b, and then it moves from state qi  to state qt  (without pushing anything ontothe stack). Once at state qt , the PDA may immediately go to state q j  (withoutpopping anything from the stack) and pushing the symbol c  onto the stack. Ineffect, the pop operation was performed in one state transition (from qi  to qt )while the push operation was performed in a separate state transition (from qt  to q j ). Hence, only one operation was performed per state transition.

    Equivalence of PDA and CFGPage 9 of 23

    Consider now the second case where the PDA has a transition as shownbelow:

    In the state diagram shown, when the input symbol is a, the PDA moves fromstate qi  to state q j  without popping or pushing anything. This is an example ofa situation where the PDA does not perform a stack operation in a statetransition.

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

    6/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 6 of 13

    Equivalence of PDA and CFGPage 10 of 23

    To ensure that the PDA will perform one stack operation (push or pop) foreach transition, the PDA will be modified as follows:

    In the modified PDA, a temporary state qt  was again placed between states qi  and q j . The transition edge from qi  to qt  with label a,   →  x  means that if theinput symbol is a, the PDA goes from state qi  to state qt  and pushes a dummysymbol x  onto the stack. Once at state qt , the PDA may immediately pop thedummy symbol x  from the stack and then go to state q j .

    In general, if there is a state transition that does not push or pop anything,replace that transition with two sequential transitions that push and then pop adummy symbol.

    The two cases mentioned above and their corresponding modificationsguarantee that all state transitions are accompanied by exactly one pop orpush operation on the stack.

    Equivalence of PDA and CFGPage 11 of 23

    Conversion of the Modified PDA to CFG

     Assume that there is a PDA P   with the three properties enumerated above. Assume further that the start state is qstart and the final state is qfinal.

    Consider the following events that can occur every time PDA P  processes an inputstring w :

    1. PDA P  starts at state qstart with an empty stack.

    2. As it starts processing the input string w , the very first action it performson the stack is a push operation since it is impossible to pop anythingfrom an empty stack.

    3. As the input symbols arrive, P   moves from one state to another whilepushing and popping symbols whenever necessary.

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

    7/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 7 of 13

    Equivalence of PDA and CFGPage 12 of 23

    4. If the string w  is accepted by P , the PDA will end in the final state qfinal withan empty stack. In other words, the last action it performs on the stack isa pop operation.

    It is possible that in the course of processing a string, the stack maybecome empty at some point in time (in the middle of the processing) andmay be filled up again as input symbols keep on arriving. Eventually, thestack becomes empty again as it reaches the final state.

    Equivalence of PDA and CFGPage 13 of 23

    The language of P  is therefore composed of all strings that cause the PDA to startat qstart with an empty stack and end at qfinal with an empty stack. This languagewill be called Lstart, final. The grammar G  that will be designed should be able tosimulate these series of events for every string that is accepted by P .

    In fact, the grammar G may also be designed to simulate the same series of eventsbetween any two states (not just the start and final states) of the PDA P .

    In other words, for any two states qi  and q j  in PDA P , there will be strings that cancause the PDA to start at state qi  with an empty stack and end at state q j  with anempty stack. There will also be strings that can cause the PDA to start at state qi  with the stack not empty and end at state q j  with the same stack contents when the

    PDA was in state qi   (the stack was preserved). Such strings belong to a certainlanguage called Lij . The grammar to be designed should have a variable called Aij  to generate all the strings belonging to Lij .

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

    8/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 8 of 13

    Equivalence of PDA and CFGPage 14 of 23

    The figure below illustrates this:

    PDA is at

    state q  i 

    stack height

    input

    string

    PDA is at

    state q  j 

    string generated by A ij 

     

     As shown in the figure, when the PDA is at state qi , the stack is empty (as indicatedby a stack height of 0). As a portion of the input string is processed, the stack gets

    filled up. By the time the PDA reaches state q j , the stack is again empty. Thestrings that can cause the PDA to start at state qi  with an empty stack and end atstate q j , also with an empty stack, are the strings that are generated by variable Aij .

    Equivalence of PDA and CFGPage 15 of 23

    Following this reasoning, the variable Aii   can be used to generate all the stringsthat can cause the PDA to move from state qi  to state qi  (back to itself). Since it isimplied that the PDA has the option of remaining in its current state if there are noinput symbols arriving, then the following production should be added:

     Aii  →   

     And last, the variable Astart,final generates all the strings that can cause the PDA tostart at state qstart with an empty stack and end at state qfinal, also with an emptystack. Since qstart  and qfinal  are the first and last states of the PDA, and all the

    strings generated by variable Astart,final  are the strings accepted by PDA P , then Astart,final will be the start variable of grammar G.

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

    9/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 9 of 13

    Equivalence of PDA and CFGPage 16 of 23

    While processing an input string as the PDA goes from state qi   to state q j , twothings may happen:

    1. The stack becomes empty in the middle of the processing.

     As mentioned earlier, it is possible that the stack becomes empty at somepoint in time (in the middle of the processing), and may again be filled upas input symbols keep on arriving. Eventually, the stack becomes emptyagain when it reaches the final state.

    This means that the stack is empty at the start of the processing (atstate qi ). And, as input symbols arrive, the stack gets filled up as thePDA moves from one state to another. At some point in time, the stackagain becomes empty (say at state q x ). Then, as input symbols continueto arrive, it gets filled up again as the PDA resumes moving from onestate to another. By the time the stack reaches state q j , it again becomesempty.

    Equivalence of PDA and CFGPage 17 of 23

    This is shown in the figure below:

    PDA is atstate q i 

    stack height

    inputstring

    PDA is atstate q  j 

    stringgenerated

    by A ix 

    stringgenerated

    by A xj 

    PDA is atstate q x   

    The grammar to be designed should have a variable called  Aix   togenerate all the strings that can cause the PDA to start at state qi  with anempty stack and end at state q x , also with an empty stack. It should alsohave a variable called  A xj   to generate all the strings that can cause the

    PDA to start at state q x  with an empty stack and end at state q j , also withan empty stack.

    From the given figure, the relationship between variable  Aij  and the twonew variables Aix  and A xj  is the production:

     Aij  →  Aix  A xj  

    This is because the strings generated by variable Aij   are the stringsgenerated by Aix  concatenated with the strings generated by variable  A xj .

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

    10/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 10 of 13

    Equivalence of PDA and CFGPage 18 of 23

    2. The stack never becomes empty in the middle of the processing.

    If the stack never becomes empty in the course of processing an inputstring as it goes from state qi  to state q j , this means that the symbol thatwas pushed at the beginning (when the PDA moved from state qi  to some

    intermediate state q x ) is exactly the same symbol that was popped at theend (when the PDA moved from some intermediate state qy   to state q j ).This series of actions may be depicted in the following figure:

    Equivalence of PDA and CFGPage 19 of 23

    From the given figure, as the PDA receives input symbol a, it moves fromstate qi  to state q x  and pushes symbol c  onto the stack. Towards the end,as the PDA receives input symbol b, it pops the same symbol c  from thestack and moves from state qy   to state q j . Hence, the stack is againempty.

    The grammar to be designed should have a variable called  A xy   togenerate all the strings that can cause the PDA to start at state q x  andend at state qy   while preserving the contents of the stack. Therelationship between variable Aij   and the new variable A xy   is theproduction:

     Aij  → aA xy b 

    where a  is the input symbol read when the PDA moved from state qi   tostate q x   while b  is the input symbol read when the PDA moved fromstate qy  to state q j . Bear in mind that a or b can be the empty string  .

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

    11/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 11 of 13

    Equivalence of PDA and CFGPage 20 of 23

    The formal procedure for the construction of grammar G from PDA P   is outlinedbelow:

    1. Introduce the variable Astart,final as the start variable of grammar G.

    2. For every state qi   of PDA P , introduce the following variables withproductions or rules:

     Aii  →   

    3. For every state qi , q j , and q x  of PDA P, introduce the following variableswith productions or rules:

     Aij  →  Aix  A xj  

    This step guarantees that grammar G will cover all possibilities where thestack becomes empty in the middle of a computation while the PDA isprocessing strings as it goes from any state qi  with an empty stack andends at any state q j  with an empty stack. State q x  is the state where the

    stack becomes empty in the middle of processing.

    Take note that this step will produce numerous possible rules. If the PDA

    has 5 states, there will be 5  5  5 = 125 rules (permutation withrepetition of 5 states taken 3 at a time). However, many of these rulesmay turn out to be useless. For example, a rule may be written as A13 →  A12  A23. This rule covers the possibility that as the PDA movesfrom state q1  to state q3, it passes through state q2   where the stackbecomes empty. However, it may be possible that the PDA neveractually passed through q2  as it goes from q1  to q3. Hence, the rule isuseless in the generation of strings.

    Equivalence of PDA and CFGPage 21 of 23

    4. For every state qi , q x , qy , and q j  in PDA P , if the following transitions exist:

    introduce the following variables with productions or rules:

     Aij  → a A xy b

    This step covers the situations where the stack never becomes empty inthe course of processing an input string as it goes from state qi  to state q j ,

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

    12/13

     

    Theory of Computation (With Automata Theory)

    Equivalence of PDA and CFG (Part 2) *Property o f STIPage 12 of 13

    Equivalence of PDA and CFGPage 22 of 23

    Formal Theorem of PDA and CFG Equivalence

    Since it has been shown that there is equivalence between pushdown automataand context-free grammars, this can be formalized through the following theoremand lemmas:

    Theorem 7:

    A language is context- free i f and only i f some push down au tomaton

    recog nizes it.

    Lemma 7.1

    If a language is context- free, then some pushdown automaton

    recog nizes it.

    Lemma 7.2

    If a push down automaton recog nizes some language, then i t iscontext- free. 

    Equivalence of PDA and CFGPage 23 of 23

    Example:

    Convert the following PDA into its corresponding context-free grammar:

    Take note that this PDA recognizes the language L = {0n1

    n  n ≥ 1}.

    Since the PDA has all three properties mentioned earlier, there is no need tomodify this PDA.

     Applying step 1 of the procedure:

    The start variable of the grammar to be constructed is A03.

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

    13/13

     

    Theory of Computation (With Automata Theory)

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

     Applying step 2 of the procedure:

    Since the PDA has four states (q0, q1, q2, and q3), introduce the followingproductions:

     A00 →    A11 →   

     A22 →   

     A33 →   

     Applying step 3 of the procedure:

    Since the PDA has four states (q0, q1, q2, and q3), introduce the followingproductions:

     A00 →  A00 A00  A00 →  A01 A10  A00 →  A02 A20 A00 →  A03 A30 A01 →  A00 A01  A00 →  A01 A11  A00 →  A02 A21 A00 →  A03 A31

    .

    .

    . A33 →  A30 A03  A33 →  A31 A13  A33 →  A32 A23 A33 →  A33 A33 

     All in all, step 3 will produce 4  4  4 = 64 rules. As mentioned earlier, not all

    of these rules will be useful.

     Applying step 4 of the procedure:

    Since there is a  ,   → $  transition ($  was pushed) from q0 to q1 and a  , $  →   transition ($   was popped) from q2  to q3, then introduce the followingproduction:

     A03 →   A12  

    Since there is a 0,   → 0 transition (0 was pushed) from q1  to q1  and a1, 0 →   transition (0 was popped) from q1 to q2, then introduce the followingproduction:

     A12 →  0 A11 1

    [Equivalence of PDA and CFG (Part 2), Pages 1 –23 of 23]