Question

Let's say that we have the context-free language L={0^n0^n, n>=0}.

For the PDA: Μ = {A, Q, H, δ, q0, h0, F} we have:

A = {"0"}
H = {X, I}
Q = {S, T}
q0 = S
h0 = X
F = {T}

Then, the δ function is:
   ___________________________________________________________________
   |           X            |             I            |      -|     |
---+------------------------+--------------------------+-------------|
S  | read("0")=> push(I)    |   read("0")=> push(I)    |             |
   | keep=> move(T)         |   pop                    |             |
---+------------------------+--------------------------+-------------|
T  |                        |                          |   success   |
---------------------------------------------------------------------+

That's my solution, but it has a problem. The automaton must accept with strings such as 00, ε, 0000 and not with 0, or 000. Generally with strings that have even number of 0s.

Let's try 2 examples:

->for string 00:
 MOVE    STACK    INPUT    STATE    DESCRIPTION_OF_MOVE
  1      X         '0'0      S        reading_of 0
  2      XI          '0'     S        reading_of 0
  3      XII        ε        S        pop
  4      XI         ε        S        pop
  5      X          ε        S        ε-transition
  6      X          ε        T        success

->for string 000:
 MOVE    STACK    INPUT    STATE    DESCRIPTION_OF_MOVE
  1      X        '0'00      S        reading_of 0
  2      XI        '0'0      S        reading_of 0
  3      XII         '0'     S        reading_of 0
  4      XIII       ε        S        pop
  5      XII        ε        S        pop
  6      XI         ε        S        pop
  7      X          ε        S        ε-transition
  8      X          ε        T        success

The last string shouldn't be accepted. I can't figure out how to check the number of pops before the recognition of the string to choose whether to accept it or not. Has anyone any idea, any clue to trigger me?

Was it helpful?

Solution

The solution is simple. In this case the PDA must accept an even number of 0s including a zero number of 0s.

In order to achieve that a simple implementation would be to pop from the stack the symbol I each time an even number of appearance of 0 is met, so the δ function would be:

   ___________________________________________________________________
   |           X            |             I            |      -|     |
---+------------------------+--------------------------+-------------|
S  | read("0")=> push(I)    |   read("0")=> pop        |             |
   | keep=> move(T)         |                          |             |
---+------------------------+--------------------------+-------------|
T  |                        |                          |   success   |
---------------------------------------------------------------------+

Let's try the 2 examples:

->for string 00:
 MOVE    STACK    INPUT    STATE    DESCRIPTION_OF_MOVE
  1      X         '0'0      S        read(0)=> push(I)
  2      XI          '0'     S        read(0)=> pop
  3      X          ε        S        ε-transition
  4      X          ε        T        success

->for string 000:
 MOVE    STACK    INPUT    STATE    DESCRIPTION_OF_MOVE
  1      X        '0'00      S        read(0)=> push(I)
  2      XI        '0'0      S        read(0)=> pop
  3      X           '0'     S        read(0)=> push(I)
  4      XI         ε        S        eoi=> failure
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top