# Cfg To Npda

**CFG to NPDA**: For any context-free grammar in GNF, it is easy to build an equivalent nondeterministic pushdown automaton (NPDA).

Any string of a context-free language has a leftmost derivation. We set up the NPDA so that the stack contents “corresponds” to this sentential form: every move of the NPDA represents one derivation step.

The sentential form is

**(The char acters already read) (sym bols on the stack)– (Final z (ini tial stack sym bol)**

In the NPDA, we will construct, the states that are not of much importance. All the real work is done on the stack. We will use only the following three states, irrespective of the complexity of the grammar.

(i) start state *q*0 just gets things initialized. We use the transition from *q*0 to *q*1 to put the grammar’s start symbol on the stack.

**d****( q**

**0**

**,**

**l**

**,**

*Z*) -> {(*q***1**

**,**

*Sz*)}
(ii) State *q*1 does the bulk of the work. We represent every derivation step as a move from *q*1 to *q*1.

(iii) We use the transition from *q*1 to *q**f *to accept the string

**d****( q**

**1**

**,**

**l**

**,**

*z*) -> {(*q*

*f***,**

*z*)}
** Example**Consider the grammar

*G*= ({

*S*,

*A*,

*B*},{

*a*,

*b*},

*S*,

*P*), where

*P *= {*S *-> *a*, *S *-> *aAB*, *A*-> *aA*, *A*-> *a*, *B*-> *bB*, *B*-> *b*}

These productions can be turned into transition functions by rearranging the components.

For example, the derivation

*S *=>*aAB *=>*aaB *=>*aabB *=>*aabb*

maps into the sequence of moves