Successfully reported this slideshow.
PushdownAutomataPDAs
Pushdown Automaton --PDA Input String                 Stack States
Initial Stack Symbol     Stack               Stack      $                       z               bottom             special...
The States   Input          Pop            Push   symbol         symbol         symbol                 a, b   c           ...
a, b   c                      q1              q2input           a                                a      stack        b...
a,      c                      q1               q2input           a                                 a      stack      ...
a, b                      q1           q2input           a                             a      stack        b       top...
a,                      q1               q2input           a                                 a      stack        b    ...
Non-Determinism a, b    c    q2q1                             , b      c                          q1                q2 a, ...
NPDA: Non-DeterministicPDAExample:           a,       a     b, aq0   ,          q1 b, a      q2   ,$   $ q                ...
Execution Example: Time 0  Input   a a a b b b                                          $                                 ...
Time 1Input a a a b b b                                       $                                   Stack         a,        ...
Time 2Input a a a b b b                           a                                       $                               ...
Time 3Input                                  a a a a b b b                           a                                    ...
Time 4                                       aInput                                       a a a a b b b                   ...
Time 5                                     aInput                                     a a a a b b b                       ...
Time 6Input                                a a a a b b b                         a                                     $  ...
Time 7Input a a a b b b                          a                                      $                                 ...
Time 8Input a a a b b b                                      $                                     Stack         a,       ...
The Language of PDAA string is accepted if there isa computation such that:     • All the input is consumed     • The last...
Formal Definition AutomatonNon-Deterministic Pushdown                 NPDA         M   (Q, Σ, Γ, δ, q0 , z, F )   FinalSta...
Instantaneous Description            ( q, u , s )  Current                  Current             Remaining  state          ...
Acceptance by Final StateL(M)= set of all strings w such that starting  from initial ID machine consumes w from  input and...
Acceptance by emptystack N(M) = (w | (q0,w,Z)    * (q,   ,   ) That is N(M) is the set of all strings w such  that M can c...
The input string aaabbb         is accepted by the NPDA:             a,       a     b, aq0   ,            q1 b, a      q2 ...
In general,                      n n              L {a b : n 0}is the language accepted by the NPDA:              a,      ...
Another NPDA example                   NPDA      M                                 R                  L( M ) {ww }a,      ...
Execution Example:   Time 0  Input       a b b a                                         $                                ...
Time 1Input     a b b a                                      a                                      $                     ...
Time 2Input                                      b     a b b a                                      a                     ...
Time 3Input                                           b     a b b a                  Guess the middle         a           ...
Time 4Input                                      b     a b b a                                      a                     ...
Time 5Input     a b b a                                      a                                      $                     ...
Time 6Input     a b b a                                      $                                    Stacka,        a       a...
Rejection Example:   Time 0  Input       a b b b                                         $                                ...
Time 1Input     a b b b                                      a                                      $                     ...
Time 2Input                                      b     a b b b                                      a                     ...
Time 3Input                                           b     a b b b                  Guess the middle         a           ...
Time 4Input                                      b     a b b b                                      a                     ...
Time 5Input             There is no possible transition.     a b b b            Input is not          a                   ...
Another computation on same string:  Input             Time 0       a b b b                                        $      ...
Time 1Input     a b b b                                      a                                      $                     ...
Time 2Input                                      b     a b b b                                      a                     ...
Time 3Input                                 b                                      b     a b b b                          ...
Time 4              bInput                                 b                                      b     a b b b           ...
Time 5                bInput                                    b                  No final state         b     a b b b   ...
There is no computation          that accepts string abbb                  abbb L(M )a,        a          a, ab,        b ...
Pushing Strings    Input          Pop            Push    symbol         symbol         string                  a, b   w   ...
Example:                           a, b    cdf                      q1                 q2input              a        ...
Another NPDA example                  NPDA   M        L( M ) {w : na            nb } a, $   0$        b, $   1$ a, 0   00 ...
Execution Example:            Time 0  Input   a b       b a a b                                                $     a, $ ...
Time 1Input a b     b a a b                                            0                                            $  a, ...
Time 3Input a b     b b a a                                            0                                            $  a, ...
Time 4Input a b     b b a a                                            1                                            $  a, ...
Time 5Input a b     b b a a                            1                                            1                     ...
Time 6Input a b     b b a a                            1                                            1                     ...
Time 7Input a b     b b a a                                            1                                            $  a, ...
Time 8Input a b     b b a a                                             $  a, $   0$        b, $    1$                    ...
Formalities forNPDAs
a, b   w               q1              q2Transition function:              (q1, a, b) {(q2 , w)}
a, b    w   q2              q1               a, b    w   q3Transition function:        (q1, a, b) {(q2 , w), (q3 , w)}
Formal Definition AutomatonNon-Deterministic Pushdown                 NPDA         M   (Q, Σ, Γ, δ, q0 , z, F )   FinalSta...
Instantaneous Description            ( q, u , s )  Current                  Current             Remaining  state          ...
Example:      Instantaneous Description                    (q1, bbb, aaa$)                                              aT...
Example:      Instantaneous Description                        (q2 , bb, aa$)                                             ...
We write:    (q1, bbb, aaa$)  (q2 , bb, aa$)      Time 4              Time 5
A computation:(q0 , aaabbb,$)  (q1, aaabbb,$) (q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) (q2 , bb, aa$)  (q2 ...
(q0 , aaabbb,$)  (q1, aaabbb,$) (q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) (q2 , bb, aa$)  (q2 , b, a$)  (q2...
Formal DefinitionLanguage of NPDA M :   L( M ) {w : (q0 , w, s )  (q f , , s  )}     Initial state                Final s...
Example:            (q0 , aaabbb,$)  (q3 , ,$)                      aaabbb L(M ) NPDA M :            a,        a      b, ...
n n           (q0 , a b ,$)  (q3 , ,$)                          n n                         a b       L(M )NPDA M :      ...
Therefore:                  n n                L( M ) {a b : n 0}NPDA M :           a,        a      b, a q0   ,         q...
NPDAs AcceptContext-FreeLanguages
Theorem:  Context-Free    Languages    Languages    Accepted by   (Grammars)      NPDAs
Proof - Step 1:  Context-Free            Languages    Languages            Accepted by   (Grammars)              NPDAs Con...
Proof - Step 2:  Context-Free           Languages    Languages           Accepted by   (Grammars)             NPDAs Conver...
ConvertingContext-FreeGrammarstoNPDAs
An example grammar:   S   aSTb                      S   b                      T   Ta                      TWhat is the eq...
Grammar:S   aSTbS   b                NPDA:T   Ta               ,S   aSTbT               ,S   b               ,T   Ta   a, ...
The NPDA simulatesleftmost derivations of the grammar       L(Grammar) = L(NPDA)
Grammar: S     aSTb          S    b          T    Ta          TA leftmost derivation:    S   aSTb       abTb   abTab   abab
NPDA execution: Time 0Input   a b a b              ,S   aSTb               $              ,S   b                Stack     ...
Time 1Input   a b a b                                      S             ,S    aSTb               $             ,S    b   ...
Time 2             a                                     SInput   a b a b                                     T           ...
Time 3             a                                     SInput   a b a b                                     T           ...
Time 4                                     bInput   a b a b                                     T                         ...
Time 5                                     bInput   a b a b                                     T                         ...
Time 6                                     TInput   a b a b                      a                                     b  ...
Time 7                                     TInput   a b a b                      a                                     b  ...
Time 8Input   a b a b                       a                                      b             ,S   aSTb                ...
Time 9Input   a b a b                                      b             ,S   aSTb                $             ,S   b    ...
Time 10Input   a b a b             ,S   aSTb                 $             ,S   b                  Stack             ,T   ...
In general:    Given any grammar    G    We can construct a NPDA    M     With     L(G )   L( M )
Constructing NPDA         M from grammar G : For any production                   For any terminal         A       w      ...
Grammar     G generates string wif and only ifNPDA      M accepts w     L(G)    L( M )
Therefore:     For any context-free language     there is an NPDA     that accepts the same language
ConvertingNPDAstoContext-FreeGrammars
For any NPDA    Mwe will constructa context-free grammar    G with             L( M )   L(G )
Intuition: The grammar simulates the machineA derivation in Grammar   G:   S        abc ABC                 abc    Cu...
A derivation in Grammar    G:           terminals      variables    S         abc ABC                 abc    Input pr...
Some NecessaryModificationsFirst, we modify the NPDA:    • It has a single final state q f    • It empties the stack      ...
Second, we modify the NPDA transitions:     all transitions will have form                        a, B       qj           ...
Example of a NPDA in correct form:           L( M ) {w : na         nb }    $ : initial stack symbol    a, $    0$        ...
The Grammar ConstructionIn grammar G :                   Stack symbol          Variables:   (qi Bq j )                    ...
For each transition                            a, B       qj                       qiWe add production     (qi Bq j )   a
For each transition   qi   a, B      CD q                                          jWe add production (qi Bqk )       a(q ...
Stack bottom symbolStart Variable:     (qo $q f )      Start state                final state
Example:    a, $   0$        b, $   1$    a, 0   00        b, 1   11    a, 1             b, 0                            ,...
Example:     a, $    0$        b, $   1$     a, 0    00        b, 1   11     a, 1              b, 0                       ...
Example:     a, $   0$        b, $   1$     a, 0   00        b, 1   11     a, 1             b, 0                          ...
Resulting Grammar:          (q0 $q f ) : start variable(q0 $q0 )    b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )(q0 $q f )   ...
(q0 0q0 )    a (q0 0q0 )(q0 0q0 ) | a (q0 0q f )(q f 0q0 )(q0 0q f )   a (q0 0q0 )(q0 0q f ) | a (q0 0q f )(q f 0q f )    ...
Derivation of string         abba(q0 $q f )    a(q0 0q0 )(q0 $q f )                      ab(q0 $q f )             abb(q01q...
In general, in Grammar:            (q0 $q f )    w              if and only if      w is accepted by the NPDA
Explanation:   By construction of Grammar:               (qi Aq j )   w               if and only if    in the NPDA going ...
Upcoming SlideShare
Loading in …5
×

Pushdown automata

4,932 views

Published on

Published in: Technology, Business

Pushdown automata

  1. 1. PushdownAutomataPDAs
  2. 2. Pushdown Automaton --PDA Input String Stack States
  3. 3. Initial Stack Symbol Stack Stack $ z bottom special symbol
  4. 4. The States Input Pop Push symbol symbol symbol a, b c q1 q2
  5. 5. a, b c q1 q2input  a   a  stack b top c h Replace h e e $ $
  6. 6. a, c q1 q2input  a   a  stack c b top b h Push h e e $ $
  7. 7. a, b q1 q2input  a   a  stack b top h Pop h e e $ $
  8. 8. a, q1 q2input  a   a  stack b top b h No Change h e e $ $
  9. 9. Non-Determinism a, b c q2q1 , b c q1 q2 a, b c transition q3 These are allowed transitions in a Non-deterministic PDA (NPDA)
  10. 10. NPDA: Non-DeterministicPDAExample: a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  11. 11. Execution Example: Time 0 Input a a a b b b $ Stackcurrent a, a b, astate , b, a ,$ $ q q0 q1 q2 3
  12. 12. Time 1Input a a a b b b $ Stack a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  13. 13. Time 2Input a a a b b b a $ Stack a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  14. 14. Time 3Input a a a a b b b a $ Stack a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  15. 15. Time 4 aInput a a a a b b b a $ Stack a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  16. 16. Time 5 aInput a a a a b b b a $ Stack a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  17. 17. Time 6Input a a a a b b b a $ Stack a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  18. 18. Time 7Input a a a b b b a $ Stack a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  19. 19. Time 8Input a a a b b b $ Stack a, a b, a acceptq0 , q1 b, a q2 ,$ $ q 3
  20. 20. The Language of PDAA string is accepted if there isa computation such that: • All the input is consumed • The last state is a final stateAt the end of the computation,we do not care about the stack contents
  21. 21. Formal Definition AutomatonNon-Deterministic Pushdown NPDA M (Q, Σ, Γ, δ, q0 , z, F ) FinalStates statesInput Stackalphabet Transition Initial start Stack function state symbol alphabet
  22. 22. Instantaneous Description ( q, u , s ) Current Current Remaining state stack input contents
  23. 23. Acceptance by Final StateL(M)= set of all strings w such that starting from initial ID machine consumes w from input and enters an accepting state.The contents of the stack at that time is irrelevant.{w | (q0, w, Z) * (q, , )For some final state q of F and any stack staring .
  24. 24. Acceptance by emptystack N(M) = (w | (q0,w,Z) * (q, , ) That is N(M) is the set of all strings w such that M can consume w and at the same time empties its stack.Here we do not care whether q is a final state or not.
  25. 25. The input string aaabbb is accepted by the NPDA: a, a b, aq0 , q1 b, a q2 ,$ $ q 3
  26. 26. In general, n n L {a b : n 0}is the language accepted by the NPDA: a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  27. 27. Another NPDA example NPDA M R L( M ) {ww }a, a a, ab, b b, b q0 , q1 ,$ $ q2
  28. 28. Execution Example: Time 0 Input a b b a $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  29. 29. Time 1Input a b b a a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  30. 30. Time 2Input b a b b a a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  31. 31. Time 3Input b a b b a Guess the middle a of string $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  32. 32. Time 4Input b a b b a a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  33. 33. Time 5Input a b b a a $ Stacka, a a, ab, b b, b q0 , ,$ $ q2 q1
  34. 34. Time 6Input a b b a $ Stacka, a a, ab, b b, b accept q0 , q1 ,$ $ q2
  35. 35. Rejection Example: Time 0 Input a b b b $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  36. 36. Time 1Input a b b b a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  37. 37. Time 2Input b a b b b a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  38. 38. Time 3Input b a b b b Guess the middle a of string $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  39. 39. Time 4Input b a b b b a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  40. 40. Time 5Input There is no possible transition. a b b b Input is not a consumed $ Stacka, a a, ab, b b, b q0 , ,$ $ q2 q1
  41. 41. Another computation on same string: Input Time 0 a b b b $ Stack a, a a, a b, b b, b q0 , q1 ,$ $ q2
  42. 42. Time 1Input a b b b a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  43. 43. Time 2Input b a b b b a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  44. 44. Time 3Input b b a b b b a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  45. 45. Time 4 bInput b b a b b b a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  46. 46. Time 5 bInput b No final state b a b b b is reached a $ Stacka, a a, ab, b b, b q0 , q1 ,$ $ q2
  47. 47. There is no computation that accepts string abbb abbb L(M )a, a a, ab, b b, b q0 , q1 ,$ $ q2
  48. 48. Pushing Strings Input Pop Push symbol symbol string a, b w q1 q2
  49. 49. Example: a, b cdf q1 q2input   a     a   c pushed stack d top string b f h Push h e e $ $
  50. 50. Another NPDA example NPDA M L( M ) {w : na nb } a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  51. 51. Execution Example: Time 0 Input a b b a a b $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 current state ,$ $ q1 q2
  52. 52. Time 1Input a b b a a b 0 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  53. 53. Time 3Input a b b b a a 0 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  54. 54. Time 4Input a b b b a a 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  55. 55. Time 5Input a b b b a a 1 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  56. 56. Time 6Input a b b b a a 1 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  57. 57. Time 7Input a b b b a a 1 $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 ,$ $ q2 q1
  58. 58. Time 8Input a b b b a a $ a, $ 0$ b, $ 1$ Stack a, 0 00 b, 1 11 a, 1 b, 0 accept ,$ $ q2 q1
  59. 59. Formalities forNPDAs
  60. 60. a, b w q1 q2Transition function: (q1, a, b) {(q2 , w)}
  61. 61. a, b w q2 q1 a, b w q3Transition function: (q1, a, b) {(q2 , w), (q3 , w)}
  62. 62. Formal Definition AutomatonNon-Deterministic Pushdown NPDA M (Q, Σ, Γ, δ, q0 , z, F ) FinalStates statesInput Stackalphabet Transition Initial start Stack function state symbol alphabet
  63. 63. Instantaneous Description ( q, u , s ) Current Current Remaining state stack input contents
  64. 64. Example: Instantaneous Description (q1, bbb, aaa$) aTime 4: Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  65. 65. Example: Instantaneous Description (q2 , bb, aa$) aTime 5: Input a a a a b b b a $ Stack a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  66. 66. We write: (q1, bbb, aaa$)  (q2 , bb, aa$) Time 4 Time 5
  67. 67. A computation:(q0 , aaabbb,$)  (q1, aaabbb,$) (q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) (q2 , bb, aa$)  (q2 , b, a$)  (q2 , ,$)  (q3 , ,$) a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  68. 68. (q0 , aaabbb,$)  (q1, aaabbb,$) (q1, aabbb, a$)  (q1, abbb, aa$)  (q1, bbb, aaa$) (q2 , bb, aa$)  (q2 , b, a$)  (q2 , ,$)  (q3 , ,$) For convenience we write: (q0 , aaabbb ,$)  (q3 , ,$)
  69. 69. Formal DefinitionLanguage of NPDA M : L( M ) {w : (q0 , w, s )  (q f , , s )} Initial state Final state
  70. 70. Example: (q0 , aaabbb,$)  (q3 , ,$) aaabbb L(M ) NPDA M : a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  71. 71. n n (q0 , a b ,$)  (q3 , ,$) n n a b L(M )NPDA M : a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  72. 72. Therefore: n n L( M ) {a b : n 0}NPDA M : a, a b, a q0 , q1 b, a q2 ,$ $ q 3
  73. 73. NPDAs AcceptContext-FreeLanguages
  74. 74. Theorem: Context-Free Languages Languages Accepted by (Grammars) NPDAs
  75. 75. Proof - Step 1: Context-Free Languages Languages Accepted by (Grammars) NPDAs Convert any context-free grammar G to a NPDA M with: L (G ) L( M )
  76. 76. Proof - Step 2: Context-Free Languages Languages Accepted by (Grammars) NPDAs Convert any NPDA M to a context-free grammar G with: L (G ) L( M )
  77. 77. ConvertingContext-FreeGrammarstoNPDAs
  78. 78. An example grammar: S aSTb S b T Ta TWhat is the equivalent NPDA?
  79. 79. Grammar:S aSTbS b NPDA:T Ta ,S aSTbT ,S b ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  80. 80. The NPDA simulatesleftmost derivations of the grammar L(Grammar) = L(NPDA)
  81. 81. Grammar: S aSTb S b T Ta TA leftmost derivation: S aSTb abTb abTab abab
  82. 82. NPDA execution: Time 0Input a b a b ,S aSTb $ ,S b Stack ,T Ta a, acurrent ,T b, bstate , S ,$ $ q0 q1 q2
  83. 83. Time 1Input a b a b S ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  84. 84. Time 2 a SInput a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  85. 85. Time 3 a SInput a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  86. 86. Time 4 bInput a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  87. 87. Time 5 bInput a b a b T b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  88. 88. Time 6 TInput a b a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  89. 89. Time 7 TInput a b a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  90. 90. Time 8Input a b a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  91. 91. Time 9Input a b a b b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b q0 , S ,$ $ q1 q2
  92. 92. Time 10Input a b a b ,S aSTb $ ,S b Stack ,T Ta a, a ,T b, b accept q0 , S ,$ $ q1 q2
  93. 93. In general: Given any grammar G We can construct a NPDA M With L(G ) L( M )
  94. 94. Constructing NPDA M from grammar G : For any production For any terminal A w a ,A w a, a q0 , S ,$ $ q1 q2
  95. 95. Grammar G generates string wif and only ifNPDA M accepts w L(G) L( M )
  96. 96. Therefore: For any context-free language there is an NPDA that accepts the same language
  97. 97. ConvertingNPDAstoContext-FreeGrammars
  98. 98. For any NPDA Mwe will constructa context-free grammar G with L( M ) L(G )
  99. 99. Intuition: The grammar simulates the machineA derivation in Grammar G: S  abc ABC   abc Current configuration in NPDA M
  100. 100. A derivation in Grammar G: terminals variables S  abc ABC   abc Input processed Stack contents in NPDA M
  101. 101. Some NecessaryModificationsFirst, we modify the NPDA: • It has a single final state q f • It empties the stack when it accepts the input Original NPDA Empty Stack qf
  102. 102. Second, we modify the NPDA transitions: all transitions will have form a, B qj qi or qi a, B CD q jB, C , D : stack symbols
  103. 103. Example of a NPDA in correct form: L( M ) {w : na nb } $ : initial stack symbol a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0
  104. 104. The Grammar ConstructionIn grammar G : Stack symbol Variables: (qi Bq j ) states Terminals: Input symbols of NPDA
  105. 105. For each transition a, B qj qiWe add production (qi Bq j ) a
  106. 106. For each transition qi a, B CD q jWe add production (qi Bqk ) a(q j Cql )(ql Dqk ) For all states qk , ql
  107. 107. Stack bottom symbolStart Variable: (qo $q f ) Start state final state
  108. 108. Example: a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0Grammar production: (q01q0 ) a
  109. 109. Example: a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0Grammar productions:(q0 $q0 ) b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )(q0 $q f ) b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )
  110. 110. Example: a, $ 0$ b, $ 1$ a, 0 00 b, 1 11 a, 1 b, 0 ,$ qf q0Grammar production: (q0 $q f )
  111. 111. Resulting Grammar: (q0 $q f ) : start variable(q0 $q0 ) b(q01q0 )(q0 $q0 ) | b(q01q f )(q f $q0 )(q0 $q f ) b(q01q0 )(q0 $q f ) | b(q01q f )(q f $q f )(q01q0 ) b(q01q0 )(q01q0 ) | b(q01q f )(q f 1q0 )(q01q f ) b(q01q0 )(q01q f ) | b(q01q f )(q f 1q f )(q0 $q0 ) a (q0 0q0 )(q0 $q0 ) | a (q0 0q f )(q f $q0 )(q0 $q f ) a (q0 0q0 )(q0 $q f ) | a (q0 0q f )(q f $q f )
  112. 112. (q0 0q0 ) a (q0 0q0 )(q0 0q0 ) | a (q0 0q f )(q f 0q0 )(q0 0q f ) a (q0 0q0 )(q0 0q f ) | a (q0 0q f )(q f 0q f ) (q01q0 ) a (q0 0q0 ) b (q0 $q f )
  113. 113. Derivation of string abba(q0 $q f ) a(q0 0q0 )(q0 $q f ) ab(q0 $q f ) abb(q01q0 )(q0 $q f ) abba(q0 $q f ) abba
  114. 114. In general, in Grammar: (q0 $q f ) w if and only if w is accepted by the NPDA
  115. 115. Explanation: By construction of Grammar: (qi Aq j ) w if and only if in the NPDA going from qi to q j the stack doesn’t change below A and A is removed from stack

×